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
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim: set ts=2 et sw=2 tw=80: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #include "IDBFactory.h" |
michael@0 | 8 | |
michael@0 | 9 | #include "nsIFile.h" |
michael@0 | 10 | #include "nsIPrincipal.h" |
michael@0 | 11 | #include "nsIScriptContext.h" |
michael@0 | 12 | #include "nsIScriptSecurityManager.h" |
michael@0 | 13 | #include "nsIXPConnect.h" |
michael@0 | 14 | #include "nsIXPCScriptable.h" |
michael@0 | 15 | |
michael@0 | 16 | #include <algorithm> |
michael@0 | 17 | #include "mozilla/dom/ContentParent.h" |
michael@0 | 18 | #include "mozilla/dom/ContentChild.h" |
michael@0 | 19 | #include "mozilla/dom/IDBFactoryBinding.h" |
michael@0 | 20 | #include "mozilla/dom/PBrowserChild.h" |
michael@0 | 21 | #include "mozilla/dom/quota/OriginOrPatternString.h" |
michael@0 | 22 | #include "mozilla/dom/quota/QuotaManager.h" |
michael@0 | 23 | #include "mozilla/dom/TabChild.h" |
michael@0 | 24 | #include "mozilla/Preferences.h" |
michael@0 | 25 | #include "mozilla/storage.h" |
michael@0 | 26 | #include "nsComponentManagerUtils.h" |
michael@0 | 27 | #include "nsCharSeparatedTokenizer.h" |
michael@0 | 28 | #include "nsContentUtils.h" |
michael@0 | 29 | #include "nsCxPusher.h" |
michael@0 | 30 | #include "nsDOMClassInfoID.h" |
michael@0 | 31 | #include "nsGlobalWindow.h" |
michael@0 | 32 | #include "nsHashKeys.h" |
michael@0 | 33 | #include "nsPIDOMWindow.h" |
michael@0 | 34 | #include "nsServiceManagerUtils.h" |
michael@0 | 35 | #include "nsThreadUtils.h" |
michael@0 | 36 | #include "nsXPCOMCID.h" |
michael@0 | 37 | |
michael@0 | 38 | #include "AsyncConnectionHelper.h" |
michael@0 | 39 | #include "CheckPermissionsHelper.h" |
michael@0 | 40 | #include "DatabaseInfo.h" |
michael@0 | 41 | #include "IDBDatabase.h" |
michael@0 | 42 | #include "IDBEvents.h" |
michael@0 | 43 | #include "IDBKeyRange.h" |
michael@0 | 44 | #include "IndexedDatabaseManager.h" |
michael@0 | 45 | #include "Key.h" |
michael@0 | 46 | #include "ProfilerHelpers.h" |
michael@0 | 47 | #include "ReportInternalError.h" |
michael@0 | 48 | #include "nsNetUtil.h" |
michael@0 | 49 | |
michael@0 | 50 | #include "ipc/IndexedDBChild.h" |
michael@0 | 51 | |
michael@0 | 52 | #define PREF_INDEXEDDB_ENABLED "dom.indexedDB.enabled" |
michael@0 | 53 | |
michael@0 | 54 | USING_INDEXEDDB_NAMESPACE |
michael@0 | 55 | USING_QUOTA_NAMESPACE |
michael@0 | 56 | |
michael@0 | 57 | using mozilla::dom::ContentChild; |
michael@0 | 58 | using mozilla::dom::ContentParent; |
michael@0 | 59 | using mozilla::dom::IDBOpenDBOptions; |
michael@0 | 60 | using mozilla::dom::NonNull; |
michael@0 | 61 | using mozilla::dom::Optional; |
michael@0 | 62 | using mozilla::dom::TabChild; |
michael@0 | 63 | using mozilla::ErrorResult; |
michael@0 | 64 | using mozilla::Preferences; |
michael@0 | 65 | |
michael@0 | 66 | namespace { |
michael@0 | 67 | |
michael@0 | 68 | struct ObjectStoreInfoMap |
michael@0 | 69 | { |
michael@0 | 70 | ObjectStoreInfoMap() |
michael@0 | 71 | : id(INT64_MIN), info(nullptr) { } |
michael@0 | 72 | |
michael@0 | 73 | int64_t id; |
michael@0 | 74 | ObjectStoreInfo* info; |
michael@0 | 75 | }; |
michael@0 | 76 | |
michael@0 | 77 | } // anonymous namespace |
michael@0 | 78 | |
michael@0 | 79 | IDBFactory::IDBFactory() |
michael@0 | 80 | : mPrivilege(Content), mDefaultPersistenceType(PERSISTENCE_TYPE_TEMPORARY), |
michael@0 | 81 | mOwningObject(nullptr), mActorChild(nullptr), mActorParent(nullptr), |
michael@0 | 82 | mContentParent(nullptr), mRootedOwningObject(false) |
michael@0 | 83 | { |
michael@0 | 84 | SetIsDOMBinding(); |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | IDBFactory::~IDBFactory() |
michael@0 | 88 | { |
michael@0 | 89 | NS_ASSERTION(!mActorParent, "Actor parent owns us, how can we be dying?!"); |
michael@0 | 90 | if (mActorChild) { |
michael@0 | 91 | NS_ASSERTION(!IndexedDatabaseManager::IsMainProcess(), "Wrong process!"); |
michael@0 | 92 | mActorChild->Send__delete__(mActorChild); |
michael@0 | 93 | NS_ASSERTION(!mActorChild, "Should have cleared in Send__delete__!"); |
michael@0 | 94 | } |
michael@0 | 95 | if (mRootedOwningObject) { |
michael@0 | 96 | mOwningObject = nullptr; |
michael@0 | 97 | mozilla::DropJSObjects(this); |
michael@0 | 98 | } |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | // static |
michael@0 | 102 | nsresult |
michael@0 | 103 | IDBFactory::Create(nsPIDOMWindow* aWindow, |
michael@0 | 104 | const nsACString& aGroup, |
michael@0 | 105 | const nsACString& aASCIIOrigin, |
michael@0 | 106 | ContentParent* aContentParent, |
michael@0 | 107 | IDBFactory** aFactory) |
michael@0 | 108 | { |
michael@0 | 109 | NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); |
michael@0 | 110 | NS_ASSERTION(aASCIIOrigin.IsEmpty() || nsContentUtils::IsCallerChrome(), |
michael@0 | 111 | "Non-chrome may not supply their own origin!"); |
michael@0 | 112 | |
michael@0 | 113 | IDB_ENSURE_TRUE(aWindow, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR); |
michael@0 | 114 | |
michael@0 | 115 | if (aWindow->IsOuterWindow()) { |
michael@0 | 116 | aWindow = aWindow->GetCurrentInnerWindow(); |
michael@0 | 117 | IDB_ENSURE_TRUE(aWindow, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR); |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | // Make sure that the manager is up before we do anything here since lots of |
michael@0 | 121 | // decisions depend on which process we're running in. |
michael@0 | 122 | indexedDB::IndexedDatabaseManager* mgr = |
michael@0 | 123 | indexedDB::IndexedDatabaseManager::GetOrCreate(); |
michael@0 | 124 | IDB_ENSURE_TRUE(mgr, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR); |
michael@0 | 125 | |
michael@0 | 126 | nsresult rv; |
michael@0 | 127 | |
michael@0 | 128 | nsCString group(aGroup); |
michael@0 | 129 | nsCString origin(aASCIIOrigin); |
michael@0 | 130 | StoragePrivilege privilege; |
michael@0 | 131 | PersistenceType defaultPersistenceType; |
michael@0 | 132 | if (origin.IsEmpty()) { |
michael@0 | 133 | NS_ASSERTION(aGroup.IsEmpty(), "Should be empty too!"); |
michael@0 | 134 | |
michael@0 | 135 | rv = QuotaManager::GetInfoFromWindow(aWindow, &group, &origin, &privilege, |
michael@0 | 136 | &defaultPersistenceType); |
michael@0 | 137 | } |
michael@0 | 138 | else { |
michael@0 | 139 | rv = QuotaManager::GetInfoFromWindow(aWindow, nullptr, nullptr, &privilege, |
michael@0 | 140 | &defaultPersistenceType); |
michael@0 | 141 | } |
michael@0 | 142 | if (NS_FAILED(rv)) { |
michael@0 | 143 | // Not allowed. |
michael@0 | 144 | *aFactory = nullptr; |
michael@0 | 145 | return NS_OK; |
michael@0 | 146 | } |
michael@0 | 147 | |
michael@0 | 148 | nsRefPtr<IDBFactory> factory = new IDBFactory(); |
michael@0 | 149 | factory->mGroup = group; |
michael@0 | 150 | factory->mASCIIOrigin = origin; |
michael@0 | 151 | factory->mPrivilege = privilege; |
michael@0 | 152 | factory->mDefaultPersistenceType = defaultPersistenceType; |
michael@0 | 153 | factory->mWindow = aWindow; |
michael@0 | 154 | factory->mContentParent = aContentParent; |
michael@0 | 155 | |
michael@0 | 156 | if (!IndexedDatabaseManager::IsMainProcess()) { |
michael@0 | 157 | TabChild* tabChild = TabChild::GetFrom(aWindow); |
michael@0 | 158 | IDB_ENSURE_TRUE(tabChild, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR); |
michael@0 | 159 | |
michael@0 | 160 | IndexedDBChild* actor = new IndexedDBChild(origin); |
michael@0 | 161 | |
michael@0 | 162 | bool allowed; |
michael@0 | 163 | tabChild->SendPIndexedDBConstructor(actor, group, origin, &allowed); |
michael@0 | 164 | |
michael@0 | 165 | if (!allowed) { |
michael@0 | 166 | actor->Send__delete__(actor); |
michael@0 | 167 | *aFactory = nullptr; |
michael@0 | 168 | return NS_OK; |
michael@0 | 169 | } |
michael@0 | 170 | |
michael@0 | 171 | actor->SetFactory(factory); |
michael@0 | 172 | } |
michael@0 | 173 | |
michael@0 | 174 | factory.forget(aFactory); |
michael@0 | 175 | return NS_OK; |
michael@0 | 176 | } |
michael@0 | 177 | |
michael@0 | 178 | // static |
michael@0 | 179 | nsresult |
michael@0 | 180 | IDBFactory::Create(JSContext* aCx, |
michael@0 | 181 | JS::Handle<JSObject*> aOwningObject, |
michael@0 | 182 | ContentParent* aContentParent, |
michael@0 | 183 | IDBFactory** aFactory) |
michael@0 | 184 | { |
michael@0 | 185 | NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); |
michael@0 | 186 | NS_ASSERTION(aCx, "Null context!"); |
michael@0 | 187 | NS_ASSERTION(aOwningObject, "Null object!"); |
michael@0 | 188 | NS_ASSERTION(JS_GetGlobalForObject(aCx, aOwningObject) == aOwningObject, |
michael@0 | 189 | "Not a global object!"); |
michael@0 | 190 | NS_ASSERTION(nsContentUtils::IsCallerChrome(), "Only for chrome!"); |
michael@0 | 191 | |
michael@0 | 192 | // Make sure that the manager is up before we do anything here since lots of |
michael@0 | 193 | // decisions depend on which process we're running in. |
michael@0 | 194 | IndexedDatabaseManager* mgr = IndexedDatabaseManager::GetOrCreate(); |
michael@0 | 195 | IDB_ENSURE_TRUE(mgr, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR); |
michael@0 | 196 | |
michael@0 | 197 | nsCString group; |
michael@0 | 198 | nsCString origin; |
michael@0 | 199 | StoragePrivilege privilege; |
michael@0 | 200 | PersistenceType defaultPersistenceType; |
michael@0 | 201 | QuotaManager::GetInfoForChrome(&group, &origin, &privilege, |
michael@0 | 202 | &defaultPersistenceType); |
michael@0 | 203 | |
michael@0 | 204 | nsRefPtr<IDBFactory> factory = new IDBFactory(); |
michael@0 | 205 | factory->mGroup = group; |
michael@0 | 206 | factory->mASCIIOrigin = origin; |
michael@0 | 207 | factory->mPrivilege = privilege; |
michael@0 | 208 | factory->mDefaultPersistenceType = defaultPersistenceType; |
michael@0 | 209 | factory->mOwningObject = aOwningObject; |
michael@0 | 210 | factory->mContentParent = aContentParent; |
michael@0 | 211 | |
michael@0 | 212 | mozilla::HoldJSObjects(factory.get()); |
michael@0 | 213 | factory->mRootedOwningObject = true; |
michael@0 | 214 | |
michael@0 | 215 | if (!IndexedDatabaseManager::IsMainProcess()) { |
michael@0 | 216 | ContentChild* contentChild = ContentChild::GetSingleton(); |
michael@0 | 217 | IDB_ENSURE_TRUE(contentChild, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR); |
michael@0 | 218 | |
michael@0 | 219 | IndexedDBChild* actor = new IndexedDBChild(origin); |
michael@0 | 220 | |
michael@0 | 221 | contentChild->SendPIndexedDBConstructor(actor); |
michael@0 | 222 | |
michael@0 | 223 | actor->SetFactory(factory); |
michael@0 | 224 | } |
michael@0 | 225 | |
michael@0 | 226 | factory.forget(aFactory); |
michael@0 | 227 | return NS_OK; |
michael@0 | 228 | } |
michael@0 | 229 | |
michael@0 | 230 | // static |
michael@0 | 231 | nsresult |
michael@0 | 232 | IDBFactory::Create(ContentParent* aContentParent, |
michael@0 | 233 | IDBFactory** aFactory) |
michael@0 | 234 | { |
michael@0 | 235 | NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); |
michael@0 | 236 | NS_ASSERTION(IndexedDatabaseManager::IsMainProcess(), "Wrong process!"); |
michael@0 | 237 | NS_ASSERTION(nsContentUtils::IsCallerChrome(), "Only for chrome!"); |
michael@0 | 238 | NS_ASSERTION(aContentParent, "Null ContentParent!"); |
michael@0 | 239 | |
michael@0 | 240 | NS_ASSERTION(!nsContentUtils::GetCurrentJSContext(), "Should be called from C++"); |
michael@0 | 241 | |
michael@0 | 242 | // We need to get this information before we push a null principal to avoid |
michael@0 | 243 | // IsCallerChrome() assertion in quota manager. |
michael@0 | 244 | nsCString group; |
michael@0 | 245 | nsCString origin; |
michael@0 | 246 | StoragePrivilege privilege; |
michael@0 | 247 | PersistenceType defaultPersistenceType; |
michael@0 | 248 | QuotaManager::GetInfoForChrome(&group, &origin, &privilege, |
michael@0 | 249 | &defaultPersistenceType); |
michael@0 | 250 | |
michael@0 | 251 | nsCOMPtr<nsIPrincipal> principal = |
michael@0 | 252 | do_CreateInstance("@mozilla.org/nullprincipal;1"); |
michael@0 | 253 | NS_ENSURE_TRUE(principal, NS_ERROR_FAILURE); |
michael@0 | 254 | |
michael@0 | 255 | AutoSafeJSContext cx; |
michael@0 | 256 | |
michael@0 | 257 | nsIXPConnect* xpc = nsContentUtils::XPConnect(); |
michael@0 | 258 | NS_ASSERTION(xpc, "This should never be null!"); |
michael@0 | 259 | |
michael@0 | 260 | nsCOMPtr<nsIXPConnectJSObjectHolder> globalHolder; |
michael@0 | 261 | nsresult rv = xpc->CreateSandbox(cx, principal, getter_AddRefs(globalHolder)); |
michael@0 | 262 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 263 | |
michael@0 | 264 | JS::Rooted<JSObject*> global(cx, globalHolder->GetJSObject()); |
michael@0 | 265 | NS_ENSURE_STATE(global); |
michael@0 | 266 | |
michael@0 | 267 | // The CreateSandbox call returns a proxy to the actual sandbox object. We |
michael@0 | 268 | // don't need a proxy here. |
michael@0 | 269 | global = js::UncheckedUnwrap(global); |
michael@0 | 270 | |
michael@0 | 271 | JSAutoCompartment ac(cx, global); |
michael@0 | 272 | |
michael@0 | 273 | nsRefPtr<IDBFactory> factory = new IDBFactory(); |
michael@0 | 274 | factory->mGroup = group; |
michael@0 | 275 | factory->mASCIIOrigin = origin; |
michael@0 | 276 | factory->mPrivilege = privilege; |
michael@0 | 277 | factory->mDefaultPersistenceType = defaultPersistenceType; |
michael@0 | 278 | factory->mOwningObject = global; |
michael@0 | 279 | factory->mContentParent = aContentParent; |
michael@0 | 280 | |
michael@0 | 281 | mozilla::HoldJSObjects(factory.get()); |
michael@0 | 282 | factory->mRootedOwningObject = true; |
michael@0 | 283 | |
michael@0 | 284 | factory.forget(aFactory); |
michael@0 | 285 | return NS_OK; |
michael@0 | 286 | } |
michael@0 | 287 | |
michael@0 | 288 | // static |
michael@0 | 289 | already_AddRefed<nsIFileURL> |
michael@0 | 290 | IDBFactory::GetDatabaseFileURL(nsIFile* aDatabaseFile, |
michael@0 | 291 | PersistenceType aPersistenceType, |
michael@0 | 292 | const nsACString& aGroup, |
michael@0 | 293 | const nsACString& aOrigin) |
michael@0 | 294 | { |
michael@0 | 295 | nsCOMPtr<nsIURI> uri; |
michael@0 | 296 | nsresult rv = NS_NewFileURI(getter_AddRefs(uri), aDatabaseFile); |
michael@0 | 297 | NS_ENSURE_SUCCESS(rv, nullptr); |
michael@0 | 298 | |
michael@0 | 299 | nsCOMPtr<nsIFileURL> fileUrl = do_QueryInterface(uri); |
michael@0 | 300 | NS_ASSERTION(fileUrl, "This should always succeed!"); |
michael@0 | 301 | |
michael@0 | 302 | nsAutoCString type; |
michael@0 | 303 | PersistenceTypeToText(aPersistenceType, type); |
michael@0 | 304 | |
michael@0 | 305 | rv = fileUrl->SetQuery(NS_LITERAL_CSTRING("persistenceType=") + type + |
michael@0 | 306 | NS_LITERAL_CSTRING("&group=") + aGroup + |
michael@0 | 307 | NS_LITERAL_CSTRING("&origin=") + aOrigin); |
michael@0 | 308 | NS_ENSURE_SUCCESS(rv, nullptr); |
michael@0 | 309 | |
michael@0 | 310 | return fileUrl.forget(); |
michael@0 | 311 | } |
michael@0 | 312 | |
michael@0 | 313 | // static |
michael@0 | 314 | already_AddRefed<mozIStorageConnection> |
michael@0 | 315 | IDBFactory::GetConnection(const nsAString& aDatabaseFilePath, |
michael@0 | 316 | PersistenceType aPersistenceType, |
michael@0 | 317 | const nsACString& aGroup, |
michael@0 | 318 | const nsACString& aOrigin) |
michael@0 | 319 | { |
michael@0 | 320 | NS_ASSERTION(IndexedDatabaseManager::IsMainProcess(), "Wrong process!"); |
michael@0 | 321 | NS_ASSERTION(StringEndsWith(aDatabaseFilePath, NS_LITERAL_STRING(".sqlite")), |
michael@0 | 322 | "Bad file path!"); |
michael@0 | 323 | |
michael@0 | 324 | nsCOMPtr<nsIFile> dbFile(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID)); |
michael@0 | 325 | NS_ENSURE_TRUE(dbFile, nullptr); |
michael@0 | 326 | |
michael@0 | 327 | nsresult rv = dbFile->InitWithPath(aDatabaseFilePath); |
michael@0 | 328 | NS_ENSURE_SUCCESS(rv, nullptr); |
michael@0 | 329 | |
michael@0 | 330 | bool exists; |
michael@0 | 331 | rv = dbFile->Exists(&exists); |
michael@0 | 332 | NS_ENSURE_SUCCESS(rv, nullptr); |
michael@0 | 333 | NS_ENSURE_TRUE(exists, nullptr); |
michael@0 | 334 | |
michael@0 | 335 | nsCOMPtr<nsIFileURL> dbFileUrl = |
michael@0 | 336 | GetDatabaseFileURL(dbFile, aPersistenceType, aGroup, aOrigin); |
michael@0 | 337 | NS_ENSURE_TRUE(dbFileUrl, nullptr); |
michael@0 | 338 | |
michael@0 | 339 | nsCOMPtr<mozIStorageService> ss = |
michael@0 | 340 | do_GetService(MOZ_STORAGE_SERVICE_CONTRACTID); |
michael@0 | 341 | NS_ENSURE_TRUE(ss, nullptr); |
michael@0 | 342 | |
michael@0 | 343 | nsCOMPtr<mozIStorageConnection> connection; |
michael@0 | 344 | rv = ss->OpenDatabaseWithFileURL(dbFileUrl, getter_AddRefs(connection)); |
michael@0 | 345 | NS_ENSURE_SUCCESS(rv, nullptr); |
michael@0 | 346 | |
michael@0 | 347 | rv = SetDefaultPragmas(connection); |
michael@0 | 348 | NS_ENSURE_SUCCESS(rv, nullptr); |
michael@0 | 349 | |
michael@0 | 350 | return connection.forget(); |
michael@0 | 351 | } |
michael@0 | 352 | |
michael@0 | 353 | // static |
michael@0 | 354 | nsresult |
michael@0 | 355 | IDBFactory::SetDefaultPragmas(mozIStorageConnection* aConnection) |
michael@0 | 356 | { |
michael@0 | 357 | NS_ASSERTION(aConnection, "Null connection!"); |
michael@0 | 358 | |
michael@0 | 359 | static const char query[] = |
michael@0 | 360 | #if defined(MOZ_WIDGET_ANDROID) || defined(MOZ_WIDGET_GONK) |
michael@0 | 361 | // Switch the journaling mode to TRUNCATE to avoid changing the directory |
michael@0 | 362 | // structure at the conclusion of every transaction for devices with slower |
michael@0 | 363 | // file systems. |
michael@0 | 364 | "PRAGMA journal_mode = TRUNCATE; " |
michael@0 | 365 | #endif |
michael@0 | 366 | // We use foreign keys in lots of places. |
michael@0 | 367 | "PRAGMA foreign_keys = ON; " |
michael@0 | 368 | // The "INSERT OR REPLACE" statement doesn't fire the update trigger, |
michael@0 | 369 | // instead it fires only the insert trigger. This confuses the update |
michael@0 | 370 | // refcount function. This behavior changes with enabled recursive triggers, |
michael@0 | 371 | // so the statement fires the delete trigger first and then the insert |
michael@0 | 372 | // trigger. |
michael@0 | 373 | "PRAGMA recursive_triggers = ON;"; |
michael@0 | 374 | |
michael@0 | 375 | nsresult rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(query)); |
michael@0 | 376 | IDB_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR); |
michael@0 | 377 | |
michael@0 | 378 | return NS_OK; |
michael@0 | 379 | } |
michael@0 | 380 | |
michael@0 | 381 | inline |
michael@0 | 382 | bool |
michael@0 | 383 | IgnoreWhitespace(char16_t c) |
michael@0 | 384 | { |
michael@0 | 385 | return false; |
michael@0 | 386 | } |
michael@0 | 387 | |
michael@0 | 388 | // static |
michael@0 | 389 | nsresult |
michael@0 | 390 | IDBFactory::LoadDatabaseInformation(mozIStorageConnection* aConnection, |
michael@0 | 391 | const nsACString& aDatabaseId, |
michael@0 | 392 | uint64_t* aVersion, |
michael@0 | 393 | ObjectStoreInfoArray& aObjectStores) |
michael@0 | 394 | { |
michael@0 | 395 | AssertIsOnIOThread(); |
michael@0 | 396 | NS_ASSERTION(aConnection, "Null pointer!"); |
michael@0 | 397 | |
michael@0 | 398 | aObjectStores.Clear(); |
michael@0 | 399 | |
michael@0 | 400 | // Load object store names and ids. |
michael@0 | 401 | nsCOMPtr<mozIStorageStatement> stmt; |
michael@0 | 402 | nsresult rv = aConnection->CreateStatement(NS_LITERAL_CSTRING( |
michael@0 | 403 | "SELECT name, id, key_path, auto_increment " |
michael@0 | 404 | "FROM object_store" |
michael@0 | 405 | ), getter_AddRefs(stmt)); |
michael@0 | 406 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 407 | |
michael@0 | 408 | nsAutoTArray<ObjectStoreInfoMap, 20> infoMap; |
michael@0 | 409 | |
michael@0 | 410 | bool hasResult; |
michael@0 | 411 | while (NS_SUCCEEDED((rv = stmt->ExecuteStep(&hasResult))) && hasResult) { |
michael@0 | 412 | nsRefPtr<ObjectStoreInfo>* element = |
michael@0 | 413 | aObjectStores.AppendElement(new ObjectStoreInfo()); |
michael@0 | 414 | |
michael@0 | 415 | ObjectStoreInfo* info = element->get(); |
michael@0 | 416 | |
michael@0 | 417 | rv = stmt->GetString(0, info->name); |
michael@0 | 418 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 419 | |
michael@0 | 420 | info->id = stmt->AsInt64(1); |
michael@0 | 421 | |
michael@0 | 422 | int32_t columnType; |
michael@0 | 423 | nsresult rv = stmt->GetTypeOfIndex(2, &columnType); |
michael@0 | 424 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 425 | |
michael@0 | 426 | // NB: We don't have to handle the NULL case, since that is the default |
michael@0 | 427 | // for a new KeyPath. |
michael@0 | 428 | if (columnType != mozIStorageStatement::VALUE_TYPE_NULL) { |
michael@0 | 429 | NS_ASSERTION(columnType == mozIStorageStatement::VALUE_TYPE_TEXT, |
michael@0 | 430 | "Should be a string"); |
michael@0 | 431 | nsString keyPathSerialization; |
michael@0 | 432 | rv = stmt->GetString(2, keyPathSerialization); |
michael@0 | 433 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 434 | |
michael@0 | 435 | info->keyPath = KeyPath::DeserializeFromString(keyPathSerialization); |
michael@0 | 436 | } |
michael@0 | 437 | |
michael@0 | 438 | info->nextAutoIncrementId = stmt->AsInt64(3); |
michael@0 | 439 | info->comittedAutoIncrementId = info->nextAutoIncrementId; |
michael@0 | 440 | |
michael@0 | 441 | info->autoIncrement = !!info->nextAutoIncrementId; |
michael@0 | 442 | |
michael@0 | 443 | ObjectStoreInfoMap* mapEntry = infoMap.AppendElement(); |
michael@0 | 444 | NS_ENSURE_TRUE(mapEntry, NS_ERROR_OUT_OF_MEMORY); |
michael@0 | 445 | |
michael@0 | 446 | mapEntry->id = info->id; |
michael@0 | 447 | mapEntry->info = info; |
michael@0 | 448 | } |
michael@0 | 449 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 450 | |
michael@0 | 451 | // Load index information |
michael@0 | 452 | rv = aConnection->CreateStatement(NS_LITERAL_CSTRING( |
michael@0 | 453 | "SELECT object_store_id, id, name, key_path, unique_index, multientry " |
michael@0 | 454 | "FROM object_store_index" |
michael@0 | 455 | ), getter_AddRefs(stmt)); |
michael@0 | 456 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 457 | |
michael@0 | 458 | while (NS_SUCCEEDED((rv = stmt->ExecuteStep(&hasResult))) && hasResult) { |
michael@0 | 459 | int64_t objectStoreId = stmt->AsInt64(0); |
michael@0 | 460 | |
michael@0 | 461 | ObjectStoreInfo* objectStoreInfo = nullptr; |
michael@0 | 462 | for (uint32_t index = 0; index < infoMap.Length(); index++) { |
michael@0 | 463 | if (infoMap[index].id == objectStoreId) { |
michael@0 | 464 | objectStoreInfo = infoMap[index].info; |
michael@0 | 465 | break; |
michael@0 | 466 | } |
michael@0 | 467 | } |
michael@0 | 468 | |
michael@0 | 469 | if (!objectStoreInfo) { |
michael@0 | 470 | NS_ERROR("Index for nonexistant object store!"); |
michael@0 | 471 | return NS_ERROR_UNEXPECTED; |
michael@0 | 472 | } |
michael@0 | 473 | |
michael@0 | 474 | IndexInfo* indexInfo = objectStoreInfo->indexes.AppendElement(); |
michael@0 | 475 | NS_ENSURE_TRUE(indexInfo, NS_ERROR_OUT_OF_MEMORY); |
michael@0 | 476 | |
michael@0 | 477 | indexInfo->id = stmt->AsInt64(1); |
michael@0 | 478 | |
michael@0 | 479 | rv = stmt->GetString(2, indexInfo->name); |
michael@0 | 480 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 481 | |
michael@0 | 482 | nsString keyPathSerialization; |
michael@0 | 483 | rv = stmt->GetString(3, keyPathSerialization); |
michael@0 | 484 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 485 | |
michael@0 | 486 | // XXX bent wants to assert here |
michael@0 | 487 | indexInfo->keyPath = KeyPath::DeserializeFromString(keyPathSerialization); |
michael@0 | 488 | indexInfo->unique = !!stmt->AsInt32(4); |
michael@0 | 489 | indexInfo->multiEntry = !!stmt->AsInt32(5); |
michael@0 | 490 | } |
michael@0 | 491 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 492 | |
michael@0 | 493 | // Load version information. |
michael@0 | 494 | rv = aConnection->CreateStatement(NS_LITERAL_CSTRING( |
michael@0 | 495 | "SELECT version " |
michael@0 | 496 | "FROM database" |
michael@0 | 497 | ), getter_AddRefs(stmt)); |
michael@0 | 498 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 499 | |
michael@0 | 500 | rv = stmt->ExecuteStep(&hasResult); |
michael@0 | 501 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 502 | |
michael@0 | 503 | if (!hasResult) { |
michael@0 | 504 | NS_ERROR("Database has no version!"); |
michael@0 | 505 | return NS_ERROR_UNEXPECTED; |
michael@0 | 506 | } |
michael@0 | 507 | |
michael@0 | 508 | int64_t version = 0; |
michael@0 | 509 | rv = stmt->GetInt64(0, &version); |
michael@0 | 510 | |
michael@0 | 511 | *aVersion = std::max<int64_t>(version, 0); |
michael@0 | 512 | |
michael@0 | 513 | return rv; |
michael@0 | 514 | } |
michael@0 | 515 | |
michael@0 | 516 | // static |
michael@0 | 517 | nsresult |
michael@0 | 518 | IDBFactory::SetDatabaseMetadata(DatabaseInfo* aDatabaseInfo, |
michael@0 | 519 | uint64_t aVersion, |
michael@0 | 520 | ObjectStoreInfoArray& aObjectStores) |
michael@0 | 521 | { |
michael@0 | 522 | NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); |
michael@0 | 523 | NS_ASSERTION(aDatabaseInfo, "Null pointer!"); |
michael@0 | 524 | |
michael@0 | 525 | ObjectStoreInfoArray objectStores; |
michael@0 | 526 | objectStores.SwapElements(aObjectStores); |
michael@0 | 527 | |
michael@0 | 528 | #ifdef DEBUG |
michael@0 | 529 | { |
michael@0 | 530 | nsTArray<nsString> existingNames; |
michael@0 | 531 | aDatabaseInfo->GetObjectStoreNames(existingNames); |
michael@0 | 532 | NS_ASSERTION(existingNames.IsEmpty(), "Should be an empty DatabaseInfo"); |
michael@0 | 533 | } |
michael@0 | 534 | #endif |
michael@0 | 535 | |
michael@0 | 536 | aDatabaseInfo->version = aVersion; |
michael@0 | 537 | |
michael@0 | 538 | for (uint32_t index = 0; index < objectStores.Length(); index++) { |
michael@0 | 539 | nsRefPtr<ObjectStoreInfo>& info = objectStores[index]; |
michael@0 | 540 | |
michael@0 | 541 | if (!aDatabaseInfo->PutObjectStore(info)) { |
michael@0 | 542 | NS_WARNING("Out of memory!"); |
michael@0 | 543 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 544 | } |
michael@0 | 545 | } |
michael@0 | 546 | |
michael@0 | 547 | return NS_OK; |
michael@0 | 548 | } |
michael@0 | 549 | |
michael@0 | 550 | NS_IMPL_CYCLE_COLLECTING_ADDREF(IDBFactory) |
michael@0 | 551 | NS_IMPL_CYCLE_COLLECTING_RELEASE(IDBFactory) |
michael@0 | 552 | |
michael@0 | 553 | NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(IDBFactory) |
michael@0 | 554 | NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY |
michael@0 | 555 | NS_INTERFACE_MAP_ENTRY(nsISupports) |
michael@0 | 556 | NS_INTERFACE_MAP_END |
michael@0 | 557 | |
michael@0 | 558 | NS_IMPL_CYCLE_COLLECTION_CLASS(IDBFactory) |
michael@0 | 559 | |
michael@0 | 560 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(IDBFactory) |
michael@0 | 561 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS |
michael@0 | 562 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mWindow) |
michael@0 | 563 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END |
michael@0 | 564 | |
michael@0 | 565 | NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(IDBFactory) |
michael@0 | 566 | NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER |
michael@0 | 567 | if (tmp->mOwningObject) { |
michael@0 | 568 | tmp->mOwningObject = nullptr; |
michael@0 | 569 | } |
michael@0 | 570 | if (tmp->mRootedOwningObject) { |
michael@0 | 571 | mozilla::DropJSObjects(tmp); |
michael@0 | 572 | tmp->mRootedOwningObject = false; |
michael@0 | 573 | } |
michael@0 | 574 | NS_IMPL_CYCLE_COLLECTION_UNLINK(mWindow) |
michael@0 | 575 | NS_IMPL_CYCLE_COLLECTION_UNLINK_END |
michael@0 | 576 | |
michael@0 | 577 | NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(IDBFactory) |
michael@0 | 578 | NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER |
michael@0 | 579 | NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mOwningObject) |
michael@0 | 580 | NS_IMPL_CYCLE_COLLECTION_TRACE_END |
michael@0 | 581 | |
michael@0 | 582 | nsresult |
michael@0 | 583 | IDBFactory::OpenInternal(const nsAString& aName, |
michael@0 | 584 | int64_t aVersion, |
michael@0 | 585 | PersistenceType aPersistenceType, |
michael@0 | 586 | const nsACString& aGroup, |
michael@0 | 587 | const nsACString& aASCIIOrigin, |
michael@0 | 588 | StoragePrivilege aPrivilege, |
michael@0 | 589 | bool aDeleting, |
michael@0 | 590 | IDBOpenDBRequest** _retval) |
michael@0 | 591 | { |
michael@0 | 592 | NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); |
michael@0 | 593 | NS_ASSERTION(mWindow || mOwningObject, "Must have one of these!"); |
michael@0 | 594 | |
michael@0 | 595 | AutoJSContext cx; |
michael@0 | 596 | nsCOMPtr<nsPIDOMWindow> window; |
michael@0 | 597 | JS::Rooted<JSObject*> scriptOwner(cx); |
michael@0 | 598 | |
michael@0 | 599 | if (mWindow) { |
michael@0 | 600 | window = mWindow; |
michael@0 | 601 | scriptOwner = |
michael@0 | 602 | static_cast<nsGlobalWindow*>(window.get())->FastGetGlobalJSObject(); |
michael@0 | 603 | } |
michael@0 | 604 | else { |
michael@0 | 605 | scriptOwner = mOwningObject; |
michael@0 | 606 | } |
michael@0 | 607 | |
michael@0 | 608 | if (aPrivilege == Chrome) { |
michael@0 | 609 | // Chrome privilege, ignore the persistence type parameter. |
michael@0 | 610 | aPersistenceType = PERSISTENCE_TYPE_PERSISTENT; |
michael@0 | 611 | } |
michael@0 | 612 | |
michael@0 | 613 | nsRefPtr<IDBOpenDBRequest> request = |
michael@0 | 614 | IDBOpenDBRequest::Create(this, window, scriptOwner); |
michael@0 | 615 | IDB_ENSURE_TRUE(request, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR); |
michael@0 | 616 | |
michael@0 | 617 | nsresult rv; |
michael@0 | 618 | |
michael@0 | 619 | if (IndexedDatabaseManager::IsMainProcess()) { |
michael@0 | 620 | nsRefPtr<OpenDatabaseHelper> openHelper = |
michael@0 | 621 | new OpenDatabaseHelper(request, aName, aGroup, aASCIIOrigin, aVersion, |
michael@0 | 622 | aPersistenceType, aDeleting, mContentParent, |
michael@0 | 623 | aPrivilege); |
michael@0 | 624 | |
michael@0 | 625 | rv = openHelper->Init(); |
michael@0 | 626 | IDB_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR); |
michael@0 | 627 | |
michael@0 | 628 | if (!Preferences::GetBool(PREF_INDEXEDDB_ENABLED)) { |
michael@0 | 629 | openHelper->SetError(NS_ERROR_DOM_INDEXEDDB_NOT_ALLOWED_ERR); |
michael@0 | 630 | rv = openHelper->WaitForOpenAllowed(); |
michael@0 | 631 | } |
michael@0 | 632 | else { |
michael@0 | 633 | if (mPrivilege != Chrome && |
michael@0 | 634 | aPersistenceType == PERSISTENCE_TYPE_PERSISTENT) { |
michael@0 | 635 | nsRefPtr<CheckPermissionsHelper> permissionHelper = |
michael@0 | 636 | new CheckPermissionsHelper(openHelper, window); |
michael@0 | 637 | |
michael@0 | 638 | QuotaManager* quotaManager = QuotaManager::Get(); |
michael@0 | 639 | NS_ASSERTION(quotaManager, "This should never be null!"); |
michael@0 | 640 | |
michael@0 | 641 | rv = quotaManager-> |
michael@0 | 642 | WaitForOpenAllowed(OriginOrPatternString::FromOrigin(aASCIIOrigin), |
michael@0 | 643 | Nullable<PersistenceType>(aPersistenceType), |
michael@0 | 644 | openHelper->Id(), permissionHelper); |
michael@0 | 645 | } |
michael@0 | 646 | else { |
michael@0 | 647 | // Chrome and temporary storage doesn't need to check the permission. |
michael@0 | 648 | rv = openHelper->WaitForOpenAllowed(); |
michael@0 | 649 | } |
michael@0 | 650 | } |
michael@0 | 651 | IDB_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR); |
michael@0 | 652 | } |
michael@0 | 653 | else if (aDeleting) { |
michael@0 | 654 | nsCString databaseId; |
michael@0 | 655 | QuotaManager::GetStorageId(aPersistenceType, aASCIIOrigin, Client::IDB, |
michael@0 | 656 | aName, databaseId); |
michael@0 | 657 | MOZ_ASSERT(!databaseId.IsEmpty()); |
michael@0 | 658 | |
michael@0 | 659 | IndexedDBDeleteDatabaseRequestChild* actor = |
michael@0 | 660 | new IndexedDBDeleteDatabaseRequestChild(this, request, databaseId); |
michael@0 | 661 | |
michael@0 | 662 | mActorChild->SendPIndexedDBDeleteDatabaseRequestConstructor( |
michael@0 | 663 | actor, |
michael@0 | 664 | nsString(aName), |
michael@0 | 665 | aPersistenceType); |
michael@0 | 666 | } |
michael@0 | 667 | else { |
michael@0 | 668 | IndexedDBDatabaseChild* dbActor = |
michael@0 | 669 | static_cast<IndexedDBDatabaseChild*>( |
michael@0 | 670 | mActorChild->SendPIndexedDBDatabaseConstructor(nsString(aName), |
michael@0 | 671 | aVersion, |
michael@0 | 672 | aPersistenceType)); |
michael@0 | 673 | |
michael@0 | 674 | dbActor->SetRequest(request); |
michael@0 | 675 | } |
michael@0 | 676 | |
michael@0 | 677 | #ifdef IDB_PROFILER_USE_MARKS |
michael@0 | 678 | { |
michael@0 | 679 | NS_ConvertUTF16toUTF8 profilerName(aName); |
michael@0 | 680 | if (aDeleting) { |
michael@0 | 681 | IDB_PROFILER_MARK("IndexedDB Request %llu: deleteDatabase(\"%s\")", |
michael@0 | 682 | "MT IDBFactory.deleteDatabase()", |
michael@0 | 683 | request->GetSerialNumber(), profilerName.get()); |
michael@0 | 684 | } |
michael@0 | 685 | else { |
michael@0 | 686 | IDB_PROFILER_MARK("IndexedDB Request %llu: open(\"%s\", %lld)", |
michael@0 | 687 | "MT IDBFactory.open()", |
michael@0 | 688 | request->GetSerialNumber(), profilerName.get(), |
michael@0 | 689 | aVersion); |
michael@0 | 690 | } |
michael@0 | 691 | } |
michael@0 | 692 | #endif |
michael@0 | 693 | |
michael@0 | 694 | request.forget(_retval); |
michael@0 | 695 | return NS_OK; |
michael@0 | 696 | } |
michael@0 | 697 | |
michael@0 | 698 | JSObject* |
michael@0 | 699 | IDBFactory::WrapObject(JSContext* aCx) |
michael@0 | 700 | { |
michael@0 | 701 | return IDBFactoryBinding::Wrap(aCx, this); |
michael@0 | 702 | } |
michael@0 | 703 | |
michael@0 | 704 | already_AddRefed<IDBOpenDBRequest> |
michael@0 | 705 | IDBFactory::Open(const nsAString& aName, const IDBOpenDBOptions& aOptions, |
michael@0 | 706 | ErrorResult& aRv) |
michael@0 | 707 | { |
michael@0 | 708 | return Open(nullptr, aName, aOptions.mVersion, aOptions.mStorage, false, aRv); |
michael@0 | 709 | } |
michael@0 | 710 | |
michael@0 | 711 | already_AddRefed<IDBOpenDBRequest> |
michael@0 | 712 | IDBFactory::DeleteDatabase(const nsAString& aName, |
michael@0 | 713 | const IDBOpenDBOptions& aOptions, |
michael@0 | 714 | ErrorResult& aRv) |
michael@0 | 715 | { |
michael@0 | 716 | return Open(nullptr, aName, Optional<uint64_t>(), aOptions.mStorage, true, |
michael@0 | 717 | aRv); |
michael@0 | 718 | } |
michael@0 | 719 | |
michael@0 | 720 | int16_t |
michael@0 | 721 | IDBFactory::Cmp(JSContext* aCx, JS::Handle<JS::Value> aFirst, |
michael@0 | 722 | JS::Handle<JS::Value> aSecond, ErrorResult& aRv) |
michael@0 | 723 | { |
michael@0 | 724 | Key first, second; |
michael@0 | 725 | nsresult rv = first.SetFromJSVal(aCx, aFirst); |
michael@0 | 726 | if (NS_FAILED(rv)) { |
michael@0 | 727 | aRv.Throw(rv); |
michael@0 | 728 | return 0; |
michael@0 | 729 | } |
michael@0 | 730 | |
michael@0 | 731 | rv = second.SetFromJSVal(aCx, aSecond); |
michael@0 | 732 | if (NS_FAILED(rv)) { |
michael@0 | 733 | aRv.Throw(rv); |
michael@0 | 734 | return 0; |
michael@0 | 735 | } |
michael@0 | 736 | |
michael@0 | 737 | if (first.IsUnset() || second.IsUnset()) { |
michael@0 | 738 | aRv.Throw(NS_ERROR_DOM_INDEXEDDB_DATA_ERR); |
michael@0 | 739 | return 0; |
michael@0 | 740 | } |
michael@0 | 741 | |
michael@0 | 742 | return Key::CompareKeys(first, second); |
michael@0 | 743 | } |
michael@0 | 744 | |
michael@0 | 745 | already_AddRefed<IDBOpenDBRequest> |
michael@0 | 746 | IDBFactory::OpenForPrincipal(nsIPrincipal* aPrincipal, const nsAString& aName, |
michael@0 | 747 | uint64_t aVersion, ErrorResult& aRv) |
michael@0 | 748 | { |
michael@0 | 749 | // Just to be on the extra-safe side |
michael@0 | 750 | if (!nsContentUtils::IsCallerChrome()) { |
michael@0 | 751 | MOZ_CRASH(); |
michael@0 | 752 | } |
michael@0 | 753 | |
michael@0 | 754 | return Open(aPrincipal, aName, Optional<uint64_t>(aVersion), |
michael@0 | 755 | Optional<mozilla::dom::StorageType>(), false, aRv); |
michael@0 | 756 | } |
michael@0 | 757 | |
michael@0 | 758 | already_AddRefed<IDBOpenDBRequest> |
michael@0 | 759 | IDBFactory::OpenForPrincipal(nsIPrincipal* aPrincipal, const nsAString& aName, |
michael@0 | 760 | const IDBOpenDBOptions& aOptions, ErrorResult& aRv) |
michael@0 | 761 | { |
michael@0 | 762 | // Just to be on the extra-safe side |
michael@0 | 763 | if (!nsContentUtils::IsCallerChrome()) { |
michael@0 | 764 | MOZ_CRASH(); |
michael@0 | 765 | } |
michael@0 | 766 | |
michael@0 | 767 | return Open(aPrincipal, aName, aOptions.mVersion, aOptions.mStorage, false, |
michael@0 | 768 | aRv); |
michael@0 | 769 | } |
michael@0 | 770 | |
michael@0 | 771 | already_AddRefed<IDBOpenDBRequest> |
michael@0 | 772 | IDBFactory::DeleteForPrincipal(nsIPrincipal* aPrincipal, const nsAString& aName, |
michael@0 | 773 | const IDBOpenDBOptions& aOptions, |
michael@0 | 774 | ErrorResult& aRv) |
michael@0 | 775 | { |
michael@0 | 776 | // Just to be on the extra-safe side |
michael@0 | 777 | if (!nsContentUtils::IsCallerChrome()) { |
michael@0 | 778 | MOZ_CRASH(); |
michael@0 | 779 | } |
michael@0 | 780 | |
michael@0 | 781 | return Open(aPrincipal, aName, Optional<uint64_t>(), aOptions.mStorage, true, |
michael@0 | 782 | aRv); |
michael@0 | 783 | } |
michael@0 | 784 | |
michael@0 | 785 | already_AddRefed<IDBOpenDBRequest> |
michael@0 | 786 | IDBFactory::Open(nsIPrincipal* aPrincipal, const nsAString& aName, |
michael@0 | 787 | const Optional<uint64_t>& aVersion, |
michael@0 | 788 | const Optional<mozilla::dom::StorageType>& aStorageType, |
michael@0 | 789 | bool aDelete, ErrorResult& aRv) |
michael@0 | 790 | { |
michael@0 | 791 | nsresult rv; |
michael@0 | 792 | |
michael@0 | 793 | nsCString group; |
michael@0 | 794 | nsCString origin; |
michael@0 | 795 | StoragePrivilege privilege; |
michael@0 | 796 | PersistenceType defaultPersistenceType; |
michael@0 | 797 | if (aPrincipal) { |
michael@0 | 798 | rv = QuotaManager::GetInfoFromPrincipal(aPrincipal, &group, &origin, |
michael@0 | 799 | &privilege, |
michael@0 | 800 | &defaultPersistenceType); |
michael@0 | 801 | if (NS_FAILED(rv)) { |
michael@0 | 802 | IDB_REPORT_INTERNAL_ERR(); |
michael@0 | 803 | aRv.Throw(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR); |
michael@0 | 804 | return nullptr; |
michael@0 | 805 | } |
michael@0 | 806 | } |
michael@0 | 807 | else { |
michael@0 | 808 | group = mGroup; |
michael@0 | 809 | origin = mASCIIOrigin; |
michael@0 | 810 | privilege = mPrivilege; |
michael@0 | 811 | defaultPersistenceType = mDefaultPersistenceType; |
michael@0 | 812 | } |
michael@0 | 813 | |
michael@0 | 814 | uint64_t version = 0; |
michael@0 | 815 | if (!aDelete && aVersion.WasPassed()) { |
michael@0 | 816 | if (aVersion.Value() < 1) { |
michael@0 | 817 | aRv.ThrowTypeError(MSG_INVALID_VERSION); |
michael@0 | 818 | return nullptr; |
michael@0 | 819 | } |
michael@0 | 820 | version = aVersion.Value(); |
michael@0 | 821 | } |
michael@0 | 822 | |
michael@0 | 823 | PersistenceType persistenceType = |
michael@0 | 824 | PersistenceTypeFromStorage(aStorageType, defaultPersistenceType); |
michael@0 | 825 | |
michael@0 | 826 | nsRefPtr<IDBOpenDBRequest> request; |
michael@0 | 827 | rv = OpenInternal(aName, version, persistenceType, group, origin, privilege, |
michael@0 | 828 | aDelete, getter_AddRefs(request)); |
michael@0 | 829 | if (NS_FAILED(rv)) { |
michael@0 | 830 | aRv.Throw(rv); |
michael@0 | 831 | return nullptr; |
michael@0 | 832 | } |
michael@0 | 833 | |
michael@0 | 834 | return request.forget(); |
michael@0 | 835 | } |