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/base_paths_android.h"
15 #include "base/bind.h"
16 #include "base/path_service.h"
17 #include "base/prefs/pref_registry_simple.h"
18 #include "base/prefs/pref_service.h"
19 #include "base/prefs/pref_service_factory.h"
20 #include "components/autofill/core/common/autofill_pref_names.h"
21 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_config_service.h"
22 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_prefs.h"
23 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.h"
24 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_statistics_prefs.h"
25 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_event_store.h"
26 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_params.h"
27 #include "components/user_prefs/user_prefs.h"
28 #include "components/visitedlink/browser/visitedlink_master.h"
29 #include "content/public/browser/browser_thread.h"
30 #include "content/public/browser/ssl_host_state_delegate.h"
31 #include "content/public/browser/storage_partition.h"
32 #include "content/public/browser/web_contents.h"
33 #include "net/cookies/cookie_store.h"
34 #include "net/proxy/proxy_config_service_android.h"
35 #include "net/proxy/proxy_service.h"
38 using content::BrowserThread
;
39 using data_reduction_proxy::DataReductionProxyConfigService
;
40 using data_reduction_proxy::DataReductionProxyEventStore
;
41 using data_reduction_proxy::DataReductionProxySettings
;
43 namespace android_webview
{
47 // Shows notifications which correspond to PersistentPrefStore's reading errors.
48 void HandleReadError(PersistentPrefStore::PrefReadError error
) {
51 void DeleteDirRecursively(const base::FilePath
& path
) {
52 if (!base::DeleteFile(path
, true)) {
53 // Deleting a non-existent file is considered successful, so this will
54 // trigger only in case of real errors.
55 LOG(WARNING
) << "Failed to delete " << path
.AsUTF8Unsafe();
59 AwBrowserContext
* g_browser_context
= NULL
;
61 net::ProxyConfigService
* CreateProxyConfigService() {
62 net::ProxyConfigServiceAndroid
* config_service
=
63 static_cast<net::ProxyConfigServiceAndroid
*>(
64 net::ProxyService::CreateSystemProxyConfigService(
65 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
),
66 nullptr /* Ignored on Android */ ));
67 config_service
->set_exclude_pac_url(true);
68 return config_service
;
73 // Data reduction proxy is disabled by default.
74 bool AwBrowserContext::data_reduction_proxy_enabled_
= false;
76 // Delete the legacy cache dir (in the app data dir) in 10 seconds after init.
77 int AwBrowserContext::legacy_cache_removal_delay_ms_
= 10000;
79 AwBrowserContext::AwBrowserContext(
81 JniDependencyFactory
* native_factory
)
82 : context_storage_path_(path
),
83 native_factory_(native_factory
) {
84 DCHECK(!g_browser_context
);
85 g_browser_context
= this;
87 // This constructor is entered during the creation of ContentBrowserClient,
88 // before browser threads are created. Therefore any checks to enforce
89 // threading (such as BrowserThread::CurrentlyOn()) will fail here.
92 AwBrowserContext::~AwBrowserContext() {
93 DCHECK_EQ(this, g_browser_context
);
94 g_browser_context
= NULL
;
98 AwBrowserContext
* AwBrowserContext::GetDefault() {
99 // TODO(joth): rather than store in a global here, lookup this instance
100 // from the Java-side peer.
101 return g_browser_context
;
105 AwBrowserContext
* AwBrowserContext::FromWebContents(
106 content::WebContents
* web_contents
) {
107 // This is safe; this is the only implementation of the browser context.
108 return static_cast<AwBrowserContext
*>(web_contents
->GetBrowserContext());
112 void AwBrowserContext::SetDataReductionProxyEnabled(bool enabled
) {
113 // Cache the setting value. It is possible that data reduction proxy is
115 data_reduction_proxy_enabled_
= enabled
;
116 AwBrowserContext
* context
= AwBrowserContext::GetDefault();
117 // Can't enable Data reduction proxy if user pref service is not ready.
118 if (context
== NULL
|| context
->user_pref_service_
.get() == NULL
)
120 DataReductionProxySettings
* proxy_settings
=
121 context
->GetDataReductionProxySettings();
122 if (proxy_settings
== NULL
)
125 context
->CreateDataReductionProxyStatisticsIfNecessary();
126 proxy_settings
->SetDataReductionProxyStatisticsPrefs(
127 context
->data_reduction_proxy_statistics_
.get());
128 proxy_settings
->SetDataReductionProxyEnabled(data_reduction_proxy_enabled_
);
132 void AwBrowserContext::SetLegacyCacheRemovalDelayForTest(int delay_ms
) {
133 legacy_cache_removal_delay_ms_
= delay_ms
;
136 void AwBrowserContext::PreMainMessageLoopRun() {
137 cookie_store_
= CreateCookieStore(this);
138 data_reduction_proxy_settings_
.reset(
139 new DataReductionProxySettings(
140 new data_reduction_proxy::DataReductionProxyParams(
141 data_reduction_proxy::DataReductionProxyParams::kAllowed
)));
142 data_reduction_proxy_event_store_
.reset(
143 new DataReductionProxyEventStore(
144 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI
)));
145 scoped_ptr
<DataReductionProxyConfigService
>
146 data_reduction_proxy_config_service(
147 new DataReductionProxyConfigService(
148 scoped_ptr
<net::ProxyConfigService
>(
149 CreateProxyConfigService()).Pass()));
150 if (data_reduction_proxy_settings_
.get()) {
151 data_reduction_proxy_configurator_
.reset(
152 new data_reduction_proxy::DataReductionProxyConfigTracker(
153 base::Bind(&DataReductionProxyConfigService::UpdateProxyConfig
,
155 data_reduction_proxy_config_service
.get())),
156 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
)));
157 data_reduction_proxy_settings_
->SetProxyConfigurator(
158 data_reduction_proxy_configurator_
.get());
162 const FilePath fallback_cache_dir
=
163 GetPath().Append(FILE_PATH_LITERAL("Cache"));
164 if (PathService::Get(base::DIR_CACHE
, &cache_path
)) {
165 cache_path
= cache_path
.Append(
166 FILE_PATH_LITERAL("org.chromium.android_webview"));
167 // Delay the legacy dir removal to not impact startup performance.
168 BrowserThread::PostDelayedTask(
169 BrowserThread::FILE, FROM_HERE
,
170 base::Bind(&DeleteDirRecursively
, fallback_cache_dir
),
171 base::TimeDelta::FromMilliseconds(legacy_cache_removal_delay_ms_
));
173 cache_path
= fallback_cache_dir
;
174 LOG(WARNING
) << "Failed to get cache directory for Android WebView. "
175 << "Using app data directory as a fallback.";
177 url_request_context_getter_
=
178 new AwURLRequestContextGetter(cache_path
,
180 data_reduction_proxy_config_service
.Pass());
182 visitedlink_master_
.reset(
183 new visitedlink::VisitedLinkMaster(this, this, false));
184 visitedlink_master_
->Init();
186 form_database_service_
.reset(
187 new AwFormDatabaseService(context_storage_path_
));
190 void AwBrowserContext::AddVisitedURLs(const std::vector
<GURL
>& urls
) {
191 DCHECK(visitedlink_master_
);
192 visitedlink_master_
->AddURLs(urls
);
195 net::URLRequestContextGetter
* AwBrowserContext::CreateRequestContext(
196 content::ProtocolHandlerMap
* protocol_handlers
,
197 content::URLRequestInterceptorScopedVector request_interceptors
) {
198 // This function cannot actually create the request context because
199 // there is a reentrant dependency on GetResourceContext() via
200 // content::StoragePartitionImplMap::Create(). This is not fixable
201 // until http://crbug.com/159193. Until then, assert that the context
202 // has already been allocated and just handle setting the protocol_handlers.
203 DCHECK(url_request_context_getter_
.get());
204 url_request_context_getter_
->SetHandlersAndInterceptors(
205 protocol_handlers
, request_interceptors
.Pass());
206 return url_request_context_getter_
.get();
209 net::URLRequestContextGetter
*
210 AwBrowserContext::CreateRequestContextForStoragePartition(
211 const base::FilePath
& partition_path
,
213 content::ProtocolHandlerMap
* protocol_handlers
,
214 content::URLRequestInterceptorScopedVector request_interceptors
) {
219 AwQuotaManagerBridge
* AwBrowserContext::GetQuotaManagerBridge() {
220 if (!quota_manager_bridge_
.get()) {
221 quota_manager_bridge_
= native_factory_
->CreateAwQuotaManagerBridge(this);
223 return quota_manager_bridge_
.get();
226 AwFormDatabaseService
* AwBrowserContext::GetFormDatabaseService() {
227 return form_database_service_
.get();
230 DataReductionProxySettings
* AwBrowserContext::GetDataReductionProxySettings() {
231 return data_reduction_proxy_settings_
.get();
234 DataReductionProxyEventStore
*
235 AwBrowserContext::GetDataReductionProxyEventStore() {
236 return data_reduction_proxy_event_store_
.get();
239 AwURLRequestContextGetter
* AwBrowserContext::GetAwURLRequestContext() {
240 return url_request_context_getter_
.get();
243 // Create user pref service for autofill functionality.
244 void AwBrowserContext::CreateUserPrefServiceIfNecessary() {
245 if (user_pref_service_
)
248 PrefRegistrySimple
* pref_registry
= new PrefRegistrySimple();
249 // We only use the autocomplete feature of the Autofill, which is
250 // controlled via the manager_delegate. We don't use the rest
251 // of autofill, which is why it is hardcoded as disabled here.
252 pref_registry
->RegisterBooleanPref(
253 autofill::prefs::kAutofillEnabled
, false);
254 pref_registry
->RegisterDoublePref(
255 autofill::prefs::kAutofillPositiveUploadRate
, 0.0);
256 pref_registry
->RegisterDoublePref(
257 autofill::prefs::kAutofillNegativeUploadRate
, 0.0);
258 data_reduction_proxy::RegisterSimpleProfilePrefs(pref_registry
);
260 base::PrefServiceFactory pref_service_factory
;
261 pref_service_factory
.set_user_prefs(make_scoped_refptr(new AwPrefStore()));
262 pref_service_factory
.set_read_error_callback(base::Bind(&HandleReadError
));
263 user_pref_service_
= pref_service_factory
.Create(pref_registry
).Pass();
265 user_prefs::UserPrefs::Set(this, user_pref_service_
.get());
267 if (data_reduction_proxy_settings_
.get()) {
268 data_reduction_proxy_settings_
->InitDataReductionProxySettings(
269 user_pref_service_
.get(),
271 GetAwURLRequestContext()->GetNetLog(),
272 GetDataReductionProxyEventStore());
273 data_reduction_proxy_settings_
->MaybeActivateDataReductionProxy(true);
275 SetDataReductionProxyEnabled(data_reduction_proxy_enabled_
);
279 scoped_ptr
<content::ZoomLevelDelegate
>
280 AwBrowserContext::CreateZoomLevelDelegate(
281 const base::FilePath
& partition_path
) {
285 base::FilePath
AwBrowserContext::GetPath() const {
286 return context_storage_path_
;
289 bool AwBrowserContext::IsOffTheRecord() const {
290 // Android WebView does not support off the record profile yet.
294 net::URLRequestContextGetter
* AwBrowserContext::GetRequestContext() {
295 return GetDefaultStoragePartition(this)->GetURLRequestContext();
298 net::URLRequestContextGetter
*
299 AwBrowserContext::GetRequestContextForRenderProcess(
300 int renderer_child_id
) {
301 return GetRequestContext();
304 net::URLRequestContextGetter
* AwBrowserContext::GetMediaRequestContext() {
305 return GetRequestContext();
308 net::URLRequestContextGetter
*
309 AwBrowserContext::GetMediaRequestContextForRenderProcess(
310 int renderer_child_id
) {
311 return GetRequestContext();
314 net::URLRequestContextGetter
*
315 AwBrowserContext::GetMediaRequestContextForStoragePartition(
316 const base::FilePath
& partition_path
,
322 content::ResourceContext
* AwBrowserContext::GetResourceContext() {
323 if (!resource_context_
) {
324 resource_context_
.reset(
325 new AwResourceContext(url_request_context_getter_
.get()));
327 return resource_context_
.get();
330 content::DownloadManagerDelegate
*
331 AwBrowserContext::GetDownloadManagerDelegate() {
332 return &download_manager_delegate_
;
335 content::BrowserPluginGuestManager
* AwBrowserContext::GetGuestManager() {
339 storage::SpecialStoragePolicy
* AwBrowserContext::GetSpecialStoragePolicy() {
340 // Intentionally returning NULL as 'Extensions' and 'Apps' not supported.
344 content::PushMessagingService
* AwBrowserContext::GetPushMessagingService() {
345 // TODO(johnme): Support push messaging in WebView.
349 content::SSLHostStateDelegate
* AwBrowserContext::GetSSLHostStateDelegate() {
350 if (!ssl_host_state_delegate_
.get()) {
351 ssl_host_state_delegate_
.reset(new AwSSLHostStateDelegate());
353 return ssl_host_state_delegate_
.get();
356 void AwBrowserContext::RebuildTable(
357 const scoped_refptr
<URLEnumerator
>& enumerator
) {
358 // Android WebView rebuilds from WebChromeClient.getVisitedHistory. The client
359 // can change in the lifetime of this WebView and may not yet be set here.
360 // Therefore this initialization path is not used.
361 enumerator
->OnComplete(true);
364 void AwBrowserContext::CreateDataReductionProxyStatisticsIfNecessary() {
365 DCHECK(user_pref_service_
.get());
367 if (!data_reduction_proxy_statistics_
.get()) {
368 // We don't care about commit_delay for now. It is just a dummy value.
369 base::TimeDelta commit_delay
= base::TimeDelta::FromMinutes(60);
370 data_reduction_proxy_statistics_
=
371 scoped_ptr
<data_reduction_proxy::DataReductionProxyStatisticsPrefs
>(
372 new data_reduction_proxy::DataReductionProxyStatisticsPrefs(
373 user_pref_service_
.get(),
374 base::MessageLoopProxy::current(),
379 } // namespace android_webview