Index: components/cronet/android/cronet_url_request_context_adapter.cc |
diff --git a/components/cronet/android/cronet_url_request_context_adapter.cc b/components/cronet/android/cronet_url_request_context_adapter.cc |
index 39c585a84423e79b81ea0c700d5bc9536c7ff90f..f33fe1ada604a1e9faf72e989101ad909ee0a8fd 100644 |
--- a/components/cronet/android/cronet_url_request_context_adapter.cc |
+++ b/components/cronet/android/cronet_url_request_context_adapter.cc |
@@ -7,11 +7,22 @@ |
#include "base/android/jni_android.h" |
#include "base/android/jni_string.h" |
#include "base/bind.h" |
+#include "base/command_line.h" |
#include "base/files/file_util.h" |
#include "base/logging.h" |
+#include "base/prefs/pref_registry_simple.h" |
+#include "base/prefs/pref_service.h" |
+#include "base/prefs/pref_service_factory.h" |
#include "base/single_thread_task_runner.h" |
#include "base/values.h" |
+#include "components/cronet/android/cronet_pref_store.h" |
#include "components/cronet/url_request_context_config.h" |
+#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data.h" |
+#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_prefs.h" |
+#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_service.h" |
+#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.h" |
+#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_statistics_prefs.h" |
+#include "components/data_reduction_proxy/core/common/data_reduction_proxy_params.h" |
#include "jni/CronetUrlRequestContext_jni.h" |
#include "net/base/load_flags.h" |
#include "net/base/net_errors.h" |
@@ -22,9 +33,15 @@ |
#include "net/proxy/proxy_service.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_getter.h" |
+ |
namespace { |
+// Shows notifications which correspond to PersistentPrefStore's reading errors. |
+void HandleReadError(PersistentPrefStore::PrefReadError error) { |
+} |
+ |
class BasicNetworkDelegate : public net::NetworkDelegateImpl { |
public: |
BasicNetworkDelegate() {} |
@@ -104,6 +121,11 @@ class BasicNetworkDelegate : public net::NetworkDelegateImpl { |
} // namespace |
+namespace data_reduction_proxy { |
+class DataReductionProxyConfigurator; |
+class DataReductionProxyStatisticsPrefs; |
+} |
+ |
namespace cronet { |
// Explicitly register static JNI functions. |
@@ -132,28 +154,65 @@ void CronetURLRequestContextAdapter::InitRequestContextOnMainThread( |
jobject jcaller) { |
base::android::ScopedJavaGlobalRef<jobject> jcaller_ref; |
jcaller_ref.Reset(env, jcaller); |
- proxy_config_service_.reset(net::ProxyService::CreateSystemProxyConfigService( |
- GetNetworkTaskRunner(), nullptr)); |
+ |
+ PrefRegistrySimple* pref_registry = new PrefRegistrySimple(); |
+ data_reduction_proxy::RegisterSimpleProfilePrefs(pref_registry); |
+ base::PrefServiceFactory pref_service_factory; |
+ pref_service_factory.set_user_prefs( |
+ make_scoped_refptr(new CronetPrefStore())); |
+ pref_service_factory.set_read_error_callback(base::Bind(&HandleReadError)); |
+ prefs_ = pref_service_factory.Create(pref_registry).Pass(); |
mmenke
2015/03/10 15:26:55
This doesn't persist anything to disk?
bengr
2015/03/10 23:43:31
Right.
|
+ |
+ ui_task_runner_ = base::MessageLoop::current()->message_loop_proxy(); |
mmenke
2015/03/10 15:26:55
Why do we need to do stuff on the main thread? Ca
bengr
2015/03/10 23:43:31
Done.
|
+ base::CommandLine::Init(0, nullptr); |
+ scoped_ptr<net::NetLog> net_log(new net::NetLog); |
+ DCHECK(!data_reduction_proxy_io_data_); |
+ data_reduction_proxy_io_data_.reset( |
+ new data_reduction_proxy::DataReductionProxyIOData( |
+ data_reduction_proxy::Client::CRONET_ANDROID, |
mmenke
2015/03/10 15:26:55
Should we really use one client name for anything
bengr
2015/03/10 23:43:31
For now. Separately, we're working on a a client m
|
+ data_reduction_proxy::DataReductionProxyParams::kAllowed | |
+ data_reduction_proxy::DataReductionProxyParams::kFallbackAllowed, |
+ net_log.get(), |
+ GetNetworkTaskRunner(), |
+ ui_task_runner_, |
+ false)); |
+ data_reduction_proxy_settings_.reset( |
+ new data_reduction_proxy::DataReductionProxySettings()); |
mmenke
2015/03/10 15:26:55
I'd like to have some way for external embedders t
bengr
2015/03/10 23:43:31
I added a quick and dirty approach.
|
+ |
GetNetworkTaskRunner()->PostTask( |
FROM_HERE, |
base::Bind(&CronetURLRequestContextAdapter::InitializeOnNetworkThread, |
base::Unretained(this), Passed(&context_config_), |
- jcaller_ref)); |
+ Passed(&net_log), jcaller_ref)); |
} |
void CronetURLRequestContextAdapter::InitializeOnNetworkThread( |
scoped_ptr<URLRequestContextConfig> config, |
+ scoped_ptr<net::NetLog> net_log, |
const base::android::ScopedJavaGlobalRef<jobject>& |
jcronet_url_request_context) { |
DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
DCHECK(!is_context_initialized_); |
+ |
+ DCHECK(data_reduction_proxy_io_data_); |
+ DCHECK(data_reduction_proxy_io_data_->request_options()); |
+ data_reduction_proxy_io_data_->request_options()->SetKeyOnIO( |
+ config->data_saver_key); |
// 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( |
- new net::ProxyConfigServiceFixed(net::ProxyConfig())); |
+ context_builder.set_network_delegate( |
+ data_reduction_proxy_io_data_->CreateNetworkDelegate( |
+ make_scoped_ptr(new BasicNetworkDelegate()).Pass(), |
+ false /* No UMA is produced to track bypasses*/ ).release()); |
+ context_builder.set_proxy_service( |
+ net::ProxyService::CreateWithoutProxyResolver( |
+ net::ProxyService::CreateSystemProxyConfigService( |
+ GetNetworkTaskRunner(), nullptr), |
+ net_log.get())); |
config->ConfigureURLRequestContextBuilder(&context_builder); |
- |
+ context_builder.set_interceptor( |
+ data_reduction_proxy_io_data_->CreateInterceptor()); |
+ context_builder.set_net_log(net_log.release()); |
context_.reset(context_builder.Build()); |
default_load_flags_ = net::LOAD_DO_NOT_SAVE_COOKIES | |
@@ -214,10 +273,55 @@ void CronetURLRequestContextAdapter::InitializeOnNetworkThread( |
tasks_waiting_for_context_.front().Run(); |
tasks_waiting_for_context_.pop(); |
} |
+ ui_task_runner_->PostTask( |
+ FROM_HERE, |
+ base::Bind( |
+ &CronetURLRequestContextAdapter:: |
+ InitializeDataReductionProxyOnMainThread, |
+ base::Unretained(this), config->enable_data_saver)); |
+} |
+ |
+void |
+CronetURLRequestContextAdapter::InitializeDataReductionProxyOnMainThread( |
+ bool enable) { |
+ DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
+ url_request_context_getter_ = |
+ new net::TrivialURLRequestContextGetter( |
+ GetURLRequestContext(), GetNetworkTaskRunner()); |
+ base::TimeDelta commit_delay = base::TimeDelta(); |
+ scoped_ptr<data_reduction_proxy::DataReductionProxyStatisticsPrefs> |
+ statistics_prefs = make_scoped_ptr( |
+ new data_reduction_proxy::DataReductionProxyStatisticsPrefs( |
+ prefs_.get(), ui_task_runner_, commit_delay)); |
+ scoped_ptr<data_reduction_proxy::DataReductionProxyService> |
+ data_reduction_proxy_service( |
+ new data_reduction_proxy::DataReductionProxyService( |
+ statistics_prefs.Pass(), |
+ data_reduction_proxy_settings_.get(), |
+ url_request_context_getter_.get())); |
+ data_reduction_proxy_io_data_->SetDataReductionProxyService( |
+ data_reduction_proxy_service->GetWeakPtr()); |
+ data_reduction_proxy_settings_->InitDataReductionProxySettings( |
+ prefs_.get(), |
+ data_reduction_proxy_io_data_.get(), |
+ data_reduction_proxy_service.Pass()); |
+ |
+ data_reduction_proxy_settings_->SetDataReductionProxyEnabled(enable); |
} |
void CronetURLRequestContextAdapter::Destroy(JNIEnv* env, jobject jcaller) { |
DCHECK(!GetNetworkTaskRunner()->BelongsToCurrentThread()); |
+ ui_task_runner_->PostTask( |
+ FROM_HERE, |
+ base::Bind( |
+ &CronetURLRequestContextAdapter::DestroyOnMainThread, |
+ base::Unretained(this))); |
+} |
+ |
+void CronetURLRequestContextAdapter::DestroyOnMainThread() { |
+ data_reduction_proxy_settings_.reset(nullptr); |
+ data_reduction_proxy_io_data_->ShutdownOnUIThread(); |
+ prefs_.reset(nullptr); |
// 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_; |