[BackgroundSync] Trigger network notifications for more events

There is concern that we're missing some network connectivitiy events on
versions >= N due to doze mode. So this CL updates the network on Android via
ConnectivityManager.NetworkCallback events as well.

Bug: 794638
Change-Id: I4c9547653d881a430eb5a696804d778182b6c346
Reviewed-on: https://chromium-review.googlesource.com/896809
Reviewed-by: Peter Beverloo <peter@chromium.org>
Commit-Queue: Josh Karlin <jkarlin@chromium.org>
Cr-Original-Commit-Position: refs/heads/master@{#535006}(cherry picked from commit 9ba2fee6d7c54b256369fc56e64dc6434207a51e)
Reviewed-on: https://chromium-review.googlesource.com/929481
Reviewed-by: Josh Karlin <jkarlin@chromium.org>
Cr-Commit-Position: refs/branch-heads/3325@{#537}
Cr-Branched-From: bc084a8b5afa3744a74927344e304c02ae54189f-refs/heads/master@{#530369}
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
index 997d05a..c51cc5d 100644
--- a/content/public/android/java/src/org/chromium/content/browser/BackgroundSyncNetworkObserver.java
+++ b/content/public/android/java/src/org/chromium/content/browser/BackgroundSyncNetworkObserver.java
@@ -47,6 +47,9 @@
     // List of native observers. These are each called when the network state changes.
     private List<Long> mNativePtrs;
 
+    private int mLastBroadcastConnectionType;
+    private boolean mHasBroadcastConnectionType;
+
     private BackgroundSyncNetworkObserver() {
         ThreadUtils.assertOnUiThread();
         mNativePtrs = new ArrayList<Long>();
@@ -100,22 +103,47 @@
         }
     }
 
-    @Override
-    public void onConnectionTypeChanged(int newConnectionType) {
-        ThreadUtils.assertOnUiThread();
+    private void broadcastNetworkChangeIfNecessary(int newConnectionType) {
+        if (mHasBroadcastConnectionType && newConnectionType == mLastBroadcastConnectionType)
+            return;
+
+        mHasBroadcastConnectionType = true;
+        mLastBroadcastConnectionType = newConnectionType;
         for (Long nativePtr : mNativePtrs) {
             nativeNotifyConnectionTypeChanged(nativePtr, newConnectionType);
         }
     }
 
     @Override
-    public void onConnectionSubtypeChanged(int newConnectionSubtype) {}
+    public void onConnectionTypeChanged(int newConnectionType) {
+        ThreadUtils.assertOnUiThread();
+        broadcastNetworkChangeIfNecessary(newConnectionType);
+    }
+
     @Override
-    public void onNetworkConnect(long netId, int connectionType) {}
+    public void onConnectionSubtypeChanged(int newConnectionSubtype) {}
+
+    @Override
+    public void onNetworkConnect(long netId, int connectionType) {
+        ThreadUtils.assertOnUiThread();
+        // If we're in doze mode (N+ devices), onConnectionTypeChanged may not
+        // be called, but this function should. So update the connection type
+        // if necessary.
+        broadcastNetworkChangeIfNecessary(mNotifier.getCurrentNetworkState().getConnectionType());
+    }
+
     @Override
     public void onNetworkSoonToDisconnect(long netId) {}
+
     @Override
-    public void onNetworkDisconnect(long netId) {}
+    public void onNetworkDisconnect(long netId) {
+        ThreadUtils.assertOnUiThread();
+        // If we're in doze mode (N+ devices), onConnectionTypeChanged may not
+        // be called, but this function should. So update the connection type
+        // if necessary.
+        broadcastNetworkChangeIfNecessary(mNotifier.getCurrentNetworkState().getConnectionType());
+    }
+
     @Override
     public void purgeActiveNetworkList(long[] activeNetIds) {}