From fe08f137f867befed12f1b9c0c1ed8e5c2d59e80 Mon Sep 17 00:00:00 2001
From: jkarlin <jkarlin@chromium.org>
Date: Fri, 6 Nov 2015 05:18:41 -0800
Subject: [BackgroundSync] Unify relaunching browser API for all platforms

Creates BackgroundSyncController::RunInBackground(bool) which can be used on all platforms. On Android it will forward to LaunchBrowserWhenNextOnline and on other platforms it will enable background mode.

Adds content_unittests for the BackgroundSyncController to verify that it's being called correctly by the BackgroundSyncManager. This requires access to a BrowserContext from tests (hence the switch to using the single argument EmbeddedWorkerTestHelper constructor).

BUG=549550,489705,549826,548052

Review URL: https://codereview.chromium.org/1422583003

Cr-Commit-Position: refs/heads/master@{#358313}
---
 .../background_sync/background_sync_manager.cc     | 37 +++++++++-------------
 1 file changed, 15 insertions(+), 22 deletions(-)

(limited to 'content/browser/background_sync/background_sync_manager.cc')

diff --git a/content/browser/background_sync/background_sync_manager.cc b/content/browser/background_sync/background_sync_manager.cc
index 5624239..df3fcb6 100644
--- a/content/browser/background_sync/background_sync_manager.cc
+++ b/content/browser/background_sync/background_sync_manager.cc
@@ -91,6 +91,18 @@ void NotifyBackgroundSyncRegisteredOnUIThread(
   background_sync_controller->NotifyBackgroundSyncRegistered(origin);
 }
 
+void RunInBackgroundOnUIThread(
+    const scoped_refptr<ServiceWorkerContextWrapper>& sw_context_wrapper,
+    bool enabled) {
+  DCHECK_CURRENTLY_ON(BrowserThread::UI);
+
+  BackgroundSyncController* background_sync_controller =
+      GetBackgroundSyncControllerOnUIThread(sw_context_wrapper);
+  if (background_sync_controller) {
+    background_sync_controller->RunInBackground(enabled);
+  }
+}
+
 }  // namespace
 
 BackgroundSyncManager::BackgroundSyncRegistrations::
@@ -973,7 +985,6 @@ bool BackgroundSyncManager::IsRegistrationReadyToFire(
 }
 
 void BackgroundSyncManager::SchedulePendingRegistrations() {
-#if defined(OS_ANDROID)
   bool keep_browser_alive_for_one_shot = false;
 
   for (const auto& sw_id_and_registrations : active_registrations_) {
@@ -994,26 +1005,8 @@ void BackgroundSyncManager::SchedulePendingRegistrations() {
 
   BrowserThread::PostTask(
       BrowserThread::UI, FROM_HERE,
-      base::Bind(&BackgroundSyncManager::SchedulePendingRegistrationsOnUIThread,
-                 base::Unretained(this), keep_browser_alive_for_one_shot));
-
-#else
-// TODO(jkarlin): Toggle Chrome's background mode.
-#endif
-}
-
-void BackgroundSyncManager::SchedulePendingRegistrationsOnUIThread(
-    bool keep_browser_alive_for_one_shot) {
-  DCHECK_CURRENTLY_ON(BrowserThread::UI);
-
-  BackgroundSyncController* background_sync_controller =
-      GetBackgroundSyncControllerOnUIThread(service_worker_context_);
-  if (background_sync_controller) {
-    // TODO(jkarlin): Use the context's path instead of the 'this' pointer as an
-    // identifier. See crbug.com/489705.
-    background_sync_controller->LaunchBrowserWhenNextOnline(
-        this, keep_browser_alive_for_one_shot);
-  }
+      base::Bind(RunInBackgroundOnUIThread, service_worker_context_,
+                 keep_browser_alive_for_one_shot));
 }
 
 void BackgroundSyncManager::FireReadyEvents() {
@@ -1058,7 +1051,7 @@ void BackgroundSyncManager::FireReadyEventsImpl(const base::Closure& callback) {
 
   // If there are no registrations currently ready, then just run |callback|.
   // Otherwise, fire them all, and record the result when done.
-  if (sw_id_and_keys_to_fire.size() == 0) {
+  if (sw_id_and_keys_to_fire.empty()) {
     base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
                                                   base::Bind(callback));
   } else {
-- 
cgit v1.1