forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BackgroundSync] Launch the browser on Android when next online
On Android devices BackgroundSync needs to launch the browser the next time the device goes online if a one-shot sync is registered. This CL adds an Android Receiver and Service (BackgroundSyncLauncherService) to listen to connectvity events and launch the browser. It also provides a BackgroundSyncLauncher JNI class that C++ can interact with to register interest in launching in the background. This feature is experimental and behind a flag. Note that the components_tests.gyp change (to add content_java) is because content::StoragePartition creates a BackgroundSyncManager which immediately talks to java therefore the content java classes are needed for testing. BUG=479665 Review URL: https://codereview.chromium.org/1140813006 Cr-Commit-Position: refs/heads/master@{#331826}
- Loading branch information
jkarlin
authored and
Commit bot
committed
May 28, 2015
1 parent
5fc6422
commit 6970b0a
Showing
19 changed files
with
584 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
content/browser/android/background_sync_launcher_android.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// 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 "content/browser/android/background_sync_launcher_android.h" | ||
|
||
#include "content/public/browser/browser_thread.h" | ||
#include "jni/BackgroundSyncLauncher_jni.h" | ||
|
||
namespace content { | ||
|
||
namespace { | ||
base::LazyInstance<BackgroundSyncLauncherAndroid> g_background_sync_launcher = | ||
LAZY_INSTANCE_INITIALIZER; | ||
} | ||
|
||
// static | ||
BackgroundSyncLauncherAndroid* BackgroundSyncLauncherAndroid::Get() { | ||
DCHECK_CURRENTLY_ON(BrowserThread::UI); | ||
|
||
return g_background_sync_launcher.Pointer(); | ||
} | ||
|
||
// static | ||
void BackgroundSyncLauncherAndroid::LaunchBrowserWhenNextOnline( | ||
const BackgroundSyncManager* registrant, | ||
bool launch_when_next_online) { | ||
DCHECK_CURRENTLY_ON(BrowserThread::UI); | ||
|
||
Get()->LaunchBrowserWhenNextOnlineImpl(registrant, launch_when_next_online); | ||
} | ||
|
||
void BackgroundSyncLauncherAndroid::LaunchBrowserWhenNextOnlineImpl( | ||
const BackgroundSyncManager* registrant, | ||
bool launch_when_next_online) { | ||
DCHECK_CURRENTLY_ON(BrowserThread::UI); | ||
|
||
bool was_launching = !launch_when_next_online_registrants_.empty(); | ||
|
||
if (launch_when_next_online) | ||
launch_when_next_online_registrants_.insert(registrant); | ||
else | ||
launch_when_next_online_registrants_.erase(registrant); | ||
|
||
bool now_launching = !launch_when_next_online_registrants_.empty(); | ||
if (was_launching != now_launching) { | ||
JNIEnv* env = base::android::AttachCurrentThread(); | ||
Java_BackgroundSyncLauncher_setLaunchWhenNextOnline( | ||
env, java_launcher_.obj(), now_launching); | ||
} | ||
} | ||
|
||
// static | ||
bool BackgroundSyncLauncherAndroid::RegisterLauncher(JNIEnv* env) { | ||
return RegisterNativesImpl(env); | ||
} | ||
|
||
BackgroundSyncLauncherAndroid::BackgroundSyncLauncherAndroid() { | ||
DCHECK_CURRENTLY_ON(BrowserThread::UI); | ||
|
||
JNIEnv* env = base::android::AttachCurrentThread(); | ||
java_launcher_.Reset(Java_BackgroundSyncLauncher_create( | ||
env, base::android::GetApplicationContext())); | ||
} | ||
|
||
BackgroundSyncLauncherAndroid::~BackgroundSyncLauncherAndroid() { | ||
DCHECK_CURRENTLY_ON(BrowserThread::UI); | ||
|
||
JNIEnv* env = base::android::AttachCurrentThread(); | ||
Java_BackgroundSyncLauncher_destroy(env, java_launcher_.obj()); | ||
} | ||
|
||
} // namespace content |
57 changes: 57 additions & 0 deletions
57
content/browser/android/background_sync_launcher_android.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// 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 CONTENT_BROWSER_ANDROID_BACKGROUND_SYNC_LAUNCHER_ANDROID_H_ | ||
#define CONTENT_BROWSER_ANDROID_BACKGROUND_SYNC_LAUNCHER_ANDROID_H_ | ||
|
||
#include <set> | ||
|
||
#include "base/android/jni_android.h" | ||
#include "base/lazy_instance.h" | ||
#include "base/macros.h" | ||
#include "content/common/content_export.h" | ||
|
||
namespace content { | ||
|
||
class BackgroundSyncManager; | ||
|
||
// The BackgroundSyncLauncherAndroid singleton owns the Java | ||
// BackgroundSyncLauncher object and is used to register interest in starting | ||
// the browser the next time the device goes online. This class runs on the UI | ||
// thread. | ||
class CONTENT_EXPORT BackgroundSyncLauncherAndroid { | ||
public: | ||
static BackgroundSyncLauncherAndroid* Get(); | ||
|
||
// Register the |registrant|'s interest (or disinterest) in starting the | ||
// browser the next time the device goes online after the browser has closed. | ||
// |registrant| is used to count the number of interested objects and is not | ||
// accessed, therefore it is okay for |registrant| to be deleted before this | ||
// class. Interest is reset the next time the BackgroundSyncLauncherAndroid is | ||
// created (browser restart). The caller can remove interest in launching the | ||
// browser by calling with |launch_when_next_online| set to false. | ||
static void LaunchBrowserWhenNextOnline( | ||
const BackgroundSyncManager* registrant, | ||
bool launch_when_next_online); | ||
|
||
static bool RegisterLauncher(JNIEnv* env); | ||
|
||
private: | ||
friend struct base::DefaultLazyInstanceTraits<BackgroundSyncLauncherAndroid>; | ||
|
||
// Constructor and destructor marked private to enforce singleton | ||
BackgroundSyncLauncherAndroid(); | ||
~BackgroundSyncLauncherAndroid(); | ||
|
||
void LaunchBrowserWhenNextOnlineImpl(const BackgroundSyncManager* registrant, | ||
bool launch_when_next_online); | ||
|
||
std::set<const BackgroundSyncManager*> launch_when_next_online_registrants_; | ||
base::android::ScopedJavaGlobalRef<jobject> java_launcher_; | ||
DISALLOW_COPY_AND_ASSIGN(BackgroundSyncLauncherAndroid); | ||
}; | ||
|
||
} // namespace content | ||
|
||
#endif // CONTENT_BROWSER_ANDROID_BACKGROUND_SYNC_LAUNCHER_ANDROID_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
content/public/android/java/src/org/chromium/content/browser/BackgroundSyncLauncher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// 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.content.browser; | ||
|
||
import android.content.Context; | ||
import android.content.SharedPreferences; | ||
import android.preference.PreferenceManager; | ||
|
||
import org.chromium.base.CalledByNative; | ||
import org.chromium.base.JNINamespace; | ||
import org.chromium.base.VisibleForTesting; | ||
|
||
/** | ||
* The {@link BackgroundSyncLauncher} singleton is created and owned by the C++ browser. It | ||
* registers interest in waking up the browser the next time the device goes online after the | ||
*browser closes via the {@link #setLaunchWhenNextOnline} method. | ||
* | ||
* Thread model: This class is to be run on the UI thread only. | ||
*/ | ||
@JNINamespace("content") | ||
public class BackgroundSyncLauncher { | ||
static final String PREF_BACKGROUND_SYNC_LAUNCH_NEXT_ONLINE = "bgsync_launch_next_online"; | ||
|
||
// The instance of BackgroundSyncLauncher currently owned by a C++ | ||
// BackgroundSyncLauncherAndroid, if any. If it is non-null then the browser is running. | ||
private static BackgroundSyncLauncher sInstance; | ||
|
||
private final SharedPreferences mSharedPreferences; | ||
|
||
/** | ||
* Create a BackgroundSyncLauncher object, which is owned by C++. | ||
* @param context The app context. | ||
*/ | ||
@VisibleForTesting | ||
@CalledByNative | ||
protected static BackgroundSyncLauncher create(Context context) { | ||
if (sInstance != null) { | ||
throw new IllegalStateException("Already instantiated"); | ||
} | ||
|
||
sInstance = new BackgroundSyncLauncher(context); | ||
return sInstance; | ||
} | ||
|
||
/** | ||
* Called when the C++ counterpart is deleted. | ||
*/ | ||
@VisibleForTesting | ||
@CalledByNative | ||
protected void destroy() { | ||
assert sInstance == this; | ||
sInstance = null; | ||
} | ||
|
||
/** | ||
* Set interest (or disinterest) in launching the browser the next time the device goes online | ||
* after the browser closes. On creation of the {@link BackgroundSyncLauncher} class (on browser | ||
* start) this value is reset to false. | ||
*/ | ||
@VisibleForTesting | ||
@CalledByNative | ||
protected void setLaunchWhenNextOnline(boolean shouldLaunch) { | ||
mSharedPreferences.edit() | ||
.putBoolean(PREF_BACKGROUND_SYNC_LAUNCH_NEXT_ONLINE, shouldLaunch) | ||
.apply(); | ||
} | ||
|
||
/** | ||
* Returns whether the browser should be launched when the device next goes online. | ||
* This is set by C++ and reset to false each time {@link BackgroundSyncLauncher}'s singleton is | ||
* created (the native browser is started). | ||
* @param sharedPreferences The shared preferences. | ||
*/ | ||
protected static boolean shouldLaunchWhenNextOnline(SharedPreferences sharedPreferences) { | ||
return sharedPreferences.getBoolean(PREF_BACKGROUND_SYNC_LAUNCH_NEXT_ONLINE, false); | ||
} | ||
|
||
/** | ||
* True if the native browser has started and created an instance of {@link | ||
* BackgroundSyncLauncher}. | ||
*/ | ||
protected static boolean hasInstance() { | ||
return sInstance != null; | ||
} | ||
|
||
private BackgroundSyncLauncher(Context context) { | ||
mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); | ||
setLaunchWhenNextOnline(false); | ||
} | ||
} |
Oops, something went wrong.