|
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/. */ |
|
6 |
|
7 #ifndef mozilla_dom_indexeddb_idbfactory_h__ |
|
8 #define mozilla_dom_indexeddb_idbfactory_h__ |
|
9 |
|
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" |
|
17 |
|
18 class mozIStorageConnection; |
|
19 class nsIFile; |
|
20 class nsIFileURL; |
|
21 class nsIPrincipal; |
|
22 class nsPIDOMWindow; |
|
23 template<typename> class nsRefPtr; |
|
24 |
|
25 namespace mozilla { |
|
26 class ErrorResult; |
|
27 |
|
28 namespace dom { |
|
29 class ContentParent; |
|
30 class IDBOpenDBOptions; |
|
31 |
|
32 namespace indexedDB { |
|
33 |
|
34 struct DatabaseInfo; |
|
35 class IDBDatabase; |
|
36 class IDBOpenDBRequest; |
|
37 class IndexedDBChild; |
|
38 class IndexedDBParent; |
|
39 |
|
40 struct ObjectStoreInfo; |
|
41 |
|
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; |
|
49 |
|
50 public: |
|
51 NS_DECL_CYCLE_COLLECTING_ISUPPORTS |
|
52 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(IDBFactory) |
|
53 |
|
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); |
|
60 |
|
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 } |
|
69 |
|
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); |
|
76 |
|
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); |
|
81 |
|
82 static already_AddRefed<nsIFileURL> |
|
83 GetDatabaseFileURL(nsIFile* aDatabaseFile, |
|
84 PersistenceType aPersistenceType, |
|
85 const nsACString& aGroup, |
|
86 const nsACString& aOrigin); |
|
87 |
|
88 static already_AddRefed<mozIStorageConnection> |
|
89 GetConnection(const nsAString& aDatabaseFilePath, |
|
90 PersistenceType aPersistenceType, |
|
91 const nsACString& aGroup, |
|
92 const nsACString& aOrigin); |
|
93 |
|
94 static nsresult |
|
95 SetDefaultPragmas(mozIStorageConnection* aConnection); |
|
96 |
|
97 static nsresult |
|
98 LoadDatabaseInformation(mozIStorageConnection* aConnection, |
|
99 const nsACString& aDatabaseId, |
|
100 uint64_t* aVersion, |
|
101 ObjectStoreInfoArray& aObjectStores); |
|
102 |
|
103 static nsresult |
|
104 SetDatabaseMetadata(DatabaseInfo* aDatabaseInfo, |
|
105 uint64_t aVersion, |
|
106 ObjectStoreInfoArray& aObjectStores); |
|
107 |
|
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); |
|
117 |
|
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 } |
|
128 |
|
129 void |
|
130 SetActor(IndexedDBChild* aActorChild) |
|
131 { |
|
132 NS_ASSERTION(!aActorChild || !mActorChild, "Shouldn't have more than one!"); |
|
133 mActorChild = aActorChild; |
|
134 } |
|
135 |
|
136 void |
|
137 SetActor(IndexedDBParent* aActorParent) |
|
138 { |
|
139 NS_ASSERTION(!aActorParent || !mActorParent, "Shouldn't have more than one!"); |
|
140 mActorParent = aActorParent; |
|
141 } |
|
142 |
|
143 const nsCString& |
|
144 GetASCIIOrigin() const |
|
145 { |
|
146 return mASCIIOrigin; |
|
147 } |
|
148 |
|
149 bool |
|
150 FromIPC() |
|
151 { |
|
152 return !!mContentParent; |
|
153 } |
|
154 |
|
155 // nsWrapperCache |
|
156 virtual JSObject* |
|
157 WrapObject(JSContext* aCx) MOZ_OVERRIDE; |
|
158 |
|
159 // WebIDL |
|
160 nsPIDOMWindow* |
|
161 GetParentObject() const |
|
162 { |
|
163 return mWindow; |
|
164 } |
|
165 |
|
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 } |
|
172 |
|
173 already_AddRefed<IDBOpenDBRequest> |
|
174 Open(const nsAString& aName, const IDBOpenDBOptions& aOptions, |
|
175 ErrorResult& aRv); |
|
176 |
|
177 already_AddRefed<IDBOpenDBRequest> |
|
178 DeleteDatabase(const nsAString& aName, const IDBOpenDBOptions& aOptions, |
|
179 ErrorResult& aRv); |
|
180 |
|
181 int16_t |
|
182 Cmp(JSContext* aCx, JS::Handle<JS::Value> aFirst, |
|
183 JS::Handle<JS::Value> aSecond, ErrorResult& aRv); |
|
184 |
|
185 already_AddRefed<IDBOpenDBRequest> |
|
186 OpenForPrincipal(nsIPrincipal* aPrincipal, const nsAString& aName, |
|
187 uint64_t aVersion, ErrorResult& aRv); |
|
188 |
|
189 already_AddRefed<IDBOpenDBRequest> |
|
190 OpenForPrincipal(nsIPrincipal* aPrincipal, const nsAString& aName, |
|
191 const IDBOpenDBOptions& aOptions, ErrorResult& aRv); |
|
192 |
|
193 already_AddRefed<IDBOpenDBRequest> |
|
194 DeleteForPrincipal(nsIPrincipal* aPrincipal, const nsAString& aName, |
|
195 const IDBOpenDBOptions& aOptions, ErrorResult& aRv); |
|
196 |
|
197 private: |
|
198 IDBFactory(); |
|
199 ~IDBFactory(); |
|
200 |
|
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); |
|
206 |
|
207 nsCString mGroup; |
|
208 nsCString mASCIIOrigin; |
|
209 StoragePrivilege mPrivilege; |
|
210 PersistenceType mDefaultPersistenceType; |
|
211 |
|
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; |
|
216 |
|
217 IndexedDBChild* mActorChild; |
|
218 IndexedDBParent* mActorParent; |
|
219 |
|
220 mozilla::dom::ContentParent* mContentParent; |
|
221 |
|
222 bool mRootedOwningObject; |
|
223 }; |
|
224 |
|
225 } // namespace indexedDB |
|
226 } // namespace dom |
|
227 } // namespace mozilla |
|
228 |
|
229 #endif // mozilla_dom_indexeddb_idbfactory_h__ |