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_Storage_h
8 : #define mozilla_dom_Storage_h
9 :
10 : #include "mozilla/Attributes.h"
11 : #include "mozilla/ErrorResult.h"
12 : #include "mozilla/Maybe.h"
13 : #include "nsIDOMStorage.h"
14 : #include "nsCycleCollectionParticipant.h"
15 : #include "nsWeakReference.h"
16 : #include "nsWrapperCache.h"
17 : #include "nsISupports.h"
18 :
19 : class nsIPrincipal;
20 : class nsPIDOMWindowInner;
21 :
22 : namespace mozilla {
23 : namespace dom {
24 :
25 : class Storage : public nsIDOMStorage
26 : , public nsWrapperCache
27 : {
28 : public:
29 : NS_DECL_CYCLE_COLLECTING_ISUPPORTS
30 9 : NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(Storage,
31 : nsIDOMStorage)
32 :
33 : Storage(nsPIDOMWindowInner* aWindow, nsIPrincipal* aPrincipal);
34 :
35 : enum StorageType {
36 : eSessionStorage,
37 : eLocalStorage,
38 : };
39 :
40 : virtual StorageType Type() const = 0;
41 :
42 : virtual bool IsForkOf(const Storage* aStorage) const = 0;
43 :
44 : virtual int64_t GetOriginQuotaUsage() const = 0;
45 :
46 : nsIPrincipal*
47 0 : Principal() const
48 : {
49 0 : return mPrincipal;
50 : }
51 :
52 : // WebIDL
53 : JSObject* WrapObject(JSContext* aCx,
54 : JS::Handle<JSObject*> aGivenProto) override;
55 :
56 0 : nsPIDOMWindowInner* GetParentObject() const
57 : {
58 0 : return mWindow;
59 : }
60 :
61 : virtual uint32_t
62 : GetLength(nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) = 0;
63 :
64 : virtual void
65 : Key(uint32_t aIndex, nsAString& aResult,
66 : nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) = 0;
67 :
68 : virtual void
69 : GetItem(const nsAString& aKey, nsAString& aResult,
70 : nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) = 0;
71 :
72 : virtual void
73 : GetSupportedNames(nsTArray<nsString>& aKeys) = 0;
74 :
75 0 : void NamedGetter(const nsAString& aKey, bool& aFound, nsAString& aResult,
76 : nsIPrincipal& aSubjectPrincipal,
77 : ErrorResult& aRv)
78 : {
79 0 : GetItem(aKey, aResult, aSubjectPrincipal, aRv);
80 0 : aFound = !aResult.IsVoid();
81 0 : }
82 :
83 : virtual void
84 : SetItem(const nsAString& aKey, const nsAString& aValue,
85 : nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) = 0;
86 :
87 0 : void NamedSetter(const nsAString& aKey, const nsAString& aValue,
88 : nsIPrincipal& aSubjectPrincipal,
89 : ErrorResult& aRv)
90 : {
91 0 : SetItem(aKey, aValue, aSubjectPrincipal, aRv);
92 0 : }
93 :
94 : virtual void
95 : RemoveItem(const nsAString& aKey, nsIPrincipal& aSubjectPrincipal,
96 : ErrorResult& aRv) = 0;
97 :
98 0 : void NamedDeleter(const nsAString& aKey, bool& aFound,
99 : nsIPrincipal& aSubjectPrincipal,
100 : ErrorResult& aRv)
101 : {
102 0 : RemoveItem(aKey, aSubjectPrincipal, aRv);
103 :
104 0 : aFound = !aRv.ErrorCodeIs(NS_SUCCESS_DOM_NO_OPERATION);
105 0 : }
106 :
107 : virtual void
108 : Clear(nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) = 0;
109 :
110 0 : bool IsSessionOnly() const { return mIsSessionOnly; }
111 :
112 : static void
113 : NotifyChange(Storage* aStorage, nsIPrincipal* aPrincipal,
114 : const nsAString& aKey, const nsAString& aOldValue,
115 : const nsAString& aNewValue, const char16_t* aStorageType,
116 : const nsAString& aDocumentURI, bool aIsPrivate,
117 : bool aImmediateDispatch);
118 :
119 : protected:
120 : virtual ~Storage();
121 :
122 : // The method checks whether the caller can use a storage.
123 : // CanUseStorage is called before any DOM initiated operation
124 : // on a storage is about to happen and ensures that the storage's
125 : // session-only flag is properly set according the current settings.
126 : // It is an optimization since the privileges check and session only
127 : // state determination are complex and share the code (comes hand in
128 : // hand together).
129 : bool CanUseStorage(nsIPrincipal& aSubjectPrincipal);
130 :
131 : private:
132 : nsCOMPtr<nsPIDOMWindowInner> mWindow;
133 : nsCOMPtr<nsIPrincipal> mPrincipal;
134 :
135 : // Whether storage is set to persist data only per session, may change
136 : // dynamically and is set by CanUseStorage function that is called
137 : // before any operation on the storage.
138 : bool mIsSessionOnly : 1;
139 : };
140 :
141 : } // namespace dom
142 : } // namespace mozilla
143 :
144 : #endif // mozilla_dom_Storage_h
|