OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 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.ConditionVariable; | |
9 import android.os.Handler; | |
10 import android.os.Looper; | |
11 | |
12 import org.chromium.base.JNINamespace; | |
13 | |
14 /** | |
15 * CronetLibraryLoader loads and initializes native library on main thread. | |
16 */ | |
17 @JNINamespace("cronet") | |
18 public class CronetLibraryLoader { | |
19 /** | |
20 * Synchronize access to mInitComplete and initialization routine. | |
21 */ | |
22 private static final Object sLoadAndInitLock = new Object(); | |
23 private static boolean sLoadAndInitComplete = false; | |
24 private static final ConditionVariable sInitialized = new ConditionVariable( ); | |
25 | |
26 public static void loadAndInitialize( | |
27 final Context context, final UrlRequestContextConfig config) { | |
28 synchronized (sLoadAndInitLock) { | |
29 if (sLoadAndInitComplete) { | |
30 return; | |
31 } | |
32 System.loadLibrary(config.libraryName()); | |
33 // Init native Chromium URLRequestContext on Main UI thread. | |
34 Runnable task = new Runnable() { | |
35 public void run() { | |
36 NetworkChangeNotifier.init(context); | |
37 // Registers to always receive network notifications. Note | |
38 // that this call is fine for Cronet because Cronet | |
39 // embedders do not have API access to create network change | |
40 // observers. Existing observers in the net stack do not | |
41 // perform expensive work. | |
42 NetworkChangeNotifier.registerToReceiveNotificationsAlways() ; | |
43 nativeCronetInitOnMainThread(context); | |
xunjieli
2015/01/05 14:48:03
I wonder whether we should do "nativeCronetInitOnM
mef
2015/01/05 16:59:21
Good question. I've copied it from ChromiumUrlRequ
pauljensen
2015/01/06 17:38:40
I don't think the order matters. The only advanta
| |
44 sInitialized.open(); | |
45 } | |
46 }; | |
47 // Run task immediately or post it to the UI thread. | |
48 if (Looper.getMainLooper() == Looper.myLooper()) { | |
49 task.run(); | |
50 } else { | |
51 new Handler(Looper.getMainLooper()).post(task); | |
52 } | |
53 // Wait for initialization to complete on the main thread. | |
54 sInitialized.block(5000); | |
xunjieli
2015/01/05 14:48:03
Why do we need to wait for initialization to compl
mef
2015/01/05 16:59:21
The goal is to prevent race in case of concurrent
| |
55 sLoadAndInitComplete = true; | |
56 } | |
57 } | |
58 | |
59 // Native methods are implemented in cronet_loader.cc. | |
60 private static native long nativeCronetInitOnMainThread(Context context); | |
61 } | |
OLD | NEW |