1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #include "mozilla/dom/StorageEvent.h"
8 #include "mozilla/dom/Storage.h"
9 #include "mozilla/dom/StorageEventBinding.h"
10 
11 namespace mozilla::dom {
12 
13 NS_IMPL_CYCLE_COLLECTION_CLASS(StorageEvent)
14 
NS_IMPL_ADDREF_INHERITED(StorageEvent,Event)15 NS_IMPL_ADDREF_INHERITED(StorageEvent, Event)
16 NS_IMPL_RELEASE_INHERITED(StorageEvent, Event)
17 
18 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(StorageEvent, Event)
19   NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mStorageArea)
20 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
21 
22 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(StorageEvent, Event)
23 NS_IMPL_CYCLE_COLLECTION_TRACE_END
24 
25 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(StorageEvent, Event)
26   NS_IMPL_CYCLE_COLLECTION_UNLINK(mStorageArea)
27 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
28 
29 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(StorageEvent)
30 NS_INTERFACE_MAP_END_INHERITING(Event)
31 
32 StorageEvent::StorageEvent(EventTarget* aOwner)
33     : Event(aOwner, nullptr, nullptr) {}
34 
35 StorageEvent::~StorageEvent() = default;
36 
AsStorageEvent()37 StorageEvent* StorageEvent::AsStorageEvent() { return this; }
38 
WrapObjectInternal(JSContext * aCx,JS::Handle<JSObject * > aGivenProto)39 JSObject* StorageEvent::WrapObjectInternal(JSContext* aCx,
40                                            JS::Handle<JSObject*> aGivenProto) {
41   return StorageEvent_Binding::Wrap(aCx, this, aGivenProto);
42 }
43 
Constructor(EventTarget * aOwner,const nsAString & aType,const StorageEventInit & aEventInitDict)44 already_AddRefed<StorageEvent> StorageEvent::Constructor(
45     EventTarget* aOwner, const nsAString& aType,
46     const StorageEventInit& aEventInitDict) {
47   RefPtr<StorageEvent> e = new StorageEvent(aOwner);
48 
49   bool trusted = e->Init(aOwner);
50   e->InitEvent(aType, aEventInitDict.mBubbles, aEventInitDict.mCancelable);
51   e->mKey = aEventInitDict.mKey;
52   e->mOldValue = aEventInitDict.mOldValue;
53   e->mNewValue = aEventInitDict.mNewValue;
54   e->mUrl = aEventInitDict.mUrl;
55   e->mStorageArea = aEventInitDict.mStorageArea;
56   e->SetTrusted(trusted);
57   e->SetComposed(aEventInitDict.mComposed);
58   return e.forget();
59 }
60 
Constructor(const GlobalObject & aGlobal,const nsAString & aType,const StorageEventInit & aEventInitDict)61 already_AddRefed<StorageEvent> StorageEvent::Constructor(
62     const GlobalObject& aGlobal, const nsAString& aType,
63     const StorageEventInit& aEventInitDict) {
64   nsCOMPtr<EventTarget> owner = do_QueryInterface(aGlobal.GetAsSupports());
65   return Constructor(owner, aType, aEventInitDict);
66 }
67 
InitStorageEvent(const nsAString & aType,bool aCanBubble,bool aCancelable,const nsAString & aKey,const nsAString & aOldValue,const nsAString & aNewValue,const nsAString & aURL,Storage * aStorageArea)68 void StorageEvent::InitStorageEvent(const nsAString& aType, bool aCanBubble,
69                                     bool aCancelable, const nsAString& aKey,
70                                     const nsAString& aOldValue,
71                                     const nsAString& aNewValue,
72                                     const nsAString& aURL,
73                                     Storage* aStorageArea) {
74   NS_ENSURE_TRUE_VOID(!mEvent->mFlags.mIsBeingDispatched);
75 
76   InitEvent(aType, aCanBubble, aCancelable);
77   mKey = aKey;
78   mOldValue = aOldValue;
79   mNewValue = aNewValue;
80   mUrl = aURL;
81   mStorageArea = aStorageArea;
82 }
83 
84 }  // namespace mozilla::dom
85