OLD | NEW |
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; | 5 package org.chromium.net; |
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.annotations.JNINamespace; | 11 import org.chromium.base.annotations.JNINamespace; |
12 | 12 |
13 /** | 13 /** |
14 * CronetLibraryLoader loads and initializes native library on main thread. | 14 * CronetLibraryLoader loads and initializes native library on main thread. |
15 */ | 15 */ |
16 @JNINamespace("cronet") | 16 @JNINamespace("cronet") |
17 class CronetLibraryLoader { | 17 class CronetLibraryLoader { |
18 /** | 18 /** |
19 * Synchronize access to sInitTaskPosted and initialization routine. | 19 * Synchronize access to sInitTaskPosted and initialization routine. |
20 */ | 20 */ |
21 private static final Object sLoadLock = new Object(); | 21 private static final Object sLoadLock = new Object(); |
22 private static boolean sInitTaskPosted = false; | 22 private static boolean sInitTaskPosted = false; |
23 | 23 |
24 /** | 24 /** |
25 * Ensure that native library is loaded and initialized. Can be called from | 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. | 26 * any thread, the load and initialization is performed on main thread. |
27 */ | 27 */ |
28 public static void ensureInitialized( | 28 public static void ensureInitialized( |
29 final Context context, final UrlRequestContextConfig config) { | 29 final Context context, final CronetEngine.Builder builder) { |
30 synchronized (sLoadLock) { | 30 synchronized (sLoadLock) { |
31 if (sInitTaskPosted) { | 31 if (sInitTaskPosted) { |
32 return; | 32 return; |
33 } | 33 } |
34 System.loadLibrary(config.libraryName()); | 34 System.loadLibrary(builder.libraryName()); |
35 if (!Version.CRONET_VERSION.equals(nativeGetCronetVersion())) { | 35 if (!Version.CRONET_VERSION.equals(nativeGetCronetVersion())) { |
36 throw new RuntimeException(String.format( | 36 throw new RuntimeException(String.format( |
37 "Expected Cronet version number %s, " | 37 "Expected Cronet version number %s, " |
38 + "actual version number %s.", | 38 + "actual version number %s.", |
39 Version.CRONET_VERSION, | 39 Version.CRONET_VERSION, |
40 nativeGetCronetVersion())); | 40 nativeGetCronetVersion())); |
41 } | 41 } |
42 nativeCronetInitApplicationContext(context.getApplicationContext()); | 42 nativeCronetInitApplicationContext(context.getApplicationContext()); |
43 // Init native Chromium URLRequestContext on Main UI thread. | 43 // Init native Chromium CronetEngine on Main UI thread. |
44 Runnable task = new Runnable() { | 44 Runnable task = new Runnable() { |
45 public void run() { | 45 public void run() { |
46 initOnMainThread(context); | 46 initOnMainThread(context); |
47 } | 47 } |
48 }; | 48 }; |
49 // Run task immediately or post it to the UI thread. | 49 // Run task immediately or post it to the UI thread. |
50 if (Looper.getMainLooper() == Looper.myLooper()) { | 50 if (Looper.getMainLooper() == Looper.myLooper()) { |
51 task.run(); | 51 task.run(); |
52 } else { | 52 } else { |
53 // The initOnMainThread will complete on the main thread prior | 53 // The initOnMainThread will complete on the main thread prior |
(...skipping 17 matching lines...) Expand all Loading... |
71 // the undesired initial network change observer notification, which | 71 // the undesired initial network change observer notification, which |
72 // will cause active requests to fail with ERR_NETWORK_CHANGED. | 72 // will cause active requests to fail with ERR_NETWORK_CHANGED. |
73 nativeCronetInitOnMainThread(); | 73 nativeCronetInitOnMainThread(); |
74 } | 74 } |
75 | 75 |
76 // Native methods are implemented in cronet_loader.cc. | 76 // Native methods are implemented in cronet_loader.cc. |
77 private static native void nativeCronetInitOnMainThread(); | 77 private static native void nativeCronetInitOnMainThread(); |
78 private static native void nativeCronetInitApplicationContext(Context appCon
text); | 78 private static native void nativeCronetInitApplicationContext(Context appCon
text); |
79 private static native String nativeGetCronetVersion(); | 79 private static native String nativeGetCronetVersion(); |
80 } | 80 } |
OLD | NEW |