Revert of Disable spdy tests for constant failures on Volantis bot (patchset #2 id...
[chromium-blink-merge.git] / android_webview / browser / aw_browser_context.cc
blob1fea722a29f1ffb92ef83aba90d1711f5bfe650c
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_configurator.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"
37 using base::FilePath;
38 using content::BrowserThread;
39 using data_reduction_proxy::DataReductionProxyConfigurator;
40 using data_reduction_proxy::DataReductionProxyEventStore;
41 using data_reduction_proxy::DataReductionProxySettings;
43 namespace android_webview {
45 namespace {
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;
71 } // namespace
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(
80 const FilePath path,
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;
97 // static
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;
104 // static
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());
111 // static
112 void AwBrowserContext::SetDataReductionProxyEnabled(bool enabled) {
113 // Cache the setting value. It is possible that data reduction proxy is
114 // not created yet.
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)
119 return;
120 DataReductionProxySettings* proxy_settings =
121 context->GetDataReductionProxySettings();
122 if (proxy_settings == NULL)
123 return;
125 context->CreateDataReductionProxyStatisticsIfNecessary();
126 proxy_settings->SetDataReductionProxyStatisticsPrefs(
127 context->data_reduction_proxy_statistics_.get());
128 proxy_settings->SetDataReductionProxyEnabled(data_reduction_proxy_enabled_);
131 // static
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 FilePath cache_path;
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_));
149 } else {
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_settings_.reset(
160 new DataReductionProxySettings(
161 new data_reduction_proxy::DataReductionProxyParams(
162 data_reduction_proxy::DataReductionProxyParams::kAllowed)));
163 data_reduction_proxy_event_store_.reset(
164 new DataReductionProxyEventStore(
165 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI)));
166 data_reduction_proxy_configurator_.reset(
167 new data_reduction_proxy::DataReductionProxyConfigurator(
168 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO),
169 url_request_context_getter_->GetNetLog(),
170 data_reduction_proxy_event_store_.get()));
171 data_reduction_proxy_settings_->SetProxyConfigurator(
172 data_reduction_proxy_configurator_.get());
174 visitedlink_master_.reset(
175 new visitedlink::VisitedLinkMaster(this, this, false));
176 visitedlink_master_->Init();
178 form_database_service_.reset(
179 new AwFormDatabaseService(context_storage_path_));
182 void AwBrowserContext::AddVisitedURLs(const std::vector<GURL>& urls) {
183 DCHECK(visitedlink_master_);
184 visitedlink_master_->AddURLs(urls);
187 net::URLRequestContextGetter* AwBrowserContext::CreateRequestContext(
188 content::ProtocolHandlerMap* protocol_handlers,
189 content::URLRequestInterceptorScopedVector request_interceptors) {
190 // This function cannot actually create the request context because
191 // there is a reentrant dependency on GetResourceContext() via
192 // content::StoragePartitionImplMap::Create(). This is not fixable
193 // until http://crbug.com/159193. Until then, assert that the context
194 // has already been allocated and just handle setting the protocol_handlers.
195 DCHECK(url_request_context_getter_.get());
196 url_request_context_getter_->SetHandlersAndInterceptors(
197 protocol_handlers, request_interceptors.Pass());
198 return url_request_context_getter_.get();
201 net::URLRequestContextGetter*
202 AwBrowserContext::CreateRequestContextForStoragePartition(
203 const base::FilePath& partition_path,
204 bool in_memory,
205 content::ProtocolHandlerMap* protocol_handlers,
206 content::URLRequestInterceptorScopedVector request_interceptors) {
207 NOTREACHED();
208 return NULL;
211 AwQuotaManagerBridge* AwBrowserContext::GetQuotaManagerBridge() {
212 if (!quota_manager_bridge_.get()) {
213 quota_manager_bridge_ = native_factory_->CreateAwQuotaManagerBridge(this);
215 return quota_manager_bridge_.get();
218 AwFormDatabaseService* AwBrowserContext::GetFormDatabaseService() {
219 return form_database_service_.get();
222 DataReductionProxySettings* AwBrowserContext::GetDataReductionProxySettings() {
223 return data_reduction_proxy_settings_.get();
226 DataReductionProxyEventStore*
227 AwBrowserContext::GetDataReductionProxyEventStore() {
228 return data_reduction_proxy_event_store_.get();
231 data_reduction_proxy::DataReductionProxyConfigurator*
232 AwBrowserContext::GetDataReductionProxyConfigurator() {
233 return data_reduction_proxy_configurator_.get();
236 AwURLRequestContextGetter* AwBrowserContext::GetAwURLRequestContext() {
237 return url_request_context_getter_.get();
240 // Create user pref service for autofill functionality.
241 void AwBrowserContext::CreateUserPrefServiceIfNecessary() {
242 if (user_pref_service_)
243 return;
245 PrefRegistrySimple* pref_registry = new PrefRegistrySimple();
246 // We only use the autocomplete feature of the Autofill, which is
247 // controlled via the manager_delegate. We don't use the rest
248 // of autofill, which is why it is hardcoded as disabled here.
249 pref_registry->RegisterBooleanPref(
250 autofill::prefs::kAutofillEnabled, false);
251 pref_registry->RegisterDoublePref(
252 autofill::prefs::kAutofillPositiveUploadRate, 0.0);
253 pref_registry->RegisterDoublePref(
254 autofill::prefs::kAutofillNegativeUploadRate, 0.0);
255 data_reduction_proxy::RegisterSimpleProfilePrefs(pref_registry);
257 base::PrefServiceFactory pref_service_factory;
258 pref_service_factory.set_user_prefs(make_scoped_refptr(new AwPrefStore()));
259 pref_service_factory.set_read_error_callback(base::Bind(&HandleReadError));
260 user_pref_service_ = pref_service_factory.Create(pref_registry).Pass();
262 user_prefs::UserPrefs::Set(this, user_pref_service_.get());
264 if (data_reduction_proxy_settings_.get()) {
265 data_reduction_proxy_settings_->InitDataReductionProxySettings(
266 user_pref_service_.get(),
267 GetRequestContext(),
268 GetAwURLRequestContext()->GetNetLog(),
269 GetDataReductionProxyEventStore());
270 data_reduction_proxy_settings_->MaybeActivateDataReductionProxy(true);
272 SetDataReductionProxyEnabled(data_reduction_proxy_enabled_);
276 scoped_ptr<content::ZoomLevelDelegate>
277 AwBrowserContext::CreateZoomLevelDelegate(
278 const base::FilePath& partition_path) {
279 return nullptr;
282 base::FilePath AwBrowserContext::GetPath() const {
283 return context_storage_path_;
286 bool AwBrowserContext::IsOffTheRecord() const {
287 // Android WebView does not support off the record profile yet.
288 return false;
291 net::URLRequestContextGetter* AwBrowserContext::GetRequestContext() {
292 return GetDefaultStoragePartition(this)->GetURLRequestContext();
295 net::URLRequestContextGetter*
296 AwBrowserContext::GetRequestContextForRenderProcess(
297 int renderer_child_id) {
298 return GetRequestContext();
301 net::URLRequestContextGetter* AwBrowserContext::GetMediaRequestContext() {
302 return GetRequestContext();
305 net::URLRequestContextGetter*
306 AwBrowserContext::GetMediaRequestContextForRenderProcess(
307 int renderer_child_id) {
308 return GetRequestContext();
311 net::URLRequestContextGetter*
312 AwBrowserContext::GetMediaRequestContextForStoragePartition(
313 const base::FilePath& partition_path,
314 bool in_memory) {
315 NOTREACHED();
316 return NULL;
319 content::ResourceContext* AwBrowserContext::GetResourceContext() {
320 if (!resource_context_) {
321 resource_context_.reset(
322 new AwResourceContext(url_request_context_getter_.get()));
324 return resource_context_.get();
327 content::DownloadManagerDelegate*
328 AwBrowserContext::GetDownloadManagerDelegate() {
329 return &download_manager_delegate_;
332 content::BrowserPluginGuestManager* AwBrowserContext::GetGuestManager() {
333 return NULL;
336 storage::SpecialStoragePolicy* AwBrowserContext::GetSpecialStoragePolicy() {
337 // Intentionally returning NULL as 'Extensions' and 'Apps' not supported.
338 return NULL;
341 content::PushMessagingService* AwBrowserContext::GetPushMessagingService() {
342 // TODO(johnme): Support push messaging in WebView.
343 return NULL;
346 content::SSLHostStateDelegate* AwBrowserContext::GetSSLHostStateDelegate() {
347 if (!ssl_host_state_delegate_.get()) {
348 ssl_host_state_delegate_.reset(new AwSSLHostStateDelegate());
350 return ssl_host_state_delegate_.get();
353 void AwBrowserContext::RebuildTable(
354 const scoped_refptr<URLEnumerator>& enumerator) {
355 // Android WebView rebuilds from WebChromeClient.getVisitedHistory. The client
356 // can change in the lifetime of this WebView and may not yet be set here.
357 // Therefore this initialization path is not used.
358 enumerator->OnComplete(true);
361 void AwBrowserContext::CreateDataReductionProxyStatisticsIfNecessary() {
362 DCHECK(user_pref_service_.get());
364 if (!data_reduction_proxy_statistics_.get()) {
365 // We don't care about commit_delay for now. It is just a dummy value.
366 base::TimeDelta commit_delay = base::TimeDelta::FromMinutes(60);
367 data_reduction_proxy_statistics_ =
368 scoped_ptr<data_reduction_proxy::DataReductionProxyStatisticsPrefs>(
369 new data_reduction_proxy::DataReductionProxyStatisticsPrefs(
370 user_pref_service_.get(),
371 base::MessageLoopProxy::current(),
372 commit_delay));
376 } // namespace android_webview