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

Unified Diff: net/android/java/src/org/chromium/net/NetworkChangeNotifierAutoDetect.java

Issue 2366503003: Non-functional optimization to Android NetworkChangeNotifier startup (Closed)
Patch Set: add comment Created 4 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/android/java/src/org/chromium/net/NetworkChangeNotifierAutoDetect.java
diff --git a/net/android/java/src/org/chromium/net/NetworkChangeNotifierAutoDetect.java b/net/android/java/src/org/chromium/net/NetworkChangeNotifierAutoDetect.java
index ff0d9a443fef24cca2c890f5f18c358eb4aac999..982299761a30f9b8096d1ce8aff885c3e68a1bd8 100644
--- a/net/android/java/src/org/chromium/net/NetworkChangeNotifierAutoDetect.java
+++ b/net/android/java/src/org/chromium/net/NetworkChangeNotifierAutoDetect.java
@@ -531,6 +531,15 @@ public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver {
private String mWifiSSID;
private double mMaxBandwidthMbps;
private int mMaxBandwidthConnectionType;
+ // When a BroadcastReceiver is registered for a sticky broadcast that has been sent out at
+ // least once, onReceive() will immediately be called. mIgnoreNextBroadcast is set to true
+ // when this class is registered in such a circumstance, and indicates that the next
+ // invokation of onReceive() can be ignored as the state hasn't actually changed. Immediately
+ // prior to mIgnoreNextBroadcast being set, all internal state is updated to the current device
+ // state so were this initial onReceive() call not ignored, no signals would be passed to
+ // observers anyhow as the state hasn't changed. This is simply an optimization to avoid
+ // useless work.
+ private boolean mIgnoreNextBroadcast;
/**
* Observer interface by which observer is notified of network changes.
@@ -608,6 +617,7 @@ public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver {
mMaxBandwidthMbps = getCurrentMaxBandwidthInMbps(networkState);
mMaxBandwidthConnectionType = mConnectionType;
mIntentFilter = new NetworkConnectivityIntentFilter();
+ mIgnoreNextBroadcast = false;
mRegistrationPolicy = policy;
mRegistrationPolicy.init(this);
}
@@ -653,7 +663,11 @@ public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver {
final NetworkState networkState = getCurrentNetworkState();
connectionTypeChanged(networkState);
maxBandwidthChanged(networkState);
- mContext.registerReceiver(this, mIntentFilter);
+ // When registering for a sticky broadcast, like CONNECTIVITY_ACTION, if registerReceiver
+ // returns non-null, it means the broadcast was previously issued and onReceive() will be
+ // immediately called with this previous Intent. Since this initial callback doesn't
+ // actually indicate a network change, we can ignore it by setting mIgnoreNextBroadcast.
+ mIgnoreNextBroadcast = mContext.registerReceiver(this, mIntentFilter) != null;
mRegistered = true;
if (mNetworkCallback != null) {
@@ -876,6 +890,10 @@ public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver {
// BroadcastReceiver
@Override
public void onReceive(Context context, Intent intent) {
+ if (mIgnoreNextBroadcast) {
+ mIgnoreNextBroadcast = false;
+ return;
+ }
final NetworkState networkState = getCurrentNetworkState();
if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
connectionTypeChanged(networkState);
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698