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

Unified Diff: components/cronet/android/java/src/org/chromium/net/impl/NetworkChangeNotifierIniter.java

Issue 2811813003: [Cronet] Move NetworkChangeNotifier initialization off UI thread (Closed)
Patch Set: fix NPE Created 3 years, 8 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: components/cronet/android/java/src/org/chromium/net/impl/NetworkChangeNotifierIniter.java
diff --git a/components/cronet/android/java/src/org/chromium/net/impl/NetworkChangeNotifierIniter.java b/components/cronet/android/java/src/org/chromium/net/impl/NetworkChangeNotifierIniter.java
new file mode 100644
index 0000000000000000000000000000000000000000..8288a01f4f510e070226cfce6ff399ea0834c028
--- /dev/null
+++ b/components/cronet/android/java/src/org/chromium/net/impl/NetworkChangeNotifierIniter.java
@@ -0,0 +1,51 @@
+// 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.net.impl;
+
+import org.chromium.base.ContextUtils;
+import org.chromium.base.annotations.CalledByNative;
+import org.chromium.base.annotations.JNINamespace;
+import org.chromium.net.NetworkChangeNotifier;
+
+import javax.annotation.concurrent.GuardedBy;
+
+/**
+ * Controls initialization of NetworkChangeNotifier.
+ */
+@JNINamespace("cronet")
+public class NetworkChangeNotifierIniter {
+ // Synchronize initialization.
+ private static final Object sInitLock = new Object();
+ // Has initialization completed?
+ @GuardedBy("sInitLock")
+ private static volatile boolean sInitDone = false;
+
+ /**
+ * Ensure that the NetworkChangeNotifier is initialized. Can be called from
+ * any thread, the initialization is performed on the first calling thread.
+ */
+ @CalledByNative
+ public static void init() {
+ synchronized (sInitLock) {
+ if (!sInitDone) {
+ NetworkChangeNotifier.init(ContextUtils.getApplicationContext());
+ // Registers to always receive network notifications. Note
+ // that this call is fine for Cronet because Cronet
+ // embedders do not have API access to create network change
+ // observers. Existing observers in the net stack do not
+ // perform expensive work.
+ NetworkChangeNotifier.registerToReceiveNotificationsAlways();
+ // registerToReceiveNotificationsAlways() is called before the native
+ // NetworkChangeNotifierAndroid is created, so as to avoid receiving
+ // the undesired initial network change observer notification, which
+ // will cause active requests to fail with ERR_NETWORK_CHANGED.
+ nativeNetworkChangeNotifierInit();
+ sInitDone = true;
+ }
+ }
+ }
+
+ private static native void nativeNetworkChangeNotifierInit();
+}

Powered by Google App Engine
This is Rietveld 408576698