package org.chromium.net;
import android.content.Context;
-import android.os.ConditionVariable;
+import android.os.Handler;
+import android.os.Looper;
import android.os.Process;
import android.util.Log;
*/
private long mChromiumUrlRequestContextAdapter;
- private final ConditionVariable mStarted = new ConditionVariable();
-
/**
* Constructor.
- *
*/
- protected ChromiumUrlRequestContext(Context context, String userAgent,
+ protected ChromiumUrlRequestContext(final Context context, String userAgent,
String config) {
mChromiumUrlRequestContextAdapter = nativeCreateRequestContextAdapter(
context, userAgent, getLoggingLevel(), config);
- if (mChromiumUrlRequestContextAdapter == 0)
+ if (mChromiumUrlRequestContextAdapter == 0) {
throw new NullPointerException("Context Adapter creation failed");
-
- // TODO(mef): Revisit the need of block here.
- mStarted.block(2000);
+ }
+ // Post a task to UI thread to init native Chromium URLRequestContext.
+ // TODO(xunjieli): This constructor is not supposed to be invoked on
+ // the main thread. Consider making the following code into a blocking
+ // API to handle the case where we are already on main thread.
+ Runnable task = new Runnable() {
+ public void run() {
+ 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();
+ nativeInitRequestContextOnMainThread(
+ mChromiumUrlRequestContextAdapter);
+ }
+ };
+ new Handler(Looper.getMainLooper()).post(task);
}
/**
private void initNetworkThread() {
Thread.currentThread().setName("ChromiumNet");
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
- mStarted.open();
}
@Override
long chromiumUrlRequestContextAdapter, String fileName);
private native void nativeStopNetLog(long chromiumUrlRequestContextAdapter);
+
+ private native void nativeInitRequestContextOnMainThread(
+ long chromiumUrlRequestContextAdapter);
}