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. 4 5#include "android_webview/browser/aw_browser_context.h" 6 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/prefs/pref_registry_simple.h" 15#include "base/prefs/pref_service.h" 16#include "base/prefs/pref_service_factory.h" 17#include "components/autofill/core/common/autofill_pref_names.h" 18#include "components/data_reduction_proxy/browser/data_reduction_proxy_config_service.h" 19#include "components/data_reduction_proxy/browser/data_reduction_proxy_params.h" 20#include "components/data_reduction_proxy/browser/data_reduction_proxy_prefs.h" 21#include "components/data_reduction_proxy/browser/data_reduction_proxy_settings.h" 22#include "components/user_prefs/user_prefs.h" 23#include "components/visitedlink/browser/visitedlink_master.h" 24#include "content/public/browser/browser_thread.h" 25#include "content/public/browser/storage_partition.h" 26#include "content/public/browser/web_contents.h" 27#include "net/cookies/cookie_store.h" 28 29using base::FilePath; 30using content::BrowserThread; 31using data_reduction_proxy::DataReductionProxySettings; 32 33namespace android_webview { 34 35namespace { 36 37// Shows notifications which correspond to PersistentPrefStore's reading errors. 38void HandleReadError(PersistentPrefStore::PrefReadError error) { 39} 40 41AwBrowserContext* g_browser_context = NULL; 42 43} // namespace 44 45// Data reduction proxy is disabled by default. 46bool AwBrowserContext::data_reduction_proxy_enabled_ = false; 47 48AwBrowserContext::AwBrowserContext( 49 const FilePath path, 50 JniDependencyFactory* native_factory) 51 : context_storage_path_(path), 52 native_factory_(native_factory) { 53 DCHECK(g_browser_context == NULL); 54 g_browser_context = this; 55 56 // This constructor is entered during the creation of ContentBrowserClient, 57 // before browser threads are created. Therefore any checks to enforce 58 // threading (such as BrowserThread::CurrentlyOn()) will fail here. 59} 60 61AwBrowserContext::~AwBrowserContext() { 62 DCHECK(g_browser_context == this); 63 g_browser_context = NULL; 64} 65 66// static 67AwBrowserContext* AwBrowserContext::GetDefault() { 68 // TODO(joth): rather than store in a global here, lookup this instance 69 // from the Java-side peer. 70 return g_browser_context; 71} 72 73// static 74AwBrowserContext* AwBrowserContext::FromWebContents( 75 content::WebContents* web_contents) { 76 // This is safe; this is the only implementation of the browser context. 77 return static_cast<AwBrowserContext*>(web_contents->GetBrowserContext()); 78} 79 80// static 81void AwBrowserContext::SetDataReductionProxyEnabled(bool enabled) { 82 // Cache the setting value. It is possible that data reduction proxy is 83 // not created yet. 84 data_reduction_proxy_enabled_ = enabled; 85 AwBrowserContext* context = AwBrowserContext::GetDefault(); 86 // Can't enable Data reduction proxy if user pref service is not ready. 87 if (context == NULL || context->user_pref_service_.get() == NULL) 88 return; 89 DataReductionProxySettings* proxy_settings = 90 context->GetDataReductionProxySettings(); 91 if (proxy_settings == NULL) 92 return; 93 proxy_settings->SetDataReductionProxyEnabled(data_reduction_proxy_enabled_); 94} 95 96void AwBrowserContext::PreMainMessageLoopRun() { 97 cookie_store_ = CreateCookieStore(this); 98#if defined(SPDY_PROXY_AUTH_ORIGIN) 99 data_reduction_proxy_settings_.reset( 100 new DataReductionProxySettings( 101 new data_reduction_proxy::DataReductionProxyParams( 102 data_reduction_proxy::DataReductionProxyParams::kAllowed))); 103#endif 104 105 url_request_context_getter_ = 106 new AwURLRequestContextGetter(GetPath(), cookie_store_.get()); 107 108 if (data_reduction_proxy_settings_.get()) { 109 scoped_ptr<data_reduction_proxy::DataReductionProxyConfigurator> 110 configurator(new data_reduction_proxy::DataReductionProxyConfigTracker( 111 url_request_context_getter_->proxy_config_service(), 112 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO))); 113 data_reduction_proxy_settings_->SetProxyConfigurator(configurator.Pass()); 114 } 115 visitedlink_master_.reset( 116 new visitedlink::VisitedLinkMaster(this, this, false)); 117 visitedlink_master_->Init(); 118 119 form_database_service_.reset( 120 new AwFormDatabaseService(context_storage_path_)); 121} 122 123void AwBrowserContext::AddVisitedURLs(const std::vector<GURL>& urls) { 124 DCHECK(visitedlink_master_); 125 visitedlink_master_->AddURLs(urls); 126} 127 128net::URLRequestContextGetter* AwBrowserContext::CreateRequestContext( 129 content::ProtocolHandlerMap* protocol_handlers, 130 content::URLRequestInterceptorScopedVector request_interceptors) { 131 // This function cannot actually create the request context because 132 // there is a reentrant dependency on GetResourceContext() via 133 // content::StoragePartitionImplMap::Create(). This is not fixable 134 // until http://crbug.com/159193. Until then, assert that the context 135 // has already been allocated and just handle setting the protocol_handlers. 136 DCHECK(url_request_context_getter_); 137 url_request_context_getter_->SetHandlersAndInterceptors( 138 protocol_handlers, request_interceptors.Pass()); 139 return url_request_context_getter_; 140} 141 142net::URLRequestContextGetter* 143AwBrowserContext::CreateRequestContextForStoragePartition( 144 const base::FilePath& partition_path, 145 bool in_memory, 146 content::ProtocolHandlerMap* protocol_handlers, 147 content::URLRequestInterceptorScopedVector request_interceptors) { 148 NOTREACHED(); 149 return NULL; 150} 151 152AwQuotaManagerBridge* AwBrowserContext::GetQuotaManagerBridge() { 153 if (!quota_manager_bridge_.get()) { 154 quota_manager_bridge_ = native_factory_->CreateAwQuotaManagerBridge(this); 155 } 156 return quota_manager_bridge_.get(); 157} 158 159AwFormDatabaseService* AwBrowserContext::GetFormDatabaseService() { 160 return form_database_service_.get(); 161} 162 163DataReductionProxySettings* AwBrowserContext::GetDataReductionProxySettings() { 164 return data_reduction_proxy_settings_.get(); 165} 166 167// Create user pref service for autofill functionality. 168void AwBrowserContext::CreateUserPrefServiceIfNecessary() { 169 if (user_pref_service_) 170 return; 171 172 PrefRegistrySimple* pref_registry = new PrefRegistrySimple(); 173 // We only use the autocomplete feature of the Autofill, which is 174 // controlled via the manager_delegate. We don't use the rest 175 // of autofill, which is why it is hardcoded as disabled here. 176 pref_registry->RegisterBooleanPref( 177 autofill::prefs::kAutofillEnabled, false); 178 pref_registry->RegisterDoublePref( 179 autofill::prefs::kAutofillPositiveUploadRate, 0.0); 180 pref_registry->RegisterDoublePref( 181 autofill::prefs::kAutofillNegativeUploadRate, 0.0); 182 data_reduction_proxy::RegisterSimpleProfilePrefs(pref_registry); 183 data_reduction_proxy::RegisterPrefs(pref_registry); 184 185 base::PrefServiceFactory pref_service_factory; 186 pref_service_factory.set_user_prefs(make_scoped_refptr(new AwPrefStore())); 187 pref_service_factory.set_read_error_callback(base::Bind(&HandleReadError)); 188 user_pref_service_ = pref_service_factory.Create(pref_registry).Pass(); 189 190 user_prefs::UserPrefs::Set(this, user_pref_service_.get()); 191 192 if (data_reduction_proxy_settings_.get()) { 193 data_reduction_proxy_settings_->InitDataReductionProxySettings( 194 user_pref_service_.get(), 195 user_pref_service_.get(), 196 GetRequestContext()); 197 198 data_reduction_proxy_settings_->SetDataReductionProxyEnabled( 199 data_reduction_proxy_enabled_); 200 } 201} 202 203base::FilePath AwBrowserContext::GetPath() const { 204 return context_storage_path_; 205} 206 207bool AwBrowserContext::IsOffTheRecord() const { 208 // Android WebView does not support off the record profile yet. 209 return false; 210} 211 212net::URLRequestContextGetter* AwBrowserContext::GetRequestContext() { 213 return GetDefaultStoragePartition(this)->GetURLRequestContext(); 214} 215 216net::URLRequestContextGetter* 217AwBrowserContext::GetRequestContextForRenderProcess( 218 int renderer_child_id) { 219 return GetRequestContext(); 220} 221 222net::URLRequestContextGetter* AwBrowserContext::GetMediaRequestContext() { 223 return GetRequestContext(); 224} 225 226net::URLRequestContextGetter* 227AwBrowserContext::GetMediaRequestContextForRenderProcess( 228 int renderer_child_id) { 229 return GetRequestContext(); 230} 231 232net::URLRequestContextGetter* 233AwBrowserContext::GetMediaRequestContextForStoragePartition( 234 const base::FilePath& partition_path, 235 bool in_memory) { 236 NOTREACHED(); 237 return NULL; 238} 239 240content::ResourceContext* AwBrowserContext::GetResourceContext() { 241 if (!resource_context_) { 242 resource_context_.reset( 243 new AwResourceContext(url_request_context_getter_.get())); 244 } 245 return resource_context_.get(); 246} 247 248content::DownloadManagerDelegate* 249AwBrowserContext::GetDownloadManagerDelegate() { 250 return &download_manager_delegate_; 251} 252 253content::BrowserPluginGuestManager* AwBrowserContext::GetGuestManager() { 254 return NULL; 255} 256 257quota::SpecialStoragePolicy* AwBrowserContext::GetSpecialStoragePolicy() { 258 // Intentionally returning NULL as 'Extensions' and 'Apps' not supported. 259 return NULL; 260} 261 262content::PushMessagingService* AwBrowserContext::GetPushMessagingService() { 263 // TODO(johnme): Support push messaging in WebView. 264 return NULL; 265} 266 267void AwBrowserContext::RebuildTable( 268 const scoped_refptr<URLEnumerator>& enumerator) { 269 // Android WebView rebuilds from WebChromeClient.getVisitedHistory. The client 270 // can change in the lifetime of this WebView and may not yet be set here. 271 // Therefore this initialization path is not used. 272 enumerator->OnComplete(true); 273} 274 275} // namespace android_webview 276