mirror of
https://github.com/servo/servo.git
synced 2025-08-03 12:40:06 +01:00
Replaced DOMString by String in storage APIs.
This commit is contained in:
parent
582273eb07
commit
cf340be758
3 changed files with 36 additions and 35 deletions
|
@ -9,7 +9,6 @@ use std::collections::BTreeMap;
|
|||
use std::collections::HashMap;
|
||||
use std::sync::mpsc::channel;
|
||||
use url::Url;
|
||||
use util::str::DOMString;
|
||||
use util::task::spawn_named;
|
||||
|
||||
const QUOTA_SIZE_LIMIT: usize = 5 * 1024 * 1024;
|
||||
|
@ -31,8 +30,8 @@ impl StorageTaskFactory for StorageTask {
|
|||
|
||||
struct StorageManager {
|
||||
port: IpcReceiver<StorageTaskMsg>,
|
||||
session_data: HashMap<String, (usize, BTreeMap<DOMString, DOMString>)>,
|
||||
local_data: HashMap<String, (usize, BTreeMap<DOMString, DOMString>)>,
|
||||
session_data: HashMap<String, (usize, BTreeMap<String, String>)>,
|
||||
local_data: HashMap<String, (usize, BTreeMap<String, String>)>,
|
||||
}
|
||||
|
||||
impl StorageManager {
|
||||
|
@ -78,7 +77,7 @@ impl StorageManager {
|
|||
}
|
||||
|
||||
fn select_data(&self, storage_type: StorageType)
|
||||
-> &HashMap<String, (usize, BTreeMap<DOMString, DOMString>)> {
|
||||
-> &HashMap<String, (usize, BTreeMap<String, String>)> {
|
||||
match storage_type {
|
||||
StorageType::Session => &self.session_data,
|
||||
StorageType::Local => &self.local_data
|
||||
|
@ -86,7 +85,7 @@ impl StorageManager {
|
|||
}
|
||||
|
||||
fn select_data_mut(&mut self, storage_type: StorageType)
|
||||
-> &mut HashMap<String, (usize, BTreeMap<DOMString, DOMString>)> {
|
||||
-> &mut HashMap<String, (usize, BTreeMap<String, String>)> {
|
||||
match storage_type {
|
||||
StorageType::Session => &mut self.session_data,
|
||||
StorageType::Local => &mut self.local_data
|
||||
|
@ -100,7 +99,7 @@ impl StorageManager {
|
|||
}
|
||||
|
||||
fn key(&self,
|
||||
sender: IpcSender<Option<DOMString>>,
|
||||
sender: IpcSender<Option<String>>,
|
||||
url: Url,
|
||||
storage_type: StorageType,
|
||||
index: u32) {
|
||||
|
@ -113,7 +112,7 @@ impl StorageManager {
|
|||
}
|
||||
|
||||
fn keys(&self,
|
||||
sender: IpcSender<Vec<DOMString>>,
|
||||
sender: IpcSender<Vec<String>>,
|
||||
url: Url,
|
||||
storage_type: StorageType) {
|
||||
let origin = self.origin_as_string(url);
|
||||
|
@ -129,11 +128,11 @@ impl StorageManager {
|
|||
/// otherwise sends Err(()) to indicate that the operation would result in
|
||||
/// exceeding the quota limit
|
||||
fn set_item(&mut self,
|
||||
sender: IpcSender<Result<(bool, Option<DOMString>), ()>>,
|
||||
sender: IpcSender<Result<(bool, Option<String>), ()>>,
|
||||
url: Url,
|
||||
storage_type: StorageType,
|
||||
name: DOMString,
|
||||
value: DOMString) {
|
||||
name: String,
|
||||
value: String) {
|
||||
let origin = self.origin_as_string(url);
|
||||
|
||||
let current_total_size = {
|
||||
|
@ -175,23 +174,23 @@ impl StorageManager {
|
|||
}
|
||||
|
||||
fn request_item(&self,
|
||||
sender: IpcSender<Option<DOMString>>,
|
||||
sender: IpcSender<Option<String>>,
|
||||
url: Url,
|
||||
storage_type: StorageType,
|
||||
name: DOMString) {
|
||||
name: String) {
|
||||
let origin = self.origin_as_string(url);
|
||||
let data = self.select_data(storage_type);
|
||||
sender.send(data.get(&origin)
|
||||
.and_then(|&(_, ref entry)| entry.get(&name))
|
||||
.map(|value| DOMString(value.to_string()))).unwrap();
|
||||
.map(String::clone)).unwrap();
|
||||
}
|
||||
|
||||
/// Sends Some(old_value) in case there was a previous value with the key name, otherwise sends None
|
||||
fn remove_item(&mut self,
|
||||
sender: IpcSender<Option<DOMString>>,
|
||||
sender: IpcSender<Option<String>>,
|
||||
url: Url,
|
||||
storage_type: StorageType,
|
||||
name: DOMString) {
|
||||
name: String) {
|
||||
let origin = self.origin_as_string(url);
|
||||
let data = self.select_data_mut(storage_type);
|
||||
let old_value = data.get_mut(&origin).and_then(|&mut (ref mut total, ref mut entry)| {
|
||||
|
|
|
@ -4,7 +4,6 @@
|
|||
|
||||
use ipc_channel::ipc::IpcSender;
|
||||
use url::Url;
|
||||
use util::str::DOMString;
|
||||
|
||||
#[derive(Copy, Clone, Deserialize, Serialize, HeapSizeOf)]
|
||||
pub enum StorageType {
|
||||
|
@ -19,19 +18,19 @@ pub enum StorageTaskMsg {
|
|||
Length(IpcSender<usize>, Url, StorageType),
|
||||
|
||||
/// gets the name of the key at the specified index in the associated storage data
|
||||
Key(IpcSender<Option<DOMString>>, Url, StorageType, u32),
|
||||
Key(IpcSender<Option<String>>, Url, StorageType, u32),
|
||||
|
||||
/// Gets the available keys in the associated storage data
|
||||
Keys(IpcSender<Vec<DOMString>>, Url, StorageType),
|
||||
Keys(IpcSender<Vec<String>>, Url, StorageType),
|
||||
|
||||
/// gets the value associated with the given key in the associated storage data
|
||||
GetItem(IpcSender<Option<DOMString>>, Url, StorageType, DOMString),
|
||||
GetItem(IpcSender<Option<String>>, Url, StorageType, String),
|
||||
|
||||
/// sets the value of the given key in the associated storage data
|
||||
SetItem(IpcSender<Result<(bool, Option<DOMString>), ()>>, Url, StorageType, DOMString, DOMString),
|
||||
SetItem(IpcSender<Result<(bool, Option<String>), ()>>, Url, StorageType, String, String),
|
||||
|
||||
/// removes the key/value pair for the given key in the associated storage data
|
||||
RemoveItem(IpcSender<Option<DOMString>>, Url, StorageType, DOMString),
|
||||
RemoveItem(IpcSender<Option<String>>, Url, StorageType, String),
|
||||
|
||||
/// clears the associated storage data by removing all the key/value pairs
|
||||
Clear(IpcSender<bool>, Url, StorageType),
|
||||
|
|
|
@ -17,7 +17,6 @@ use ipc_channel::ipc;
|
|||
use net_traits::storage_task::{StorageTask, StorageTaskMsg, StorageType};
|
||||
use page::IterablePage;
|
||||
use script_task::{MainThreadRunnable, MainThreadScriptMsg, ScriptTask};
|
||||
use std::borrow::ToOwned;
|
||||
use std::sync::mpsc::channel;
|
||||
use url::Url;
|
||||
use util::str::DOMString;
|
||||
|
@ -70,21 +69,24 @@ impl StorageMethods for Storage {
|
|||
let (sender, receiver) = ipc::channel().unwrap();
|
||||
|
||||
self.get_storage_task().send(StorageTaskMsg::Key(sender, self.get_url(), self.storage_type, index)).unwrap();
|
||||
receiver.recv().unwrap()
|
||||
receiver.recv().unwrap().map(DOMString::from)
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#dom-storage-getitem
|
||||
fn GetItem(&self, name: DOMString) -> Option<DOMString> {
|
||||
let (sender, receiver) = ipc::channel().unwrap();
|
||||
let name = String::from(name);
|
||||
|
||||
let msg = StorageTaskMsg::GetItem(sender, self.get_url(), self.storage_type, name);
|
||||
self.get_storage_task().send(msg).unwrap();
|
||||
receiver.recv().unwrap()
|
||||
receiver.recv().unwrap().map(DOMString::from)
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#dom-storage-setitem
|
||||
fn SetItem(&self, name: DOMString, value: DOMString) -> ErrorResult {
|
||||
let (sender, receiver) = ipc::channel().unwrap();
|
||||
let name = String::from(name);
|
||||
let value = String::from(value);
|
||||
|
||||
let msg = StorageTaskMsg::SetItem(sender, self.get_url(), self.storage_type, name.clone(), value.clone());
|
||||
self.get_storage_task().send(msg).unwrap();
|
||||
|
@ -102,6 +104,7 @@ impl StorageMethods for Storage {
|
|||
// https://html.spec.whatwg.org/multipage/#dom-storage-removeitem
|
||||
fn RemoveItem(&self, name: DOMString) {
|
||||
let (sender, receiver) = ipc::channel().unwrap();
|
||||
let name = String::from(name);
|
||||
|
||||
let msg = StorageTaskMsg::RemoveItem(sender, self.get_url(), self.storage_type, name.clone());
|
||||
self.get_storage_task().send(msg).unwrap();
|
||||
|
@ -125,7 +128,7 @@ impl StorageMethods for Storage {
|
|||
let (sender, receiver) = ipc::channel().unwrap();
|
||||
|
||||
self.get_storage_task().send(StorageTaskMsg::Keys(sender, self.get_url(), self.storage_type)).unwrap();
|
||||
receiver.recv().unwrap()
|
||||
receiver.recv().unwrap().iter().cloned().map(DOMString::from).collect() // FIXME: inefficient?
|
||||
}
|
||||
|
||||
// check-tidy: no specs after this line
|
||||
|
@ -147,8 +150,8 @@ impl StorageMethods for Storage {
|
|||
|
||||
impl Storage {
|
||||
/// https://html.spec.whatwg.org/multipage/#send-a-storage-notification
|
||||
fn broadcast_change_notification(&self, key: Option<DOMString>, old_value: Option<DOMString>,
|
||||
new_value: Option<DOMString>) {
|
||||
fn broadcast_change_notification(&self, key: Option<String>, old_value: Option<String>,
|
||||
new_value: Option<String>) {
|
||||
let global_root = self.global.root();
|
||||
let global_ref = global_root.r();
|
||||
let main_script_chan = global_ref.as_window().main_thread_script_chan();
|
||||
|
@ -162,14 +165,14 @@ impl Storage {
|
|||
|
||||
pub struct StorageEventRunnable {
|
||||
element: Trusted<Storage>,
|
||||
key: Option<DOMString>,
|
||||
old_value: Option<DOMString>,
|
||||
new_value: Option<DOMString>
|
||||
key: Option<String>,
|
||||
old_value: Option<String>,
|
||||
new_value: Option<String>
|
||||
}
|
||||
|
||||
impl StorageEventRunnable {
|
||||
fn new(storage: Trusted<Storage>, key: Option<DOMString>, old_value: Option<DOMString>,
|
||||
new_value: Option<DOMString>) -> StorageEventRunnable {
|
||||
fn new(storage: Trusted<Storage>, key: Option<String>, old_value: Option<String>,
|
||||
new_value: Option<String>) -> StorageEventRunnable {
|
||||
StorageEventRunnable { element: storage, key: key, old_value: old_value, new_value: new_value }
|
||||
}
|
||||
}
|
||||
|
@ -186,10 +189,10 @@ impl MainThreadRunnable for StorageEventRunnable {
|
|||
|
||||
let storage_event = StorageEvent::new(
|
||||
global_ref,
|
||||
DOMString("storage".to_owned()),
|
||||
DOMString::from("storage"),
|
||||
EventBubbles::DoesNotBubble, EventCancelable::NotCancelable,
|
||||
this.key, this.old_value, this.new_value,
|
||||
DOMString(ev_url.to_string()),
|
||||
this.key.map(DOMString::from), this.old_value.map(DOMString::from), this.new_value.map(DOMString::from),
|
||||
DOMString::from(ev_url.to_string()),
|
||||
Some(storage)
|
||||
);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue