Chromium Code Reviews| 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..ce38e528b8ed401b1f5e391c77bf993a2f1af0ed |
| --- /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.Log; |
| +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; |
|
jkarlin
2015/09/24 17:51:30
insert newline
iclelland
2015/09/24 20:37:20
Done.
|
| +/** |
| + * 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 lives on the main thread. |
| + */ |
| +@JNINamespace("content") |
| +class BackgroundSyncNetworkObserver implements NetworkChangeNotifierAutoDetect.Observer { |
| + private static final String TAG = "cr_BgSyncNetObserver"; |
| + |
| + private NetworkChangeNotifierAutoDetect mNotifier; |
| + private Context mContext; |
| + |
| + // The singleton instance. |
| + private static BackgroundSyncNetworkObserver sInstance = null; |
| + |
| + // List of native observers. These are each called when the network state changes. |
| + private List<Long> mNativePtrs; |
| + |
| + private BackgroundSyncNetworkObserver(Context ctx) { |
| + ThreadUtils.assertOnUiThread(); |
| + mContext = ctx; |
| + mNativePtrs = new ArrayList<Long>(); |
| + } |
| + |
| + @CalledByNative |
| + private static BackgroundSyncNetworkObserver createObserver(Context ctx, long nativePtr) { |
| + ThreadUtils.assertOnUiThread(); |
| + if (sInstance == null) { |
| + sInstance = new BackgroundSyncNetworkObserver(ctx); |
| + } |
| + sInstance.registerObserver(nativePtr); |
| + return sInstance; |
| + } |
| + |
| + private void registerObserver(final long nativePtr) { |
| + ThreadUtils.assertOnUiThread(); |
| + // Create the NetworkChangeNotifierAutoDetect if it does not exist already. |
| + if (mNotifier == null) { |
| + mNotifier = new NetworkChangeNotifierAutoDetect(this, mContext, true); |
| + } |
| + mNativePtrs.add(nativePtr); |
| + |
| + // This may fail in unit tests, if the JNI native methods have not been registered. |
|
jkarlin
2015/09/24 17:51:30
Rather than add try/catch and a comment to product
iclelland
2015/09/24 20:37:20
Acknowledged.
|
| + try { |
| + nativeNotifyConnectionTypeChanged(nativePtr, |
| + mNotifier.getCurrentConnectionType(mNotifier.getCurrentNetworkState())); |
| + } catch (UnsatisfiedLinkError j) { |
| + Log.d(TAG, "JNI native methods are not registered. Unable to notify new observer"); |
| + } |
| + } |
| + |
| + @CalledByNative |
| + private void removeObserver(long nativePtr) { |
| + ThreadUtils.assertOnUiThread(); |
| + mNativePtrs.remove(nativePtr); |
| + // Destroy the NetworkChangeNotifierAutoDetect if there are no more observers. |
| + if (mNativePtrs.size() == 0 && mNotifier != null) { |
| + mNotifier.destroy(); |
| + mNotifier = null; |
| + } |
| + } |
| + |
| + @Override |
| + public void onConnectionTypeChanged(int newConnectionType) { |
| + ThreadUtils.assertOnUiThread(); |
| + for (Long nativePtr : mNativePtrs) { |
| + // This may fail in unit tests, if the JNI native methods have not been registered. |
|
jkarlin
2015/09/24 17:51:30
Ditto.
iclelland
2015/09/24 20:37:20
Acknowledged.
|
| + try { |
| + nativeNotifyConnectionTypeChanged(nativePtr, newConnectionType); |
| + } catch (UnsatisfiedLinkError j) { |
| + Log.d(TAG, "JNI native methods are not registered. Unable to notify observers"); |
| + } |
| + } |
| + } |
| + |
| + @Override |
| + public void onMaxBandwidthChanged(double maxBandwidthMbps) {} |
| + |
| + @NativeClassQualifiedName("BackgroundSyncNetworkObserverAndroid::Observer") |
| + private native void nativeNotifyConnectionTypeChanged(long nativePtr, int newConnectionType); |
| +} |