ProfilePolicyConnectorFactory: Refactoring from Profile to BrowserContext.
[chromium-blink-merge.git] / android_webview / browser / aw_browser_context.cc
blobd113d74e23d3dac2ebfdae2bfcf399f3ac6bad4a
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_settings.h"
24 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_params.h"
25 #include "components/user_prefs/user_prefs.h"
26 #include "components/visitedlink/browser/visitedlink_master.h"
27 #include "content/public/browser/browser_thread.h"
28 #include "content/public/browser/ssl_host_state_delegate.h"
29 #include "content/public/browser/storage_partition.h"
30 #include "content/public/browser/web_contents.h"
31 #include "net/cookies/cookie_store.h"
32 #include "net/proxy/proxy_config_service_android.h"
33 #include "net/proxy/proxy_service.h"
35 using base::FilePath;
36 using content::BrowserThread;
38 namespace data_reduction_proxy {
39 class DataReductionProxyConfigurator;
40 class DataReductionProxyStatisticsPrefs;
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 data_reduction_proxy::DataReductionProxySettings* proxy_settings =
121 context->GetDataReductionProxySettings();
122 if (proxy_settings == NULL)
123 return;
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_);
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 data_reduction_proxy::DataReductionProxySettings(
161 scoped_ptr<data_reduction_proxy::DataReductionProxyParams>(
162 new data_reduction_proxy::DataReductionProxyParams(
163 data_reduction_proxy::DataReductionProxyParams::kAllowed))
164 .Pass()));
165 data_reduction_proxy_io_data_.reset(
166 new data_reduction_proxy::DataReductionProxyIOData(
167 data_reduction_proxy::Client::WEBVIEW_ANDROID,
168 scoped_ptr<data_reduction_proxy::DataReductionProxyStatisticsPrefs>(),
169 data_reduction_proxy_settings_.get(),
170 url_request_context_getter_->GetNetLog(),
171 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO),
172 BrowserThread::GetMessageLoopProxyForThread(
173 BrowserThread::UI)));
174 data_reduction_proxy_io_data_->Init();
175 data_reduction_proxy_settings_->SetProxyConfigurator(
176 data_reduction_proxy_io_data_->configurator());
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,
208 bool in_memory,
209 content::ProtocolHandlerMap* protocol_handlers,
210 content::URLRequestInterceptorScopedVector request_interceptors) {
211 NOTREACHED();
212 return NULL;
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 data_reduction_proxy::DataReductionProxyConfigurator*
237 AwBrowserContext::GetDataReductionProxyConfigurator() {
238 return data_reduction_proxy_io_data_->configurator();
241 AwURLRequestContextGetter* AwBrowserContext::GetAwURLRequestContext() {
242 return url_request_context_getter_.get();
245 AwMessagePortService* AwBrowserContext::GetMessagePortService() {
246 if (!message_port_service_.get()) {
247 message_port_service_.reset(
248 native_factory_->CreateAwMessagePortService());
250 return message_port_service_.get();
253 // Create user pref service for autofill functionality.
254 void AwBrowserContext::CreateUserPrefServiceIfNecessary() {
255 if (user_pref_service_)
256 return;
258 PrefRegistrySimple* pref_registry = new PrefRegistrySimple();
259 // We only use the autocomplete feature of the Autofill, which is
260 // controlled via the manager_delegate. We don't use the rest
261 // of autofill, which is why it is hardcoded as disabled here.
262 pref_registry->RegisterBooleanPref(
263 autofill::prefs::kAutofillEnabled, false);
264 pref_registry->RegisterDoublePref(
265 autofill::prefs::kAutofillPositiveUploadRate, 0.0);
266 pref_registry->RegisterDoublePref(
267 autofill::prefs::kAutofillNegativeUploadRate, 0.0);
268 data_reduction_proxy::RegisterSimpleProfilePrefs(pref_registry);
270 base::PrefServiceFactory pref_service_factory;
271 pref_service_factory.set_user_prefs(make_scoped_refptr(new AwPrefStore()));
272 pref_service_factory.set_read_error_callback(base::Bind(&HandleReadError));
273 user_pref_service_ = pref_service_factory.Create(pref_registry).Pass();
275 user_prefs::UserPrefs::Set(this, user_pref_service_.get());
277 if (data_reduction_proxy_settings_) {
278 data_reduction_proxy_settings_->InitDataReductionProxySettings(
279 user_pref_service_.get(),
280 scoped_ptr<data_reduction_proxy::DataReductionProxyStatisticsPrefs>(),
281 GetRequestContext(),
282 GetAwURLRequestContext()->GetNetLog(),
283 data_reduction_proxy_io_data_->event_store());
284 data_reduction_proxy_settings_->MaybeActivateDataReductionProxy(true);
286 SetDataReductionProxyEnabled(data_reduction_proxy_enabled_);
290 scoped_ptr<content::ZoomLevelDelegate>
291 AwBrowserContext::CreateZoomLevelDelegate(
292 const base::FilePath& partition_path) {
293 return nullptr;
296 base::FilePath AwBrowserContext::GetPath() const {
297 return context_storage_path_;
300 bool AwBrowserContext::IsOffTheRecord() const {
301 // Android WebView does not support off the record profile yet.
302 return false;
305 net::URLRequestContextGetter* AwBrowserContext::GetRequestContext() {
306 return GetDefaultStoragePartition(this)->GetURLRequestContext();
309 net::URLRequestContextGetter*
310 AwBrowserContext::GetRequestContextForRenderProcess(
311 int renderer_child_id) {
312 return GetRequestContext();
315 net::URLRequestContextGetter* AwBrowserContext::GetMediaRequestContext() {
316 return GetRequestContext();
319 net::URLRequestContextGetter*
320 AwBrowserContext::GetMediaRequestContextForRenderProcess(
321 int renderer_child_id) {
322 return GetRequestContext();
325 net::URLRequestContextGetter*
326 AwBrowserContext::GetMediaRequestContextForStoragePartition(
327 const base::FilePath& partition_path,
328 bool in_memory) {
329 NOTREACHED();
330 return NULL;
333 content::ResourceContext* AwBrowserContext::GetResourceContext() {
334 if (!resource_context_) {
335 resource_context_.reset(
336 new AwResourceContext(url_request_context_getter_.get()));
338 return resource_context_.get();
341 content::DownloadManagerDelegate*
342 AwBrowserContext::GetDownloadManagerDelegate() {
343 return &download_manager_delegate_;
346 content::BrowserPluginGuestManager* AwBrowserContext::GetGuestManager() {
347 return NULL;
350 storage::SpecialStoragePolicy* AwBrowserContext::GetSpecialStoragePolicy() {
351 // Intentionally returning NULL as 'Extensions' and 'Apps' not supported.
352 return NULL;
355 content::PushMessagingService* AwBrowserContext::GetPushMessagingService() {
356 // TODO(johnme): Support push messaging in WebView.
357 return NULL;
360 content::SSLHostStateDelegate* AwBrowserContext::GetSSLHostStateDelegate() {
361 if (!ssl_host_state_delegate_.get()) {
362 ssl_host_state_delegate_.reset(new AwSSLHostStateDelegate());
364 return ssl_host_state_delegate_.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 if (GetDataReductionProxySettings()->statistics_prefs())
379 return;
380 // We don't care about commit_delay for now. It is just a dummy value.
381 base::TimeDelta commit_delay = base::TimeDelta::FromMinutes(60);
382 GetDataReductionProxySettings()->EnableCompressionStatisticsLogging(
383 user_pref_service_.get(),
384 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
385 commit_delay);
386 GetDataReductionProxyIOData()->SetDataReductionProxyStatisticsPrefs(
387 GetDataReductionProxySettings()->statistics_prefs());
390 } // namespace android_webview