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_io_data.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_service.h"
24 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.h"
25 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_statistics_prefs.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
;
40 namespace android_webview
{
44 // Shows notifications which correspond to PersistentPrefStore's reading errors.
45 void HandleReadError(PersistentPrefStore::PrefReadError error
) {
48 void DeleteDirRecursively(const base::FilePath
& path
) {
49 if (!base::DeleteFile(path
, true)) {
50 // Deleting a non-existent file is considered successful, so this will
51 // trigger only in case of real errors.
52 LOG(WARNING
) << "Failed to delete " << path
.AsUTF8Unsafe();
56 AwBrowserContext
* g_browser_context
= NULL
;
58 net::ProxyConfigService
* CreateProxyConfigService() {
59 net::ProxyConfigServiceAndroid
* config_service
=
60 static_cast<net::ProxyConfigServiceAndroid
*>(
61 net::ProxyService::CreateSystemProxyConfigService(
62 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
),
63 nullptr /* Ignored on Android */ ));
64 config_service
->set_exclude_pac_url(true);
65 return config_service
;
70 // Data reduction proxy is disabled by default.
71 bool AwBrowserContext::data_reduction_proxy_enabled_
= false;
73 // Delete the legacy cache dir (in the app data dir) in 10 seconds after init.
74 int AwBrowserContext::legacy_cache_removal_delay_ms_
= 10000;
76 AwBrowserContext::AwBrowserContext(
78 JniDependencyFactory
* native_factory
)
79 : context_storage_path_(path
),
80 native_factory_(native_factory
) {
81 DCHECK(!g_browser_context
);
82 g_browser_context
= this;
84 // This constructor is entered during the creation of ContentBrowserClient,
85 // before browser threads are created. Therefore any checks to enforce
86 // threading (such as BrowserThread::CurrentlyOn()) will fail here.
89 AwBrowserContext::~AwBrowserContext() {
90 DCHECK_EQ(this, g_browser_context
);
91 g_browser_context
= NULL
;
95 AwBrowserContext
* AwBrowserContext::GetDefault() {
96 // TODO(joth): rather than store in a global here, lookup this instance
97 // from the Java-side peer.
98 return g_browser_context
;
102 AwBrowserContext
* AwBrowserContext::FromWebContents(
103 content::WebContents
* web_contents
) {
104 // This is safe; this is the only implementation of the browser context.
105 return static_cast<AwBrowserContext
*>(web_contents
->GetBrowserContext());
109 void AwBrowserContext::SetDataReductionProxyEnabled(bool enabled
) {
110 // Cache the setting value. It is possible that data reduction proxy is
112 data_reduction_proxy_enabled_
= enabled
;
113 AwBrowserContext
* context
= AwBrowserContext::GetDefault();
114 // Can't enable Data reduction proxy if user pref service is not ready.
115 if (context
== NULL
|| context
->user_pref_service_
.get() == NULL
)
117 data_reduction_proxy::DataReductionProxySettings
* proxy_settings
=
118 context
->GetDataReductionProxySettings();
119 if (proxy_settings
== NULL
)
121 // At this point, context->PreMainMessageLoopRun() has run, so
122 // context->data_reduction_proxy_io_data() is valid.
123 DCHECK(context
->GetDataReductionProxyIOData());
124 context
->CreateDataReductionProxyStatisticsIfNecessary();
125 proxy_settings
->SetDataReductionProxyEnabled(data_reduction_proxy_enabled_
);
129 void AwBrowserContext::SetLegacyCacheRemovalDelayForTest(int delay_ms
) {
130 legacy_cache_removal_delay_ms_
= delay_ms
;
133 void AwBrowserContext::PreMainMessageLoopRun() {
134 cookie_store_
= CreateCookieStore(this);
136 const FilePath fallback_cache_dir
=
137 GetPath().Append(FILE_PATH_LITERAL("Cache"));
138 if (PathService::Get(base::DIR_CACHE
, &cache_path
)) {
139 cache_path
= cache_path
.Append(
140 FILE_PATH_LITERAL("org.chromium.android_webview"));
141 // Delay the legacy dir removal to not impact startup performance.
142 BrowserThread::PostDelayedTask(
143 BrowserThread::FILE, FROM_HERE
,
144 base::Bind(&DeleteDirRecursively
, fallback_cache_dir
),
145 base::TimeDelta::FromMilliseconds(legacy_cache_removal_delay_ms_
));
147 cache_path
= fallback_cache_dir
;
148 LOG(WARNING
) << "Failed to get cache directory for Android WebView. "
149 << "Using app data directory as a fallback.";
151 url_request_context_getter_
=
152 new AwURLRequestContextGetter(
153 cache_path
, cookie_store_
.get(),
154 make_scoped_ptr(CreateProxyConfigService()).Pass());
156 data_reduction_proxy_io_data_
.reset(
157 new data_reduction_proxy::DataReductionProxyIOData(
158 data_reduction_proxy::Client::WEBVIEW_ANDROID
,
159 data_reduction_proxy::DataReductionProxyParams::kAllowed
,
160 url_request_context_getter_
->GetNetLog(),
161 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
),
162 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI
),
163 false /* enable_quic */));
164 data_reduction_proxy_settings_
.reset(
165 new data_reduction_proxy::DataReductionProxySettings());
166 data_reduction_proxy_service_
.reset(
167 new data_reduction_proxy::DataReductionProxyService(
168 scoped_ptr
<data_reduction_proxy::DataReductionProxyStatisticsPrefs
>(),
169 data_reduction_proxy_settings_
.get(), GetAwURLRequestContext()));
170 data_reduction_proxy_io_data_
->SetDataReductionProxyService(
171 data_reduction_proxy_service_
->GetWeakPtr());
173 visitedlink_master_
.reset(
174 new visitedlink::VisitedLinkMaster(this, this, false));
175 visitedlink_master_
->Init();
177 form_database_service_
.reset(
178 new AwFormDatabaseService(context_storage_path_
));
181 void AwBrowserContext::AddVisitedURLs(const std::vector
<GURL
>& urls
) {
182 DCHECK(visitedlink_master_
);
183 visitedlink_master_
->AddURLs(urls
);
186 net::URLRequestContextGetter
* AwBrowserContext::CreateRequestContext(
187 content::ProtocolHandlerMap
* protocol_handlers
,
188 content::URLRequestInterceptorScopedVector request_interceptors
) {
189 // This function cannot actually create the request context because
190 // there is a reentrant dependency on GetResourceContext() via
191 // content::StoragePartitionImplMap::Create(). This is not fixable
192 // until http://crbug.com/159193. Until then, assert that the context
193 // has already been allocated and just handle setting the protocol_handlers.
194 DCHECK(url_request_context_getter_
.get());
195 url_request_context_getter_
->SetHandlersAndInterceptors(
196 protocol_handlers
, request_interceptors
.Pass());
197 return url_request_context_getter_
.get();
200 net::URLRequestContextGetter
*
201 AwBrowserContext::CreateRequestContextForStoragePartition(
202 const base::FilePath
& partition_path
,
204 content::ProtocolHandlerMap
* protocol_handlers
,
205 content::URLRequestInterceptorScopedVector request_interceptors
) {
210 AwQuotaManagerBridge
* AwBrowserContext::GetQuotaManagerBridge() {
211 if (!quota_manager_bridge_
.get()) {
212 quota_manager_bridge_
= native_factory_
->CreateAwQuotaManagerBridge(this);
214 return quota_manager_bridge_
.get();
217 AwFormDatabaseService
* AwBrowserContext::GetFormDatabaseService() {
218 return form_database_service_
.get();
221 data_reduction_proxy::DataReductionProxySettings
*
222 AwBrowserContext::GetDataReductionProxySettings() {
223 return data_reduction_proxy_settings_
.get();
226 data_reduction_proxy::DataReductionProxyIOData
*
227 AwBrowserContext::GetDataReductionProxyIOData() {
228 return data_reduction_proxy_io_data_
.get();
231 AwURLRequestContextGetter
* AwBrowserContext::GetAwURLRequestContext() {
232 return url_request_context_getter_
.get();
235 AwMessagePortService
* AwBrowserContext::GetMessagePortService() {
236 if (!message_port_service_
.get()) {
237 message_port_service_
.reset(
238 native_factory_
->CreateAwMessagePortService());
240 return message_port_service_
.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_
) {
268 data_reduction_proxy_settings_
->InitDataReductionProxySettings(
269 user_pref_service_
.get(), data_reduction_proxy_io_data_
.get(),
270 data_reduction_proxy_service_
.Pass());
271 data_reduction_proxy_settings_
->MaybeActivateDataReductionProxy(true);
273 SetDataReductionProxyEnabled(data_reduction_proxy_enabled_
);
277 scoped_ptr
<content::ZoomLevelDelegate
>
278 AwBrowserContext::CreateZoomLevelDelegate(
279 const base::FilePath
& partition_path
) {
283 base::FilePath
AwBrowserContext::GetPath() const {
284 return context_storage_path_
;
287 bool AwBrowserContext::IsOffTheRecord() const {
288 // Android WebView does not support off the record profile yet.
292 net::URLRequestContextGetter
* AwBrowserContext::GetRequestContext() {
293 return GetDefaultStoragePartition(this)->GetURLRequestContext();
296 net::URLRequestContextGetter
*
297 AwBrowserContext::GetRequestContextForRenderProcess(
298 int renderer_child_id
) {
299 return GetRequestContext();
302 net::URLRequestContextGetter
* AwBrowserContext::GetMediaRequestContext() {
303 return GetRequestContext();
306 net::URLRequestContextGetter
*
307 AwBrowserContext::GetMediaRequestContextForRenderProcess(
308 int renderer_child_id
) {
309 return GetRequestContext();
312 net::URLRequestContextGetter
*
313 AwBrowserContext::GetMediaRequestContextForStoragePartition(
314 const base::FilePath
& partition_path
,
320 content::ResourceContext
* AwBrowserContext::GetResourceContext() {
321 if (!resource_context_
) {
322 resource_context_
.reset(
323 new AwResourceContext(url_request_context_getter_
.get()));
325 return resource_context_
.get();
328 content::DownloadManagerDelegate
*
329 AwBrowserContext::GetDownloadManagerDelegate() {
330 return &download_manager_delegate_
;
333 content::BrowserPluginGuestManager
* AwBrowserContext::GetGuestManager() {
337 storage::SpecialStoragePolicy
* AwBrowserContext::GetSpecialStoragePolicy() {
338 // Intentionally returning NULL as 'Extensions' and 'Apps' not supported.
342 content::PushMessagingService
* AwBrowserContext::GetPushMessagingService() {
343 // TODO(johnme): Support push messaging in WebView.
347 content::SSLHostStateDelegate
* AwBrowserContext::GetSSLHostStateDelegate() {
348 if (!ssl_host_state_delegate_
.get()) {
349 ssl_host_state_delegate_
.reset(new AwSSLHostStateDelegate());
351 return ssl_host_state_delegate_
.get();
354 void AwBrowserContext::RebuildTable(
355 const scoped_refptr
<URLEnumerator
>& enumerator
) {
356 // Android WebView rebuilds from WebChromeClient.getVisitedHistory. The client
357 // can change in the lifetime of this WebView and may not yet be set here.
358 // Therefore this initialization path is not used.
359 enumerator
->OnComplete(true);
362 void AwBrowserContext::CreateDataReductionProxyStatisticsIfNecessary() {
363 DCHECK(user_pref_service_
.get());
364 DCHECK(GetDataReductionProxySettings());
365 data_reduction_proxy::DataReductionProxyService
*
366 data_reduction_proxy_service
=
367 GetDataReductionProxySettings()->data_reduction_proxy_service();
368 DCHECK(data_reduction_proxy_service
);
369 if (data_reduction_proxy_service
->statistics_prefs())
371 // We don't care about commit_delay for now. It is just a dummy value.
372 base::TimeDelta commit_delay
= base::TimeDelta::FromMinutes(60);
373 data_reduction_proxy_service
->EnableCompressionStatisticsLogging(
374 user_pref_service_
.get(),
375 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI
),
379 } // namespace android_webview