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

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: Added test 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/files/file_util.h" 10 #include "base/files/file_util.h"
11 #include "base/files/scoped_file.h" 11 #include "base/files/scoped_file.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/memory/scoped_vector.h"
13 #include "base/single_thread_task_runner.h" 14 #include "base/single_thread_task_runner.h"
14 #include "base/values.h" 15 #include "base/values.h"
16 #include "components/cronet/android/cronet_data_reduction_proxy.h"
15 #include "components/cronet/url_request_context_config.h" 17 #include "components/cronet/url_request_context_config.h"
16 #include "jni/CronetUrlRequestContext_jni.h" 18 #include "jni/CronetUrlRequestContext_jni.h"
17 #include "net/base/load_flags.h" 19 #include "net/base/load_flags.h"
18 #include "net/base/net_errors.h" 20 #include "net/base/net_errors.h"
19 #include "net/base/net_log_logger.h" 21 #include "net/base/net_log_logger.h"
20 #include "net/base/network_delegate_impl.h" 22 #include "net/base/network_delegate_impl.h"
21 #include "net/http/http_auth_handler_factory.h" 23 #include "net/http/http_auth_handler_factory.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(
136 GetNetworkTaskRunner(), nullptr));
137 GetNetworkTaskRunner()->PostTask( 138 GetNetworkTaskRunner()->PostTask(
138 FROM_HERE, 139 FROM_HERE,
139 base::Bind(&CronetURLRequestContextAdapter::InitializeOnNetworkThread, 140 base::Bind(&CronetURLRequestContextAdapter::InitializeOnNetworkThread,
140 base::Unretained(this), Passed(&context_config_), 141 base::Unretained(this), Passed(&context_config_),
141 jcaller_ref)); 142 jcaller_ref));
142 } 143 }
143 144
144 void CronetURLRequestContextAdapter::InitializeOnNetworkThread( 145 void CronetURLRequestContextAdapter::InitializeOnNetworkThread(
145 scoped_ptr<URLRequestContextConfig> config, 146 scoped_ptr<URLRequestContextConfig> config,
146 const base::android::ScopedJavaGlobalRef<jobject>& 147 const base::android::ScopedJavaGlobalRef<jobject>&
147 jcronet_url_request_context) { 148 jcronet_url_request_context) {
148 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); 149 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
149 DCHECK(!is_context_initialized_); 150 DCHECK(!is_context_initialized_);
150 DCHECK(proxy_config_service_);
151 // TODO(mmenke): Add method to have the builder enable SPDY. 151 // TODO(mmenke): Add method to have the builder enable SPDY.
152 net::URLRequestContextBuilder context_builder; 152 net::URLRequestContextBuilder context_builder;
153 context_builder.set_network_delegate(new BasicNetworkDelegate()); 153 scoped_ptr<net::NetLog> net_log(new net::NetLog);
154 context_builder.set_proxy_config_service(proxy_config_service_.release()); 154 DCHECK(!data_reduction_proxy_);
155 // For now, the choice to enable the data reduction proxy happens once,
156 // at initialization. It cannot be disabled thereafter.
157 if (config->enable_data_reduction_proxy) {
158 data_reduction_proxy_.reset(
159 new CronetDataReductionProxy(
160 config->data_reduction_proxy_key,
161 config->data_reduction_proxy_options,
162 GetNetworkTaskRunner(),
163 net_log.get()));
164 context_builder.set_network_delegate(
165 data_reduction_proxy_->CreateNetworkDelegate(
166 make_scoped_ptr(new BasicNetworkDelegate())).release());
167 ScopedVector<net::URLRequestInterceptor> interceptors;
168 interceptors.push_back(data_reduction_proxy_->CreateInterceptor());
169 context_builder.SetInterceptors(interceptors.Pass());
170 } else {
171 context_builder.set_network_delegate(new BasicNetworkDelegate());
172 }
173 context_builder.set_proxy_service(
174 net::ProxyService::CreateWithoutProxyResolver(
mmenke 2015/04/01 15:46:14 Why aren't you using CreateUsingSystemProxyResolve
bengr 2015/04/24 02:30:43 Done.
175 net::ProxyService::CreateSystemProxyConfigService(
mmenke 2015/04/01 15:46:14 It's my understanding that the Java proxy config s
bengr 2015/04/24 02:30:43 Done.
176 GetNetworkTaskRunner(), nullptr),
177 net_log.get()));
155 config->ConfigureURLRequestContextBuilder(&context_builder); 178 config->ConfigureURLRequestContextBuilder(&context_builder);
156 179 context_builder.set_net_log(net_log.release());
157 context_.reset(context_builder.Build()); 180 context_.reset(context_builder.Build());
158 181
159 default_load_flags_ = net::LOAD_DO_NOT_SAVE_COOKIES | 182 default_load_flags_ = net::LOAD_DO_NOT_SAVE_COOKIES |
160 net::LOAD_DO_NOT_SEND_COOKIES; 183 net::LOAD_DO_NOT_SEND_COOKIES;
161 if (config->load_disable_cache) 184 if (config->load_disable_cache)
162 default_load_flags_ |= net::LOAD_DISABLE_CACHE; 185 default_load_flags_ |= net::LOAD_DISABLE_CACHE;
163 186
164 // Currently (circa M39) enabling QUIC requires setting probability threshold. 187 // Currently (circa M39) enabling QUIC requires setting probability threshold.
165 if (config->enable_quic) { 188 if (config->enable_quic) {
166 context_->http_server_properties() 189 context_->http_server_properties()
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
207 230
208 JNIEnv* env = base::android::AttachCurrentThread(); 231 JNIEnv* env = base::android::AttachCurrentThread();
209 Java_CronetUrlRequestContext_initNetworkThread( 232 Java_CronetUrlRequestContext_initNetworkThread(
210 env, jcronet_url_request_context.obj()); 233 env, jcronet_url_request_context.obj());
211 234
212 is_context_initialized_ = true; 235 is_context_initialized_ = true;
213 while (!tasks_waiting_for_context_.empty()) { 236 while (!tasks_waiting_for_context_.empty()) {
214 tasks_waiting_for_context_.front().Run(); 237 tasks_waiting_for_context_.front().Run();
215 tasks_waiting_for_context_.pop(); 238 tasks_waiting_for_context_.pop();
216 } 239 }
240 if (data_reduction_proxy_) {
241 data_reduction_proxy_->Init(config->enable_data_reduction_proxy,
242 GetURLRequestContext());
243 }
217 } 244 }
218 245
219 void CronetURLRequestContextAdapter::Destroy(JNIEnv* env, jobject jcaller) { 246 void CronetURLRequestContextAdapter::Destroy(JNIEnv* env, jobject jcaller) {
220 DCHECK(!GetNetworkTaskRunner()->BelongsToCurrentThread()); 247 DCHECK(!GetNetworkTaskRunner()->BelongsToCurrentThread());
221 // Stick network_thread_ in a local, as |this| may be destroyed from the 248 // Stick network_thread_ in a local, as |this| may be destroyed from the
222 // network thread before delete network_thread is called. 249 // network thread before delete network_thread is called.
223 base::Thread* network_thread = network_thread_; 250 base::Thread* network_thread = network_thread_;
224 GetNetworkTaskRunner()->DeleteSoon(FROM_HERE, this); 251 GetNetworkTaskRunner()->DeleteSoon(FROM_HERE, this);
225 // Deleting thread stops it after all tasks are completed. 252 // Deleting thread stops it after all tasks are completed.
226 delete network_thread; 253 delete network_thread;
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
322 } 349 }
323 350
324 static jint SetMinLogLevel(JNIEnv* env, jclass jcaller, jint jlog_level) { 351 static jint SetMinLogLevel(JNIEnv* env, jclass jcaller, jint jlog_level) {
325 jint old_log_level = static_cast<jint>(logging::GetMinLogLevel()); 352 jint old_log_level = static_cast<jint>(logging::GetMinLogLevel());
326 // MinLogLevel is global, shared by all URLRequestContexts. 353 // MinLogLevel is global, shared by all URLRequestContexts.
327 logging::SetMinLogLevel(static_cast<int>(jlog_level)); 354 logging::SetMinLogLevel(static_cast<int>(jlog_level));
328 return old_log_level; 355 return old_log_level;
329 } 356 }
330 357
331 } // namespace cronet 358 } // namespace cronet
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698