Bug 1370454 - Part2: Expose runInTransaction in nsICookieService.idl, r=jduell
authorJunior Hsu <juhsu@mozilla.com>
Tue, 06 Jun 2017 05:36:00 +0200
changeset 411364 94df53e8cc7eefbd20cd999a2e89f91b7083fd2b
parent 411363 2ae4d9b0f74c0e0d3bdc8d3e5e70706db713d52a
child 411365 6efd3b837691c005a140c2325e7ce19e26869edd
push id7391
push usermtabara@mozilla.com
push dateMon, 12 Jun 2017 13:08:53 +0000
treeherdermozilla-beta@2191d7f87e2e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjduell
bugs1370454
milestone55.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1370454 - Part2: Expose runInTransaction in nsICookieService.idl, r=jduell
extensions/cookie/test/unit/test_cookies_async_failure.js
netwerk/cookie/CookieServiceChild.cpp
netwerk/cookie/nsCookieService.cpp
netwerk/cookie/nsICookieService.idl
--- a/extensions/cookie/test/unit/test_cookies_async_failure.js
+++ b/extensions/cookie/test/unit/test_cookies_async_failure.js
@@ -206,20 +206,22 @@ function* run_test_1(generator)
   do_check_false(do_get_backup_file(profile).exists());
   do_run_generator(generator);
 }
 
 function* run_test_2(generator)
 {
   // Load the profile and populate it.
   do_load_profile();
-  for (let i = 0; i < 3000; ++i) {
-    let uri = NetUtil.newURI("http://" + i + ".com/");
-    Services.cookies.setCookieString(uri, null, "oh=hai; max-age=1000", null);
-  }
+  Services.cookies.runInTransaction(_=>{
+    for (let i = 0; i < 3000; ++i) {
+      let uri = NetUtil.newURI("http://" + i + ".com/");
+      Services.cookies.setCookieString(uri, null, "oh=hai; max-age=1000", null);
+    }
+  });
 
   // Close the profile.
   do_close_profile(sub_generator);
   yield;
 
   // Corrupt the database file.
   let size = do_corrupt_db(do_get_cookie_file(profile));
 
@@ -276,26 +278,28 @@ function* run_test_2(generator)
 function* run_test_3(generator)
 {
   // Set the maximum cookies per base domain limit to a large value, so that
   // corrupting the database is easier.
   Services.prefs.setIntPref("network.cookie.maxPerHost", 3000);
 
   // Load the profile and populate it.
   do_load_profile();
-  for (let i = 0; i < 10; ++i) {
-    let uri = NetUtil.newURI("http://hither.com/");
-    Services.cookies.setCookieString(uri, null, "oh" + i + "=hai; max-age=1000",
-      null);
-  }
-  for (let i = 10; i < 3000; ++i) {
-    let uri = NetUtil.newURI("http://haithur.com/");
-    Services.cookies.setCookieString(uri, null, "oh" + i + "=hai; max-age=1000",
-      null);
-  }
+  Services.cookies.runInTransaction(_=>{
+    for (let i = 0; i < 10; ++i) {
+      let uri = NetUtil.newURI("http://hither.com/");
+      Services.cookies.setCookieString(uri, null, "oh" + i + "=hai; max-age=1000",
+        null);
+    }
+    for (let i = 10; i < 3000; ++i) {
+      let uri = NetUtil.newURI("http://haithur.com/");
+      Services.cookies.setCookieString(uri, null, "oh" + i + "=hai; max-age=1000",
+        null);
+    }
+  });
 
   // Close the profile.
   do_close_profile(sub_generator);
   yield;
 
   // Corrupt the database file.
   let size = do_corrupt_db(do_get_cookie_file(profile));
 
@@ -367,20 +371,22 @@ function* run_test_3(generator)
   do_check_false(do_get_backup_file(profile).exists());
   do_run_generator(generator);
 }
 
 function* run_test_4(generator)
 {
   // Load the profile and populate it.
   do_load_profile();
-  for (let i = 0; i < 3000; ++i) {
-    let uri = NetUtil.newURI("http://" + i + ".com/");
-    Services.cookies.setCookieString(uri, null, "oh=hai; max-age=1000", null);
-  }
+  Services.cookies.runInTransaction(_=>{
+    for (let i = 0; i < 3000; ++i) {
+      let uri = NetUtil.newURI("http://" + i + ".com/");
+      Services.cookies.setCookieString(uri, null, "oh=hai; max-age=1000", null);
+    }
+  });
 
   // Close the profile.
   do_close_profile(sub_generator);
   yield;
 
   // Corrupt the database file.
   let size = do_corrupt_db(do_get_cookie_file(profile));
 
@@ -439,23 +445,25 @@ function* run_test_4(generator)
   do_check_false(do_get_backup_file(profile).exists());
   do_run_generator(generator);
 }
 
 function* run_test_5(generator)
 {
   // Load the profile and populate it.
   do_load_profile();
-  let uri = NetUtil.newURI("http://bar.com/");
-  Services.cookies.setCookieString(uri, null, "oh=hai; path=/; max-age=1000",
-    null);
-  for (let i = 0; i < 3000; ++i) {
-    let uri = NetUtil.newURI("http://" + i + ".com/");
-    Services.cookies.setCookieString(uri, null, "oh=hai; max-age=1000", null);
-  }
+  Services.cookies.runInTransaction(_=>{
+    let uri = NetUtil.newURI("http://bar.com/");
+    Services.cookies.setCookieString(uri, null, "oh=hai; path=/; max-age=1000",
+      null);
+    for (let i = 0; i < 3000; ++i) {
+      let uri = NetUtil.newURI("http://" + i + ".com/");
+      Services.cookies.setCookieString(uri, null, "oh=hai; max-age=1000", null);
+    }
+  });
 
   // Close the profile.
   do_close_profile(sub_generator);
   yield;
 
   // Corrupt the database file.
   let size = do_corrupt_db(do_get_cookie_file(profile));
 
--- a/netwerk/cookie/CookieServiceChild.cpp
+++ b/netwerk/cookie/CookieServiceChild.cpp
@@ -239,11 +239,17 @@ CookieServiceChild::SetCookieStringFromH
                                             const char *aCookieString,
                                             const char *aServerTime,
                                             nsIChannel *aChannel) 
 {
   return SetCookieStringInternal(aHostURI, aChannel, aCookieString,
                                  aServerTime, true);
 }
 
+NS_IMETHODIMP
+CookieServiceChild::RunInTransaction(nsICookieTransactionCallback* aCallback)
+{
+  return NS_ERROR_NOT_IMPLEMENTED;
+}
+
 } // namespace net
 } // namespace mozilla
 
