Bumping manifests a=b2g-bump
[gecko.git] / dom / storage / DOMStorage.h
blobb9ffcce23cd0b3747c15ce6d8a21d8d5c6376203
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef nsDOMStorage_h___
7 #define nsDOMStorage_h___
9 #include "mozilla/Attributes.h"
10 #include "mozilla/ErrorResult.h"
11 #include "nsIDOMStorage.h"
12 #include "nsAutoPtr.h"
13 #include "nsCycleCollectionParticipant.h"
14 #include "nsWeakReference.h"
15 #include "nsWrapperCache.h"
16 #include "nsISupports.h"
18 class nsIPrincipal;
19 class nsIDOMWindow;
21 namespace mozilla {
22 namespace dom {
24 class DOMStorageManager;
25 class DOMStorageCache;
27 class DOMStorage MOZ_FINAL
28 : public nsIDOMStorage
29 , public nsSupportsWeakReference
30 , public nsWrapperCache
32 public:
33 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
34 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(DOMStorage,
35 nsIDOMStorage)
37 enum StorageType {
38 LocalStorage = 1,
39 SessionStorage = 2
42 StorageType GetType() const;
44 DOMStorageManager* GetManager() const
46 return mManager;
49 DOMStorageCache const* GetCache() const
51 return mCache;
54 nsIPrincipal* GetPrincipal();
55 bool PrincipalEquals(nsIPrincipal* aPrincipal);
56 bool CanAccess(nsIPrincipal* aPrincipal);
57 bool IsPrivate()
59 return mIsPrivate;
62 DOMStorage(nsIDOMWindow* aWindow,
63 DOMStorageManager* aManager,
64 DOMStorageCache* aCache,
65 const nsAString& aDocumentURI,
66 nsIPrincipal* aPrincipal,
67 bool aIsPrivate);
69 // WebIDL
70 JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE;
72 nsIDOMWindow* GetParentObject() const
74 return mWindow;
77 uint32_t GetLength(ErrorResult& aRv);
79 void Key(uint32_t aIndex, nsAString& aResult, ErrorResult& aRv);
81 void GetItem(const nsAString& aKey, nsAString& aResult, ErrorResult& aRv);
83 bool NameIsEnumerable(const nsAString& aName) const
85 return true;
88 void GetSupportedNames(unsigned, nsTArray<nsString>& aKeys);
90 void NamedGetter(const nsAString& aKey, bool& aFound, nsAString& aResult,
91 ErrorResult& aRv)
93 GetItem(aKey, aResult, aRv);
94 aFound = !aResult.IsVoid();
97 void SetItem(const nsAString& aKey, const nsAString& aValue,
98 ErrorResult& aRv);
100 void NamedSetter(const nsAString& aKey, const nsAString& aValue,
101 ErrorResult& aRv)
103 SetItem(aKey, aValue, aRv);
106 void RemoveItem(const nsAString& aKey, ErrorResult& aRv);
108 void NamedDeleter(const nsAString& aKey, bool& aFound, ErrorResult& aRv)
110 RemoveItem(aKey, aRv);
112 aFound = (aRv.ErrorCode() != NS_SUCCESS_DOM_NO_OPERATION);
115 void Clear(ErrorResult& aRv);
117 // The method checks whether the caller can use a storage.
118 // CanUseStorage is called before any DOM initiated operation
119 // on a storage is about to happen and ensures that the storage's
120 // session-only flag is properly set according the current settings.
121 // It is an optimization since the privileges check and session only
122 // state determination are complex and share the code (comes hand in
123 // hand together).
124 static bool CanUseStorage(DOMStorage* aStorage = nullptr);
126 bool IsPrivate() const { return mIsPrivate; }
127 bool IsSessionOnly() const { return mIsSessionOnly; }
129 bool IsForkOf(const DOMStorage* aOther) const
131 MOZ_ASSERT(aOther);
132 return mCache == aOther->mCache;
135 private:
136 ~DOMStorage();
138 friend class DOMStorageManager;
139 friend class DOMStorageCache;
141 nsCOMPtr<nsIDOMWindow> mWindow;
142 nsRefPtr<DOMStorageManager> mManager;
143 nsRefPtr<DOMStorageCache> mCache;
144 nsString mDocumentURI;
146 // Principal this DOMStorage (i.e. localStorage or sessionStorage) has
147 // been created for
148 nsCOMPtr<nsIPrincipal> mPrincipal;
150 // Whether this storage is running in private-browsing window.
151 bool mIsPrivate : 1;
153 // Whether storage is set to persist data only per session, may change
154 // dynamically and is set by CanUseStorage function that is called
155 // before any operation on the storage.
156 bool mIsSessionOnly : 1;
158 void BroadcastChangeNotification(const nsSubstring& aKey,
159 const nsSubstring& aOldValue,
160 const nsSubstring& aNewValue);
163 } // ::dom
164 } // ::mozilla
166 #endif /* nsDOMStorage_h___ */