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

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

Issue 586143002: Initial implementation of Cronet Async API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Sync Created 6 years, 1 month 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/cronet_url_request_context_adapter.cc
diff --git a/components/cronet/android/url_request_context_adapter.cc b/components/cronet/android/cronet_url_request_context_adapter.cc
similarity index 57%
copy from components/cronet/android/url_request_context_adapter.cc
copy to components/cronet/android/cronet_url_request_context_adapter.cc
index baec6e8f4b21a14b8685f56caca36bfb0ead9c40..a20880f2ab71c797b4f8459927d4a94182359cc2 100644
--- a/components/cronet/android/url_request_context_adapter.cc
+++ b/components/cronet/android/cronet_url_request_context_adapter.cc
@@ -2,39 +2,29 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "components/cronet/android/url_request_context_adapter.h"
-
-#include <limits>
+#include "components/cronet/android/cronet_url_request_context_adapter.h"
#include "base/bind.h"
#include "base/files/file_util.h"
-#include "base/message_loop/message_loop.h"
#include "base/single_thread_task_runner.h"
#include "components/cronet/url_request_context_config.h"
-#include "net/android/network_change_notifier_factory_android.h"
#include "net/base/net_errors.h"
#include "net/base/net_log_logger.h"
-#include "net/base/net_util.h"
-#include "net/base/network_change_notifier.h"
#include "net/cert/cert_verifier.h"
#include "net/http/http_auth_handler_factory.h"
#include "net/http/http_network_layer.h"
#include "net/http/http_server_properties.h"
+#include "net/proxy/proxy_config_service_fixed.h"
#include "net/proxy/proxy_service.h"
#include "net/ssl/ssl_config_service_defaults.h"
#include "net/url_request/static_http_user_agent_settings.h"
+#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_builder.h"
#include "net/url_request/url_request_context_storage.h"
#include "net/url_request/url_request_job_factory_impl.h"
namespace {
-// MessageLoop on the main thread, which is where objects that receive Java
-// notifications generally live.
-base::MessageLoop* g_main_message_loop = nullptr;
-
-net::NetworkChangeNotifier* g_network_change_notifier = nullptr;
-
class BasicNetworkDelegate : public net::NetworkDelegate {
public:
BasicNetworkDelegate() {}
@@ -71,15 +61,15 @@ class BasicNetworkDelegate : public net::NetworkDelegate {
void OnResponseStarted(net::URLRequest* request) override {}
- void OnRawBytesRead(const net::URLRequest& request,
- int bytes_read) override {}
+ void OnRawBytesRead(const net::URLRequest& request, int bytes_read) override {
+ }
void OnCompleted(net::URLRequest* request, bool started) override {}
void OnURLRequestDestroyed(net::URLRequest* request) override {}
- void OnPACScriptError(int line_number,
- const base::string16& error) override {}
+ void OnPACScriptError(int line_number, const base::string16& error) override {
+ }
NetworkDelegate::AuthRequiredResponse OnAuthRequired(
net::URLRequest* request,
@@ -105,8 +95,7 @@ class BasicNetworkDelegate : public net::NetworkDelegate {
return false;
}
- bool OnCanThrottleRequest(
- const net::URLRequest& request) const override {
+ bool OnCanThrottleRequest(const net::URLRequest& request) const override {
return false;
}
@@ -123,60 +112,50 @@ class BasicNetworkDelegate : public net::NetworkDelegate {
namespace cronet {
-URLRequestContextAdapter::URLRequestContextAdapter(
- URLRequestContextAdapterDelegate* delegate,
- std::string user_agent) {
- delegate_ = delegate;
- user_agent_ = user_agent;
+CronetURLRequestContextAdapter::CronetURLRequestContextAdapter() {
}
-void URLRequestContextAdapter::Initialize(
- scoped_ptr<URLRequestContextConfig> config) {
+CronetURLRequestContextAdapter::~CronetURLRequestContextAdapter() {
+ DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
+ StopNetLogOnNetworkThread();
+}
+
+void CronetURLRequestContextAdapter::Initialize(
+ scoped_ptr<URLRequestContextConfig> config,
+ const base::Closure& java_init_network_thread) {
network_thread_ = new base::Thread("network");
base::Thread::Options options;
options.message_loop_type = base::MessageLoop::TYPE_IO;
network_thread_->StartWithOptions(options);
- config_ = config.Pass();
-}
-void URLRequestContextAdapter::InitRequestContextOnMainThread() {
- if (!base::MessageLoop::current()) {
- DCHECK(!g_main_message_loop);
- g_main_message_loop = new base::MessageLoopForUI();
- base::MessageLoopForUI::current()->Start();
- }
- DCHECK_EQ(g_main_message_loop, base::MessageLoop::current());
- if (!g_network_change_notifier) {
- net::NetworkChangeNotifier::SetFactory(
- new net::NetworkChangeNotifierFactoryAndroid());
- g_network_change_notifier = net::NetworkChangeNotifier::Create();
- }
- proxy_config_service_.reset(net::ProxyService::CreateSystemProxyConfigService(
- GetNetworkTaskRunner(), NULL));
GetNetworkTaskRunner()->PostTask(
FROM_HERE,
- base::Bind(&URLRequestContextAdapter::InitRequestContextOnNetworkThread,
- this));
+ base::Bind(&CronetURLRequestContextAdapter::InitializeOnNetworkThread,
+ base::Unretained(this),
+ Passed(&config),
+ java_init_network_thread));
}
-void URLRequestContextAdapter::InitRequestContextOnNetworkThread() {
+void CronetURLRequestContextAdapter::InitializeOnNetworkThread(
+ scoped_ptr<URLRequestContextConfig> config,
+ const base::Closure& java_init_network_thread) {
DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
- DCHECK(config_);
// TODO(mmenke): Add method to have the builder enable SPDY.
net::URLRequestContextBuilder context_builder;
context_builder.set_network_delegate(new BasicNetworkDelegate());
- context_builder.set_proxy_config_service(proxy_config_service_.get());
- config_->ConfigureURLRequestContextBuilder(&context_builder);
+ context_builder.set_proxy_config_service(
+ new net::ProxyConfigServiceFixed(net::ProxyConfig()));
+ config->ConfigureURLRequestContextBuilder(&context_builder);
context_.reset(context_builder.Build());
// Currently (circa M39) enabling QUIC requires setting probability threshold.
- if (config_->enable_quic) {
+ if (config->enable_quic) {
context_->http_server_properties()
->SetAlternateProtocolProbabilityThreshold(0.0f);
- for (size_t hint = 0; hint < config_->quic_hints.size(); ++hint) {
- const URLRequestContextConfig::QuicHint& quic_hint =
- *config_->quic_hints[hint];
+ for (auto hint = config->quic_hints.begin();
+ hint != config->quic_hints.end(); ++hint) {
+ const URLRequestContextConfig::QuicHint& quic_hint = **hint;
if (quic_hint.host.empty()) {
LOG(ERROR) << "Empty QUIC hint host: " << quic_hint.host;
continue;
@@ -213,61 +192,21 @@ void URLRequestContextAdapter::InitRequestContextOnNetworkThread() {
1.0f);
}
}
- config_.reset(NULL);
- if (VLOG_IS_ON(2)) {
- net_log_observer_.reset(new NetLogObserver());
- context_->net_log()->AddThreadSafeObserver(net_log_observer_.get(),
- net::NetLog::LOG_ALL_BUT_BYTES);
- }
-
- is_context_initialized_ = true;
- while (!tasks_waiting_for_context_.empty()) {
- tasks_waiting_for_context_.front().Run();
- tasks_waiting_for_context_.pop();
- }
-
- delegate_->OnContextInitialized(this);
+ java_init_network_thread.Run();
}
-void URLRequestContextAdapter::PostTaskToNetworkThread(
- const tracked_objects::Location& posted_from,
- const RunAfterContextInitTask& callback) {
- GetNetworkTaskRunner()->PostTask(
- posted_from,
- base::Bind(
- &URLRequestContextAdapter::RunTaskAfterContextInitOnNetworkThread,
- this,
- callback));
+void CronetURLRequestContextAdapter::Destroy() {
+ DCHECK(!GetNetworkTaskRunner()->BelongsToCurrentThread());
+ // Stick network_thread_ in a local, as |this| may be destroyed from the
+ // network thread before delete network_thread is called.
+ base::Thread* network_thread = network_thread_;
+ GetNetworkTaskRunner()->DeleteSoon(FROM_HERE, this);
+ // Deleting thread stops it after all tasks are completed.
+ delete network_thread;
}
-void URLRequestContextAdapter::RunTaskAfterContextInitOnNetworkThread(
- const RunAfterContextInitTask& callback) {
- DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
- if (is_context_initialized_) {
- callback.Run();
- return;
- }
- tasks_waiting_for_context_.push(callback);
-}
-
-URLRequestContextAdapter::~URLRequestContextAdapter() {
- DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
- if (net_log_observer_) {
- context_->net_log()->RemoveThreadSafeObserver(net_log_observer_.get());
- net_log_observer_.reset();
- }
- StopNetLogHelper();
- // TODO(mef): Ensure that |network_thread_| is destroyed properly.
-}
-
-const std::string& URLRequestContextAdapter::GetUserAgent(
- const GURL& url) const {
- return user_agent_;
-}
-
-net::URLRequestContext* URLRequestContextAdapter::GetURLRequestContext() {
- DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
+net::URLRequestContext* CronetURLRequestContextAdapter::GetURLRequestContext() {
if (!context_) {
LOG(ERROR) << "URLRequestContext is not set up";
}
@@ -275,23 +214,28 @@ net::URLRequestContext* URLRequestContextAdapter::GetURLRequestContext() {
}
scoped_refptr<base::SingleThreadTaskRunner>
-URLRequestContextAdapter::GetNetworkTaskRunner() const {
- return network_thread_->message_loop_proxy();
+CronetURLRequestContextAdapter::GetNetworkTaskRunner() const {
+ return network_thread_->task_runner();
}
-void URLRequestContextAdapter::StartNetLogToFile(const std::string& file_name) {
- PostTaskToNetworkThread(
+void CronetURLRequestContextAdapter::StartNetLogToFile(
+ const std::string& file_name) {
+ GetNetworkTaskRunner()->PostTask(
FROM_HERE,
base::Bind(
- &URLRequestContextAdapter::StartNetLogToFileHelper, this, file_name));
+ &CronetURLRequestContextAdapter::StartNetLogToFileOnNetworkThread,
+ base::Unretained(this),
+ file_name));
}
-void URLRequestContextAdapter::StopNetLog() {
- PostTaskToNetworkThread(
- FROM_HERE, base::Bind(&URLRequestContextAdapter::StopNetLogHelper, this));
+void CronetURLRequestContextAdapter::StopNetLog() {
+ GetNetworkTaskRunner()->PostTask(
+ FROM_HERE,
+ base::Bind(&CronetURLRequestContextAdapter::StopNetLogOnNetworkThread,
+ base::Unretained(this)));
}
-void URLRequestContextAdapter::StartNetLogToFileHelper(
+void CronetURLRequestContextAdapter::StartNetLogToFileOnNetworkThread(
const std::string& file_name) {
DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
// Do nothing if already logging to a file.
@@ -308,7 +252,7 @@ void URLRequestContextAdapter::StartNetLogToFileHelper(
net_log_logger_->StartObserving(context_->net_log());
}
-void URLRequestContextAdapter::StopNetLogHelper() {
+void CronetURLRequestContextAdapter::StopNetLogOnNetworkThread() {
DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
if (net_log_logger_) {
net_log_logger_->StopObserving();
@@ -316,9 +260,4 @@ void URLRequestContextAdapter::StopNetLogHelper() {
}
}
-void NetLogObserver::OnAddEntry(const net::NetLog::Entry& entry) {
- VLOG(2) << "Net log entry: type=" << entry.type()
- << ", source=" << entry.source().type << ", phase=" << entry.phase();
-}
-
} // namespace cronet

Powered by Google App Engine
This is Rietveld 408576698