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/browser/data_store.h"
22 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_params.h"
23 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_switches.h"
24 #include "net/url_request/url_request_context_getter.h"
25 #include "net/url_request/url_request_interceptor.h"
30 scoped_ptr
<PrefService
> CreatePrefService() {
31 scoped_refptr
<PrefRegistrySimple
> pref_registry(new PrefRegistrySimple());
32 data_reduction_proxy::RegisterSimpleProfilePrefs(pref_registry
.get());
33 base::PrefServiceFactory pref_service_factory
;
34 pref_service_factory
.set_user_prefs(
35 make_scoped_refptr(new CronetInMemoryPrefStore()));
36 scoped_ptr
<PrefService
> pref_service
=
37 pref_service_factory
.Create(pref_registry
.get()).Pass();
38 pref_registry
= nullptr;
39 return pref_service
.Pass();
42 // TODO(bengr): Apply test configurations directly, instead of via the
44 void AddOptionsToCommandLine(const std::string
& primary_proxy
,
45 const std::string
& fallback_proxy
,
46 const std::string
& secure_proxy_check_url
,
47 base::CommandLine
* command_line
) {
48 DCHECK((primary_proxy
.empty() && fallback_proxy
.empty() &&
49 secure_proxy_check_url
.empty()) ||
50 (!primary_proxy
.empty() && !fallback_proxy
.empty() &&
51 !secure_proxy_check_url
.empty()));
52 if (primary_proxy
.empty())
54 command_line
->AppendSwitchASCII(
55 data_reduction_proxy::switches::kDataReductionProxy
, primary_proxy
);
56 command_line
->AppendSwitchASCII(
57 data_reduction_proxy::switches::kDataReductionProxyFallback
,
59 command_line
->AppendSwitchASCII(
60 data_reduction_proxy::switches::kDataReductionProxySecureProxyCheckURL
,
61 secure_proxy_check_url
);
66 CronetDataReductionProxy::CronetDataReductionProxy(
67 const std::string
& key
,
68 const std::string
& primary_proxy
,
69 const std::string
& fallback_proxy
,
70 const std::string
& secure_proxy_check_url
,
71 const std::string
& user_agent
,
72 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner
,
74 : task_runner_(task_runner
) {
75 DCHECK(task_runner_
->BelongsToCurrentThread());
76 AddOptionsToCommandLine(primary_proxy
, fallback_proxy
, secure_proxy_check_url
,
77 base::CommandLine::ForCurrentProcess());
78 prefs_
= CreatePrefService();
79 // In Cronet, the Data Reduction Proxy's UI classes are Created on Cronet's
82 new data_reduction_proxy::DataReductionProxySettings());
84 new data_reduction_proxy::DataReductionProxyIOData(
85 data_reduction_proxy::Client::CRONET_ANDROID
,
86 data_reduction_proxy::DataReductionProxyParams::kAllowed
|
87 data_reduction_proxy::DataReductionProxyParams::kFallbackAllowed
,
88 net_log
, task_runner
, task_runner
, false, false, user_agent
));
89 io_data_
->request_options()->SetKeyOnIO(key
);
92 CronetDataReductionProxy::~CronetDataReductionProxy() {
93 io_data_
->ShutdownOnUIThread();
96 scoped_ptr
<net::NetworkDelegate
>
97 CronetDataReductionProxy::CreateNetworkDelegate(
98 scoped_ptr
<net::NetworkDelegate
> wrapped_network_delegate
) {
99 return io_data_
->CreateNetworkDelegate(wrapped_network_delegate
.Pass(),
100 false /* No bypass UMA */ );
103 scoped_ptr
<net::URLRequestInterceptor
>
104 CronetDataReductionProxy::CreateInterceptor() {
105 return io_data_
->CreateInterceptor();
108 void CronetDataReductionProxy::Init(bool enable
,
109 net::URLRequestContext
* context
) {
110 url_request_context_getter_
=
111 new net::TrivialURLRequestContextGetter(
112 context
, task_runner_
);
113 scoped_ptr
<data_reduction_proxy::DataReductionProxyService
>
114 data_reduction_proxy_service(
115 new data_reduction_proxy::DataReductionProxyService(
116 settings_
.get(), prefs_
.get(),
117 url_request_context_getter_
.get(),
118 make_scoped_ptr(new data_reduction_proxy::DataStore()),
119 task_runner_
, task_runner_
, task_runner_
, base::TimeDelta()));
120 io_data_
->SetDataReductionProxyService(
121 data_reduction_proxy_service
->GetWeakPtr());
122 settings_
->InitDataReductionProxySettings(
123 prefs_
.get(), io_data_
.get(), data_reduction_proxy_service
.Pass());
124 settings_
->SetDataReductionProxyEnabled(enable
);
125 settings_
->MaybeActivateDataReductionProxy(true);
128 } // namespace cronet