| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 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. | |
| 4 | |
| 5 #include "components/cronet/android/cronet_data_reduction_proxy.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 | |
| 9 #include "base/command_line.h" | |
| 10 #include "base/logging.h" | |
| 11 #include "base/memory/ptr_util.h" | |
| 12 #include "base/single_thread_task_runner.h" | |
| 13 #include "components/cronet/android/cronet_in_memory_pref_store.h" | |
| 14 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_comp
ression_stats.h" | |
| 15 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_dele
gate.h" | |
| 16 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_io_d
ata.h" | |
| 17 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_netw
ork_delegate.h" | |
| 18 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_pref
s.h" | |
| 19 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_requ
est_options.h" | |
| 20 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_serv
ice.h" | |
| 21 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_sett
ings.h" | |
| 22 #include "components/data_reduction_proxy/core/browser/data_store.h" | |
| 23 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_param
s.h" | |
| 24 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_switc
hes.h" | |
| 25 #include "components/prefs/pref_registry_simple.h" | |
| 26 #include "components/prefs/pref_service.h" | |
| 27 #include "components/prefs/pref_service_factory.h" | |
| 28 #include "net/url_request/url_request_context_getter.h" | |
| 29 #include "net/url_request/url_request_interceptor.h" | |
| 30 | |
| 31 namespace cronet { | |
| 32 namespace { | |
| 33 | |
| 34 // Name of the preference that governs enabling the Data Reduction Proxy. | |
| 35 const char kDataReductionProxyEnabled[] = "data_reduction_proxy.enabled"; | |
| 36 | |
| 37 std::unique_ptr<PrefService> CreatePrefService() { | |
| 38 scoped_refptr<PrefRegistrySimple> pref_registry(new PrefRegistrySimple()); | |
| 39 pref_registry->RegisterBooleanPref(kDataReductionProxyEnabled, false); | |
| 40 data_reduction_proxy::RegisterSimpleProfilePrefs(pref_registry.get()); | |
| 41 PrefServiceFactory pref_service_factory; | |
| 42 pref_service_factory.set_user_prefs( | |
| 43 make_scoped_refptr(new CronetInMemoryPrefStore())); | |
| 44 std::unique_ptr<PrefService> pref_service = | |
| 45 pref_service_factory.Create(pref_registry.get()); | |
| 46 pref_registry = nullptr; | |
| 47 return pref_service; | |
| 48 } | |
| 49 | |
| 50 // TODO(bengr): Apply test configurations directly, instead of via the | |
| 51 // command line. | |
| 52 void AddOptionsToCommandLine(const std::string& primary_proxy, | |
| 53 const std::string& fallback_proxy, | |
| 54 const std::string& secure_proxy_check_url, | |
| 55 base::CommandLine* command_line) { | |
| 56 DCHECK((primary_proxy.empty() && fallback_proxy.empty() && | |
| 57 secure_proxy_check_url.empty()) || | |
| 58 (!primary_proxy.empty() && !fallback_proxy.empty() && | |
| 59 !secure_proxy_check_url.empty())); | |
| 60 if (primary_proxy.empty()) | |
| 61 return; | |
| 62 command_line->AppendSwitchASCII( | |
| 63 data_reduction_proxy::switches::kDataReductionProxy, primary_proxy); | |
| 64 command_line->AppendSwitchASCII( | |
| 65 data_reduction_proxy::switches::kDataReductionProxyFallback, | |
| 66 fallback_proxy); | |
| 67 command_line->AppendSwitchASCII( | |
| 68 data_reduction_proxy::switches::kDataReductionProxySecureProxyCheckURL, | |
| 69 secure_proxy_check_url); | |
| 70 } | |
| 71 | |
| 72 } // namespace | |
| 73 | |
| 74 CronetDataReductionProxy::CronetDataReductionProxy( | |
| 75 const std::string& key, | |
| 76 const std::string& primary_proxy, | |
| 77 const std::string& fallback_proxy, | |
| 78 const std::string& secure_proxy_check_url, | |
| 79 const std::string& user_agent, | |
| 80 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | |
| 81 net::NetLog* net_log) | |
| 82 : task_runner_(task_runner) { | |
| 83 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 84 AddOptionsToCommandLine(primary_proxy, fallback_proxy, secure_proxy_check_url, | |
| 85 base::CommandLine::ForCurrentProcess()); | |
| 86 prefs_ = CreatePrefService(); | |
| 87 // In Cronet, the Data Reduction Proxy's UI classes are Created on Cronet's | |
| 88 // network thread. | |
| 89 settings_.reset( | |
| 90 new data_reduction_proxy::DataReductionProxySettings()); | |
| 91 io_data_.reset(new data_reduction_proxy::DataReductionProxyIOData( | |
| 92 data_reduction_proxy::Client::CRONET_ANDROID, 0, net_log, task_runner, | |
| 93 task_runner, false, user_agent, std::string())); | |
| 94 io_data_->request_options()->SetKeyOnIO(key); | |
| 95 } | |
| 96 | |
| 97 CronetDataReductionProxy::~CronetDataReductionProxy() { | |
| 98 io_data_->ShutdownOnUIThread(); | |
| 99 } | |
| 100 | |
| 101 std::unique_ptr<net::NetworkDelegate> | |
| 102 CronetDataReductionProxy::CreateNetworkDelegate( | |
| 103 std::unique_ptr<net::NetworkDelegate> wrapped_network_delegate) { | |
| 104 return io_data_->CreateNetworkDelegate(std::move(wrapped_network_delegate), | |
| 105 false /* No bypass UMA */); | |
| 106 } | |
| 107 | |
| 108 std::unique_ptr<net::ProxyDelegate> | |
| 109 CronetDataReductionProxy::CreateProxyDelegate() { | |
| 110 return io_data_->CreateProxyDelegate(); | |
| 111 } | |
| 112 | |
| 113 std::unique_ptr<net::URLRequestInterceptor> | |
| 114 CronetDataReductionProxy::CreateInterceptor() { | |
| 115 return io_data_->CreateInterceptor(); | |
| 116 } | |
| 117 | |
| 118 void CronetDataReductionProxy::Init(bool enable, | |
| 119 net::URLRequestContext* context) { | |
| 120 url_request_context_getter_ = | |
| 121 new net::TrivialURLRequestContextGetter( | |
| 122 context, task_runner_); | |
| 123 std::unique_ptr<data_reduction_proxy::DataReductionProxyService> | |
| 124 data_reduction_proxy_service( | |
| 125 new data_reduction_proxy::DataReductionProxyService( | |
| 126 settings_.get(), prefs_.get(), url_request_context_getter_.get(), | |
| 127 base::MakeUnique<data_reduction_proxy::DataStore>(), task_runner_, | |
| 128 task_runner_, task_runner_, base::TimeDelta())); | |
| 129 io_data_->SetDataReductionProxyService( | |
| 130 data_reduction_proxy_service->GetWeakPtr()); | |
| 131 settings_->InitDataReductionProxySettings( | |
| 132 kDataReductionProxyEnabled, prefs_.get(), io_data_.get(), | |
| 133 std::move(data_reduction_proxy_service)); | |
| 134 settings_->SetDataReductionProxyEnabled(enable); | |
| 135 settings_->MaybeActivateDataReductionProxy(true); | |
| 136 } | |
| 137 | |
| 138 } // namespace cronet | |
| OLD | NEW |