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.
5 #include "components/cronet/android/cronet_data_reduction_proxy.h"
7 #include "base/command_line.h"
8 #include "base/logging.h"
9 #include "base/prefs/pref_registry_simple.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/prefs/pref_service_factory.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_compression_stats.h"
15 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data.h"
16 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate.h"
17 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_prefs.h"
18 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_request_options.h"
19 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_service.h"
20 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.h"
21 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_params.h"
22 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_switches.h"
23 #include "net/url_request/url_request_context_getter.h"
24 #include "net/url_request/url_request_interceptor.h"
29 scoped_ptr
<PrefService
> CreatePrefService() {
30 scoped_refptr
<PrefRegistrySimple
> pref_registry(new PrefRegistrySimple());
31 data_reduction_proxy::RegisterSimpleProfilePrefs(pref_registry
.get());
32 base::PrefServiceFactory pref_service_factory
;
33 pref_service_factory
.set_user_prefs(
34 make_scoped_refptr(new CronetInMemoryPrefStore()));
35 scoped_ptr
<PrefService
> pref_service
=
36 pref_service_factory
.Create(pref_registry
.get()).Pass();
37 pref_registry
= nullptr;
38 return pref_service
.Pass();
41 // TODO(bengr): Apply test configurations directly, instead of via the
43 void AddOptionsToCommandLine(const std::string
& primary_proxy
,
44 const std::string
& fallback_proxy
,
45 const std::string
& secure_proxy_check_url
,
46 base::CommandLine
* command_line
) {
47 DCHECK((primary_proxy
.empty() && fallback_proxy
.empty() &&
48 secure_proxy_check_url
.empty()) ||
49 (!primary_proxy
.empty() && !fallback_proxy
.empty() &&
50 !secure_proxy_check_url
.empty()));
51 if (primary_proxy
.empty())
53 command_line
->AppendSwitchASCII(
54 data_reduction_proxy::switches::kDataReductionProxy
, primary_proxy
);
55 command_line
->AppendSwitchASCII(
56 data_reduction_proxy::switches::kDataReductionProxyFallback
,
58 command_line
->AppendSwitchASCII(
59 data_reduction_proxy::switches::kDataReductionProxySecureProxyCheckURL
,
60 secure_proxy_check_url
);
65 CronetDataReductionProxy::CronetDataReductionProxy(
66 const std::string
& key
,
67 const std::string
& primary_proxy
,
68 const std::string
& fallback_proxy
,
69 const std::string
& secure_proxy_check_url
,
70 const std::string
& user_agent
,
71 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner
,
73 : task_runner_(task_runner
) {
74 DCHECK(task_runner_
->BelongsToCurrentThread());
75 AddOptionsToCommandLine(primary_proxy
, fallback_proxy
, secure_proxy_check_url
,
76 base::CommandLine::ForCurrentProcess());
77 prefs_
= CreatePrefService();
78 // In Cronet, the Data Reduction Proxy's UI classes are Created on Cronet's
81 new data_reduction_proxy::DataReductionProxySettings());
83 new data_reduction_proxy::DataReductionProxyIOData(
84 data_reduction_proxy::Client::CRONET_ANDROID
,
85 data_reduction_proxy::DataReductionProxyParams::kAllowed
|
86 data_reduction_proxy::DataReductionProxyParams::kFallbackAllowed
,
87 net_log
, task_runner
, task_runner
, false, false, user_agent
));
88 io_data_
->request_options()->SetKeyOnIO(key
);
91 CronetDataReductionProxy::~CronetDataReductionProxy() {
92 io_data_
->ShutdownOnUIThread();
95 scoped_ptr
<net::NetworkDelegate
>
96 CronetDataReductionProxy::CreateNetworkDelegate(
97 scoped_ptr
<net::NetworkDelegate
> wrapped_network_delegate
) {
98 return io_data_
->CreateNetworkDelegate(wrapped_network_delegate
.Pass(),
99 false /* No bypass UMA */ );
102 scoped_ptr
<net::URLRequestInterceptor
>
103 CronetDataReductionProxy::CreateInterceptor() {
104 return io_data_
->CreateInterceptor();
107 void CronetDataReductionProxy::Init(bool enable
,
108 net::URLRequestContext
* context
) {
109 url_request_context_getter_
=
110 new net::TrivialURLRequestContextGetter(
111 context
, task_runner_
);
112 scoped_ptr
<data_reduction_proxy::DataReductionProxyCompressionStats
>
114 new data_reduction_proxy::DataReductionProxyCompressionStats(
115 prefs_
.get(), task_runner_
, base::TimeDelta()));
116 scoped_ptr
<data_reduction_proxy::DataReductionProxyService
>
117 data_reduction_proxy_service(
118 new data_reduction_proxy::DataReductionProxyService(
119 compression_stats
.Pass(), settings_
.get(), prefs_
.get(),
120 url_request_context_getter_
.get(), task_runner_
));
121 io_data_
->SetDataReductionProxyService(
122 data_reduction_proxy_service
->GetWeakPtr());
123 settings_
->InitDataReductionProxySettings(
124 prefs_
.get(), io_data_
.get(), data_reduction_proxy_service
.Pass());
125 settings_
->SetDataReductionProxyEnabled(enable
);
126 settings_
->MaybeActivateDataReductionProxy(true);
129 } // namespace cronet