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

Unified Diff: components/cronet/android/java/src/org/chromium/net/CronetLibraryLoader.java

Issue 726013002: [Cronet] Hook up library loader, system proxy and network change notifier to async api. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address Helen's comments. Created 5 years, 11 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 side-by-side diff with in-line comments
Download patch
Index: components/cronet/android/java/src/org/chromium/net/CronetLibraryLoader.java
diff --git a/components/cronet/android/java/src/org/chromium/net/CronetLibraryLoader.java b/components/cronet/android/java/src/org/chromium/net/CronetLibraryLoader.java
new file mode 100644
index 0000000000000000000000000000000000000000..3e49a3ad799e6b3f7b2ced65f3a4afa5adab3935
--- /dev/null
+++ b/components/cronet/android/java/src/org/chromium/net/CronetLibraryLoader.java
@@ -0,0 +1,69 @@
+// Copyright 2014 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;
+
+import android.content.Context;
+import android.os.Handler;
+import android.os.Looper;
+
+import org.chromium.base.JNINamespace;
+
+/**
+ * CronetLibraryLoader loads and initializes native library on main thread.
+ */
+@JNINamespace("cronet")
+public class CronetLibraryLoader {
+ /**
+ * Synchronize access to mInitComplete and initialization routine.
+ */
+ private static final Object sLoadAndInitLock = new Object();
+ private static boolean sLoadAndInitComplete = false;
+
+ /**
+ * Ensure that native library is loaded and initialized. Can be called from
+ * any thread, the load and initialization is performed on main thread.
+ */
+ public static void ensureInitialized(
+ final Context context, final UrlRequestContextConfig config) {
+ synchronized (sLoadAndInitLock) {
+ if (sLoadAndInitComplete) {
+ return;
+ }
+ System.loadLibrary(config.libraryName());
+ // Init native Chromium URLRequestContext on Main UI thread.
+ Runnable task = new Runnable() {
+ public void run() {
+ initOnMainThread(context);
+ }
+ };
+ // Run task immediately or post it to the UI thread.
+ if (Looper.getMainLooper() == Looper.myLooper()) {
+ task.run();
+ } else {
+ new Handler(Looper.getMainLooper()).post(task);
+ }
+ }
+ }
+
+ private static void initOnMainThread(final Context context) {
+ synchronized (sLoadAndInitLock) {
+ if (sLoadAndInitComplete) {
+ return;
+ }
+ NetworkChangeNotifier.init(context);
+ // 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();
+ nativeCronetInitOnMainThread(context);
+ sLoadAndInitComplete = true;
+ }
+ }
+
+ // Native methods are implemented in cronet_loader.cc.
+ private static native long nativeCronetInitOnMainThread(Context context);
+}

Powered by Google App Engine
This is Rietveld 408576698