Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=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/. */
7 #ifndef mozilla_dom_indexeddb_idbfactory_h__
8 #define mozilla_dom_indexeddb_idbfactory_h__
10 #include "mozilla/dom/BindingDeclarations.h" // for Optional
11 #include "mozilla/dom/StorageTypeBinding.h"
12 #include "mozilla/dom/quota/PersistenceType.h"
13 #include "mozilla/dom/quota/StoragePrivilege.h"
14 #include "nsCOMPtr.h"
15 #include "nsCycleCollectionParticipant.h"
16 #include "nsWrapperCache.h"
18 class mozIStorageConnection;
19 class nsIFile;
20 class nsIFileURL;
21 class nsIPrincipal;
22 class nsPIDOMWindow;
23 template<typename> class nsRefPtr;
25 namespace mozilla {
26 class ErrorResult;
28 namespace dom {
29 class ContentParent;
30 class IDBOpenDBOptions;
32 namespace indexedDB {
34 struct DatabaseInfo;
35 class IDBDatabase;
36 class IDBOpenDBRequest;
37 class IndexedDBChild;
38 class IndexedDBParent;
40 struct ObjectStoreInfo;
42 class IDBFactory MOZ_FINAL : public nsISupports,
43 public nsWrapperCache
44 {
45 typedef mozilla::dom::ContentParent ContentParent;
46 typedef mozilla::dom::quota::PersistenceType PersistenceType;
47 typedef nsTArray<nsRefPtr<ObjectStoreInfo> > ObjectStoreInfoArray;
48 typedef mozilla::dom::quota::StoragePrivilege StoragePrivilege;
50 public:
51 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
52 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(IDBFactory)
54 // Called when using IndexedDB from a window in a different process.
55 static nsresult Create(nsPIDOMWindow* aWindow,
56 const nsACString& aGroup,
57 const nsACString& aASCIIOrigin,
58 ContentParent* aContentParent,
59 IDBFactory** aFactory);
61 // Called when using IndexedDB from a window in the current process.
62 static nsresult Create(nsPIDOMWindow* aWindow,
63 ContentParent* aContentParent,
64 IDBFactory** aFactory)
65 {
66 return Create(aWindow, EmptyCString(), EmptyCString(), aContentParent,
67 aFactory);
68 }
70 // Called when using IndexedDB from a JS component or a JSM in the current
71 // process.
72 static nsresult Create(JSContext* aCx,
73 JS::Handle<JSObject*> aOwningObject,
74 ContentParent* aContentParent,
75 IDBFactory** aFactory);
77 // Called when using IndexedDB from a JS component or a JSM in a different
78 // process.
79 static nsresult Create(ContentParent* aContentParent,
80 IDBFactory** aFactory);
82 static already_AddRefed<nsIFileURL>
83 GetDatabaseFileURL(nsIFile* aDatabaseFile,
84 PersistenceType aPersistenceType,
85 const nsACString& aGroup,
86 const nsACString& aOrigin);
88 static already_AddRefed<mozIStorageConnection>
89 GetConnection(const nsAString& aDatabaseFilePath,
90 PersistenceType aPersistenceType,
91 const nsACString& aGroup,
92 const nsACString& aOrigin);
94 static nsresult
95 SetDefaultPragmas(mozIStorageConnection* aConnection);
97 static nsresult
98 LoadDatabaseInformation(mozIStorageConnection* aConnection,
99 const nsACString& aDatabaseId,
100 uint64_t* aVersion,
101 ObjectStoreInfoArray& aObjectStores);
103 static nsresult
104 SetDatabaseMetadata(DatabaseInfo* aDatabaseInfo,
105 uint64_t aVersion,
106 ObjectStoreInfoArray& aObjectStores);
108 nsresult
109 OpenInternal(const nsAString& aName,
110 int64_t aVersion,
111 PersistenceType aPersistenceType,
112 const nsACString& aGroup,
113 const nsACString& aASCIIOrigin,
114 StoragePrivilege aStoragePrivilege,
115 bool aDeleting,
116 IDBOpenDBRequest** _retval);
118 nsresult
119 OpenInternal(const nsAString& aName,
120 int64_t aVersion,
121 PersistenceType aPersistenceType,
122 bool aDeleting,
123 IDBOpenDBRequest** _retval)
124 {
125 return OpenInternal(aName, aVersion, aPersistenceType, mGroup, mASCIIOrigin,
126 mPrivilege, aDeleting, _retval);
127 }
129 void
130 SetActor(IndexedDBChild* aActorChild)
131 {
132 NS_ASSERTION(!aActorChild || !mActorChild, "Shouldn't have more than one!");
133 mActorChild = aActorChild;
134 }
136 void
137 SetActor(IndexedDBParent* aActorParent)
138 {
139 NS_ASSERTION(!aActorParent || !mActorParent, "Shouldn't have more than one!");
140 mActorParent = aActorParent;
141 }
143 const nsCString&
144 GetASCIIOrigin() const
145 {
146 return mASCIIOrigin;
147 }
149 bool
150 FromIPC()
151 {
152 return !!mContentParent;
153 }
155 // nsWrapperCache
156 virtual JSObject*
157 WrapObject(JSContext* aCx) MOZ_OVERRIDE;
159 // WebIDL
160 nsPIDOMWindow*
161 GetParentObject() const
162 {
163 return mWindow;
164 }
166 already_AddRefed<IDBOpenDBRequest>
167 Open(const nsAString& aName, uint64_t aVersion, ErrorResult& aRv)
168 {
169 return Open(nullptr, aName, Optional<uint64_t>(aVersion),
170 Optional<mozilla::dom::StorageType>(), false, aRv);
171 }
173 already_AddRefed<IDBOpenDBRequest>
174 Open(const nsAString& aName, const IDBOpenDBOptions& aOptions,
175 ErrorResult& aRv);
177 already_AddRefed<IDBOpenDBRequest>
178 DeleteDatabase(const nsAString& aName, const IDBOpenDBOptions& aOptions,
179 ErrorResult& aRv);
181 int16_t
182 Cmp(JSContext* aCx, JS::Handle<JS::Value> aFirst,
183 JS::Handle<JS::Value> aSecond, ErrorResult& aRv);
185 already_AddRefed<IDBOpenDBRequest>
186 OpenForPrincipal(nsIPrincipal* aPrincipal, const nsAString& aName,
187 uint64_t aVersion, ErrorResult& aRv);
189 already_AddRefed<IDBOpenDBRequest>
190 OpenForPrincipal(nsIPrincipal* aPrincipal, const nsAString& aName,
191 const IDBOpenDBOptions& aOptions, ErrorResult& aRv);
193 already_AddRefed<IDBOpenDBRequest>
194 DeleteForPrincipal(nsIPrincipal* aPrincipal, const nsAString& aName,
195 const IDBOpenDBOptions& aOptions, ErrorResult& aRv);
197 private:
198 IDBFactory();
199 ~IDBFactory();
201 already_AddRefed<IDBOpenDBRequest>
202 Open(nsIPrincipal* aPrincipal, const nsAString& aName,
203 const Optional<uint64_t>& aVersion,
204 const Optional<mozilla::dom::StorageType>& aStorageType, bool aDelete,
205 ErrorResult& aRv);
207 nsCString mGroup;
208 nsCString mASCIIOrigin;
209 StoragePrivilege mPrivilege;
210 PersistenceType mDefaultPersistenceType;
212 // If this factory lives on a window then mWindow must be non-null. Otherwise
213 // mOwningObject must be non-null.
214 nsCOMPtr<nsPIDOMWindow> mWindow;
215 JS::Heap<JSObject*> mOwningObject;
217 IndexedDBChild* mActorChild;
218 IndexedDBParent* mActorParent;
220 mozilla::dom::ContentParent* mContentParent;
222 bool mRootedOwningObject;
223 };
225 } // namespace indexedDB
226 } // namespace dom
227 } // namespace mozilla
229 #endif // mozilla_dom_indexeddb_idbfactory_h__