Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(480)

Unified Diff: content/public/android/java/src/org/chromium/content/browser/BackgroundSyncNetworkObserver.java

Issue 1294603003: [BackgroundSync] Trigger Background Sync events when Chrome is backgrounded on Android (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Switch to single autodetect, register native observers, change observer creation pattern Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/public/android/java/src/org/chromium/content/browser/BackgroundSyncNetworkObserver.java
diff --git a/content/public/android/java/src/org/chromium/content/browser/BackgroundSyncNetworkObserver.java b/content/public/android/java/src/org/chromium/content/browser/BackgroundSyncNetworkObserver.java
new file mode 100644
index 0000000000000000000000000000000000000000..ca527ceda8c94e9fb54ca6ab0c725be0abbf6239
--- /dev/null
+++ b/content/public/android/java/src/org/chromium/content/browser/BackgroundSyncNetworkObserver.java
@@ -0,0 +1,105 @@
+// 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 org.chromium.base.ThreadUtils;
+import org.chromium.base.annotations.CalledByNative;
+import org.chromium.base.annotations.JNINamespace;
+import org.chromium.base.annotations.NativeClassQualifiedName;
+import org.chromium.net.NetworkChangeNotifierAutoDetect;
+
+import java.util.ArrayList;
+import java.util.List;
+/**
+ * Contains the Java code used by the BackgroundSyncNetworkObserverAndroid C++ class.
+ *
+ * The purpose of this class is to listen for and forward network connectivity events to the
+ * BackgroundSyncNetworkObserverAndroid objects even when the application is paused. The standard
+ * NetworkChangeNotifier does not listen for connectivity events when the application is paused.
+ *
+ * This class maintains a NetworkChangeNotifierAutoDetect, which exists for as long as any
+ * BackgroundSyncNetworkObserverAndroid objects are registered.
+ *
+ * This class should live on the main thread.
jkarlin 2015/09/19 00:16:07 s/should live/lives/
iclelland 2015/09/21 18:49:30 Done.
+ */
+@JNINamespace("content")
+class BackgroundSyncNetworkObserver implements NetworkChangeNotifierAutoDetect.Observer {
+ private NetworkChangeNotifierAutoDetect mNotifier;
+ private Context mContext;
+
+ // The singleton instance. There should only be one BackgroundSyncNetworkObserver.
jkarlin 2015/09/19 00:16:07 The second sentence of the comment is redundant.
iclelland 2015/09/21 18:49:30 Done. Done.
+ private static BackgroundSyncNetworkObserver sInstance = null;
+
+ // For unit tests, this flag can be set to stop the actual connectivity state changes from
+ // being sent to observers.
+ private static boolean sNotificationDisabledForTesting = false;
+
+ // List of native observers. These are each called when the network state changes.
+ private List<Long> mNativePtrs;
+
+ private BackgroundSyncNetworkObserver(Context ctx) {
+ mContext = ctx;
+ mNativePtrs = new ArrayList<Long>();
+ }
+
+ @CalledByNative
+ private static BackgroundSyncNetworkObserver createObserver(Context ctx, long nativePtr) {
+ if (sInstance == null) {
+ sInstance = new BackgroundSyncNetworkObserver(ctx);
+ }
+ sInstance.registerObserver(nativePtr);
+ return sInstance;
+ }
+
+ private void registerObserver(final long nativePtr) {
+ // Create the NetworkChangeNotifierAutoDetect if it does not exist already.
+ if (mNotifier == null) {
+ mNotifier = new NetworkChangeNotifierAutoDetect(this, mContext, true);
+ }
+ mNativePtrs.add(nativePtr);
+
+ if (sNotificationDisabledForTesting) return;
+
+ ThreadUtils.runOnUiThread(new Runnable() {
jkarlin 2015/09/19 00:16:07 This shouldn't be necessary, we should be on the U
iclelland 2015/09/21 18:49:30 Done.
+ @Override
+ public void run() {
+ nativeNotifyConnectionTypeChanged(nativePtr,
+ mNotifier.getCurrentConnectionType(mNotifier.getCurrentNetworkState()));
+ }
+ });
+ }
+
+ @CalledByNative
+ private void removeObserver(long nativePtr) {
+ mNativePtrs.remove(nativePtr);
+ // Destroy the NetworkChangeNotifierAutoDetect if there are no more observers.
+ if (mNativePtrs.size() == 0 && mNotifier != null) {
+ mNotifier.destroy();
+ mNotifier = null;
+ }
+ }
+
+ @CalledByNative
+ private static void disableNotificationForTesting() {
+ sNotificationDisabledForTesting = true;
+ }
+
+ @Override
+ public void onConnectionTypeChanged(int newConnectionType) {
+ if (sNotificationDisabledForTesting) return;
+
+ for (Long nativePtr : mNativePtrs) {
+ nativeNotifyConnectionTypeChanged(nativePtr, newConnectionType);
+ }
+ }
+
+ @Override
+ public void onMaxBandwidthChanged(double maxBandwidthMbps) {}
+
+ @NativeClassQualifiedName("BackgroundSyncNetworkObserverAndroid::Observer")
+ private native void nativeNotifyConnectionTypeChanged(long nativePtr, int newConnectionType);
+}
« content/browser/background_sync/background_sync_manager.h ('K') | « content/content_jni.gypi ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698