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

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: Addressed make_scoped_ptr 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(new CronetDataReductionProxy(
159 config->data_reduction_proxy_key, GetNetworkTaskRunner(),
160 net_log.get()));
161 context_builder.set_network_delegate(
162 data_reduction_proxy_->CreateNetworkDelegate(
163 make_scoped_ptr(new BasicNetworkDelegate())).release());
164 ScopedVector<net::URLRequestInterceptor> interceptors;
165 interceptors.push_back(data_reduction_proxy_->CreateInterceptor());
166 context_builder.SetInterceptors(interceptors.Pass());
167 } else {
168 context_builder.set_network_delegate(new BasicNetworkDelegate());
169 }
170 context_builder.set_proxy_service(
171 net::ProxyService::CreateWithoutProxyResolver(
172 net::ProxyService::CreateSystemProxyConfigService(
173 GetNetworkTaskRunner(), nullptr),
174 net_log.get()));
155 config->ConfigureURLRequestContextBuilder(&context_builder); 175 config->ConfigureURLRequestContextBuilder(&context_builder);
156 176 context_builder.set_net_log(net_log.release());
157 context_.reset(context_builder.Build()); 177 context_.reset(context_builder.Build());
158 178
159 default_load_flags_ = net::LOAD_DO_NOT_SAVE_COOKIES | 179 default_load_flags_ = net::LOAD_DO_NOT_SAVE_COOKIES |
160 net::LOAD_DO_NOT_SEND_COOKIES; 180 net::LOAD_DO_NOT_SEND_COOKIES;
161 if (config->load_disable_cache) 181 if (config->load_disable_cache)
162 default_load_flags_ |= net::LOAD_DISABLE_CACHE; 182 default_load_flags_ |= net::LOAD_DISABLE_CACHE;
163 183
164 // Currently (circa M39) enabling QUIC requires setting probability threshold. 184 // Currently (circa M39) enabling QUIC requires setting probability threshold.
165 if (config->enable_quic) { 185 if (config->enable_quic) {
166 context_->http_server_properties() 186 context_->http_server_properties()
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
207 227
208 JNIEnv* env = base::android::AttachCurrentThread(); 228 JNIEnv* env = base::android::AttachCurrentThread();
209 Java_CronetUrlRequestContext_initNetworkThread( 229 Java_CronetUrlRequestContext_initNetworkThread(
210 env, jcronet_url_request_context.obj()); 230 env, jcronet_url_request_context.obj());
211 231
212 is_context_initialized_ = true; 232 is_context_initialized_ = true;
213 while (!tasks_waiting_for_context_.empty()) { 233 while (!tasks_waiting_for_context_.empty()) {
214 tasks_waiting_for_context_.front().Run(); 234 tasks_waiting_for_context_.front().Run();
215 tasks_waiting_for_context_.pop(); 235 tasks_waiting_for_context_.pop();
216 } 236 }
237 if (data_reduction_proxy_) {
238 data_reduction_proxy_->Init(config->enable_data_reduction_proxy,
239 GetURLRequestContext());
240 }
217 } 241 }
218 242
219 void CronetURLRequestContextAdapter::Destroy(JNIEnv* env, jobject jcaller) { 243 void CronetURLRequestContextAdapter::Destroy(JNIEnv* env, jobject jcaller) {
220 DCHECK(!GetNetworkTaskRunner()->BelongsToCurrentThread()); 244 DCHECK(!GetNetworkTaskRunner()->BelongsToCurrentThread());
221 // Stick network_thread_ in a local, as |this| may be destroyed from the 245 // Stick network_thread_ in a local, as |this| may be destroyed from the
222 // network thread before delete network_thread is called. 246 // network thread before delete network_thread is called.
223 base::Thread* network_thread = network_thread_; 247 base::Thread* network_thread = network_thread_;
224 GetNetworkTaskRunner()->DeleteSoon(FROM_HERE, this); 248 GetNetworkTaskRunner()->DeleteSoon(FROM_HERE, this);
225 // Deleting thread stops it after all tasks are completed. 249 // Deleting thread stops it after all tasks are completed.
226 delete network_thread; 250 delete network_thread;
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
322 } 346 }
323 347
324 static jint SetMinLogLevel(JNIEnv* env, jclass jcaller, jint jlog_level) { 348 static jint SetMinLogLevel(JNIEnv* env, jclass jcaller, jint jlog_level) {
325 jint old_log_level = static_cast<jint>(logging::GetMinLogLevel()); 349 jint old_log_level = static_cast<jint>(logging::GetMinLogLevel());
326 // MinLogLevel is global, shared by all URLRequestContexts. 350 // MinLogLevel is global, shared by all URLRequestContexts.
327 logging::SetMinLogLevel(static_cast<int>(jlog_level)); 351 logging::SetMinLogLevel(static_cast<int>(jlog_level));
328 return old_log_level; 352 return old_log_level;
329 } 353 }
330 354
331 } // namespace cronet 355 } // namespace cronet
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698