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

Side by Side Diff: components/cronet/android/cronet_url_request_context_adapter.cc

Issue 937513003: Add Data Saver support to Cronet (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: nits Created 5 years, 9 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 unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/cronet/android/cronet_url_request_context_adapter.h" 5 #include "components/cronet/android/cronet_url_request_context_adapter.h"
6 6
7 #include "base/android/jni_android.h" 7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h" 8 #include "base/android/jni_string.h"
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/command_line.h"
10 #include "base/files/file_util.h" 11 #include "base/files/file_util.h"
11 #include "base/logging.h" 12 #include "base/logging.h"
12 #include "base/single_thread_task_runner.h" 13 #include "base/single_thread_task_runner.h"
13 #include "base/values.h" 14 #include "base/values.h"
15 #include "components/cronet/android/cronet_data_reduction_proxy.h"
14 #include "components/cronet/url_request_context_config.h" 16 #include "components/cronet/url_request_context_config.h"
15 #include "jni/CronetUrlRequestContext_jni.h" 17 #include "jni/CronetUrlRequestContext_jni.h"
16 #include "net/base/load_flags.h" 18 #include "net/base/load_flags.h"
17 #include "net/base/net_errors.h" 19 #include "net/base/net_errors.h"
18 #include "net/base/net_log_logger.h" 20 #include "net/base/net_log_logger.h"
19 #include "net/base/network_delegate_impl.h" 21 #include "net/base/network_delegate_impl.h"
20 #include "net/http/http_auth_handler_factory.h" 22 #include "net/http/http_auth_handler_factory.h"
21 #include "net/proxy/proxy_config_service_fixed.h" 23 #include "net/proxy/proxy_config_service_fixed.h"
22 #include "net/proxy/proxy_service.h" 24 #include "net/proxy/proxy_service.h"
23 #include "net/url_request/url_request_context.h" 25 #include "net/url_request/url_request_context.h"
24 #include "net/url_request/url_request_context_builder.h" 26 #include "net/url_request/url_request_context_builder.h"
27 #include "net/url_request/url_request_interceptor.h"
25 28
26 namespace { 29 namespace {
27 30
28 class BasicNetworkDelegate : public net::NetworkDelegateImpl { 31 class BasicNetworkDelegate : public net::NetworkDelegateImpl {
29 public: 32 public:
30 BasicNetworkDelegate() {} 33 BasicNetworkDelegate() {}
31 ~BasicNetworkDelegate() override {} 34 ~BasicNetworkDelegate() override {}
32 35
33 private: 36 private:
34 // net::NetworkDelegate implementation. 37 // net::NetworkDelegate implementation.
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 CronetURLRequestContextAdapter::~CronetURLRequestContextAdapter() { 128 CronetURLRequestContextAdapter::~CronetURLRequestContextAdapter() {
126 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); 129 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
127 StopNetLogOnNetworkThread(); 130 StopNetLogOnNetworkThread();
128 } 131 }
129 132
130 void CronetURLRequestContextAdapter::InitRequestContextOnMainThread( 133 void CronetURLRequestContextAdapter::InitRequestContextOnMainThread(
131 JNIEnv* env, 134 JNIEnv* env,
132 jobject jcaller) { 135 jobject jcaller) {
133 base::android::ScopedJavaGlobalRef<jobject> jcaller_ref; 136 base::android::ScopedJavaGlobalRef<jobject> jcaller_ref;
134 jcaller_ref.Reset(env, jcaller); 137 jcaller_ref.Reset(env, jcaller);
135 proxy_config_service_.reset(net::ProxyService::CreateSystemProxyConfigService( 138 base::CommandLine::Init(0, nullptr);
mmenke 2015/03/13 18:16:48 Hrm...Wonder if we should move command line checki
bengr 2015/03/19 01:03:51 Done.
136 GetNetworkTaskRunner(), nullptr));
mmenke 2015/03/13 18:16:48 Think a CL to fix this has already landed, make su
bengr 2015/03/19 01:03:51 Acknowledged, though I didn't see a fix yet.
137 GetNetworkTaskRunner()->PostTask( 139 GetNetworkTaskRunner()->PostTask(
138 FROM_HERE, 140 FROM_HERE,
139 base::Bind(&CronetURLRequestContextAdapter::InitializeOnNetworkThread, 141 base::Bind(&CronetURLRequestContextAdapter::InitializeOnNetworkThread,
140 base::Unretained(this), Passed(&context_config_), 142 base::Unretained(this), Passed(&context_config_),
141 jcaller_ref)); 143 jcaller_ref));
142 } 144 }
143 145
144 void CronetURLRequestContextAdapter::InitializeOnNetworkThread( 146 void CronetURLRequestContextAdapter::InitializeOnNetworkThread(
145 scoped_ptr<URLRequestContextConfig> config, 147 scoped_ptr<URLRequestContextConfig> config,
146 const base::android::ScopedJavaGlobalRef<jobject>& 148 const base::android::ScopedJavaGlobalRef<jobject>&
147 jcronet_url_request_context) { 149 jcronet_url_request_context) {
148 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); 150 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
149 DCHECK(!is_context_initialized_); 151 DCHECK(!is_context_initialized_);
152 scoped_ptr<net::NetLog> net_log(new net::NetLog);
153 DCHECK(!data_reduction_proxy_);
154 // For now, the choice to enable the data reduction proxy happens once,
155 // at initialization. It cannot be disabled thereafter.
156 if (config->enable_data_reduction_proxy) {
157 data_reduction_proxy_.reset(new CronetDataReductionProxy(
158 config->data_reduction_proxy_key, GetNetworkTaskRunner(), net_log.get()));
159 }
mmenke 2015/03/13 18:16:48 Can we just merge this with the "if (DRP)" block b
bengr 2015/03/19 01:03:51 Done.
150 // TODO(mmenke): Add method to have the builder enable SPDY. 160 // TODO(mmenke): Add method to have the builder enable SPDY.
151 net::URLRequestContextBuilder context_builder; 161 net::URLRequestContextBuilder context_builder;
152 context_builder.set_network_delegate(new BasicNetworkDelegate()); 162 if (data_reduction_proxy_) {
153 context_builder.set_proxy_config_service( 163 context_builder.set_network_delegate(
154 new net::ProxyConfigServiceFixed(net::ProxyConfig())); 164 data_reduction_proxy_->CreateNetworkDelegate(
165 make_scoped_ptr(new BasicNetworkDelegate())));
166 net::URLRequestContextBuilder::URLRequestInterceptors interceptors;
167 interceptors.push_back(data_reduction_proxy_->CreateInterceptor());
168 context_builder.SetInterceptors(interceptors.Pass());
169 } else {
170 context_builder.set_network_delegate(new BasicNetworkDelegate());
171 }
172 context_builder.set_proxy_service(
173 net::ProxyService::CreateWithoutProxyResolver(
174 net::ProxyService::CreateSystemProxyConfigService(
175 GetNetworkTaskRunner(), nullptr),
176 net_log.get()));
155 config->ConfigureURLRequestContextBuilder(&context_builder); 177 config->ConfigureURLRequestContextBuilder(&context_builder);
156 178 context_builder.set_net_log(net_log.release());
157 context_.reset(context_builder.Build()); 179 context_.reset(context_builder.Build());
158 180
159 default_load_flags_ = net::LOAD_DO_NOT_SAVE_COOKIES | 181 default_load_flags_ = net::LOAD_DO_NOT_SAVE_COOKIES |
160 net::LOAD_DO_NOT_SEND_COOKIES; 182 net::LOAD_DO_NOT_SEND_COOKIES;
161 if (config->load_disable_cache) 183 if (config->load_disable_cache)
162 default_load_flags_ |= net::LOAD_DISABLE_CACHE; 184 default_load_flags_ |= net::LOAD_DISABLE_CACHE;
163 185
164 // Currently (circa M39) enabling QUIC requires setting probability threshold. 186 // Currently (circa M39) enabling QUIC requires setting probability threshold.
165 if (config->enable_quic) { 187 if (config->enable_quic) {
166 context_->http_server_properties() 188 context_->http_server_properties()
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
207 229
208 JNIEnv* env = base::android::AttachCurrentThread(); 230 JNIEnv* env = base::android::AttachCurrentThread();
209 Java_CronetUrlRequestContext_initNetworkThread( 231 Java_CronetUrlRequestContext_initNetworkThread(
210 env, jcronet_url_request_context.obj()); 232 env, jcronet_url_request_context.obj());
211 233
212 is_context_initialized_ = true; 234 is_context_initialized_ = true;
213 while (!tasks_waiting_for_context_.empty()) { 235 while (!tasks_waiting_for_context_.empty()) {
214 tasks_waiting_for_context_.front().Run(); 236 tasks_waiting_for_context_.front().Run();
215 tasks_waiting_for_context_.pop(); 237 tasks_waiting_for_context_.pop();
216 } 238 }
239 if (data_reduction_proxy_)
240 data_reduction_proxy_->Init(config->enable_data_reduction_proxy,
241 GetURLRequestContext());
mmenke 2015/03/13 18:16:48 nit: Use braces.
bengr 2015/03/19 01:03:51 Done.
217 } 242 }
218 243
219 void CronetURLRequestContextAdapter::Destroy(JNIEnv* env, jobject jcaller) { 244 void CronetURLRequestContextAdapter::Destroy(JNIEnv* env, jobject jcaller) {
220 DCHECK(!GetNetworkTaskRunner()->BelongsToCurrentThread()); 245 DCHECK(!GetNetworkTaskRunner()->BelongsToCurrentThread());
221 // Stick network_thread_ in a local, as |this| may be destroyed from the 246 // Stick network_thread_ in a local, as |this| may be destroyed from the
222 // network thread before delete network_thread is called. 247 // network thread before delete network_thread is called.
223 base::Thread* network_thread = network_thread_; 248 base::Thread* network_thread = network_thread_;
224 GetNetworkTaskRunner()->DeleteSoon(FROM_HERE, this); 249 GetNetworkTaskRunner()->DeleteSoon(FROM_HERE, this);
225 // Deleting thread stops it after all tasks are completed. 250 // Deleting thread stops it after all tasks are completed.
226 delete network_thread; 251 delete network_thread;
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
324 } 349 }
325 350
326 static jint SetMinLogLevel(JNIEnv* env, jclass jcaller, jint jlog_level) { 351 static jint SetMinLogLevel(JNIEnv* env, jclass jcaller, jint jlog_level) {
327 jint old_log_level = static_cast<jint>(logging::GetMinLogLevel()); 352 jint old_log_level = static_cast<jint>(logging::GetMinLogLevel());
328 // MinLogLevel is global, shared by all URLRequestContexts. 353 // MinLogLevel is global, shared by all URLRequestContexts.
329 logging::SetMinLogLevel(static_cast<int>(jlog_level)); 354 logging::SetMinLogLevel(static_cast<int>(jlog_level));
330 return old_log_level; 355 return old_log_level;
331 } 356 }
332 357
333 } // namespace cronet 358 } // namespace cronet
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698