Commit bb7cf7be authored by xunjieli's avatar xunjieli Committed by Commit bot

[Cronet] Delay StartNetLog and StopNetLog until native request context is initialized

Use CronetURLRequestAdapter::PostTaskToNetworkThread instead of posting the task
directly to network thread to make sure the start/stop netlog task is executed
after request context is fully initialized.

Also changed the test cases to make sure that we don't regress.

BUG=470196

Review URL: https://codereview.chromium.org/996163005

Cr-Commit-Position: refs/heads/master@{#322577}
parent 1dc04bbe
...@@ -265,7 +265,7 @@ CronetURLRequestContextAdapter::GetNetworkTaskRunner() const { ...@@ -265,7 +265,7 @@ CronetURLRequestContextAdapter::GetNetworkTaskRunner() const {
void CronetURLRequestContextAdapter::StartNetLogToFile(JNIEnv* env, void CronetURLRequestContextAdapter::StartNetLogToFile(JNIEnv* env,
jobject jcaller, jobject jcaller,
jstring jfile_name) { jstring jfile_name) {
GetNetworkTaskRunner()->PostTask( PostTaskToNetworkThread(
FROM_HERE, FROM_HERE,
base::Bind( base::Bind(
&CronetURLRequestContextAdapter::StartNetLogToFileOnNetworkThread, &CronetURLRequestContextAdapter::StartNetLogToFileOnNetworkThread,
...@@ -274,7 +274,7 @@ void CronetURLRequestContextAdapter::StartNetLogToFile(JNIEnv* env, ...@@ -274,7 +274,7 @@ void CronetURLRequestContextAdapter::StartNetLogToFile(JNIEnv* env,
} }
void CronetURLRequestContextAdapter::StopNetLog(JNIEnv* env, jobject jcaller) { void CronetURLRequestContextAdapter::StopNetLog(JNIEnv* env, jobject jcaller) {
GetNetworkTaskRunner()->PostTask( PostTaskToNetworkThread(
FROM_HERE, FROM_HERE,
base::Bind(&CronetURLRequestContextAdapter::StopNetLogOnNetworkThread, base::Bind(&CronetURLRequestContextAdapter::StopNetLogOnNetworkThread,
base::Unretained(this))); base::Unretained(this)));
...@@ -283,6 +283,8 @@ void CronetURLRequestContextAdapter::StopNetLog(JNIEnv* env, jobject jcaller) { ...@@ -283,6 +283,8 @@ void CronetURLRequestContextAdapter::StopNetLog(JNIEnv* env, jobject jcaller) {
void CronetURLRequestContextAdapter::StartNetLogToFileOnNetworkThread( void CronetURLRequestContextAdapter::StartNetLogToFileOnNetworkThread(
const std::string& file_name) { const std::string& file_name) {
DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
DCHECK(is_context_initialized_);
DCHECK(context_);
// Do nothing if already logging to a file. // Do nothing if already logging to a file.
if (net_log_logger_) if (net_log_logger_)
return; return;
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
package org.chromium.net; package org.chromium.net;
import android.content.Context;
import android.content.ContextWrapper; import android.content.ContextWrapper;
import android.os.ConditionVariable; import android.os.ConditionVariable;
import android.os.Handler; import android.os.Handler;
...@@ -293,18 +294,24 @@ public class CronetUrlRequestContextTest extends CronetTestBase { ...@@ -293,18 +294,24 @@ public class CronetUrlRequestContextTest extends CronetTestBase {
@SmallTest @SmallTest
@Feature({"Cronet"}) @Feature({"Cronet"})
public void testNetLog() throws Exception { public void testNetLog() throws Exception {
mActivity = launchCronetTestApp(); Context context = getInstrumentation().getTargetContext();
File directory = new File(PathUtils.getDataDirectory( File directory = new File(PathUtils.getDataDirectory(context));
getInstrumentation().getTargetContext()));
File file = File.createTempFile("cronet", "json", directory); File file = File.createTempFile("cronet", "json", directory);
mActivity.mUrlRequestContext.startNetLogToFile(file.getPath()); CronetUrlRequestContext requestContext = new CronetUrlRequestContext(
context,
new UrlRequestContextConfig().setLibraryName("cronet_tests"));
// Start NetLog immediately after the request context is created to make
// sure that the call won't crash the app even when the native request
// context is not fully initialized. See crbug.com/470196.
requestContext.startNetLogToFile(file.getPath());
// Start a request. // Start a request.
TestUrlRequestListener listener = new TestUrlRequestListener(); TestUrlRequestListener listener = new TestUrlRequestListener();
UrlRequest urlRequest = mActivity.mUrlRequestContext.createRequest( UrlRequest request = requestContext.createRequest(
TEST_URL, listener, listener.getExecutor()); TEST_URL, listener, listener.getExecutor());
urlRequest.start(); request.start();
listener.blockForDone(); listener.blockForDone();
mActivity.mUrlRequestContext.stopNetLog(); requestContext.stopNetLog();
assertTrue(file.exists()); assertTrue(file.exists());
assertTrue(file.length() != 0); assertTrue(file.length() != 0);
assertTrue(file.delete()); assertTrue(file.delete());
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
package org.chromium.net; package org.chromium.net;
import android.content.Context;
import android.test.suitebuilder.annotation.SmallTest; import android.test.suitebuilder.annotation.SmallTest;
import org.chromium.base.PathUtils; import org.chromium.base.PathUtils;
...@@ -56,15 +57,24 @@ public class CronetUrlTest extends CronetTestBase { ...@@ -56,15 +57,24 @@ public class CronetUrlTest extends CronetTestBase {
@SmallTest @SmallTest
@Feature({"Cronet"}) @Feature({"Cronet"})
public void testNetLog() throws Exception { public void testNetLog() throws Exception {
CronetTestActivity activity = launchCronetTestApp(); Context context = getInstrumentation().getTargetContext();
File directory = new File(PathUtils.getDataDirectory( File directory = new File(PathUtils.getDataDirectory(context));
getInstrumentation().getTargetContext()));
File file = File.createTempFile("cronet", "json", directory); File file = File.createTempFile("cronet", "json", directory);
activity.mRequestFactory.startNetLogToFile(file.getPath()); HttpUrlRequestFactory factory = HttpUrlRequestFactory.createFactory(
context,
new UrlRequestContextConfig().setLibraryName("cronet_tests"));
// Start NetLog immediately after the request context is created to make
// sure that the call won't crash the app even when the native request
// context is not fully initialized. See crbug.com/470196.
factory.startNetLogToFile(file.getPath());
// Starts a request. // Starts a request.
activity.startWithURL(URL); HashMap<String, String> headers = new HashMap<String, String>();
Thread.sleep(5000); TestHttpUrlRequestListener listener = new TestHttpUrlRequestListener();
activity.mRequestFactory.stopNetLog(); HttpUrlRequest request = factory.createRequest(
URL, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener);
request.start();
listener.blockForComplete();
factory.stopNetLog();
assertTrue(file.exists()); assertTrue(file.exists());
assertTrue(file.length() != 0); assertTrue(file.length() != 0);
assertTrue(file.delete()); assertTrue(file.delete());
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment