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"
24 class DOMStorageManager
;
25 class DOMStorageCache
;
27 class DOMStorage MOZ_FINAL
28 : public nsIDOMStorage
29 , public nsSupportsWeakReference
30 , public nsWrapperCache
33 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
34 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(DOMStorage
,
42 StorageType
GetType() const;
44 DOMStorageManager
* GetManager() const
49 DOMStorageCache
const* GetCache() const
54 nsIPrincipal
* GetPrincipal();
55 bool PrincipalEquals(nsIPrincipal
* aPrincipal
);
56 bool CanAccess(nsIPrincipal
* aPrincipal
);
62 DOMStorage(nsIDOMWindow
* aWindow
,
63 DOMStorageManager
* aManager
,
64 DOMStorageCache
* aCache
,
65 const nsAString
& aDocumentURI
,
66 nsIPrincipal
* aPrincipal
,
70 JSObject
* WrapObject(JSContext
* aCx
) MOZ_OVERRIDE
;
72 nsIDOMWindow
* GetParentObject() const
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
88 void GetSupportedNames(unsigned, nsTArray
<nsString
>& aKeys
);
90 void NamedGetter(const nsAString
& aKey
, bool& aFound
, nsAString
& aResult
,
93 GetItem(aKey
, aResult
, aRv
);
94 aFound
= !aResult
.IsVoid();
97 void SetItem(const nsAString
& aKey
, const nsAString
& aValue
,
100 void NamedSetter(const nsAString
& aKey
, const nsAString
& aValue
,
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
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
132 return mCache
== aOther
->mCache
;
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
148 nsCOMPtr
<nsIPrincipal
> mPrincipal
;
150 // Whether this storage is running in private-browsing window.
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
);
166 #endif /* nsDOMStorage_h___ */