Correctly track texture cleared state for sharing
[chromium-blink-merge.git] / components / cronet / android / url_request_context_adapter.cc
blob6234aa942406bf4d04c058a78bde8298ad83f589
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/cronet/android/url_request_context_adapter.h"
7 #include <limits>
9 #include "base/bind.h"
10 #include "base/files/file_util.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/single_thread_task_runner.h"
13 #include "components/cronet/url_request_context_config.h"
14 #include "net/android/network_change_notifier_factory_android.h"
15 #include "net/base/net_errors.h"
16 #include "net/base/net_log_logger.h"
17 #include "net/base/net_util.h"
18 #include "net/base/network_change_notifier.h"
19 #include "net/base/network_delegate_impl.h"
20 #include "net/cert/cert_verifier.h"
21 #include "net/http/http_auth_handler_factory.h"
22 #include "net/http/http_network_layer.h"
23 #include "net/http/http_server_properties.h"
24 #include "net/proxy/proxy_service.h"
25 #include "net/ssl/ssl_config_service_defaults.h"
26 #include "net/url_request/static_http_user_agent_settings.h"
27 #include "net/url_request/url_request_context_builder.h"
28 #include "net/url_request/url_request_context_storage.h"
29 #include "net/url_request/url_request_job_factory_impl.h"
31 namespace {
33 // MessageLoop on the main thread, which is where objects that receive Java
34 // notifications generally live.
35 base::MessageLoop* g_main_message_loop = nullptr;
37 net::NetworkChangeNotifier* g_network_change_notifier = nullptr;
39 class BasicNetworkDelegate : public net::NetworkDelegateImpl {
40 public:
41 BasicNetworkDelegate() {}
42 virtual ~BasicNetworkDelegate() {}
44 private:
45 // net::NetworkDelegate implementation.
46 int OnBeforeURLRequest(net::URLRequest* request,
47 const net::CompletionCallback& callback,
48 GURL* new_url) override {
49 return net::OK;
52 int OnBeforeSendHeaders(net::URLRequest* request,
53 const net::CompletionCallback& callback,
54 net::HttpRequestHeaders* headers) override {
55 return net::OK;
58 void OnSendHeaders(net::URLRequest* request,
59 const net::HttpRequestHeaders& headers) override {}
61 int OnHeadersReceived(
62 net::URLRequest* request,
63 const net::CompletionCallback& callback,
64 const net::HttpResponseHeaders* original_response_headers,
65 scoped_refptr<net::HttpResponseHeaders>* _response_headers,
66 GURL* allowed_unsafe_redirect_url) override {
67 return net::OK;
70 void OnBeforeRedirect(net::URLRequest* request,
71 const GURL& new_location) override {}
73 void OnResponseStarted(net::URLRequest* request) override {}
75 void OnRawBytesRead(const net::URLRequest& request,
76 int bytes_read) override {}
78 void OnCompleted(net::URLRequest* request, bool started) override {}
80 void OnURLRequestDestroyed(net::URLRequest* request) override {}
82 void OnPACScriptError(int line_number,
83 const base::string16& error) override {}
85 NetworkDelegate::AuthRequiredResponse OnAuthRequired(
86 net::URLRequest* request,
87 const net::AuthChallengeInfo& auth_info,
88 const AuthCallback& callback,
89 net::AuthCredentials* credentials) override {
90 return net::NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION;
93 bool OnCanGetCookies(const net::URLRequest& request,
94 const net::CookieList& cookie_list) override {
95 return false;
98 bool OnCanSetCookie(const net::URLRequest& request,
99 const std::string& cookie_line,
100 net::CookieOptions* options) override {
101 return false;
104 bool OnCanAccessFile(const net::URLRequest& request,
105 const base::FilePath& path) const override {
106 return false;
109 bool OnCanThrottleRequest(
110 const net::URLRequest& request) const override {
111 return false;
114 DISALLOW_COPY_AND_ASSIGN(BasicNetworkDelegate);
117 } // namespace
119 namespace cronet {
121 URLRequestContextAdapter::URLRequestContextAdapter(
122 URLRequestContextAdapterDelegate* delegate,
123 std::string user_agent)
124 : load_disable_cache_(true),
125 is_context_initialized_(false) {
126 delegate_ = delegate;
127 user_agent_ = user_agent;
130 void URLRequestContextAdapter::Initialize(
131 scoped_ptr<URLRequestContextConfig> config) {
132 network_thread_ = new base::Thread("network");
133 base::Thread::Options options;
134 options.message_loop_type = base::MessageLoop::TYPE_IO;
135 network_thread_->StartWithOptions(options);
136 config_ = config.Pass();
139 void URLRequestContextAdapter::InitRequestContextOnMainThread() {
140 if (!base::MessageLoop::current()) {
141 DCHECK(!g_main_message_loop);
142 g_main_message_loop = new base::MessageLoopForUI();
143 base::MessageLoopForUI::current()->Start();
145 DCHECK_EQ(g_main_message_loop, base::MessageLoop::current());
146 if (!g_network_change_notifier) {
147 net::NetworkChangeNotifier::SetFactory(
148 new net::NetworkChangeNotifierFactoryAndroid());
149 g_network_change_notifier = net::NetworkChangeNotifier::Create();
151 proxy_config_service_.reset(net::ProxyService::CreateSystemProxyConfigService(
152 GetNetworkTaskRunner(), NULL));
153 GetNetworkTaskRunner()->PostTask(
154 FROM_HERE,
155 base::Bind(&URLRequestContextAdapter::InitRequestContextOnNetworkThread,
156 this));
159 void URLRequestContextAdapter::InitRequestContextOnNetworkThread() {
160 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
161 DCHECK(config_);
162 // TODO(mmenke): Add method to have the builder enable SPDY.
163 net::URLRequestContextBuilder context_builder;
164 context_builder.set_network_delegate(new BasicNetworkDelegate());
165 context_builder.set_proxy_config_service(proxy_config_service_.get());
166 config_->ConfigureURLRequestContextBuilder(&context_builder);
168 context_.reset(context_builder.Build());
170 // Currently (circa M39) enabling QUIC requires setting probability threshold.
171 if (config_->enable_quic) {
172 context_->http_server_properties()
173 ->SetAlternateProtocolProbabilityThreshold(0.0f);
174 for (size_t hint = 0; hint < config_->quic_hints.size(); ++hint) {
175 const URLRequestContextConfig::QuicHint& quic_hint =
176 *config_->quic_hints[hint];
177 if (quic_hint.host.empty()) {
178 LOG(ERROR) << "Empty QUIC hint host: " << quic_hint.host;
179 continue;
182 url::CanonHostInfo host_info;
183 std::string canon_host(net::CanonicalizeHost(quic_hint.host, &host_info));
184 if (!host_info.IsIPAddress() &&
185 !net::IsCanonicalizedHostCompliant(canon_host)) {
186 LOG(ERROR) << "Invalid QUIC hint host: " << quic_hint.host;
187 continue;
190 if (quic_hint.port <= std::numeric_limits<uint16>::min() ||
191 quic_hint.port > std::numeric_limits<uint16>::max()) {
192 LOG(ERROR) << "Invalid QUIC hint port: "
193 << quic_hint.port;
194 continue;
197 if (quic_hint.alternate_port <= std::numeric_limits<uint16>::min() ||
198 quic_hint.alternate_port > std::numeric_limits<uint16>::max()) {
199 LOG(ERROR) << "Invalid QUIC hint alternate port: "
200 << quic_hint.alternate_port;
201 continue;
204 net::HostPortPair quic_hint_host_port_pair(canon_host,
205 quic_hint.port);
206 context_->http_server_properties()->SetAlternateProtocol(
207 quic_hint_host_port_pair,
208 static_cast<uint16>(quic_hint.alternate_port),
209 net::AlternateProtocol::QUIC,
210 1.0f);
213 load_disable_cache_ = config_->load_disable_cache;
214 config_.reset(NULL);
216 if (VLOG_IS_ON(2)) {
217 net_log_observer_.reset(new NetLogObserver());
218 context_->net_log()->AddThreadSafeObserver(net_log_observer_.get(),
219 net::NetLog::LOG_ALL_BUT_BYTES);
222 is_context_initialized_ = true;
223 while (!tasks_waiting_for_context_.empty()) {
224 tasks_waiting_for_context_.front().Run();
225 tasks_waiting_for_context_.pop();
228 delegate_->OnContextInitialized(this);
231 void URLRequestContextAdapter::PostTaskToNetworkThread(
232 const tracked_objects::Location& posted_from,
233 const RunAfterContextInitTask& callback) {
234 GetNetworkTaskRunner()->PostTask(
235 posted_from,
236 base::Bind(
237 &URLRequestContextAdapter::RunTaskAfterContextInitOnNetworkThread,
238 this,
239 callback));
242 void URLRequestContextAdapter::RunTaskAfterContextInitOnNetworkThread(
243 const RunAfterContextInitTask& callback) {
244 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
245 if (is_context_initialized_) {
246 callback.Run();
247 return;
249 tasks_waiting_for_context_.push(callback);
252 URLRequestContextAdapter::~URLRequestContextAdapter() {
253 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
254 if (net_log_observer_) {
255 context_->net_log()->RemoveThreadSafeObserver(net_log_observer_.get());
256 net_log_observer_.reset();
258 StopNetLogHelper();
259 // TODO(mef): Ensure that |network_thread_| is destroyed properly.
262 const std::string& URLRequestContextAdapter::GetUserAgent(
263 const GURL& url) const {
264 return user_agent_;
267 net::URLRequestContext* URLRequestContextAdapter::GetURLRequestContext() {
268 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
269 if (!context_) {
270 LOG(ERROR) << "URLRequestContext is not set up";
272 return context_.get();
275 scoped_refptr<base::SingleThreadTaskRunner>
276 URLRequestContextAdapter::GetNetworkTaskRunner() const {
277 return network_thread_->message_loop_proxy();
280 void URLRequestContextAdapter::StartNetLogToFile(const std::string& file_name) {
281 PostTaskToNetworkThread(
282 FROM_HERE,
283 base::Bind(
284 &URLRequestContextAdapter::StartNetLogToFileHelper, this, file_name));
287 void URLRequestContextAdapter::StopNetLog() {
288 PostTaskToNetworkThread(
289 FROM_HERE, base::Bind(&URLRequestContextAdapter::StopNetLogHelper, this));
292 void URLRequestContextAdapter::StartNetLogToFileHelper(
293 const std::string& file_name) {
294 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
295 // Do nothing if already logging to a file.
296 if (net_log_logger_)
297 return;
299 base::FilePath file_path(file_name);
300 FILE* file = base::OpenFile(file_path, "w");
301 if (!file)
302 return;
304 scoped_ptr<base::Value> constants(net::NetLogLogger::GetConstants());
305 net_log_logger_.reset(new net::NetLogLogger(file, *constants));
306 net_log_logger_->StartObserving(context_->net_log());
309 void URLRequestContextAdapter::StopNetLogHelper() {
310 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
311 if (net_log_logger_) {
312 net_log_logger_->StopObserving();
313 net_log_logger_.reset();
317 void NetLogObserver::OnAddEntry(const net::NetLog::Entry& entry) {
318 VLOG(2) << "Net log entry: type=" << entry.type()
319 << ", source=" << entry.source().type << ", phase=" << entry.phase();
322 } // namespace cronet