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 {
void CronetURLRequestContextAdapter::StartNetLogToFile(JNIEnv* env,
jobject jcaller,
jstring jfile_name) {
GetNetworkTaskRunner()->PostTask(
PostTaskToNetworkThread(
FROM_HERE,
base::Bind(
&CronetURLRequestContextAdapter::StartNetLogToFileOnNetworkThread,
......@@ -274,7 +274,7 @@ void CronetURLRequestContextAdapter::StartNetLogToFile(JNIEnv* env,
}
void CronetURLRequestContextAdapter::StopNetLog(JNIEnv* env, jobject jcaller) {
GetNetworkTaskRunner()->PostTask(
PostTaskToNetworkThread(
FROM_HERE,
base::Bind(&CronetURLRequestContextAdapter::StopNetLogOnNetworkThread,
base::Unretained(this)));
......@@ -283,6 +283,8 @@ void CronetURLRequestContextAdapter::StopNetLog(JNIEnv* env, jobject jcaller) {
void CronetURLRequestContextAdapter::StartNetLogToFileOnNetworkThread(
const std::string& file_name) {
DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
DCHECK(is_context_initialized_);
DCHECK(context_);
// Do nothing if already logging to a file.
if (net_log_logger_)
return;
......
......@@ -4,6 +4,7 @@
package org.chromium.net;
import android.content.Context;
import android.content.ContextWrapper;
import android.os.ConditionVariable;
import android.os.Handler;
......@@ -293,18 +294,24 @@ public class CronetUrlRequestContextTest extends CronetTestBase {
@SmallTest
@Feature({"Cronet"})
public void testNetLog() throws Exception {
mActivity = launchCronetTestApp();
File directory = new File(PathUtils.getDataDirectory(
getInstrumentation().getTargetContext()));
Context context = getInstrumentation().getTargetContext();
File directory = new File(PathUtils.getDataDirectory(context));
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.
TestUrlRequestListener listener = new TestUrlRequestListener();
UrlRequest urlRequest = mActivity.mUrlRequestContext.createRequest(
UrlRequest request = requestContext.createRequest(
TEST_URL, listener, listener.getExecutor());
urlRequest.start();
request.start();
listener.blockForDone();
mActivity.mUrlRequestContext.stopNetLog();
requestContext.stopNetLog();
assertTrue(file.exists());
assertTrue(file.length() != 0);
assertTrue(file.delete());
......
......@@ -4,6 +4,7 @@
package org.chromium.net;
import android.content.Context;
import android.test.suitebuilder.annotation.SmallTest;
import org.chromium.base.PathUtils;
......@@ -56,15 +57,24 @@ public class CronetUrlTest extends CronetTestBase {
@SmallTest
@Feature({"Cronet"})
public void testNetLog() throws Exception {
CronetTestActivity activity = launchCronetTestApp();
File directory = new File(PathUtils.getDataDirectory(
getInstrumentation().getTargetContext()));
Context context = getInstrumentation().getTargetContext();
File directory = new File(PathUtils.getDataDirectory(context));
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.
activity.startWithURL(URL);
Thread.sleep(5000);
activity.mRequestFactory.stopNetLog();
HashMap<String, String> headers = new HashMap<String, String>();
TestHttpUrlRequestListener listener = new TestHttpUrlRequestListener();
HttpUrlRequest request = factory.createRequest(
URL, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener);
request.start();
listener.blockForComplete();
factory.stopNetLog();
assertTrue(file.exists());
assertTrue(file.length() != 0);
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