gecko-dev/servo/components/script/dom/storage.rs
Michael Wu 7512d04e93 servo: Merge #6150 - Upgrade to Spidermonkey 39 (from servo:smupgrade3); r=mbrubeck
> Here it is.
>
> ~~There's two major things that are unfinished here:~~
> - ~~Dealing with the unroot_must_root lint. I'm not sure about the value of this lint with the new rooting API.~~ Done.
> - ~~Updating the Cargo.locks to point to the new SM and SM binding.~~ Done.
>
> I also included my fixes for the rust update, but these will disappear in a rebase. A rust update is necessary to support calling `Drop` on `Heap<T>` correctly when `Heap<T>` is inside a `Rc<T>`. Otherwise `&self` points to the wrong location.
>
> Incremental GC is disabled here. I'm not sure how to deal with the incremental barriers so that's left for later.
>
> Generational GC works. SM doesn't work without it.
>
> The biggest change here is to the rooting API. `Root` was made movable, and `Temporary` and `JSRef` was removed. Movable `Root`s means there's no need for `Temporary`, and `JSRef`s aren't needed generally since it can be assumed that being able to obtain a reference to a dom object means it's already rooted. References have their lifetime bound to the Roots that provided them. DOM objects that haven't passed through `reflect_dom_object` don't need to be rooted, and DOM objects that have passed through `reflect_dom_object` can't be obtained without being rooted through `native_from_reflector_jsmanaged` or `JS::<T>::root()`.
>
> Support for `Heap<T>` ended up messier than I expected. It's split into two commits, but only because it's a bit difficult to fold them together. Supporting `Heap<T>` properly requires that that `Heap::<T>::set()` be called on something that won't move. I removed the Copy and Clone trait from `Heap<T>` so `Cell` can't hold `Heap<T>` - only `UnsafeCell` can hold it.
>
> `CallbackObject` is a bit tricky - I moved all callbacks into `Rc<T>` in order to make sure that the pointer inside to a `*mut JSObject` doesn't move. This is necessary for supporting `Heap<T>`.
>
> `RootedCollectionSet` is very general purpose now. Anything with `JSTraceable` can be rooted by `RootedCollectionSet`/`RootedTraceable`. Right now, `RootedTraceable` is only used to hold down dom objects before they're fully attached to their reflector. I had to make a custom mechanism to dispatch the trace call - couldn't figure out how to get trait objects working for this case.
>
> This has been tested with the following zeal settings:
>
> GC after every allocation
> JS_GC_ZEAL=2,1
>
> GC after every 100 allocations (important for catching use-after-free bugs)
> JS_GC_ZEAL=2,100
>
> Verify pre barriers
> JS_GC_ZEAL=4,1
>
> Verify post barriers
> JS_GC_ZEAL=11,1

Source-Repo: https://github.com/servo/servo
Source-Revision: e7808c526c348fea5e3b48af70b7f1a066652097
2015-06-19 16:46:55 -06:00

198 lines
7.1 KiB
Rust

/* This Source Code Form is subject to the terms of the Mozilla Public
* 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 dom::bindings::codegen::Bindings::StorageBinding;
use dom::bindings::codegen::Bindings::StorageBinding::StorageMethods;
use dom::bindings::global::{GlobalRef, GlobalField};
use dom::bindings::js::{Root, RootedReference};
use dom::bindings::refcounted::Trusted;
use dom::bindings::utils::{Reflector, reflect_dom_object};
use dom::bindings::codegen::InheritTypes::{EventCast, EventTargetCast};
use dom::event::{EventHelpers, EventBubbles, EventCancelable};
use dom::storageevent::StorageEvent;
use dom::urlhelper::UrlHelper;
use dom::window::WindowHelpers;
use util::str::DOMString;
use page::IterablePage;
use net_traits::storage_task::{StorageTask, StorageTaskMsg, StorageType};
use std::borrow::ToOwned;
use std::sync::mpsc::channel;
use url::Url;
use script_task::{ScriptTask, ScriptMsg, MainThreadRunnable};
#[dom_struct]
pub struct Storage {
reflector_: Reflector,
global: GlobalField,
storage_type: StorageType
}
impl Storage {
fn new_inherited(global: &GlobalRef, storage_type: StorageType) -> Storage {
Storage {
reflector_: Reflector::new(),
global: GlobalField::from_rooted(global),
storage_type: storage_type
}
}
pub fn new(global: &GlobalRef, storage_type: StorageType) -> Root<Storage> {
reflect_dom_object(box Storage::new_inherited(global, storage_type), *global, StorageBinding::Wrap)
}
fn get_url(&self) -> Url {
let global_root = self.global.root();
let global_ref = global_root.r();
global_ref.get_url()
}
fn get_storage_task(&self) -> StorageTask {
let global_root = self.global.root();
let global_ref = global_root.r();
global_ref.as_window().storage_task()
}
}
impl<'a> StorageMethods for &'a Storage {
fn Length(self) -> u32 {
let (sender, receiver) = channel();
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();
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 msg = StorageTaskMsg::GetItem(sender, self.get_url(), self.storage_type, name);
self.get_storage_task().send(msg).unwrap();
receiver.recv().unwrap()
}
fn NamedGetter(self, name: DOMString, found: &mut bool) -> Option<DOMString> {
let item = self.GetItem(name);
*found = item.is_some();
item
}
fn SetItem(self, name: DOMString, value: DOMString) {
let (sender, receiver) = channel();
let msg = StorageTaskMsg::SetItem(sender, self.get_url(), self.storage_type, name.clone(), value.clone());
self.get_storage_task().send(msg).unwrap();
let (changed, old_value) = receiver.recv().unwrap();
if changed {
self.broadcast_change_notification(Some(name), old_value, Some(value));
}
}
fn NamedSetter(self, name: DOMString, value: DOMString) {
self.SetItem(name, value);
}
fn NamedCreator(self, name: DOMString, value: DOMString) {
self.SetItem(name, value);
}
fn RemoveItem(self, name: DOMString) {
let (sender, receiver) = channel();
let msg = StorageTaskMsg::RemoveItem(sender, self.get_url(), self.storage_type, name.clone());
self.get_storage_task().send(msg).unwrap();
if let Some(old_value) = receiver.recv().unwrap() {
self.broadcast_change_notification(Some(name), Some(old_value), None);
}
}
fn NamedDeleter(self, name: DOMString) {
self.RemoveItem(name);
}
fn Clear(self) {
let (sender, receiver) = channel();
self.get_storage_task().send(StorageTaskMsg::Clear(sender, self.get_url(), self.storage_type)).unwrap();
if receiver.recv().unwrap() {
self.broadcast_change_notification(None, None, None);
}
}
}
trait PrivateStorageHelpers {
fn broadcast_change_notification(self, key: Option<DOMString>, old_value: Option<DOMString>,
new_value: Option<DOMString>);
}
impl<'a> PrivateStorageHelpers for &'a 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>){
let global_root = self.global.root();
let global_ref = global_root.r();
let script_chan = global_ref.script_chan();
let trusted_storage = Trusted::new(global_ref.get_cx(), self,
script_chan.clone());
script_chan.send(ScriptMsg::MainThreadRunnableMsg(
box StorageEventRunnable::new(trusted_storage, key,
old_value, new_value))).unwrap();
}
}
pub struct StorageEventRunnable {
element: Trusted<Storage>,
key: Option<DOMString>,
old_value: Option<DOMString>,
new_value: Option<DOMString>
}
impl StorageEventRunnable {
fn new(storage: Trusted<Storage>, key: Option<DOMString>, old_value: Option<DOMString>,
new_value: Option<DOMString>) -> StorageEventRunnable {
StorageEventRunnable { element: storage, key: key, old_value: old_value, new_value: new_value }
}
}
impl MainThreadRunnable for StorageEventRunnable {
fn handler(self: Box<StorageEventRunnable>, script_task: &ScriptTask) {
let this = *self;
let storage_root = this.element.root();
let storage = storage_root.r();
let global_root = storage.global.root();
let global_ref = global_root.r();
let ev_window = global_ref.as_window();
let ev_url = storage.get_url();
let storage_event = StorageEvent::new(
global_ref,
"storage".to_owned(),
EventBubbles::DoesNotBubble, EventCancelable::NotCancelable,
this.key, this.old_value, this.new_value,
ev_url.to_string(),
Some(storage)
);
let event = EventCast::from_ref(storage_event.r());
let root_page = script_task.root_page();
for it_page in root_page.iter() {
let it_window_root = it_page.window();
let it_window = it_window_root.r();
assert!(UrlHelper::SameOrigin(&ev_url, &it_window.get_url()));
// TODO: Such a Document object is not necessarily fully active, but events fired on such
// objects are ignored by the event loop until the Document becomes fully active again.
if ev_window.pipeline() != it_window.pipeline() {
let target = EventTargetCast::from_ref(it_window);
event.fire(target);
}
}
}
}