--- a/netwerk/cookie/nsCookieService.cpp
+++ b/netwerk/cookie/nsCookieService.cpp
@@ -2261,16 +2261,37 @@ nsCookieService::CreatePurgeList(nsICook
   nsCOMPtr<nsIMutableArray> removedList =
     do_CreateInstance(NS_ARRAY_CONTRACTID);
   removedList->AppendElement(aCookie, false);
   return removedList.forget();
 }
 
 /******************************************************************************
  * nsCookieService:
+ * public transaction helper impl
+ ******************************************************************************/
+
+NS_IMETHODIMP
+nsCookieService::RunInTransaction(nsICookieTransactionCallback* aCallback)
+{
+  NS_ENSURE_ARG(aCallback);
+  if (NS_WARN_IF(!mDefaultDBState->dbConn)) {
+    return NS_ERROR_NOT_AVAILABLE;
+  }
+  mozStorageTransaction transaction(mDefaultDBState->dbConn, true);
+
+  if (NS_FAILED(aCallback->Callback())) {
+    Unused << transaction.Rollback();
+    return NS_ERROR_FAILURE;
+  }
+  return NS_OK;
+}
+
+/******************************************************************************
+ * nsCookieService:
  * pref observer impl
  ******************************************************************************/
 
 void
 nsCookieService::PrefChanged(nsIPrefBranch *aPrefBranch)
 {
   int32_t val;
   if (NS_SUCCEEDED(aPrefBranch->GetIntPref(kPrefCookieBehavior, &val)))
--- a/netwerk/cookie/nsICookieService.idl
+++ b/netwerk/cookie/nsICookieService.idl
@@ -5,16 +5,25 @@
 
 #include "nsISupports.idl"
 
 interface nsIURI;
 interface nsIPrompt;
 interface nsIChannel;
 
 /**
+ * @see nsICookieService::runInTransaction
+ */
+[scriptable, function, uuid(0fc41ffb-f1b7-42d9-9a42-8dc420c158c1)]
+interface nsICookieTransactionCallback : nsISupports
+{
+  void callback();
+};
+
+/**
  * nsICookieService
  *
  * Provides methods for setting and getting cookies in the context of a
  * page load.  See nsICookieManager for methods to manipulate the cookie
  * database directly.  This separation of interface is mainly historical.
  *
  * This service broadcasts the notifications detailed below when the cookie
  * list is changed, or a cookie is rejected.
@@ -185,9 +194,24 @@ interface nsICookieService : nsISupports
    * @param aChannel
    *        the channel used to load the document.  this parameter should not
    *        be null, otherwise the cookies will not be set if third-party
    *        cookies have been disabled by the user. (the channel is used
    *        to determine the originating URI of the document; if it is not
    *        provided, the cookies will be assumed third-party.)
    */
   void setCookieStringFromHttp(in nsIURI aURI, in nsIURI aFirstURI, in nsIPrompt aPrompt, in string aCookie, in string aServerTime, in nsIChannel aChannel);
+
+
+  /*
+   * Batch SQLite operations into one transaction. By default each call to
+   * CookieService that affects the underlying SQLite database (add, remove,
+   * setCookieString etc.) runs in a separate transaction.  If you do this many
+   * times in a row, it's faster and suggested to wrap them all in a single
+   * transaction by setting all the operations into the callback parameter.
+   * Example: test scripts that need to construct a large cookie database.
+   * @param aCallback
+   *        nsICookieTransactionCallback interface to call
+   * @throws NS_ERROR_FAILURE if aCallback() fails.
+   * @throws NS_ERROR_NOT_AVAILABLE if the connection is not established.
+   */
+   void runInTransaction(in nsICookieTransactionCallback aCallback);
 };