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_browser_policy_connector.h"
8 #include "android_webview/browser/aw_form_database_service.h"
9 #include "android_webview/browser/aw_permission_manager.h"
10 #include "android_webview/browser/aw_pref_store.h"
11 #include "android_webview/browser/aw_quota_manager_bridge.h"
12 #include "android_webview/browser/aw_resource_context.h"
13 #include "android_webview/browser/jni_dependency_factory.h"
14 #include "android_webview/browser/net/aw_url_request_context_getter.h"
15 #include "android_webview/browser/net/init_native_callback.h"
16 #include "android_webview/common/aw_content_client.h"
17 #include "base/base_paths_android.h"
18 #include "base/bind.h"
19 #include "base/path_service.h"
20 #include "base/prefs/pref_service.h"
21 #include "base/prefs/pref_service_factory.h"
22 #include "components/autofill/core/common/autofill_pref_names.h"
23 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_compression_stats.h"
24 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data.h"
25 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_prefs.h"
26 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_service.h"
27 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.h"
28 #include "components/data_reduction_proxy/core/browser/data_store.h"
29 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_params.h"
30 #include "components/policy/core/browser/browser_policy_connector_base.h"
31 #include "components/policy/core/browser/configuration_policy_pref_store.h"
32 #include "components/policy/core/browser/url_blacklist_manager.h"
33 #include "components/pref_registry/pref_registry_syncable.h"
34 #include "components/url_formatter/url_fixer.h"
35 #include "components/user_prefs/user_prefs.h"
36 #include "components/visitedlink/browser/visitedlink_master.h"
37 #include "content/public/browser/browser_thread.h"
38 #include "content/public/browser/ssl_host_state_delegate.h"
39 #include "content/public/browser/storage_partition.h"
40 #include "content/public/browser/web_contents.h"
41 #include "net/cookies/cookie_store.h"
42 #include "net/proxy/proxy_config_service_android.h"
43 #include "net/proxy/proxy_service.h"
46 using content::BrowserThread
;
48 namespace android_webview
{
52 // Shows notifications which correspond to PersistentPrefStore's reading errors.
53 void HandleReadError(PersistentPrefStore::PrefReadError error
) {
56 void DeleteDirRecursively(const base::FilePath
& path
) {
57 if (!base::DeleteFile(path
, true)) {
58 // Deleting a non-existent file is considered successful, so this will
59 // trigger only in case of real errors.
60 LOG(WARNING
) << "Failed to delete " << path
.AsUTF8Unsafe();
64 AwBrowserContext
* g_browser_context
= NULL
;
66 net::ProxyConfigService
* CreateProxyConfigService() {
67 net::ProxyConfigServiceAndroid
* config_service
=
68 static_cast<net::ProxyConfigServiceAndroid
*>(
69 net::ProxyService::CreateSystemProxyConfigService(
70 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
),
71 nullptr /* Ignored on Android */ ));
72 config_service
->set_exclude_pac_url(true);
73 return config_service
;
76 bool OverrideBlacklistForURL(const GURL
& url
, bool* block
, int* reason
) {
77 // We don't have URLs that should never be blacklisted here.
81 policy::URLBlacklistManager
* CreateURLBlackListManager(
82 PrefService
* pref_service
) {
83 policy::URLBlacklist::SegmentURLCallback segment_url_callback
=
84 static_cast<policy::URLBlacklist::SegmentURLCallback
>(
85 url_formatter::SegmentURL
);
86 base::SequencedWorkerPool
* pool
= BrowserThread::GetBlockingPool();
87 scoped_refptr
<base::SequencedTaskRunner
> background_task_runner
=
88 pool
->GetSequencedTaskRunner(pool
->GetSequenceToken());
89 scoped_refptr
<base::SingleThreadTaskRunner
> io_task_runner
=
90 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
);
92 return new policy::URLBlacklistManager(pref_service
, background_task_runner
,
93 io_task_runner
, segment_url_callback
,
94 base::Bind(OverrideBlacklistForURL
));
99 // Data reduction proxy is disabled by default.
100 bool AwBrowserContext::data_reduction_proxy_enabled_
= false;
102 // Delete the legacy cache dir (in the app data dir) in 10 seconds after init.
103 int AwBrowserContext::legacy_cache_removal_delay_ms_
= 10000;
105 AwBrowserContext::AwBrowserContext(
107 JniDependencyFactory
* native_factory
)
108 : context_storage_path_(path
),
109 native_factory_(native_factory
) {
110 DCHECK(!g_browser_context
);
111 g_browser_context
= this;
113 // This constructor is entered during the creation of ContentBrowserClient,
114 // before browser threads are created. Therefore any checks to enforce
115 // threading (such as BrowserThread::CurrentlyOn()) will fail here.
118 AwBrowserContext::~AwBrowserContext() {
119 DCHECK_EQ(this, g_browser_context
);
120 g_browser_context
= NULL
;
124 AwBrowserContext
* AwBrowserContext::GetDefault() {
125 // TODO(joth): rather than store in a global here, lookup this instance
126 // from the Java-side peer.
127 return g_browser_context
;
131 AwBrowserContext
* AwBrowserContext::FromWebContents(
132 content::WebContents
* web_contents
) {
133 // This is safe; this is the only implementation of the browser context.
134 return static_cast<AwBrowserContext
*>(web_contents
->GetBrowserContext());
138 void AwBrowserContext::SetDataReductionProxyEnabled(bool enabled
) {
139 // Cache the setting value. It is possible that data reduction proxy is
141 data_reduction_proxy_enabled_
= enabled
;
142 AwBrowserContext
* context
= AwBrowserContext::GetDefault();
143 // Can't enable Data reduction proxy if user pref service is not ready.
144 if (context
== NULL
|| context
->user_pref_service_
.get() == NULL
)
146 data_reduction_proxy::DataReductionProxySettings
* proxy_settings
=
147 context
->GetDataReductionProxySettings();
148 if (proxy_settings
== NULL
)
150 // At this point, context->PreMainMessageLoopRun() has run, so
151 // context->data_reduction_proxy_io_data() is valid.
152 DCHECK(context
->GetDataReductionProxyIOData());
153 context
->CreateDataReductionProxyStatisticsIfNecessary();
154 proxy_settings
->SetDataReductionProxyEnabled(data_reduction_proxy_enabled_
);
158 void AwBrowserContext::SetLegacyCacheRemovalDelayForTest(int delay_ms
) {
159 legacy_cache_removal_delay_ms_
= delay_ms
;
162 void AwBrowserContext::PreMainMessageLoopRun() {
163 cookie_store_
= CreateCookieStore(this);
165 const FilePath fallback_cache_dir
=
166 GetPath().Append(FILE_PATH_LITERAL("Cache"));
167 if (PathService::Get(base::DIR_CACHE
, &cache_path
)) {
168 cache_path
= cache_path
.Append(
169 FILE_PATH_LITERAL("org.chromium.android_webview"));
170 // Delay the legacy dir removal to not impact startup performance.
171 BrowserThread::PostDelayedTask(
172 BrowserThread::FILE, FROM_HERE
,
173 base::Bind(&DeleteDirRecursively
, fallback_cache_dir
),
174 base::TimeDelta::FromMilliseconds(legacy_cache_removal_delay_ms_
));
176 cache_path
= fallback_cache_dir
;
177 LOG(WARNING
) << "Failed to get cache directory for Android WebView. "
178 << "Using app data directory as a fallback.";
180 url_request_context_getter_
=
181 new AwURLRequestContextGetter(
182 cache_path
, cookie_store_
.get(),
183 make_scoped_ptr(CreateProxyConfigService()).Pass());
185 data_reduction_proxy_io_data_
.reset(
186 new data_reduction_proxy::DataReductionProxyIOData(
187 data_reduction_proxy::Client::WEBVIEW_ANDROID
,
188 data_reduction_proxy::DataReductionProxyParams::kAllowed
,
189 url_request_context_getter_
->GetNetLog(),
190 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
),
191 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI
),
193 false /* enable_quic */,
195 data_reduction_proxy_settings_
.reset(
196 new data_reduction_proxy::DataReductionProxySettings());
197 scoped_ptr
<data_reduction_proxy::DataStore
> store(
198 new data_reduction_proxy::DataStore());
199 base::SequencedWorkerPool
* pool
= BrowserThread::GetBlockingPool();
200 scoped_refptr
<base::SequencedTaskRunner
> db_task_runner
=
201 pool
->GetSequencedTaskRunnerWithShutdownBehavior(
202 pool
->GetSequenceToken(),
203 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN
);
204 data_reduction_proxy_service_
.reset(
205 new data_reduction_proxy::DataReductionProxyService(
206 data_reduction_proxy_settings_
.get(), nullptr,
207 GetAwURLRequestContext(), store
.Pass(),
208 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI
),
209 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
),
210 db_task_runner
, base::TimeDelta()));
211 data_reduction_proxy_io_data_
->SetDataReductionProxyService(
212 data_reduction_proxy_service_
->GetWeakPtr());
214 visitedlink_master_
.reset(
215 new visitedlink::VisitedLinkMaster(this, this, false));
216 visitedlink_master_
->Init();
218 form_database_service_
.reset(
219 new AwFormDatabaseService(context_storage_path_
));
221 browser_policy_connector_
.reset(new AwBrowserPolicyConnector());
223 InitUserPrefService();
225 // Ensure the storage partition is initialized in time for DataReductionProxy.
226 EnsureResourceContextInitialized(this);
228 // TODO(dgn) lazy init, see http://crbug.com/521542
229 data_reduction_proxy_settings_
->InitDataReductionProxySettings(
230 user_pref_service_
.get(), data_reduction_proxy_io_data_
.get(),
231 data_reduction_proxy_service_
.Pass());
232 data_reduction_proxy_settings_
->MaybeActivateDataReductionProxy(true);
234 blacklist_manager_
.reset(CreateURLBlackListManager(user_pref_service_
.get()));
237 void AwBrowserContext::AddVisitedURLs(const std::vector
<GURL
>& urls
) {
238 DCHECK(visitedlink_master_
);
239 visitedlink_master_
->AddURLs(urls
);
242 net::URLRequestContextGetter
* AwBrowserContext::CreateRequestContext(
243 content::ProtocolHandlerMap
* protocol_handlers
,
244 content::URLRequestInterceptorScopedVector request_interceptors
) {
245 // This function cannot actually create the request context because
246 // there is a reentrant dependency on GetResourceContext() via
247 // content::StoragePartitionImplMap::Create(). This is not fixable
248 // until http://crbug.com/159193. Until then, assert that the context
249 // has already been allocated and just handle setting the protocol_handlers.
250 DCHECK(url_request_context_getter_
.get());
251 url_request_context_getter_
->SetHandlersAndInterceptors(
252 protocol_handlers
, request_interceptors
.Pass());
253 return url_request_context_getter_
.get();
256 net::URLRequestContextGetter
*
257 AwBrowserContext::CreateRequestContextForStoragePartition(
258 const base::FilePath
& partition_path
,
260 content::ProtocolHandlerMap
* protocol_handlers
,
261 content::URLRequestInterceptorScopedVector request_interceptors
) {
266 AwQuotaManagerBridge
* AwBrowserContext::GetQuotaManagerBridge() {
267 if (!quota_manager_bridge_
.get()) {
268 quota_manager_bridge_
= native_factory_
->CreateAwQuotaManagerBridge(this);
270 return quota_manager_bridge_
.get();
273 AwFormDatabaseService
* AwBrowserContext::GetFormDatabaseService() {
274 return form_database_service_
.get();
277 data_reduction_proxy::DataReductionProxySettings
*
278 AwBrowserContext::GetDataReductionProxySettings() {
279 return data_reduction_proxy_settings_
.get();
282 data_reduction_proxy::DataReductionProxyIOData
*
283 AwBrowserContext::GetDataReductionProxyIOData() {
284 return data_reduction_proxy_io_data_
.get();
287 AwURLRequestContextGetter
* AwBrowserContext::GetAwURLRequestContext() {
288 return url_request_context_getter_
.get();
291 AwMessagePortService
* AwBrowserContext::GetMessagePortService() {
292 if (!message_port_service_
.get()) {
293 message_port_service_
.reset(
294 native_factory_
->CreateAwMessagePortService());
296 return message_port_service_
.get();
299 // Create user pref service for autofill functionality.
300 void AwBrowserContext::InitUserPrefService() {
301 user_prefs::PrefRegistrySyncable
* pref_registry
=
302 new user_prefs::PrefRegistrySyncable();
303 // We only use the autocomplete feature of the Autofill, which is
304 // controlled via the manager_delegate. We don't use the rest
305 // of autofill, which is why it is hardcoded as disabled here.
306 pref_registry
->RegisterBooleanPref(
307 autofill::prefs::kAutofillEnabled
, false);
308 pref_registry
->RegisterDoublePref(
309 autofill::prefs::kAutofillPositiveUploadRate
, 0.0);
310 pref_registry
->RegisterDoublePref(
311 autofill::prefs::kAutofillNegativeUploadRate
, 0.0);
312 data_reduction_proxy::RegisterSimpleProfilePrefs(pref_registry
);
313 policy::URLBlacklistManager::RegisterProfilePrefs(pref_registry
);
315 base::PrefServiceFactory pref_service_factory
;
316 pref_service_factory
.set_user_prefs(make_scoped_refptr(new AwPrefStore()));
317 pref_service_factory
.set_managed_prefs(
318 make_scoped_refptr(new policy::ConfigurationPolicyPrefStore(
319 browser_policy_connector_
->GetPolicyService(),
320 browser_policy_connector_
->GetHandlerList(),
321 policy::POLICY_LEVEL_MANDATORY
)));
322 pref_service_factory
.set_read_error_callback(base::Bind(&HandleReadError
));
323 user_pref_service_
= pref_service_factory
.Create(pref_registry
).Pass();
325 user_prefs::UserPrefs::Set(this, user_pref_service_
.get());
328 scoped_ptr
<content::ZoomLevelDelegate
>
329 AwBrowserContext::CreateZoomLevelDelegate(
330 const base::FilePath
& partition_path
) {
334 base::FilePath
AwBrowserContext::GetPath() const {
335 return context_storage_path_
;
338 bool AwBrowserContext::IsOffTheRecord() const {
339 // Android WebView does not support off the record profile yet.
343 net::URLRequestContextGetter
* AwBrowserContext::GetRequestContext() {
344 return GetDefaultStoragePartition(this)->GetURLRequestContext();
347 net::URLRequestContextGetter
*
348 AwBrowserContext::GetRequestContextForRenderProcess(
349 int renderer_child_id
) {
350 return GetRequestContext();
353 net::URLRequestContextGetter
* AwBrowserContext::GetMediaRequestContext() {
354 return GetRequestContext();
357 net::URLRequestContextGetter
*
358 AwBrowserContext::GetMediaRequestContextForRenderProcess(
359 int renderer_child_id
) {
360 return GetRequestContext();
363 net::URLRequestContextGetter
*
364 AwBrowserContext::GetMediaRequestContextForStoragePartition(
365 const base::FilePath
& partition_path
,
371 content::ResourceContext
* AwBrowserContext::GetResourceContext() {
372 if (!resource_context_
) {
373 resource_context_
.reset(
374 new AwResourceContext(url_request_context_getter_
.get()));
376 return resource_context_
.get();
379 content::DownloadManagerDelegate
*
380 AwBrowserContext::GetDownloadManagerDelegate() {
381 return &download_manager_delegate_
;
384 content::BrowserPluginGuestManager
* AwBrowserContext::GetGuestManager() {
388 storage::SpecialStoragePolicy
* AwBrowserContext::GetSpecialStoragePolicy() {
389 // Intentionally returning NULL as 'Extensions' and 'Apps' not supported.
393 content::PushMessagingService
* AwBrowserContext::GetPushMessagingService() {
394 // TODO(johnme): Support push messaging in WebView.
398 content::SSLHostStateDelegate
* AwBrowserContext::GetSSLHostStateDelegate() {
399 if (!ssl_host_state_delegate_
.get()) {
400 ssl_host_state_delegate_
.reset(new AwSSLHostStateDelegate());
402 return ssl_host_state_delegate_
.get();
405 content::PermissionManager
* AwBrowserContext::GetPermissionManager() {
406 if (!permission_manager_
.get())
407 permission_manager_
.reset(new AwPermissionManager());
408 return permission_manager_
.get();
411 policy::URLBlacklistManager
* AwBrowserContext::GetURLBlacklistManager() {
412 // Should not be called until the end of PreMainMessageLoopRun, where
413 // blacklist_manager_ is initialized.
414 DCHECK(blacklist_manager_
);
415 return blacklist_manager_
.get();
418 void AwBrowserContext::RebuildTable(
419 const scoped_refptr
<URLEnumerator
>& enumerator
) {
420 // Android WebView rebuilds from WebChromeClient.getVisitedHistory. The client
421 // can change in the lifetime of this WebView and may not yet be set here.
422 // Therefore this initialization path is not used.
423 enumerator
->OnComplete(true);
426 void AwBrowserContext::CreateDataReductionProxyStatisticsIfNecessary() {
427 DCHECK(user_pref_service_
.get());
428 DCHECK(GetDataReductionProxySettings());
429 data_reduction_proxy::DataReductionProxyService
*
430 data_reduction_proxy_service
=
431 GetDataReductionProxySettings()->data_reduction_proxy_service();
432 DCHECK(data_reduction_proxy_service
);
433 if (data_reduction_proxy_service
->compression_stats())
435 // We don't care about commit_delay for now. It is just a dummy value.
436 base::TimeDelta commit_delay
= base::TimeDelta::FromMinutes(60);
437 data_reduction_proxy_service
->EnableCompressionStatisticsLogging(
438 user_pref_service_
.get(),
439 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI
),
443 } // namespace android_webview