Line data Source code
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 "Storage.h"
8 : #include "StorageNotifierService.h"
9 :
10 : #include "mozilla/dom/StorageBinding.h"
11 : #include "nsIPrincipal.h"
12 : #include "nsPIDOMWindow.h"
13 :
14 : namespace mozilla {
15 : namespace dom {
16 :
17 0 : NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(Storage, mWindow, mPrincipal)
18 :
19 4 : NS_IMPL_CYCLE_COLLECTING_ADDREF(Storage)
20 3 : NS_IMPL_CYCLE_COLLECTING_RELEASE(Storage)
21 :
22 2 : NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Storage)
23 2 : NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
24 2 : NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMStorage)
25 2 : NS_INTERFACE_MAP_ENTRY(nsIDOMStorage)
26 0 : NS_INTERFACE_MAP_END
27 :
28 1 : Storage::Storage(nsPIDOMWindowInner* aWindow, nsIPrincipal* aPrincipal)
29 : : mWindow(aWindow)
30 : , mPrincipal(aPrincipal)
31 1 : , mIsSessionOnly(false)
32 : {
33 1 : MOZ_ASSERT(aPrincipal);
34 1 : }
35 :
36 0 : Storage::~Storage()
37 0 : {}
38 :
39 : bool
40 0 : Storage::CanUseStorage(nsIPrincipal& aSubjectPrincipal)
41 : {
42 : // This method is responsible for correct setting of mIsSessionOnly.
43 0 : if (!mozilla::Preferences::GetBool(kStorageEnabled)) {
44 0 : return false;
45 : }
46 :
47 : nsContentUtils::StorageAccess access =
48 0 : nsContentUtils::StorageAllowedForPrincipal(Principal());
49 :
50 0 : if (access == nsContentUtils::StorageAccess::eDeny) {
51 0 : return false;
52 : }
53 :
54 0 : mIsSessionOnly = access <= nsContentUtils::StorageAccess::eSessionScoped;
55 :
56 0 : return aSubjectPrincipal.Subsumes(mPrincipal);
57 : }
58 :
59 : /* virtual */ JSObject*
60 0 : Storage::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
61 : {
62 0 : return StorageBinding::Wrap(aCx, this, aGivenProto);
63 : }
64 :
65 : namespace {
66 :
67 0 : class StorageNotifierRunnable : public Runnable
68 : {
69 : public:
70 0 : StorageNotifierRunnable(nsISupports* aSubject, const char16_t *aStorageType,
71 : bool aPrivateBrowsing)
72 0 : : Runnable("StorageNotifierRunnable")
73 : , mSubject(aSubject)
74 : , mStorageType(aStorageType)
75 0 : , mPrivateBrowsing(aPrivateBrowsing)
76 0 : {}
77 :
78 : NS_IMETHOD
79 0 : Run() override
80 : {
81 : nsCOMPtr<nsIObserverService> observerService =
82 0 : mozilla::services::GetObserverService();
83 0 : if (observerService) {
84 0 : observerService->NotifyObservers(mSubject,
85 0 : mPrivateBrowsing
86 : ? "dom-private-storage2-changed"
87 : : "dom-storage2-changed",
88 0 : mStorageType);
89 : }
90 0 : return NS_OK;
91 : }
92 :
93 : private:
94 : nsCOMPtr<nsISupports> mSubject;
95 : const char16_t* mStorageType;
96 : const bool mPrivateBrowsing;
97 : };
98 :
99 : } // namespace
100 :
101 : /* static */ void
102 0 : Storage::NotifyChange(Storage* aStorage, nsIPrincipal* aPrincipal,
103 : const nsAString& aKey,
104 : const nsAString& aOldValue, const nsAString& aNewValue,
105 : const char16_t* aStorageType,
106 : const nsAString& aDocumentURI, bool aIsPrivate,
107 : bool aImmediateDispatch)
108 : {
109 0 : StorageEventInit dict;
110 0 : dict.mBubbles = false;
111 0 : dict.mCancelable = false;
112 0 : dict.mKey = aKey;
113 0 : dict.mNewValue = aNewValue;
114 0 : dict.mOldValue = aOldValue;
115 0 : dict.mStorageArea = aStorage;
116 0 : dict.mUrl = aDocumentURI;
117 :
118 : // Note, this DOM event should never reach JS. It is cloned later in
119 : // nsGlobalWindow.
120 : RefPtr<StorageEvent> event =
121 0 : StorageEvent::Constructor(nullptr, NS_LITERAL_STRING("storage"), dict);
122 :
123 0 : event->SetPrincipal(aPrincipal);
124 :
125 : // This will send the event to any registered window.
126 0 : StorageNotifierService::Broadcast(event, aStorageType, aIsPrivate,
127 0 : aImmediateDispatch);
128 :
129 : // This runnable is mainly used by devtools. Windows receive notification by
130 : // StorageNotifierService.
131 :
132 : RefPtr<StorageNotifierRunnable> r =
133 0 : new StorageNotifierRunnable(event, aStorageType, aIsPrivate);
134 :
135 0 : if (aImmediateDispatch) {
136 0 : Unused << r->Run();
137 : } else {
138 0 : SystemGroup::Dispatch("Storage::NotifyChange", TaskCategory::Other,
139 0 : r.forget());
140 : }
141 0 : }
142 :
143 : } // namespace dom
144 : } // namespace mozilla
|