Bug 1152169 - DataStoreService should check if the first revision exists, r=bent
authorAndrea Marchesini <amarchesini@mozilla.com>
Thu, 16 Apr 2015 12:25:42 +0100
changeset 258415 9d3e2f50407284404b7052f5c5e7da2f78cb7689
parent 258414 51f634b3ede68273585cc48492f340293fbef49c
child 258416 ebba4cbd6c4ba5997f83b1bddb97949dbfecde4d
push id8007
push userraliiev@mozilla.com
push dateMon, 11 May 2015 19:23:16 +0000
treeherdermozilla-aurora@e2ce1aac996e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbent
bugs1152169
milestone40.0a1
Bug 1152169 - DataStoreService should check if the first revision exists, r=bent
dom/datastore/DataStoreCursorImpl.jsm
dom/datastore/DataStoreService.cpp
--- a/dom/datastore/DataStoreCursorImpl.jsm
+++ b/dom/datastore/DataStoreCursorImpl.jsm
@@ -165,16 +165,22 @@ this.DataStoreCursor.prototype = {
       this._state = STATE_REVISION_INIT;
       this.stateMachine(aStore, aRevisionStore, aResolve, aReject);
       return;
     }
 
     let self = this;
     let request = aRevisionStore.openCursor(null, 'prev');
     request.onsuccess = function(aEvent) {
+      if (aEvent.target.result === undefined) {
+        aReject(self._window.DOMError("InvalidRevision",
+                                      "The DataStore is corrupted"));
+        return;
+      }
+
       self._revision = aEvent.target.result.value;
       self._objectId = 0;
       self._state = STATE_SEND_ALL;
       aResolve(self.createTask('clear', null, '', null));
     }
   },
 
   stateMachineRevisionInit: function(aStore, aRevisionStore, aResolve, aReject) {
--- a/dom/datastore/DataStoreService.cpp
+++ b/dom/datastore/DataStoreService.cpp
@@ -537,19 +537,20 @@ private:
   uint32_t mAppId;
   nsString mName;
   nsString mManifestURL;
 };
 
 // This DataStoreDBCallback is called when DataStoreDB opens the DataStore DB.
 // Then the first revision will be created if it's needed.
 class FirstRevisionIdCallback final : public DataStoreDBCallback
