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
== NULL
);
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(g_browser_context
== this);
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 #if defined(SPDY_PROXY_AUTH_ORIGIN)
103 data_reduction_proxy_settings_
.reset(
104 new DataReductionProxySettings(
105 new data_reduction_proxy::DataReductionProxyParams(
106 data_reduction_proxy::DataReductionProxyParams::kAllowed
)));
108 scoped_ptr
<DataReductionProxyConfigService
>
109 data_reduction_proxy_config_service(
110 new DataReductionProxyConfigService(
111 scoped_ptr
<net::ProxyConfigService
>(
112 net::ProxyService::CreateSystemProxyConfigService(
113 BrowserThread::GetMessageLoopProxyForThread(
115 NULL
/* Ignored on Android */)).Pass()));
116 if (data_reduction_proxy_settings_
.get()) {
117 data_reduction_proxy_configurator_
.reset(
118 new data_reduction_proxy::DataReductionProxyConfigTracker(
119 base::Bind(&DataReductionProxyConfigService::UpdateProxyConfig
,
121 data_reduction_proxy_config_service
.get())),
122 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
)));
123 data_reduction_proxy_settings_
->SetProxyConfigurator(
124 data_reduction_proxy_configurator_
.get());
127 url_request_context_getter_
=
128 new AwURLRequestContextGetter(GetPath(),
130 data_reduction_proxy_config_service
.Pass());
132 visitedlink_master_
.reset(
133 new visitedlink::VisitedLinkMaster(this, this, false));
134 visitedlink_master_
->Init();
136 form_database_service_
.reset(
137 new AwFormDatabaseService(context_storage_path_
));
140 void AwBrowserContext::AddVisitedURLs(const std::vector
<GURL
>& urls
) {
141 DCHECK(visitedlink_master_
);
142 visitedlink_master_
->AddURLs(urls
);
145 net::URLRequestContextGetter
* AwBrowserContext::CreateRequestContext(
146 content::ProtocolHandlerMap
* protocol_handlers
,
147 content::URLRequestInterceptorScopedVector request_interceptors
) {
148 // This function cannot actually create the request context because
149 // there is a reentrant dependency on GetResourceContext() via
150 // content::StoragePartitionImplMap::Create(). This is not fixable
151 // until http://crbug.com/159193. Until then, assert that the context
152 // has already been allocated and just handle setting the protocol_handlers.
153 DCHECK(url_request_context_getter_
);
154 url_request_context_getter_
->SetHandlersAndInterceptors(
155 protocol_handlers
, request_interceptors
.Pass());
156 return url_request_context_getter_
;
159 net::URLRequestContextGetter
*
160 AwBrowserContext::CreateRequestContextForStoragePartition(
161 const base::FilePath
& partition_path
,
163 content::ProtocolHandlerMap
* protocol_handlers
,
164 content::URLRequestInterceptorScopedVector request_interceptors
) {
169 AwQuotaManagerBridge
* AwBrowserContext::GetQuotaManagerBridge() {
170 if (!quota_manager_bridge_
.get()) {
171 quota_manager_bridge_
= native_factory_
->CreateAwQuotaManagerBridge(this);
173 return quota_manager_bridge_
.get();
176 AwFormDatabaseService
* AwBrowserContext::GetFormDatabaseService() {
177 return form_database_service_
.get();
180 DataReductionProxySettings
* AwBrowserContext::GetDataReductionProxySettings() {
181 return data_reduction_proxy_settings_
.get();
184 // Create user pref service for autofill functionality.
185 void AwBrowserContext::CreateUserPrefServiceIfNecessary() {
186 if (user_pref_service_
)
189 PrefRegistrySimple
* pref_registry
= new PrefRegistrySimple();
190 // We only use the autocomplete feature of the Autofill, which is
191 // controlled via the manager_delegate. We don't use the rest
192 // of autofill, which is why it is hardcoded as disabled here.
193 pref_registry
->RegisterBooleanPref(
194 autofill::prefs::kAutofillEnabled
, false);
195 pref_registry
->RegisterDoublePref(
196 autofill::prefs::kAutofillPositiveUploadRate
, 0.0);
197 pref_registry
->RegisterDoublePref(
198 autofill::prefs::kAutofillNegativeUploadRate
, 0.0);
199 data_reduction_proxy::RegisterSimpleProfilePrefs(pref_registry
);
200 data_reduction_proxy::RegisterPrefs(pref_registry
);
202 base::PrefServiceFactory pref_service_factory
;
203 pref_service_factory
.set_user_prefs(make_scoped_refptr(new AwPrefStore()));
204 pref_service_factory
.set_read_error_callback(base::Bind(&HandleReadError
));
205 user_pref_service_
= pref_service_factory
.Create(pref_registry
).Pass();
207 user_prefs::UserPrefs::Set(this, user_pref_service_
.get());
209 if (data_reduction_proxy_settings_
.get()) {
210 data_reduction_proxy_settings_
->InitDataReductionProxySettings(
211 user_pref_service_
.get(),
212 user_pref_service_
.get(),
213 GetRequestContext());
215 data_reduction_proxy_settings_
->SetDataReductionProxyEnabled(
216 data_reduction_proxy_enabled_
);
220 base::FilePath
AwBrowserContext::GetPath() const {
221 return context_storage_path_
;
224 bool AwBrowserContext::IsOffTheRecord() const {
225 // Android WebView does not support off the record profile yet.
229 net::URLRequestContextGetter
* AwBrowserContext::GetRequestContext() {
230 return GetDefaultStoragePartition(this)->GetURLRequestContext();
233 net::URLRequestContextGetter
*
234 AwBrowserContext::GetRequestContextForRenderProcess(
235 int renderer_child_id
) {
236 return GetRequestContext();
239 net::URLRequestContextGetter
* AwBrowserContext::GetMediaRequestContext() {
240 return GetRequestContext();
243 net::URLRequestContextGetter
*
244 AwBrowserContext::GetMediaRequestContextForRenderProcess(
245 int renderer_child_id
) {
246 return GetRequestContext();
249 net::URLRequestContextGetter
*
250 AwBrowserContext::GetMediaRequestContextForStoragePartition(
251 const base::FilePath
& partition_path
,
257 content::ResourceContext
* AwBrowserContext::GetResourceContext() {
258 if (!resource_context_
) {
259 resource_context_
.reset(
260 new AwResourceContext(url_request_context_getter_
.get()));
262 return resource_context_
.get();
265 content::DownloadManagerDelegate
*
266 AwBrowserContext::GetDownloadManagerDelegate() {
267 return &download_manager_delegate_
;
270 content::BrowserPluginGuestManager
* AwBrowserContext::GetGuestManager() {
274 storage::SpecialStoragePolicy
* AwBrowserContext::GetSpecialStoragePolicy() {
275 // Intentionally returning NULL as 'Extensions' and 'Apps' not supported.
279 content::PushMessagingService
* AwBrowserContext::GetPushMessagingService() {
280 // TODO(johnme): Support push messaging in WebView.
284 content::SSLHostStateDelegate
* AwBrowserContext::GetSSLHostStateDelegate() {
288 void AwBrowserContext::RebuildTable(
289 const scoped_refptr
<URLEnumerator
>& enumerator
) {
290 // Android WebView rebuilds from WebChromeClient.getVisitedHistory. The client
291 // can change in the lifetime of this WebView and may not yet be set here.
292 // Therefore this initialization path is not used.
293 enumerator
->OnComplete(true);
296 } // namespace android_webview