Returning scoped_ptr instead of raw pointer in SpdySessionPoolInfoToValue() in net/
[chromium-blink-merge.git] / android_webview / browser / aw_browser_context.cc
blobfbdb5cd0be07b8b649e0874b5a344b756f76489b
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"
39 using base::FilePath;
40 using content::BrowserThread;
42 namespace android_webview {
44 namespace {
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;
70 } // namespace
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(
79 const FilePath path,
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;
96 // static
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;
103 // static
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());
110 // static
111 void AwBrowserContext::SetDataReductionProxyEnabled(bool enabled) {
112 // Cache the setting value. It is possible that data reduction proxy is
113 // not created yet.
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)
118 return;
119 data_reduction_proxy::DataReductionProxySettings* proxy_settings =
120 context->GetDataReductionProxySettings();
121 if (proxy_settings == NULL)
122 return;
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_);
130 // static
131 void AwBrowserContext::SetLegacyCacheRemovalDelayForTest(int delay_ms) {
132 legacy_cache_removal_delay_ms_ = delay_ms;
135 void AwBrowserContext::PreMainMessageLoopRun() {
136 cookie_store_ = CreateCookieStore(this);
137 FilePath cache_path;
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_));
148 } else {
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 */,
166 false /* enable_quic */,
167 GetUserAgent()));
168 data_reduction_proxy_settings_.reset(
169 new data_reduction_proxy::DataReductionProxySettings());
170 data_reduction_proxy_service_.reset(
171 new data_reduction_proxy::DataReductionProxyService(
172 scoped_ptr<
173 data_reduction_proxy::DataReductionProxyCompressionStats>(),
174 data_reduction_proxy_settings_.get(), nullptr,
175 GetAwURLRequestContext(),
176 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)));
177 data_reduction_proxy_io_data_->SetDataReductionProxyService(
178 data_reduction_proxy_service_->GetWeakPtr());
180 visitedlink_master_.reset(
181 new visitedlink::VisitedLinkMaster(this, this, false));
182 visitedlink_master_->Init();
184 form_database_service_.reset(
185 new AwFormDatabaseService(context_storage_path_));
188 void AwBrowserContext::AddVisitedURLs(const std::vector<GURL>& urls) {
189 DCHECK(visitedlink_master_);
190 visitedlink_master_->AddURLs(urls);
193 net::URLRequestContextGetter* AwBrowserContext::CreateRequestContext(
194 content::ProtocolHandlerMap* protocol_handlers,
195 content::URLRequestInterceptorScopedVector request_interceptors) {
196 // This function cannot actually create the request context because
197 // there is a reentrant dependency on GetResourceContext() via
198 // content::StoragePartitionImplMap::Create(). This is not fixable
199 // until http://crbug.com/159193. Until then, assert that the context
200 // has already been allocated and just handle setting the protocol_handlers.
201 DCHECK(url_request_context_getter_.get());
202 url_request_context_getter_->SetHandlersAndInterceptors(
203 protocol_handlers, request_interceptors.Pass());
204 return url_request_context_getter_.get();
207 net::URLRequestContextGetter*
208 AwBrowserContext::CreateRequestContextForStoragePartition(
209 const base::FilePath& partition_path,
210 bool in_memory,
211 content::ProtocolHandlerMap* protocol_handlers,
212 content::URLRequestInterceptorScopedVector request_interceptors) {
213 NOTREACHED();
214 return NULL;
217 AwQuotaManagerBridge* AwBrowserContext::GetQuotaManagerBridge() {
218 if (!quota_manager_bridge_.get()) {
219 quota_manager_bridge_ = native_factory_->CreateAwQuotaManagerBridge(this);
221 return quota_manager_bridge_.get();
224 AwFormDatabaseService* AwBrowserContext::GetFormDatabaseService() {
225 return form_database_service_.get();
228 data_reduction_proxy::DataReductionProxySettings*
229 AwBrowserContext::GetDataReductionProxySettings() {
230 return data_reduction_proxy_settings_.get();
233 data_reduction_proxy::DataReductionProxyIOData*
234 AwBrowserContext::GetDataReductionProxyIOData() {
235 return data_reduction_proxy_io_data_.get();
238 AwURLRequestContextGetter* AwBrowserContext::GetAwURLRequestContext() {
239 return url_request_context_getter_.get();
242 AwMessagePortService* AwBrowserContext::GetMessagePortService() {
243 if (!message_port_service_.get()) {
244 message_port_service_.reset(
245 native_factory_->CreateAwMessagePortService());
247 return message_port_service_.get();
250 // Create user pref service for autofill functionality.
251 void AwBrowserContext::CreateUserPrefServiceIfNecessary() {
252 if (user_pref_service_)
253 return;
255 PrefRegistrySimple* pref_registry = new PrefRegistrySimple();
256 // We only use the autocomplete feature of the Autofill, which is
257 // controlled via the manager_delegate. We don't use the rest
258 // of autofill, which is why it is hardcoded as disabled here.
259 pref_registry->RegisterBooleanPref(
260 autofill::prefs::kAutofillEnabled, false);
261 pref_registry->RegisterDoublePref(
262 autofill::prefs::kAutofillPositiveUploadRate, 0.0);
263 pref_registry->RegisterDoublePref(
264 autofill::prefs::kAutofillNegativeUploadRate, 0.0);
265 data_reduction_proxy::RegisterSimpleProfilePrefs(pref_registry);
267 base::PrefServiceFactory pref_service_factory;
268 pref_service_factory.set_user_prefs(make_scoped_refptr(new AwPrefStore()));
269 pref_service_factory.set_read_error_callback(base::Bind(&HandleReadError));
270 user_pref_service_ = pref_service_factory.Create(pref_registry).Pass();
272 user_prefs::UserPrefs::Set(this, user_pref_service_.get());
274 if (data_reduction_proxy_settings_) {
275 data_reduction_proxy_settings_->InitDataReductionProxySettings(
276 user_pref_service_.get(), data_reduction_proxy_io_data_.get(),
277 data_reduction_proxy_service_.Pass());
278 data_reduction_proxy_settings_->MaybeActivateDataReductionProxy(true);
280 SetDataReductionProxyEnabled(data_reduction_proxy_enabled_);
284 scoped_ptr<content::ZoomLevelDelegate>
285 AwBrowserContext::CreateZoomLevelDelegate(
286 const base::FilePath& partition_path) {
287 return nullptr;
290 base::FilePath AwBrowserContext::GetPath() const {
291 return context_storage_path_;
294 bool AwBrowserContext::IsOffTheRecord() const {
295 // Android WebView does not support off the record profile yet.
296 return false;
299 net::URLRequestContextGetter* AwBrowserContext::GetRequestContext() {
300 return GetDefaultStoragePartition(this)->GetURLRequestContext();
303 net::URLRequestContextGetter*
304 AwBrowserContext::GetRequestContextForRenderProcess(
305 int renderer_child_id) {
306 return GetRequestContext();
309 net::URLRequestContextGetter* AwBrowserContext::GetMediaRequestContext() {
310 return GetRequestContext();
313 net::URLRequestContextGetter*
314 AwBrowserContext::GetMediaRequestContextForRenderProcess(
315 int renderer_child_id) {
316 return GetRequestContext();
319 net::URLRequestContextGetter*
320 AwBrowserContext::GetMediaRequestContextForStoragePartition(
321 const base::FilePath& partition_path,
322 bool in_memory) {
323 NOTREACHED();
324 return NULL;
327 content::ResourceContext* AwBrowserContext::GetResourceContext() {
328 if (!resource_context_) {
329 resource_context_.reset(
330 new AwResourceContext(url_request_context_getter_.get()));
332 return resource_context_.get();
335 content::DownloadManagerDelegate*
336 AwBrowserContext::GetDownloadManagerDelegate() {
337 return &download_manager_delegate_;
340 content::BrowserPluginGuestManager* AwBrowserContext::GetGuestManager() {
341 return NULL;
344 storage::SpecialStoragePolicy* AwBrowserContext::GetSpecialStoragePolicy() {
345 // Intentionally returning NULL as 'Extensions' and 'Apps' not supported.
346 return NULL;
349 content::PushMessagingService* AwBrowserContext::GetPushMessagingService() {
350 // TODO(johnme): Support push messaging in WebView.
351 return NULL;
354 content::SSLHostStateDelegate* AwBrowserContext::GetSSLHostStateDelegate() {
355 if (!ssl_host_state_delegate_.get()) {
356 ssl_host_state_delegate_.reset(new AwSSLHostStateDelegate());
358 return ssl_host_state_delegate_.get();
361 content::PermissionManager* AwBrowserContext::GetPermissionManager() {
362 if (!permission_manager_.get())
363 permission_manager_.reset(new AwPermissionManager());
364 return permission_manager_.get();
367 void AwBrowserContext::RebuildTable(
368 const scoped_refptr<URLEnumerator>& enumerator) {
369 // Android WebView rebuilds from WebChromeClient.getVisitedHistory. The client
370 // can change in the lifetime of this WebView and may not yet be set here.
371 // Therefore this initialization path is not used.
372 enumerator->OnComplete(true);
375 void AwBrowserContext::CreateDataReductionProxyStatisticsIfNecessary() {
376 DCHECK(user_pref_service_.get());
377 DCHECK(GetDataReductionProxySettings());
378 data_reduction_proxy::DataReductionProxyService*
379 data_reduction_proxy_service =
380 GetDataReductionProxySettings()->data_reduction_proxy_service();
381 DCHECK(data_reduction_proxy_service);
382 if (data_reduction_proxy_service->compression_stats())
383 return;
384 // We don't care about commit_delay for now. It is just a dummy value.
385 base::TimeDelta commit_delay = base::TimeDelta::FromMinutes(60);
386 data_reduction_proxy_service->EnableCompressionStatisticsLogging(
387 user_pref_service_.get(),
388 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
389 commit_delay);
392 } // namespace android_webview