+                                    , public nsIDOMEventListener
 {
 public:
-  NS_INLINE_DECL_REFCOUNTING(FirstRevisionIdCallback)
+  NS_DECL_ISUPPORTS
 
   FirstRevisionIdCallback(uint32_t aAppId, const nsAString& aName,
                           const nsAString& aManifestURL)
     : mAppId(aAppId)
     , mName(aName)
     , mManifestURL(aManifestURL)
   {
     AssertIsInMainProcess();
@@ -563,65 +564,155 @@ public:
     MOZ_ASSERT(NS_IsMainThread());
     MOZ_ASSERT(aDb);
 
     if (aStatus == Error) {
       NS_WARNING("Failed to create the first revision.");
       return;
     }
 
+    ErrorResult error;
+
     if (aStatus == Success) {
-      nsRefPtr<DataStoreService> service = DataStoreService::Get();
-      MOZ_ASSERT(service);
+      mTxn = aDb->Transaction();
+
+      nsRefPtr<IDBObjectStore> store =
+      mTxn->ObjectStore(NS_LITERAL_STRING(DATASTOREDB_REVISION), error);
+      if (NS_WARN_IF(error.Failed())) {
+        return;
+      }
 
-      nsresult rv = service->EnableDataStore(mAppId, mName, mManifestURL);
+      AutoSafeJSContext cx;
+      mRequest = store->OpenCursor(cx, JS::UndefinedHandleValue,
+                                   IDBCursorDirection::Prev, error);
+      if (NS_WARN_IF(error.Failed())) {
+        return;
+      }
+
+      nsresult rv;
+      rv = mRequest->EventTarget::AddEventListener(NS_LITERAL_STRING("success"),
+                                                   this, false);
       if (NS_FAILED(rv)) {
-        NS_WARNING("Failed to enable a DataStore.");
+        NS_WARNING("Failed to add an EventListener.");
+        return;
       }
 
       return;
     }
 
     // The DB has just been created.
 
+    error = CreateFirstRevision(aDb->Transaction());
+    if (error.Failed()) {
+      NS_WARNING("Failed to add a revision to a DataStore.");
+    }
+  }
+
+  nsresult
+  CreateFirstRevision(IDBTransaction* aTxn)
+  {
+    MOZ_ASSERT(aTxn);
+
     ErrorResult error;
     nsRefPtr<IDBObjectStore> store =
-      aDb->Transaction()->ObjectStore(NS_LITERAL_STRING(DATASTOREDB_REVISION),
-                                      error);
-    if (error.Failed()) {
-      NS_WARNING("Failed to get an ObjectStore object.");
-      return;
+      aTxn->ObjectStore(NS_LITERAL_STRING(DATASTOREDB_REVISION), error);
+    if (NS_WARN_IF(error.Failed())) {
+      return error.ErrorCode();
     }
     MOZ_ASSERT(store);
 
     nsRefPtr<RevisionAddedEnableStoreCallback> callback =
       new RevisionAddedEnableStoreCallback(mAppId, mName, mManifestURL);
 
     // Note: this cx is only used for rooting and AddRevision, neither of which
     // actually care which compartment we're in.
     AutoSafeJSContext cx;
 
     // If the revision doesn't exist, let's create it.
     nsRefPtr<DataStoreRevision> revision = new DataStoreRevision();
     nsresult rv = revision->AddRevision(cx, store, 0,
                                         DataStoreRevision::RevisionVoid,
                                         callback);
-    if (NS_FAILED(rv)) {
-      NS_WARNING("Failed to add a revision to a DataStore.");
+    if (NS_WARN_IF(NS_FAILED(rv))) {
+      return rv;
+    }
+
+    return NS_OK;
+  }
+
+  // nsIDOMEventListener
+  NS_IMETHOD
+  HandleEvent(nsIDOMEvent* aEvent)
+  {
+    AssertIsInMainProcess();
+    MOZ_ASSERT(NS_IsMainThread());
+
+    nsRefPtr<IDBRequest> request;
+    request.swap(mRequest);
+
+    nsRefPtr<IDBTransaction> txn;
+    txn.swap(mTxn);
+
+    request->RemoveEventListener(NS_LITERAL_STRING("success"), this, false);
+
+    nsString type;
+    nsresult rv = aEvent->GetType(type);
+    if (NS_WARN_IF(NS_FAILED(rv))) {
+      return rv;
     }
+
+#ifdef DEBUG
+    MOZ_ASSERT(type.EqualsASCII("success"));
+#endif
+
+    AutoSafeJSContext cx;
+
+    ErrorResult error;
+    JS::Rooted<JS::Value> result(cx);
+    request->GetResult(cx, &result, error);
+    if (NS_WARN_IF(error.Failed())) {
+      return error.ErrorCode();
+    }
+
+    // This means that the content is a IDBCursor, so the first revision already
+    // exists.
+    if (result.isObject()) {
+#ifdef DEBUG
+      IDBCursor* cursor = nullptr;
+      error = UNWRAP_OBJECT(IDBCursor, &result.toObject(), cursor);
+      MOZ_ASSERT(!error.Failed());
+#endif
+
+      nsRefPtr<DataStoreService> service = DataStoreService::Get();
+      MOZ_ASSERT(service);
+
+      return service->EnableDataStore(mAppId, mName, mManifestURL);
+    }
+
+    rv = CreateFirstRevision(txn);
+    if (NS_WARN_IF(NS_FAILED(rv))) {
+      return rv;
+    }
+
+    return NS_OK;
   }
 
 private:
   ~FirstRevisionIdCallback() {}
 
+  nsRefPtr<IDBRequest> mRequest;
+  nsRefPtr<IDBTransaction> mTxn;
+
   uint32_t mAppId;
   nsString mName;
   nsString mManifestURL;
 };
 
+NS_IMPL_ISUPPORTS(FirstRevisionIdCallback, nsIDOMEventListener)
+
 // This class calls the 'retrieveRevisionId' method of the DataStore object for
 // any DataStore in the 'mResults' array. When all of them are called, the
 // promise is resolved with 'mResults'.
 // The reson why this has to be done is because DataStore are object that can be
 // created in any thread and in any process. The first revision has been
 // created, but they don't know its value yet.
 class RetrieveRevisionsCounter
 {