web: Scope entries in localStorage to the current url (#379)

This commit is contained in:
CUB3D 2020-06-16 00:54:00 +01:00
parent f2a5f4fd8e
commit 0644168f12
2 changed files with 14 additions and 9 deletions

View File

@ -18,6 +18,8 @@ use std::sync::{Arc, Mutex};
use std::{cell::RefCell, error::Error, num::NonZeroI32};
use wasm_bindgen::{prelude::*, JsCast, JsValue};
use web_sys::{Element, EventTarget, HtmlCanvasElement, HtmlElement, KeyboardEvent, PointerEvent};
use ruffle_core::backend::storage::StorageBackend;
thread_local! {
/// We store the actual instances of the ruffle core in a static pool.
@ -96,14 +98,13 @@ impl Ruffle {
if let Some(window) = web_sys::window() {
return window.cancel_animation_frame(id.into());
}
}
}
} }
// Player is dropped at this point.
Ok(())
}
}
use ruffle_core::backend::storage::StorageBackend;
impl Ruffle {
fn new_internal(parent: HtmlElement, swf_data: Uint8Array) -> Result<Ruffle, Box<dyn Error>> {
console_error_panic_hook::set_once();
@ -127,10 +128,12 @@ impl Ruffle {
let navigator = Box::new(WebNavigatorBackend::new());
let input = Box::new(WebInputBackend::new(&canvas));
let current_domain = window.location().href().unwrap();
let local_storage = window
.local_storage()
.unwrap()
.map(|s| Box::new(LocalStorageBackend::new(s)) as Box<dyn StorageBackend>)
.map(|s| Box::new(LocalStorageBackend::new(s, current_domain)) as Box<dyn StorageBackend>)
.unwrap_or_else(|| Box::new(MemoryStorageBackend::default()));
let core =

View File

@ -3,25 +3,27 @@ use web_sys::Storage;
pub struct LocalStorageBackend {
storage: Storage,
prefix: String,
}
impl LocalStorageBackend {
pub(crate) fn new(storage: Storage) -> Self {
LocalStorageBackend { storage }
pub(crate) fn new(storage: Storage, prefix: String) -> Self {
LocalStorageBackend { storage,
prefix}
}
}
//TODO: scope to current url
impl StorageBackend for LocalStorageBackend {
fn get_string(&self, name: &str) -> Option<String> {
self.storage.get(name).unwrap()
self.storage.get(&format!("{}-{}", self.prefix, name)).unwrap()
}
fn put_string(&mut self, name: &str, value: String) -> bool {
self.storage.set(name, &value).is_ok()
self.storage.set(&format!("{}-{}", self.prefix, name), &value).is_ok()
}
fn remove_key(&mut self, name: &str) {
let _ = self.storage.delete(name);
let _ = self.storage.delete(&format!("{}-{}", self.prefix, name));
}
}