| 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();
|
| +}
|
|
|