Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.net; | |
| 6 | |
| 7 import android.content.Context; | |
| 8 import android.os.Handler; | |
| 9 import android.os.Looper; | |
| 10 | |
| 11 import org.chromium.base.JNINamespace; | |
| 12 | |
| 13 /** | |
| 14 * CronetLibraryLoader loads and initializes native library on main thread. | |
| 15 */ | |
| 16 @JNINamespace("cronet") | |
| 17 class CronetLibraryLoader { | |
| 18 /** | |
| 19 * Synchronize access to sInitTaskPosted and initialization routine. | |
| 20 */ | |
| 21 private static final Object sLoadLock = new Object(); | |
| 22 private static boolean sInitTaskPosted = false; | |
| 23 | |
| 24 /** | |
| 25 * Ensure that native library is loaded and initialized. Can be called from | |
| 26 * any thread, the load and initialization is performed on main thread. | |
| 27 */ | |
| 28 public static void ensureInitialized( | |
| 29 final Context context, final UrlRequestContextConfig config) { | |
| 30 synchronized (sLoadLock) { | |
| 31 if (sInitTaskPosted) { | |
| 32 return; | |
| 33 } | |
| 34 System.loadLibrary(config.libraryName()); | |
| 35 // Init native Chromium URLRequestContext on Main UI thread. | |
| 36 Runnable task = new Runnable() { | |
| 37 public void run() { | |
| 38 initOnMainThread(context); | |
| 39 } | |
| 40 }; | |
| 41 // Run task immediately or post it to the UI thread. | |
| 42 if (Looper.getMainLooper() == Looper.myLooper()) { | |
| 43 task.run(); | |
| 44 } else { | |
| 45 // The initOnMainThread will complete on the main thread prior | |
| 46 // to other tasks posted to the main thread. | |
| 47 new Handler(Looper.getMainLooper()).post(task); | |
| 48 } | |
| 49 sInitTaskPosted = true; | |
| 50 } | |
| 51 } | |
| 52 | |
| 53 private static void initOnMainThread(final Context context) { | |
| 54 NetworkChangeNotifier.init(context); | |
| 55 // Registers to always receive network notifications. Note | |
| 56 // that this call is fine for Cronet because Cronet | |
| 57 // embedders do not have API access to create network change | |
| 58 // observers. Existing observers in the net stack do not | |
| 59 // perform expensive work. | |
| 60 NetworkChangeNotifier.registerToReceiveNotificationsAlways(); | |
| 61 nativeCronetInitOnMainThread(context); | |
|
xunjieli
2015/02/03 18:37:59
Maybe move nativeCronetInitOnMainThread(context) i
mef
2015/02/03 20:08:55
I'll be glad to do that as I was just copying what
xunjieli
2015/02/03 20:11:48
Yes, that should do it. Thanks!
mef
2015/02/03 22:09:26
Done.
mef
2015/02/05 19:28:56
This change is causing first request to fail with
xunjieli
2015/02/05 19:47:49
I don't understand why this happens. We post initO
mef
2015/02/05 20:31:06
Why was it originally done in this order here: htt
| |
| 62 } | |
| 63 | |
| 64 // Native methods are implemented in cronet_loader.cc. | |
| 65 private static native long nativeCronetInitOnMainThread(Context context); | |
| 66 } | |
| OLD | NEW |