net: Convert the storage task to use IPC.

This commit is contained in:
Patrick Walton 2015-07-25 00:05:46 -07:00
parent 8edf1a5ecd
commit 43cb7d5abd
8 changed files with 58 additions and 31 deletions

View file

@ -24,6 +24,9 @@ features = [ "serde-serialization" ]
version = "0.6"
features = [ "serde-serialization" ]
[dependencies.ipc-channel]
git = "https://github.com/pcwalton/ipc-channel"
[dependencies]
log = "0.3"
url = "0.2.36"

View file

@ -18,6 +18,7 @@ extern crate devtools_traits;
extern crate flate2;
extern crate euclid;
extern crate hyper;
extern crate ipc_channel;
extern crate png;
#[macro_use]
extern crate log;

View file

@ -2,10 +2,11 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use ipc_channel::ipc::{self, IpcReceiver, IpcSender};
use std::borrow::ToOwned;
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::sync::mpsc::{channel, Receiver, Sender};
use std::sync::mpsc::channel;
use url::Url;
use net_traits::storage_task::{StorageTask, StorageTaskMsg, StorageType};
@ -19,7 +20,7 @@ pub trait StorageTaskFactory {
impl StorageTaskFactory for StorageTask {
/// Create a StorageTask
fn new() -> StorageTask {
let (chan, port) = channel();
let (chan, port) = ipc::channel().unwrap();
spawn_named("StorageManager".to_owned(), move || {
StorageManager::new(port).start();
});
@ -28,13 +29,13 @@ impl StorageTaskFactory for StorageTask {
}
struct StorageManager {
port: Receiver<StorageTaskMsg>,
port: IpcReceiver<StorageTaskMsg>,
session_data: HashMap<String, BTreeMap<DOMString, DOMString>>,
local_data: HashMap<String, BTreeMap<DOMString, DOMString>>,
}
impl StorageManager {
fn new(port: Receiver<StorageTaskMsg>) -> StorageManager {
fn new(port: IpcReceiver<StorageTaskMsg>) -> StorageManager {
StorageManager {
port: port,
session_data: HashMap::new(),
@ -72,27 +73,33 @@ impl StorageManager {
}
}
fn select_data(& self, storage_type: StorageType) -> &HashMap<String, BTreeMap<DOMString, DOMString>> {
fn select_data(&self, storage_type: StorageType)
-> &HashMap<String, BTreeMap<DOMString, DOMString>> {
match storage_type {
StorageType::Session => &self.session_data,
StorageType::Local => &self.local_data
}
}
fn select_data_mut(&mut self, storage_type: StorageType) -> &mut HashMap<String, BTreeMap<DOMString, DOMString>> {
fn select_data_mut(&mut self, storage_type: StorageType)
-> &mut HashMap<String, BTreeMap<DOMString, DOMString>> {
match storage_type {
StorageType::Session => &mut self.session_data,
StorageType::Local => &mut self.local_data
}
}
fn length(&self, sender: Sender<usize>, url: Url, storage_type: StorageType) {
fn length(&self, sender: IpcSender<usize>, url: Url, storage_type: StorageType) {
let origin = self.get_origin_as_string(url);
let data = self.select_data(storage_type);
sender.send(data.get(&origin).map_or(0, |entry| entry.len())).unwrap();
}
fn key(&self, sender: Sender<Option<DOMString>>, url: Url, storage_type: StorageType, index: u32) {
fn key(&self,
sender: IpcSender<Option<DOMString>>,
url: Url,
storage_type: StorageType,
index: u32) {
let origin = self.get_origin_as_string(url);
let data = self.select_data(storage_type);
sender.send(data.get(&origin)
@ -102,8 +109,12 @@ impl StorageManager {
/// Sends Some(old_value) in case there was a previous value with the same key name but with different
/// value name, otherwise sends None
fn set_item(&mut self, sender: Sender<(bool, Option<DOMString>)>, url: Url, storage_type: StorageType,
name: DOMString, value: DOMString) {
fn set_item(&mut self,
sender: IpcSender<(bool, Option<DOMString>)>,
url: Url,
storage_type: StorageType,
name: DOMString,
value: DOMString) {
let origin = self.get_origin_as_string(url);
let data = self.select_data_mut(storage_type);
if !data.contains_key(&origin) {
@ -122,7 +133,11 @@ impl StorageManager {
sender.send((changed, old_value)).unwrap();
}
fn get_item(&self, sender: Sender<Option<DOMString>>, url: Url, storage_type: StorageType, name: DOMString) {
fn get_item(&self,
sender: IpcSender<Option<DOMString>>,
url: Url,
storage_type: StorageType,
name: DOMString) {
let origin = self.get_origin_as_string(url);
let data = self.select_data(storage_type);
sender.send(data.get(&origin)
@ -131,7 +146,10 @@ impl StorageManager {
}
/// Sends Some(old_value) in case there was a previous value with the key name, otherwise sends None
fn remove_item(&mut self, sender: Sender<Option<DOMString>>, url: Url, storage_type: StorageType,
fn remove_item(&mut self,
sender: IpcSender<Option<DOMString>>,
url: Url,
storage_type: StorageType,
name: DOMString) {
let origin = self.get_origin_as_string(url);
let data = self.select_data_mut(storage_type);
@ -141,7 +159,7 @@ impl StorageManager {
sender.send(old_value).unwrap();
}
fn clear(&mut self, sender: Sender<bool>, url: Url, storage_type: StorageType) {
fn clear(&mut self, sender: IpcSender<bool>, url: Url, storage_type: StorageType) {
let origin = self.get_origin_as_string(url);
let data = self.select_data_mut(storage_type);
sender.send(data.get_mut(&origin)

View file

@ -2,43 +2,44 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use std::sync::mpsc::Sender;
use ipc_channel::ipc::IpcSender;
use url::Url;
use util::str::DOMString;
#[derive(Copy, Clone)]
#[derive(Copy, Clone, Deserialize, Serialize)]
pub enum StorageType {
Session,
Local
}
/// Request operations on the storage data associated with a particular url
#[derive(Deserialize, Serialize)]
pub enum StorageTaskMsg {
/// gets the number of key/value pairs present in the associated storage data
Length(Sender<usize>, Url, StorageType),
Length(IpcSender<usize>, Url, StorageType),
/// gets the name of the key at the specified index in the associated storage data
Key(Sender<Option<DOMString>>, Url, StorageType, u32),
Key(IpcSender<Option<DOMString>>, Url, StorageType, u32),
/// gets the value associated with the given key in the associated storage data
GetItem(Sender<Option<DOMString>>, Url, StorageType, DOMString),
GetItem(IpcSender<Option<DOMString>>, Url, StorageType, DOMString),
/// sets the value of the given key in the associated storage data
/// TODO throw QuotaExceededError in case of error
SetItem(Sender<(bool, Option<DOMString>)>, Url, StorageType, DOMString, DOMString),
SetItem(IpcSender<(bool, Option<DOMString>)>, Url, StorageType, DOMString, DOMString),
/// removes the key/value pair for the given key in the associated storage data
RemoveItem(Sender<Option<DOMString>>, Url, StorageType, DOMString),
RemoveItem(IpcSender<Option<DOMString>>, Url, StorageType, DOMString),
/// clears the associated storage data by removing all the key/value pairs
Clear(Sender<bool>, Url, StorageType),
Clear(IpcSender<bool>, Url, StorageType),
/// shut down this task
Exit
}
/// Handle to a storage task
pub type StorageTask = Sender<StorageTaskMsg>;
pub type StorageTask = IpcSender<StorageTaskMsg>;

View file

@ -13,6 +13,7 @@ use dom::event::{EventHelpers, EventBubbles, EventCancelable};
use dom::storageevent::StorageEvent;
use dom::urlhelper::UrlHelper;
use dom::window::WindowHelpers;
use ipc_channel::ipc;
use util::str::DOMString;
use page::IterablePage;
use net_traits::storage_task::{StorageTask, StorageTaskMsg, StorageType};
@ -58,21 +59,21 @@ impl Storage {
impl<'a> StorageMethods for &'a Storage {
fn Length(self) -> u32 {
let (sender, receiver) = channel();
let (sender, receiver) = ipc::channel().unwrap();
self.get_storage_task().send(StorageTaskMsg::Length(sender, self.get_url(), self.storage_type)).unwrap();
receiver.recv().unwrap() as u32
}
fn Key(self, index: u32) -> Option<DOMString> {
let (sender, receiver) = channel();
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()
}
fn GetItem(self, name: DOMString) -> Option<DOMString> {
let (sender, receiver) = channel();
let (sender, receiver) = ipc::channel().unwrap();
let msg = StorageTaskMsg::GetItem(sender, self.get_url(), self.storage_type, name);
self.get_storage_task().send(msg).unwrap();
@ -86,7 +87,7 @@ impl<'a> StorageMethods for &'a Storage {
}
fn SetItem(self, name: DOMString, value: DOMString) {
let (sender, receiver) = channel();
let (sender, receiver) = ipc::channel().unwrap();
let msg = StorageTaskMsg::SetItem(sender, self.get_url(), self.storage_type, name.clone(), value.clone());
self.get_storage_task().send(msg).unwrap();
@ -105,7 +106,7 @@ impl<'a> StorageMethods for &'a Storage {
}
fn RemoveItem(self, name: DOMString) {
let (sender, receiver) = channel();
let (sender, receiver) = ipc::channel().unwrap();
let msg = StorageTaskMsg::RemoveItem(sender, self.get_url(), self.storage_type, name.clone());
self.get_storage_task().send(msg).unwrap();
@ -119,7 +120,7 @@ impl<'a> StorageMethods for &'a Storage {
}
fn Clear(self) {
let (sender, receiver) = channel();
let (sender, receiver) = ipc::channel().unwrap();
self.get_storage_task().send(StorageTaskMsg::Clear(sender, self.get_url(), self.storage_type)).unwrap();
if receiver.recv().unwrap() {

View file

@ -648,7 +648,7 @@ dependencies = [
[[package]]
name = "ipc-channel"
version = "0.1.0"
source = "git+https://github.com/pcwalton/ipc-channel#01f43c1851774023b0bd9b7fdd12d00f087fafb2"
source = "git+https://github.com/pcwalton/ipc-channel#13af22aa2ba8d40f80a7f91cf67a397ffc3df55b"
dependencies = [
"byteorder 0.3.11 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)",
@ -869,6 +869,7 @@ dependencies = [
"euclid 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
"flate2 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
"hyper 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)",
"ipc-channel 0.1.0 (git+https://github.com/pcwalton/ipc-channel)",
"log 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
"net_traits 0.0.1",
"openssl 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",

3
ports/cef/Cargo.lock generated
View file

@ -640,7 +640,7 @@ dependencies = [
[[package]]
name = "ipc-channel"
version = "0.1.0"
source = "git+https://github.com/pcwalton/ipc-channel#01f43c1851774023b0bd9b7fdd12d00f087fafb2"
source = "git+https://github.com/pcwalton/ipc-channel#13af22aa2ba8d40f80a7f91cf67a397ffc3df55b"
dependencies = [
"byteorder 0.3.11 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)",
@ -861,6 +861,7 @@ dependencies = [
"euclid 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
"flate2 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
"hyper 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)",
"ipc-channel 0.1.0 (git+https://github.com/pcwalton/ipc-channel)",
"log 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
"net_traits 0.0.1",
"openssl 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",

3
ports/gonk/Cargo.lock generated
View file

@ -574,7 +574,7 @@ dependencies = [
[[package]]
name = "ipc-channel"
version = "0.1.0"
source = "git+https://github.com/pcwalton/ipc-channel#01f43c1851774023b0bd9b7fdd12d00f087fafb2"
source = "git+https://github.com/pcwalton/ipc-channel#13af22aa2ba8d40f80a7f91cf67a397ffc3df55b"
dependencies = [
"byteorder 0.3.11 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)",
@ -787,6 +787,7 @@ dependencies = [
"euclid 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
"flate2 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
"hyper 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)",
"ipc-channel 0.1.0 (git+https://github.com/pcwalton/ipc-channel)",
"log 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
"net_traits 0.0.1",
"openssl 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",