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

Side by Side Diff: components/cronet/android/java/src/org/chromium/net/impl/CronetLibraryLoader.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 unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.net.impl; 5 package org.chromium.net.impl;
6 6
7 import android.content.Context; 7 import android.content.Context;
8 import android.os.Handler; 8 import android.os.Handler;
9 import android.os.Looper; 9 import android.os.Looper;
10 10
11 import org.chromium.base.ContextUtils; 11 import org.chromium.base.ContextUtils;
12 import org.chromium.base.Log; 12 import org.chromium.base.Log;
13 import org.chromium.base.VisibleForTesting; 13 import org.chromium.base.VisibleForTesting;
14 import org.chromium.base.annotations.JNINamespace; 14 import org.chromium.base.annotations.JNINamespace;
15 import org.chromium.net.NetworkChangeNotifier;
16 15
17 /** 16 /**
18 * CronetLibraryLoader loads and initializes native library on main thread. 17 * CronetLibraryLoader loads and initializes native library on main thread.
19 */ 18 */
20 @JNINamespace("cronet") 19 @JNINamespace("cronet")
21 @VisibleForTesting 20 @VisibleForTesting
22 public class CronetLibraryLoader { 21 public class CronetLibraryLoader {
23 // Synchronize initialization. 22 // Synchronize initialization.
24 private static final Object sLoadLock = new Object(); 23 private static final Object sLoadLock = new Object();
25 private static final String LIBRARY_NAME = "cronet." + ImplVersion.getCronet Version(); 24 private static final String LIBRARY_NAME = "cronet." + ImplVersion.getCronet Version();
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
79 * Ensure that the main thread initialization has completed. Can only be cal led from 78 * Ensure that the main thread initialization has completed. Can only be cal led from
80 * the main thread. Ensures that the NetworkChangeNotifier is initialzied an d the 79 * the main thread. Ensures that the NetworkChangeNotifier is initialzied an d the
81 * main thread native MessageLoop is initialized. 80 * main thread native MessageLoop is initialized.
82 */ 81 */
83 static void ensureInitializedOnMainThread(Context context) { 82 static void ensureInitializedOnMainThread(Context context) {
84 assert sLibraryLoaded; 83 assert sLibraryLoaded;
85 assert Looper.getMainLooper() == Looper.myLooper(); 84 assert Looper.getMainLooper() == Looper.myLooper();
86 if (sMainThreadInitDone) { 85 if (sMainThreadInitDone) {
87 return; 86 return;
88 } 87 }
89 NetworkChangeNotifier.init(context);
90 // Registers to always receive network notifications. Note
91 // that this call is fine for Cronet because Cronet
92 // embedders do not have API access to create network change
93 // observers. Existing observers in the net stack do not
94 // perform expensive work.
95 NetworkChangeNotifier.registerToReceiveNotificationsAlways();
96 // registerToReceiveNotificationsAlways() is called before the native
97 // NetworkChangeNotifierAndroid is created, so as to avoid receiving
98 // the undesired initial network change observer notification, which
99 // will cause active requests to fail with ERR_NETWORK_CHANGED.
100 nativeCronetInitOnMainThread(); 88 nativeCronetInitOnMainThread();
101 sMainThreadInitDone = true; 89 sMainThreadInitDone = true;
102 } 90 }
103 91
104 // Native methods are implemented in cronet_library_loader.cc. 92 // Native methods are implemented in cronet_library_loader.cc.
105 private static native void nativeCronetInitOnMainThread(); 93 private static native void nativeCronetInitOnMainThread();
106 private static native String nativeGetCronetVersion(); 94 private static native String nativeGetCronetVersion();
107 } 95 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698