Skip to content

Commit

Permalink
Defer ServiceWorker update checks until after browser startup is comp…
Browse files Browse the repository at this point in the history
…lete.

Also implement after startup task deferral more reasonably on android. Now its based on a signal from DeferredStartupHandler.java instead of an arbitrary 10 second timer.

BUG=460265

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

Cr-Commit-Position: refs/heads/master@{#342768}
  • Loading branch information
michaeln authored and Commit bot committed Aug 11, 2015
1 parent 698215b commit 68bf4a8
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.chrome.browser;

/**
* JNI call glue for AfterStartupTaskUtils in C++.
*/
public final class AfterStartupTaskUtils {
private AfterStartupTaskUtils() {}

/**
* Informs the C++ side that startup is complete. Tasks that
* have been deferred until after startup will be scheduled
* to run and newly posted tasks will no longer be deferred.
*/
public static void setStartupComplete() {
nativeSetStartupComplete();
}

private static native void nativeSetStartupComplete();
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ protected Void doInBackground(Void... params) {
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

AfterStartupTaskUtils.setStartupComplete();

// TODO(aruslan): http://b/6397072 This will be moved elsewhere
PartnerBookmarksShim.kickOffReading(application);

Expand Down
8 changes: 4 additions & 4 deletions chrome/browser/after_startup_task_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,10 @@ void StartupObserver::Start() {
delay = base::TimeDelta::FromMinutes(kLongerDelayMins);
}
#else
// TODO(michaeln): We should probably monitor the initial page load here too,
// but since ChromeBrowserMainExtraPartsMetrics doesn't, not doing that yet.
const int kAndroidDelaySecs = 10;
delay = base::TimeDelta::FromSeconds(kAndroidDelaySecs);
// Startup completion is signaled via AfterStartupTaskUtils.java,
// this is just a failsafe timeout.
const int kLongerDelayMins = 3;
delay = base::TimeDelta::FromMinutes(kLongerDelayMins);
#endif // !defined(OS_ANDROID)

BrowserThread::PostDelayedTask(BrowserThread::UI, FROM_HERE,
Expand Down
6 changes: 6 additions & 0 deletions chrome/browser/after_startup_task_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
#include "base/gtest_prod_util.h"
#include "base/memory/ref_counted.h"

namespace android {
class AfterStartupTaskUtilsJNI;
}

namespace base {
class TaskRunner;
}
Expand All @@ -36,6 +40,8 @@ class AfterStartupTaskUtils {

private:
friend class AfterStartupTaskTest;
friend class android::AfterStartupTaskUtilsJNI;
friend class InProcessBrowserTest;
FRIEND_TEST_ALL_PREFIXES(AfterStartupTaskTest, IsStartupComplete);
FRIEND_TEST_ALL_PREFIXES(AfterStartupTaskTest, PostTask);

Expand Down
27 changes: 27 additions & 0 deletions chrome/browser/after_startup_task_utils_android.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/after_startup_task_utils_android.h"

#include "chrome/browser/after_startup_task_utils.h"
#include "jni/AfterStartupTaskUtils_jni.h"

namespace android {

class AfterStartupTaskUtilsJNI {
public:
static void SetBrowserStartupIsComplete() {
AfterStartupTaskUtils::SetBrowserStartupIsComplete();
}
};

} // android

static void SetStartupComplete(JNIEnv* env, jclass obj) {
android::AfterStartupTaskUtilsJNI::SetBrowserStartupIsComplete();
}

bool RegisterAfterStartupTaskUtilsJNI(JNIEnv* env) {
return RegisterNativesImpl(env);
}
12 changes: 12 additions & 0 deletions chrome/browser/after_startup_task_utils_android.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_AFTER_STARTUP_TASK_UTILS_ANDROID_H_
#define CHROME_BROWSER_AFTER_STARTUP_TASK_UTILS_ANDROID_H_

#include <jni.h>

bool RegisterAfterStartupTaskUtilsJNI(JNIEnv* env);

#endif // CHROME_BROWSER_AFTER_STARTUP_TASK_UTILS_ANDROID_H_
2 changes: 2 additions & 0 deletions chrome/browser/android/chrome_jni_registrar.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "base/android/jni_android.h"
#include "base/android/jni_registrar.h"
#include "base/trace_event/trace_event.h"
#include "chrome/browser/after_startup_task_utils_android.h"
#include "chrome/browser/android/accessibility/font_size_prefs_android.h"
#include "chrome/browser/android/accessibility_util.h"
#include "chrome/browser/android/appmenu/app_menu_drag_helper.h"
Expand Down Expand Up @@ -168,6 +169,7 @@ static base::android::RegistrationMethod kChromeRegisteredMethods[] = {
{"AccessibilityUtils", AccessibilityUtil::Register},
{"AccountChooserInfoBar", RegisterAccountChooserInfoBar},
{"AccountManagementScreenHelper", AccountManagementScreenHelper::Register},
{"AfterStartupTaskUtils", RegisterAfterStartupTaskUtilsJNI},
{"AnswersImageBridge", RegisterAnswersImageBridge},
{"AppBannerInfoBarAndroid", RegisterAppBannerInfoBarAndroid},
{"AppBannerInfoBarDelegateAndroid",
Expand Down
3 changes: 3 additions & 0 deletions chrome/chrome_browser.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
'browser/about_flags.h',
'browser/after_startup_task_utils.cc',
'browser/after_startup_task_utils.h',
'browser/after_startup_task_utils_android.cc',
'browser/after_startup_task_utils_android.h',
'browser/android/accessibility/font_size_prefs_android.cc',
'browser/android/accessibility/font_size_prefs_android.h',
'browser/android/accessibility_util.cc',
Expand Down Expand Up @@ -1681,6 +1683,7 @@
],
'chrome_browser_jni_sources': [
'android/java/src/org/chromium/chrome/browser/AccessibilityUtil.java',
'android/java/src/org/chromium/chrome/browser/AfterStartupTaskUtils.java',
'android/java/src/org/chromium/chrome/browser/ApplicationLifetime.java',
'android/java/src/org/chromium/chrome/browser/accessibility/FontSizePrefs.java',
'android/java/src/org/chromium/chrome/browser/appmenu/AppMenuDragHelper.java',
Expand Down
3 changes: 3 additions & 0 deletions chrome/test/base/in_process_browser_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "base/test/test_file_util.h"
#include "base/thread_task_runner_handle.h"
#include "base/threading/non_thread_safe.h"
#include "chrome/browser/after_startup_task_utils.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/devtools/devtools_window.h"
Expand Down Expand Up @@ -537,6 +538,8 @@ base::CommandLine InProcessBrowserTest::GetCommandLineForRelaunch() {
#endif

void InProcessBrowserTest::RunTestOnMainThreadLoop() {
AfterStartupTaskUtils::SetBrowserStartupIsComplete();

// Pump startup related events.
content::RunAllPendingInMessageLoop();

Expand Down
7 changes: 7 additions & 0 deletions chrome/test/base/in_process_browser_test_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "base/files/file_util.h"
#include "base/path_service.h"
#include "chrome/browser/after_startup_task_utils.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/in_process_browser_test.h"
Expand Down Expand Up @@ -84,6 +85,12 @@ IN_PROC_BROWSER_TEST_F(InProcessBrowserTest, ExternalConnectionFail) {
}
}

// Verify that AfterStartupTaskUtils considers startup to be complete
// prior to test execution so tasks posted by tests are never deferred.
IN_PROC_BROWSER_TEST_F(InProcessBrowserTest, AfterStartupTaskUtils) {
EXPECT_TRUE(AfterStartupTaskUtils::IsBrowserStartupComplete());
}

// Paths are to very simple HTML files. One is accessible, the other is not.
const base::FilePath::CharType kPassHTML[] =
FILE_PATH_LITERAL("chrome/test/data/accessibility_pass.html");
Expand Down
8 changes: 8 additions & 0 deletions content/browser/service_worker/service_worker_register_job.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "content/browser/service_worker/service_worker_storage.h"
#include "content/common/service_worker/service_worker_types.h"
#include "content/common/service_worker/service_worker_utils.h"
#include "content/public/browser/browser_thread.h"
#include "net/base/net_errors.h"

namespace content {
Expand Down Expand Up @@ -86,6 +87,13 @@ void ServiceWorkerRegisterJob::AddCallback(
}

void ServiceWorkerRegisterJob::Start() {
BrowserThread::PostAfterStartupTask(
FROM_HERE, base::ThreadTaskRunnerHandle::Get(),
base::Bind(&ServiceWorkerRegisterJob::StartImpl,
weak_factory_.GetWeakPtr()));
}

void ServiceWorkerRegisterJob::StartImpl() {
SetPhase(START);
ServiceWorkerStorage::FindRegistrationCallback next_step;
if (job_type_ == REGISTRATION_JOB) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class ServiceWorkerRegisterJob : public ServiceWorkerRegisterJobBase {

void SetPhase(Phase phase);

void StartImpl();
void ContinueWithRegistration(
ServiceWorkerStatusCode status,
const scoped_refptr<ServiceWorkerRegistration>& registration);
Expand Down

0 comments on commit 68bf4a8

Please sign in to comment.