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

Unified Diff: components/cronet/android/url_request_context_adapter.cc

Issue 617393005: Make URLRequestContextAdapter initialization asynchronous (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed comments Created 6 years, 2 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/url_request_context_adapter.cc
diff --git a/components/cronet/android/url_request_context_adapter.cc b/components/cronet/android/url_request_context_adapter.cc
index 7c99244781d467e9d641e830c58adc62c440ea5c..47955bcc34eb1497326b8da013652afedf29ef2d 100644
--- a/components/cronet/android/url_request_context_adapter.cc
+++ b/components/cronet/android/url_request_context_adapter.cc
@@ -135,6 +135,7 @@ void URLRequestContextAdapter::Initialize(
void URLRequestContextAdapter::InitializeURLRequestContext(
scoped_ptr<URLRequestContextConfig> config) {
+ DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
// TODO(mmenke): Add method to have the builder enable SPDY.
net::URLRequestContextBuilder context_builder;
context_builder.set_network_delegate(new BasicNetworkDelegate());
@@ -178,6 +179,11 @@ void URLRequestContextAdapter::InitializeURLRequestContext(
net::AlternateProtocol::QUIC,
1.0f);
}
+ is_context_initialized_ = true;
+ while (!tasks_waiting_for_context_.empty()) {
+ tasks_waiting_for_context_.front().Run();
+ tasks_waiting_for_context_.pop();
+ }
}
if (VLOG_IS_ON(2)) {
@@ -189,12 +195,32 @@ void URLRequestContextAdapter::InitializeURLRequestContext(
delegate_->OnContextInitialized(this);
}
+void URLRequestContextAdapter::RunTaskAfterContextInit(
+ const RunAfterContextInitTask& callback) {
+ GetNetworkTaskRunner()->PostTask(
+ FROM_HERE,
+ base::Bind(
+ &URLRequestContextAdapter::RunTaskAfterContextInitOnNetworkThread,
+ this,
+ callback));
+}
+
+void URLRequestContextAdapter::RunTaskAfterContextInitOnNetworkThread(
+ const RunAfterContextInitTask& callback) {
+ DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
+ if (is_context_initialized_) {
+ callback.Run();
+ return;
+ }
+ tasks_waiting_for_context_.push(callback);
+}
+
URLRequestContextAdapter::~URLRequestContextAdapter() {
mmenke 2014/10/03 20:26:58 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThr
xunjieli 2014/10/03 20:39:52 Done.
if (net_log_observer_) {
context_->net_log()->RemoveThreadSafeObserver(net_log_observer_.get());
net_log_observer_.reset();
}
- StopNetLog();
+ StopNetLogHelper();
// TODO(mef): Ensure that |network_thread_| is destroyed properly.
}
@@ -204,6 +230,7 @@ const std::string& URLRequestContextAdapter::GetUserAgent(
}
net::URLRequestContext* URLRequestContextAdapter::GetURLRequestContext() {
+ DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
if (!context_) {
LOG(ERROR) << "URLRequestContext is not set up";
}
@@ -216,6 +243,18 @@ URLRequestContextAdapter::GetNetworkTaskRunner() const {
}
void URLRequestContextAdapter::StartNetLogToFile(const std::string& file_name) {
+ RunTaskAfterContextInit(base::Bind(
+ &URLRequestContextAdapter::StartNetLogToFileHelper, this, file_name));
+}
+
+void URLRequestContextAdapter::StopNetLog() {
+ RunTaskAfterContextInit(
+ base::Bind(&URLRequestContextAdapter::StopNetLogHelper, this));
+}
+
+void URLRequestContextAdapter::StartNetLogToFileHelper(
+ const std::string& file_name) {
+ DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
// Do nothing if already logging to a file.
if (net_log_logger_)
return;
@@ -230,7 +269,8 @@ void URLRequestContextAdapter::StartNetLogToFile(const std::string& file_name) {
net_log_logger_->StartObserving(context_->net_log());
}
-void URLRequestContextAdapter::StopNetLog() {
+void URLRequestContextAdapter::StopNetLogHelper() {
+ DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
if (net_log_logger_) {
net_log_logger_->StopObserving();
net_log_logger_.reset();

Powered by Google App Engine
This is Rietveld 408576698