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 00e463e7f83d0f3cefbb14abfe0891ed28366c08..a180344c46f8eabcf3bb4381c9e9e8926ff14207 100644 |
--- a/components/cronet/android/url_request_context_adapter.cc |
+++ b/components/cronet/android/url_request_context_adapter.cc |
@@ -178,6 +178,11 @@ void URLRequestContextAdapter::InitializeURLRequestContext( |
net::AlternateProtocol::QUIC, |
1.0f); |
} |
+ is_context_initialized_ = true; |
+ while (!tasks_.empty()) { |
+ tasks_.front().Run(); |
+ tasks_.pop(); |
+ } |
} |
if (VLOG_IS_ON(2)) { |
@@ -189,6 +194,26 @@ void URLRequestContextAdapter::InitializeURLRequestContext( |
delegate_->OnContextInitialized(this); |
} |
+// Runs a task that might depend on the context being initialized. |
+// This method can be called on any thread. |
+void URLRequestContextAdapter::RunTask( |
+ const RunAfterContextInitTask& callback) { |
+ GetNetworkTaskRunner()->PostTask( |
+ FROM_HERE, |
+ base::Bind( |
+ &URLRequestContextAdapter::RunTaskOnNetworkThread, this, callback)); |
+} |
+ |
+// This method should only be run on the network thread. |
+void URLRequestContextAdapter::RunTaskOnNetworkThread( |
+ const RunAfterContextInitTask& callback) { |
+ if (is_context_initialized_) { |
+ callback.Run(); |
+ return; |
+ } |
+ tasks_.push(callback); |
+} |
+ |
URLRequestContextAdapter::~URLRequestContextAdapter() { |
if (net_log_observer_) { |
context_->net_log()->RemoveThreadSafeObserver(net_log_observer_.get()); |