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/common/data_reduction_proxy_params.h"
29 #include "components/user_prefs/user_prefs.h"
30 #include "components/visitedlink/browser/visitedlink_master.h"
31 #include "content/public/browser/browser_thread.h"
32 #include "content/public/browser/ssl_host_state_delegate.h"
33 #include "content/public/browser/storage_partition.h"
34 #include "content/public/browser/web_contents.h"
35 #include "net/cookies/cookie_store.h"
36 #include "net/proxy/proxy_config_service_android.h"
37 #include "net/proxy/proxy_service.h"
40 using content::BrowserThread
;
42 namespace android_webview
{
46 // Shows notifications which correspond to PersistentPrefStore's reading errors.
47 void HandleReadError(PersistentPrefStore::PrefReadError error
) {
50 void DeleteDirRecursively(const base::FilePath
& path
) {
51 if (!base::DeleteFile(path
, true)) {
52 // Deleting a non-existent file is considered successful, so this will
53 // trigger only in case of real errors.
54 LOG(WARNING
) << "Failed to delete " << path
.AsUTF8Unsafe();
58 AwBrowserContext
* g_browser_context
= NULL
;
60 net::ProxyConfigService
* CreateProxyConfigService() {
61 net::ProxyConfigServiceAndroid
* config_service
=
62 static_cast<net::ProxyConfigServiceAndroid
*>(
63 net::ProxyService::CreateSystemProxyConfigService(
64 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
),
65 nullptr /* Ignored on Android */ ));
66 config_service
->set_exclude_pac_url(true);
67 return config_service
;
72 // Data reduction proxy is disabled by default.
73 bool AwBrowserContext::data_reduction_proxy_enabled_
= false;
75 // Delete the legacy cache dir (in the app data dir) in 10 seconds after init.
76 int AwBrowserContext::legacy_cache_removal_delay_ms_
= 10000;
78 AwBrowserContext::AwBrowserContext(
80 JniDependencyFactory
* native_factory
)
81 : context_storage_path_(path
),
82 native_factory_(native_factory
) {
83 DCHECK(!g_browser_context
);
84 g_browser_context
= this;
86 // This constructor is entered during the creation of ContentBrowserClient,
87 // before browser threads are created. Therefore any checks to enforce
88 // threading (such as BrowserThread::CurrentlyOn()) will fail here.
91 AwBrowserContext::~AwBrowserContext() {
92 DCHECK_EQ(this, g_browser_context
);
93 g_browser_context
= NULL
;
97 AwBrowserContext
* AwBrowserContext::GetDefault() {
98 // TODO(joth): rather than store in a global here, lookup this instance
99 // from the Java-side peer.
100 return g_browser_context
;
104 AwBrowserContext
* AwBrowserContext::FromWebContents(
105 content::WebContents
* web_contents
) {
106 // This is safe; this is the only implementation of the browser context.
107 return static_cast<AwBrowserContext
*>(web_contents
->GetBrowserContext());
111 void AwBrowserContext::SetDataReductionProxyEnabled(bool enabled
) {
112 // Cache the setting value. It is possible that data reduction proxy is
114 data_reduction_proxy_enabled_
= enabled
;
115 AwBrowserContext
* context
= AwBrowserContext::GetDefault();
116 // Can't enable Data reduction proxy if user pref service is not ready.
117 if (context
== NULL
|| context
->user_pref_service_
.get() == NULL
)
119 data_reduction_proxy::DataReductionProxySettings
* proxy_settings
=
120 context
->GetDataReductionProxySettings();
121 if (proxy_settings
== NULL
)
123 // At this point, context->PreMainMessageLoopRun() has run, so
124 // context->data_reduction_proxy_io_data() is valid.
125 DCHECK(context
->GetDataReductionProxyIOData());
126 context
->CreateDataReductionProxyStatisticsIfNecessary();
127 proxy_settings
->SetDataReductionProxyEnabled(data_reduction_proxy_enabled_
);
131 void AwBrowserContext::SetLegacyCacheRemovalDelayForTest(int delay_ms
) {
132 legacy_cache_removal_delay_ms_
= delay_ms
;
135 void AwBrowserContext::PreMainMessageLoopRun() {
136 cookie_store_
= CreateCookieStore(this);
138 const FilePath fallback_cache_dir
=
139 GetPath().Append(FILE_PATH_LITERAL("Cache"));
140 if (PathService::Get(base::DIR_CACHE
, &cache_path
)) {
141 cache_path
= cache_path
.Append(
142 FILE_PATH_LITERAL("org.chromium.android_webview"));
143 // Delay the legacy dir removal to not impact startup performance.
144 BrowserThread::PostDelayedTask(
145 BrowserThread::FILE, FROM_HERE
,
146 base::Bind(&DeleteDirRecursively
, fallback_cache_dir
),
147 base::TimeDelta::FromMilliseconds(legacy_cache_removal_delay_ms_
));
149 cache_path
= fallback_cache_dir
;
150 LOG(WARNING
) << "Failed to get cache directory for Android WebView. "
151 << "Using app data directory as a fallback.";
153 url_request_context_getter_
=
154 new AwURLRequestContextGetter(
155 cache_path
, cookie_store_
.get(),
156 make_scoped_ptr(CreateProxyConfigService()).Pass());
158 data_reduction_proxy_io_data_
.reset(
159 new data_reduction_proxy::DataReductionProxyIOData(
160 data_reduction_proxy::Client::WEBVIEW_ANDROID
,
161 data_reduction_proxy::DataReductionProxyParams::kAllowed
,
162 url_request_context_getter_
->GetNetLog(),
163 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
),
164 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI
),
165 false /* enable_quic */,
167 data_reduction_proxy_settings_
.reset(
168 new data_reduction_proxy::DataReductionProxySettings());
169 data_reduction_proxy_service_
.reset(
170 new data_reduction_proxy::DataReductionProxyService(
172 data_reduction_proxy::DataReductionProxyCompressionStats
>(),
173 data_reduction_proxy_settings_
.get(), GetAwURLRequestContext(),
174 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
)));
175 data_reduction_proxy_io_data_
->SetDataReductionProxyService(
176 data_reduction_proxy_service_
->GetWeakPtr());
178 visitedlink_master_
.reset(
179 new visitedlink::VisitedLinkMaster(this, this, false));
180 visitedlink_master_
->Init();
182 form_database_service_
.reset(
183 new AwFormDatabaseService(context_storage_path_
));
186 void AwBrowserContext::AddVisitedURLs(const std::vector
<GURL
>& urls
) {
187 DCHECK(visitedlink_master_
);
188 visitedlink_master_
->AddURLs(urls
);
191 net::URLRequestContextGetter
* AwBrowserContext::CreateRequestContext(
192 content::ProtocolHandlerMap
* protocol_handlers
,
193 content::URLRequestInterceptorScopedVector request_interceptors
) {
194 // This function cannot actually create the request context because
195 // there is a reentrant dependency on GetResourceContext() via
196 // content::StoragePartitionImplMap::Create(). This is not fixable
197 // until http://crbug.com/159193. Until then, assert that the context
198 // has already been allocated and just handle setting the protocol_handlers.
199 DCHECK(url_request_context_getter_
.get());
200 url_request_context_getter_
->SetHandlersAndInterceptors(
201 protocol_handlers
, request_interceptors
.Pass());
202 return url_request_context_getter_
.get();
205 net::URLRequestContextGetter
*
206 AwBrowserContext::CreateRequestContextForStoragePartition(
207 const base::FilePath
& partition_path
,
209 content::ProtocolHandlerMap
* protocol_handlers
,
210 content::URLRequestInterceptorScopedVector request_interceptors
) {
215 AwQuotaManagerBridge
* AwBrowserContext::GetQuotaManagerBridge() {
216 if (!quota_manager_bridge_
.get()) {
217 quota_manager_bridge_
= native_factory_
->CreateAwQuotaManagerBridge(this);
219 return quota_manager_bridge_
.get();
222 AwFormDatabaseService
* AwBrowserContext::GetFormDatabaseService() {
223 return form_database_service_
.get();
226 data_reduction_proxy::DataReductionProxySettings
*
227 AwBrowserContext::GetDataReductionProxySettings() {
228 return data_reduction_proxy_settings_
.get();
231 data_reduction_proxy::DataReductionProxyIOData
*
232 AwBrowserContext::GetDataReductionProxyIOData() {
233 return data_reduction_proxy_io_data_
.get();
236 AwURLRequestContextGetter
* AwBrowserContext::GetAwURLRequestContext() {
237 return url_request_context_getter_
.get();
240 AwMessagePortService
* AwBrowserContext::GetMessagePortService() {
241 if (!message_port_service_
.get()) {
242 message_port_service_
.reset(
243 native_factory_
->CreateAwMessagePortService());
245 return message_port_service_
.get();
248 // Create user pref service for autofill functionality.
249 void AwBrowserContext::CreateUserPrefServiceIfNecessary() {
250 if (user_pref_service_
)
253 PrefRegistrySimple
* pref_registry
= new PrefRegistrySimple();
254 // We only use the autocomplete feature of the Autofill, which is
255 // controlled via the manager_delegate. We don't use the rest
256 // of autofill, which is why it is hardcoded as disabled here.
257 pref_registry
->RegisterBooleanPref(
258 autofill::prefs::kAutofillEnabled
, false);
259 pref_registry
->RegisterDoublePref(
260 autofill::prefs::kAutofillPositiveUploadRate
, 0.0);
261 pref_registry
->RegisterDoublePref(
262 autofill::prefs::kAutofillNegativeUploadRate
, 0.0);
263 data_reduction_proxy::RegisterSimpleProfilePrefs(pref_registry
);
265 base::PrefServiceFactory pref_service_factory
;
266 pref_service_factory
.set_user_prefs(make_scoped_refptr(new AwPrefStore()));
267 pref_service_factory
.set_read_error_callback(base::Bind(&HandleReadError
));
268 user_pref_service_
= pref_service_factory
.Create(pref_registry
).Pass();
270 user_prefs::UserPrefs::Set(this, user_pref_service_
.get());
272 if (data_reduction_proxy_settings_
) {
273 data_reduction_proxy_settings_
->InitDataReductionProxySettings(
274 user_pref_service_
.get(), data_reduction_proxy_io_data_
.get(),
275 data_reduction_proxy_service_
.Pass());
276 data_reduction_proxy_settings_
->MaybeActivateDataReductionProxy(true);
278 SetDataReductionProxyEnabled(data_reduction_proxy_enabled_
);
282 scoped_ptr
<content::ZoomLevelDelegate
>
283 AwBrowserContext::CreateZoomLevelDelegate(
284 const base::FilePath
& partition_path
) {
288 base::FilePath
AwBrowserContext::GetPath() const {
289 return context_storage_path_
;
292 bool AwBrowserContext::IsOffTheRecord() const {
293 // Android WebView does not support off the record profile yet.
297 net::URLRequestContextGetter
* AwBrowserContext::GetRequestContext() {
298 return GetDefaultStoragePartition(this)->GetURLRequestContext();
301 net::URLRequestContextGetter
*
302 AwBrowserContext::GetRequestContextForRenderProcess(
303 int renderer_child_id
) {
304 return GetRequestContext();
307 net::URLRequestContextGetter
* AwBrowserContext::GetMediaRequestContext() {
308 return GetRequestContext();
311 net::URLRequestContextGetter
*
312 AwBrowserContext::GetMediaRequestContextForRenderProcess(
313 int renderer_child_id
) {
314 return GetRequestContext();
317 net::URLRequestContextGetter
*
318 AwBrowserContext::GetMediaRequestContextForStoragePartition(
319 const base::FilePath
& partition_path
,
325 content::ResourceContext
* AwBrowserContext::GetResourceContext() {
326 if (!resource_context_
) {
327 resource_context_
.reset(
328 new AwResourceContext(url_request_context_getter_
.get()));
330 return resource_context_
.get();
333 content::DownloadManagerDelegate
*
334 AwBrowserContext::GetDownloadManagerDelegate() {
335 return &download_manager_delegate_
;
338 content::BrowserPluginGuestManager
* AwBrowserContext::GetGuestManager() {
342 storage::SpecialStoragePolicy
* AwBrowserContext::GetSpecialStoragePolicy() {
343 // Intentionally returning NULL as 'Extensions' and 'Apps' not supported.
347 content::PushMessagingService
* AwBrowserContext::GetPushMessagingService() {
348 // TODO(johnme): Support push messaging in WebView.
352 content::SSLHostStateDelegate
* AwBrowserContext::GetSSLHostStateDelegate() {
353 if (!ssl_host_state_delegate_
.get()) {
354 ssl_host_state_delegate_
.reset(new AwSSLHostStateDelegate());
356 return ssl_host_state_delegate_
.get();
359 content::PermissionManager
* AwBrowserContext::GetPermissionManager() {
360 if (!permission_manager_
.get())
361 permission_manager_
.reset(new AwPermissionManager());
362 return permission_manager_
.get();
365 void AwBrowserContext::RebuildTable(
366 const scoped_refptr
<URLEnumerator
>& enumerator
) {
367 // Android WebView rebuilds from WebChromeClient.getVisitedHistory. The client
368 // can change in the lifetime of this WebView and may not yet be set here.
369 // Therefore this initialization path is not used.
370 enumerator
->OnComplete(true);
373 void AwBrowserContext::CreateDataReductionProxyStatisticsIfNecessary() {
374 DCHECK(user_pref_service_
.get());
375 DCHECK(GetDataReductionProxySettings());
376 data_reduction_proxy::DataReductionProxyService
*
377 data_reduction_proxy_service
=
378 GetDataReductionProxySettings()->data_reduction_proxy_service();
379 DCHECK(data_reduction_proxy_service
);
380 if (data_reduction_proxy_service
->compression_stats())
382 // We don't care about commit_delay for now. It is just a dummy value.
383 base::TimeDelta commit_delay
= base::TimeDelta::FromMinutes(60);
384 data_reduction_proxy_service
->EnableCompressionStatisticsLogging(
385 user_pref_service_
.get(),
386 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI
),
390 } // namespace android_webview