1 // Copyright (c) 2012 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 "android_webview/browser/aw_browser_context.h"
7 #include "android_webview/browser/aw_form_database_service.h"
8 #include "android_webview/browser/aw_pref_store.h"
9 #include "android_webview/browser/aw_quota_manager_bridge.h"
10 #include "android_webview/browser/aw_resource_context.h"
11 #include "android_webview/browser/jni_dependency_factory.h"
12 #include "android_webview/browser/net/aw_url_request_context_getter.h"
13 #include "android_webview/browser/net/init_native_callback.h"
14 #include "base/bind.h"
15 #include "base/prefs/pref_registry_simple.h"
16 #include "base/prefs/pref_service.h"
17 #include "base/prefs/pref_service_factory.h"
18 #include "components/autofill/core/common/autofill_pref_names.h"
19 #include "components/data_reduction_proxy/browser/data_reduction_proxy_config_service.h"
20 #include "components/data_reduction_proxy/browser/data_reduction_proxy_params.h"
21 #include "components/data_reduction_proxy/browser/data_reduction_proxy_prefs.h"
22 #include "components/data_reduction_proxy/browser/data_reduction_proxy_settings.h"
23 #include "components/user_prefs/user_prefs.h"
24 #include "components/visitedlink/browser/visitedlink_master.h"
25 #include "content/public/browser/browser_thread.h"
26 #include "content/public/browser/ssl_host_state_delegate.h"
27 #include "content/public/browser/storage_partition.h"
28 #include "content/public/browser/web_contents.h"
29 #include "net/cookies/cookie_store.h"
30 #include "net/proxy/proxy_service.h"
33 using content::BrowserThread
;
34 using data_reduction_proxy::DataReductionProxyConfigService
;
35 using data_reduction_proxy::DataReductionProxySettings
;
37 namespace android_webview
{
41 // Shows notifications which correspond to PersistentPrefStore's reading errors.
42 void HandleReadError(PersistentPrefStore::PrefReadError error
) {
45 AwBrowserContext
* g_browser_context
= NULL
;
49 // Data reduction proxy is disabled by default.
50 bool AwBrowserContext::data_reduction_proxy_enabled_
= false;
52 AwBrowserContext::AwBrowserContext(
54 JniDependencyFactory
* native_factory
)
55 : context_storage_path_(path
),
56 native_factory_(native_factory
) {
57 DCHECK(!g_browser_context
);
58 g_browser_context
= this;
60 // This constructor is entered during the creation of ContentBrowserClient,
61 // before browser threads are created. Therefore any checks to enforce
62 // threading (such as BrowserThread::CurrentlyOn()) will fail here.
65 AwBrowserContext::~AwBrowserContext() {
66 DCHECK_EQ(this, g_browser_context
);
67 g_browser_context
= NULL
;
71 AwBrowserContext
* AwBrowserContext::GetDefault() {
72 // TODO(joth): rather than store in a global here, lookup this instance
73 // from the Java-side peer.
74 return g_browser_context
;
78 AwBrowserContext
* AwBrowserContext::FromWebContents(
79 content::WebContents
* web_contents
) {
80 // This is safe; this is the only implementation of the browser context.
81 return static_cast<AwBrowserContext
*>(web_contents
->GetBrowserContext());
85 void AwBrowserContext::SetDataReductionProxyEnabled(bool enabled
) {
86 // Cache the setting value. It is possible that data reduction proxy is
88 data_reduction_proxy_enabled_
= enabled
;
89 AwBrowserContext
* context
= AwBrowserContext::GetDefault();
90 // Can't enable Data reduction proxy if user pref service is not ready.
91 if (context
== NULL
|| context
->user_pref_service_
.get() == NULL
)
93 DataReductionProxySettings
* proxy_settings
=
94 context
->GetDataReductionProxySettings();
95 if (proxy_settings
== NULL
)
97 proxy_settings
->SetDataReductionProxyEnabled(data_reduction_proxy_enabled_
);
100 void AwBrowserContext::PreMainMessageLoopRun() {
101 cookie_store_
= CreateCookieStore(this);
102 data_reduction_proxy_settings_
.reset(
103 new DataReductionProxySettings(
104 new data_reduction_proxy::DataReductionProxyParams(
105 data_reduction_proxy::DataReductionProxyParams::kAllowed
)));
106 scoped_ptr
<DataReductionProxyConfigService
>
107 data_reduction_proxy_config_service(
108 new DataReductionProxyConfigService(
109 scoped_ptr
<net::ProxyConfigService
>(
110 net::ProxyService::CreateSystemProxyConfigService(
111 BrowserThread::GetMessageLoopProxyForThread(
113 NULL
/* Ignored on Android */)).Pass()));
114 if (data_reduction_proxy_settings_
.get()) {
115 data_reduction_proxy_configurator_
.reset(
116 new data_reduction_proxy::DataReductionProxyConfigTracker(
117 base::Bind(&DataReductionProxyConfigService::UpdateProxyConfig
,
119 data_reduction_proxy_config_service
.get())),
120 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
)));
121 data_reduction_proxy_settings_
->SetProxyConfigurator(
122 data_reduction_proxy_configurator_
.get());
125 url_request_context_getter_
=
126 new AwURLRequestContextGetter(GetPath(),
128 data_reduction_proxy_config_service
.Pass());
130 visitedlink_master_
.reset(
131 new visitedlink::VisitedLinkMaster(this, this, false));
132 visitedlink_master_
->Init();
134 form_database_service_
.reset(
135 new AwFormDatabaseService(context_storage_path_
));
138 void AwBrowserContext::AddVisitedURLs(const std::vector
<GURL
>& urls
) {
139 DCHECK(visitedlink_master_
);
140 visitedlink_master_
->AddURLs(urls
);
143 net::URLRequestContextGetter
* AwBrowserContext::CreateRequestContext(
144 content::ProtocolHandlerMap
* protocol_handlers
,
145 content::URLRequestInterceptorScopedVector request_interceptors
) {
146 // This function cannot actually create the request context because
147 // there is a reentrant dependency on GetResourceContext() via
148 // content::StoragePartitionImplMap::Create(). This is not fixable
149 // until http://crbug.com/159193. Until then, assert that the context
150 // has already been allocated and just handle setting the protocol_handlers.
151 DCHECK(url_request_context_getter_
);
152 url_request_context_getter_
->SetHandlersAndInterceptors(
153 protocol_handlers
, request_interceptors
.Pass());
154 return url_request_context_getter_
;
157 net::URLRequestContextGetter
*
158 AwBrowserContext::CreateRequestContextForStoragePartition(
159 const base::FilePath
& partition_path
,
161 content::ProtocolHandlerMap
* protocol_handlers
,
162 content::URLRequestInterceptorScopedVector request_interceptors
) {
167 AwQuotaManagerBridge
* AwBrowserContext::GetQuotaManagerBridge() {
168 if (!quota_manager_bridge_
.get()) {
169 quota_manager_bridge_
= native_factory_
->CreateAwQuotaManagerBridge(this);
171 return quota_manager_bridge_
.get();
174 AwFormDatabaseService
* AwBrowserContext::GetFormDatabaseService() {
175 return form_database_service_
.get();
178 DataReductionProxySettings
* AwBrowserContext::GetDataReductionProxySettings() {
179 return data_reduction_proxy_settings_
.get();
182 AwURLRequestContextGetter
* AwBrowserContext::GetAwURLRequestContext() {
183 return url_request_context_getter_
.get();
186 // Create user pref service for autofill functionality.
187 void AwBrowserContext::CreateUserPrefServiceIfNecessary() {
188 if (user_pref_service_
)
191 PrefRegistrySimple
* pref_registry
= new PrefRegistrySimple();
192 // We only use the autocomplete feature of the Autofill, which is
193 // controlled via the manager_delegate. We don't use the rest
194 // of autofill, which is why it is hardcoded as disabled here.
195 pref_registry
->RegisterBooleanPref(
196 autofill::prefs::kAutofillEnabled
, false);
197 pref_registry
->RegisterDoublePref(
198 autofill::prefs::kAutofillPositiveUploadRate
, 0.0);
199 pref_registry
->RegisterDoublePref(
200 autofill::prefs::kAutofillNegativeUploadRate
, 0.0);
201 data_reduction_proxy::RegisterSimpleProfilePrefs(pref_registry
);
202 data_reduction_proxy::RegisterPrefs(pref_registry
);
204 base::PrefServiceFactory pref_service_factory
;
205 pref_service_factory
.set_user_prefs(make_scoped_refptr(new AwPrefStore()));
206 pref_service_factory
.set_read_error_callback(base::Bind(&HandleReadError
));
207 user_pref_service_
= pref_service_factory
.Create(pref_registry
).Pass();
209 user_prefs::UserPrefs::Set(this, user_pref_service_
.get());
211 if (data_reduction_proxy_settings_
.get()) {
212 data_reduction_proxy_settings_
->InitDataReductionProxySettings(
213 user_pref_service_
.get(),
214 GetRequestContext());
216 data_reduction_proxy_settings_
->SetDataReductionProxyEnabled(
217 data_reduction_proxy_enabled_
);
221 base::FilePath
AwBrowserContext::GetPath() const {
222 return context_storage_path_
;
225 bool AwBrowserContext::IsOffTheRecord() const {
226 // Android WebView does not support off the record profile yet.
230 net::URLRequestContextGetter
* AwBrowserContext::GetRequestContext() {
231 return GetDefaultStoragePartition(this)->GetURLRequestContext();
234 net::URLRequestContextGetter
*
235 AwBrowserContext::GetRequestContextForRenderProcess(
236 int renderer_child_id
) {
237 return GetRequestContext();
240 net::URLRequestContextGetter
* AwBrowserContext::GetMediaRequestContext() {
241 return GetRequestContext();
244 net::URLRequestContextGetter
*
245 AwBrowserContext::GetMediaRequestContextForRenderProcess(
246 int renderer_child_id
) {
247 return GetRequestContext();
250 net::URLRequestContextGetter
*
251 AwBrowserContext::GetMediaRequestContextForStoragePartition(
252 const base::FilePath
& partition_path
,
258 content::ResourceContext
* AwBrowserContext::GetResourceContext() {
259 if (!resource_context_
) {
260 resource_context_
.reset(
261 new AwResourceContext(url_request_context_getter_
.get()));
263 return resource_context_
.get();
266 content::DownloadManagerDelegate
*
267 AwBrowserContext::GetDownloadManagerDelegate() {
268 return &download_manager_delegate_
;
271 content::BrowserPluginGuestManager
* AwBrowserContext::GetGuestManager() {
275 storage::SpecialStoragePolicy
* AwBrowserContext::GetSpecialStoragePolicy() {
276 // Intentionally returning NULL as 'Extensions' and 'Apps' not supported.
280 content::PushMessagingService
* AwBrowserContext::GetPushMessagingService() {
281 // TODO(johnme): Support push messaging in WebView.
285 content::SSLHostStateDelegate
* AwBrowserContext::GetSSLHostStateDelegate() {
289 void AwBrowserContext::RebuildTable(
290 const scoped_refptr
<URLEnumerator
>& enumerator
) {
291 // Android WebView rebuilds from WebChromeClient.getVisitedHistory. The client
292 // can change in the lifetime of this WebView and may not yet be set here.
293 // Therefore this initialization path is not used.
294 enumerator
->OnComplete(true);
297 } // namespace android_webview