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_permission_manager.h"
9 #include "android_webview/browser/aw_pref_store.h"
10 #include "android_webview/browser/aw_quota_manager_bridge.h"
11 #include "android_webview/browser/aw_resource_context.h"
12 #include "android_webview/browser/jni_dependency_factory.h"
13 #include "android_webview/browser/net/aw_url_request_context_getter.h"
14 #include "android_webview/browser/net/init_native_callback.h"
15 #include "android_webview/common/aw_content_client.h"
16 #include "base/base_paths_android.h"
17 #include "base/bind.h"
18 #include "base/path_service.h"
19 #include "base/prefs/pref_registry_simple.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/user_prefs/user_prefs.h"
31 #include "components/visitedlink/browser/visitedlink_master.h"
32 #include "content/public/browser/browser_thread.h"
33 #include "content/public/browser/ssl_host_state_delegate.h"
34 #include "content/public/browser/storage_partition.h"
35 #include "content/public/browser/web_contents.h"
36 #include "net/cookies/cookie_store.h"
37 #include "net/proxy/proxy_config_service_android.h"
38 #include "net/proxy/proxy_service.h"
41 using content::BrowserThread
;
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 data_reduction_proxy::DataReductionProxySettings
* proxy_settings
=
121 context
->GetDataReductionProxySettings();
122 if (proxy_settings
== NULL
)
124 // At this point, context->PreMainMessageLoopRun() has run, so
125 // context->data_reduction_proxy_io_data() is valid.
126 DCHECK(context
->GetDataReductionProxyIOData());
127 context
->CreateDataReductionProxyStatisticsIfNecessary();
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);
139 const FilePath fallback_cache_dir
=
140 GetPath().Append(FILE_PATH_LITERAL("Cache"));
141 if (PathService::Get(base::DIR_CACHE
, &cache_path
)) {
142 cache_path
= cache_path
.Append(
143 FILE_PATH_LITERAL("org.chromium.android_webview"));
144 // Delay the legacy dir removal to not impact startup performance.
145 BrowserThread::PostDelayedTask(
146 BrowserThread::FILE, FROM_HERE
,
147 base::Bind(&DeleteDirRecursively
, fallback_cache_dir
),
148 base::TimeDelta::FromMilliseconds(legacy_cache_removal_delay_ms_
));
150 cache_path
= fallback_cache_dir
;
151 LOG(WARNING
) << "Failed to get cache directory for Android WebView. "
152 << "Using app data directory as a fallback.";
154 url_request_context_getter_
=
155 new AwURLRequestContextGetter(
156 cache_path
, cookie_store_
.get(),
157 make_scoped_ptr(CreateProxyConfigService()).Pass());
159 data_reduction_proxy_io_data_
.reset(
160 new data_reduction_proxy::DataReductionProxyIOData(
161 data_reduction_proxy::Client::WEBVIEW_ANDROID
,
162 data_reduction_proxy::DataReductionProxyParams::kAllowed
,
163 url_request_context_getter_
->GetNetLog(),
164 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
),
165 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI
),
167 false /* enable_quic */,
169 data_reduction_proxy_settings_
.reset(
170 new data_reduction_proxy::DataReductionProxySettings());
171 scoped_ptr
<data_reduction_proxy::DataStore
> store(
172 new data_reduction_proxy::DataStore());
173 base::SequencedWorkerPool
* pool
= BrowserThread::GetBlockingPool();
174 scoped_refptr
<base::SequencedTaskRunner
> db_task_runner
=
175 pool
->GetSequencedTaskRunnerWithShutdownBehavior(
176 pool
->GetSequenceToken(),
177 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN
);
178 data_reduction_proxy_service_
.reset(
179 new data_reduction_proxy::DataReductionProxyService(
180 data_reduction_proxy_settings_
.get(), nullptr,
181 GetAwURLRequestContext(), store
.Pass(),
182 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI
),
183 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
),
184 db_task_runner
, base::TimeDelta()));
185 data_reduction_proxy_io_data_
->SetDataReductionProxyService(
186 data_reduction_proxy_service_
->GetWeakPtr());
188 visitedlink_master_
.reset(
189 new visitedlink::VisitedLinkMaster(this, this, false));
190 visitedlink_master_
->Init();
192 form_database_service_
.reset(
193 new AwFormDatabaseService(context_storage_path_
));
196 void AwBrowserContext::AddVisitedURLs(const std::vector
<GURL
>& urls
) {
197 DCHECK(visitedlink_master_
);
198 visitedlink_master_
->AddURLs(urls
);
201 net::URLRequestContextGetter
* AwBrowserContext::CreateRequestContext(
202 content::ProtocolHandlerMap
* protocol_handlers
,
203 content::URLRequestInterceptorScopedVector request_interceptors
) {
204 // This function cannot actually create the request context because
205 // there is a reentrant dependency on GetResourceContext() via
206 // content::StoragePartitionImplMap::Create(). This is not fixable
207 // until http://crbug.com/159193. Until then, assert that the context
208 // has already been allocated and just handle setting the protocol_handlers.
209 DCHECK(url_request_context_getter_
.get());
210 url_request_context_getter_
->SetHandlersAndInterceptors(
211 protocol_handlers
, request_interceptors
.Pass());
212 return url_request_context_getter_
.get();
215 net::URLRequestContextGetter
*
216 AwBrowserContext::CreateRequestContextForStoragePartition(
217 const base::FilePath
& partition_path
,
219 content::ProtocolHandlerMap
* protocol_handlers
,
220 content::URLRequestInterceptorScopedVector request_interceptors
) {
225 AwQuotaManagerBridge
* AwBrowserContext::GetQuotaManagerBridge() {
226 if (!quota_manager_bridge_
.get()) {
227 quota_manager_bridge_
= native_factory_
->CreateAwQuotaManagerBridge(this);
229 return quota_manager_bridge_
.get();
232 AwFormDatabaseService
* AwBrowserContext::GetFormDatabaseService() {
233 return form_database_service_
.get();
236 data_reduction_proxy::DataReductionProxySettings
*
237 AwBrowserContext::GetDataReductionProxySettings() {
238 return data_reduction_proxy_settings_
.get();
241 data_reduction_proxy::DataReductionProxyIOData
*
242 AwBrowserContext::GetDataReductionProxyIOData() {
243 return data_reduction_proxy_io_data_
.get();
246 AwURLRequestContextGetter
* AwBrowserContext::GetAwURLRequestContext() {
247 return url_request_context_getter_
.get();
250 AwMessagePortService
* AwBrowserContext::GetMessagePortService() {
251 if (!message_port_service_
.get()) {
252 message_port_service_
.reset(
253 native_factory_
->CreateAwMessagePortService());
255 return message_port_service_
.get();
258 // Create user pref service for autofill functionality.
259 void AwBrowserContext::CreateUserPrefServiceIfNecessary() {
260 if (user_pref_service_
)
263 PrefRegistrySimple
* pref_registry
= new PrefRegistrySimple();
264 // We only use the autocomplete feature of the Autofill, which is
265 // controlled via the manager_delegate. We don't use the rest
266 // of autofill, which is why it is hardcoded as disabled here.
267 pref_registry
->RegisterBooleanPref(
268 autofill::prefs::kAutofillEnabled
, false);
269 pref_registry
->RegisterDoublePref(
270 autofill::prefs::kAutofillPositiveUploadRate
, 0.0);
271 pref_registry
->RegisterDoublePref(
272 autofill::prefs::kAutofillNegativeUploadRate
, 0.0);
273 data_reduction_proxy::RegisterSimpleProfilePrefs(pref_registry
);
275 base::PrefServiceFactory pref_service_factory
;
276 pref_service_factory
.set_user_prefs(make_scoped_refptr(new AwPrefStore()));
277 pref_service_factory
.set_read_error_callback(base::Bind(&HandleReadError
));
278 user_pref_service_
= pref_service_factory
.Create(pref_registry
).Pass();
280 user_prefs::UserPrefs::Set(this, user_pref_service_
.get());
282 if (data_reduction_proxy_settings_
) {
283 data_reduction_proxy_settings_
->InitDataReductionProxySettings(
284 user_pref_service_
.get(), data_reduction_proxy_io_data_
.get(),
285 data_reduction_proxy_service_
.Pass());
286 data_reduction_proxy_settings_
->MaybeActivateDataReductionProxy(true);
288 SetDataReductionProxyEnabled(data_reduction_proxy_enabled_
);
292 scoped_ptr
<content::ZoomLevelDelegate
>
293 AwBrowserContext::CreateZoomLevelDelegate(
294 const base::FilePath
& partition_path
) {
298 base::FilePath
AwBrowserContext::GetPath() const {
299 return context_storage_path_
;
302 bool AwBrowserContext::IsOffTheRecord() const {
303 // Android WebView does not support off the record profile yet.
307 net::URLRequestContextGetter
* AwBrowserContext::GetRequestContext() {
308 return GetDefaultStoragePartition(this)->GetURLRequestContext();
311 net::URLRequestContextGetter
*
312 AwBrowserContext::GetRequestContextForRenderProcess(
313 int renderer_child_id
) {
314 return GetRequestContext();
317 net::URLRequestContextGetter
* AwBrowserContext::GetMediaRequestContext() {
318 return GetRequestContext();
321 net::URLRequestContextGetter
*
322 AwBrowserContext::GetMediaRequestContextForRenderProcess(
323 int renderer_child_id
) {
324 return GetRequestContext();
327 net::URLRequestContextGetter
*
328 AwBrowserContext::GetMediaRequestContextForStoragePartition(
329 const base::FilePath
& partition_path
,
335 content::ResourceContext
* AwBrowserContext::GetResourceContext() {
336 if (!resource_context_
) {
337 resource_context_
.reset(
338 new AwResourceContext(url_request_context_getter_
.get()));
340 return resource_context_
.get();
343 content::DownloadManagerDelegate
*
344 AwBrowserContext::GetDownloadManagerDelegate() {
345 return &download_manager_delegate_
;
348 content::BrowserPluginGuestManager
* AwBrowserContext::GetGuestManager() {
352 storage::SpecialStoragePolicy
* AwBrowserContext::GetSpecialStoragePolicy() {
353 // Intentionally returning NULL as 'Extensions' and 'Apps' not supported.
357 content::PushMessagingService
* AwBrowserContext::GetPushMessagingService() {
358 // TODO(johnme): Support push messaging in WebView.
362 content::SSLHostStateDelegate
* AwBrowserContext::GetSSLHostStateDelegate() {
363 if (!ssl_host_state_delegate_
.get()) {
364 ssl_host_state_delegate_
.reset(new AwSSLHostStateDelegate());
366 return ssl_host_state_delegate_
.get();
369 content::PermissionManager
* AwBrowserContext::GetPermissionManager() {
370 if (!permission_manager_
.get())
371 permission_manager_
.reset(new AwPermissionManager());
372 return permission_manager_
.get();
375 void AwBrowserContext::RebuildTable(
376 const scoped_refptr
<URLEnumerator
>& enumerator
) {
377 // Android WebView rebuilds from WebChromeClient.getVisitedHistory. The client
378 // can change in the lifetime of this WebView and may not yet be set here.
379 // Therefore this initialization path is not used.
380 enumerator
->OnComplete(true);
383 void AwBrowserContext::CreateDataReductionProxyStatisticsIfNecessary() {
384 DCHECK(user_pref_service_
.get());
385 DCHECK(GetDataReductionProxySettings());
386 data_reduction_proxy::DataReductionProxyService
*
387 data_reduction_proxy_service
=
388 GetDataReductionProxySettings()->data_reduction_proxy_service();
389 DCHECK(data_reduction_proxy_service
);
390 if (data_reduction_proxy_service
->compression_stats())
392 // We don't care about commit_delay for now. It is just a dummy value.
393 base::TimeDelta commit_delay
= base::TimeDelta::FromMinutes(60);
394 data_reduction_proxy_service
->EnableCompressionStatisticsLogging(
395 user_pref_service_
.get(),
396 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI
),
400 } // namespace android_webview