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 : #ifndef mozilla_dom_LocalStorage_h
8 : #define mozilla_dom_LocalStorage_h
9 :
10 : #include "Storage.h"
11 : #include "nsWeakReference.h"
12 :
13 : namespace mozilla {
14 : namespace dom {
15 :
16 : class LocalStorageCache;
17 : class LocalStorageManager;
18 : class StorageEvent;
19 :
20 : class LocalStorage final : public Storage
21 : , public nsSupportsWeakReference
22 : {
23 : public:
24 : NS_DECL_ISUPPORTS_INHERITED
25 1 : NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(LocalStorage, Storage)
26 :
27 0 : StorageType Type() const override { return eLocalStorage; }
28 :
29 : LocalStorageManager* GetManager() const
30 : {
31 : return mManager;
32 : }
33 :
34 0 : LocalStorageCache const* GetCache() const
35 : {
36 0 : return mCache;
37 : }
38 :
39 : bool PrincipalEquals(nsIPrincipal* aPrincipal);
40 :
41 : LocalStorage(nsPIDOMWindowInner* aWindow,
42 : LocalStorageManager* aManager,
43 : LocalStorageCache* aCache,
44 : const nsAString& aDocumentURI,
45 : nsIPrincipal* aPrincipal,
46 : bool aIsPrivate);
47 :
48 : bool IsForkOf(const Storage* aOther) const override;
49 :
50 : // WebIDL
51 :
52 : int64_t GetOriginQuotaUsage() const override;
53 :
54 : uint32_t GetLength(nsIPrincipal& aSubjectPrincipal,
55 : ErrorResult& aRv) override;
56 :
57 : void Key(uint32_t aIndex, nsAString& aResult,
58 : nsIPrincipal& aSubjectPrincipal,
59 : ErrorResult& aRv) override;
60 :
61 : void GetItem(const nsAString& aKey, nsAString& aResult,
62 : nsIPrincipal& aSubjectPrincipal,
63 : ErrorResult& aRv) override;
64 :
65 : void GetSupportedNames(nsTArray<nsString>& aKeys) override;
66 :
67 : void SetItem(const nsAString& aKey, const nsAString& aValue,
68 : nsIPrincipal& aSubjectPrincipal,
69 : ErrorResult& aRv) override;
70 :
71 : void RemoveItem(const nsAString& aKey,
72 : nsIPrincipal& aSubjectPrincipal,
73 : ErrorResult& aRv) override;
74 :
75 : void Clear(nsIPrincipal& aSubjectPrincipal,
76 : ErrorResult& aRv) override;
77 :
78 0 : bool IsPrivate() const { return mIsPrivate; }
79 :
80 : // aStorage can be null if this method is called by ContentChild.
81 : //
82 : // aImmediateDispatch is for use by (main-thread) IPC code so that PContent
83 : // ordering can be maintained. Without this, the event would be enqueued and
84 : // run in a future turn of the event loop, potentially allowing other PContent
85 : // Recv* methods to trigger script that wants to assume our localstorage
86 : // changes have already been applied. This is the case for message manager
87 : // messages which are used by ContentTask testing logic and webextensions.
88 : static void
89 : DispatchStorageEvent(const nsAString& aDocumentURI,
90 : const nsAString& aKey,
91 : const nsAString& aOldValue,
92 : const nsAString& aNewValue,
93 : nsIPrincipal* aPrincipal,
94 : bool aIsPrivate,
95 : Storage* aStorage,
96 : bool aImmediateDispatch);
97 :
98 : void
99 : ApplyEvent(StorageEvent* aStorageEvent);
100 :
101 : private:
102 : ~LocalStorage();
103 :
104 : friend class LocalStorageManager;
105 : friend class LocalStorageCache;
106 :
107 : RefPtr<LocalStorageManager> mManager;
108 : RefPtr<LocalStorageCache> mCache;
109 : nsString mDocumentURI;
110 :
111 : // Principal this Storage (i.e. localStorage or sessionStorage) has
112 : // been created for
113 : nsCOMPtr<nsIPrincipal> mPrincipal;
114 :
115 : // Whether this storage is running in private-browsing window.
116 : bool mIsPrivate : 1;
117 :
118 : void BroadcastChangeNotification(const nsAString& aKey,
119 : const nsAString& aOldValue,
120 : const nsAString& aNewValue);
121 : };
122 :
123 : } // namespace dom
124 : } // namespace mozilla
125 :
126 : #endif // mozilla_dom_LocalStorage_h
|