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/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_prefs.h"
20 #include "components/data_reduction_proxy/browser/data_reduction_proxy_settings.h"
21 #include "components/user_prefs/user_prefs.h"
22 #include "components/visitedlink/browser/visitedlink_master.h"
23 #include "content/public/browser/browser_thread.h"
24 #include "content/public/browser/storage_partition.h"
25 #include "content/public/browser/web_contents.h"
26 #include "net/cookies/cookie_store.h"
29 using content::BrowserThread;
30 using data_reduction_proxy::DataReductionProxySettings;
32 namespace android_webview {
36 // Shows notifications which correspond to PersistentPrefStore's reading errors.
37 void HandleReadError(PersistentPrefStore::PrefReadError error) {
40 AwBrowserContext* g_browser_context = NULL;
44 // Data reduction proxy is disabled by default.
45 bool AwBrowserContext::data_reduction_proxy_enabled_ = false;
47 AwBrowserContext::AwBrowserContext(
49 JniDependencyFactory* native_factory)
50 : context_storage_path_(path),
51 native_factory_(native_factory) {
52 DCHECK(g_browser_context == NULL);
53 g_browser_context = this;
55 // This constructor is entered during the creation of ContentBrowserClient,
56 // before browser threads are created. Therefore any checks to enforce
57 // threading (such as BrowserThread::CurrentlyOn()) will fail here.
60 AwBrowserContext::~AwBrowserContext() {
61 DCHECK(g_browser_context == this);
62 g_browser_context = NULL;
66 AwBrowserContext* AwBrowserContext::GetDefault() {
67 // TODO(joth): rather than store in a global here, lookup this instance
68 // from the Java-side peer.
69 return g_browser_context;
73 AwBrowserContext* AwBrowserContext::FromWebContents(
74 content::WebContents* web_contents) {
75 // This is safe; this is the only implementation of the browser context.
76 return static_cast<AwBrowserContext*>(web_contents->GetBrowserContext());
80 void AwBrowserContext::SetDataReductionProxyEnabled(bool enabled) {
81 // Cache the setting value. It is possible that data reduction proxy is
83 data_reduction_proxy_enabled_ = enabled;
84 AwBrowserContext* context = AwBrowserContext::GetDefault();
85 // Can't enable Data reduction proxy if user pref service is not ready.
86 if (context == NULL || context->user_pref_service_.get() == NULL)
88 DataReductionProxySettings* proxy_settings =
89 context->GetDataReductionProxySettings();
90 if (proxy_settings == NULL)
92 proxy_settings->SetDataReductionProxyEnabled(data_reduction_proxy_enabled_);
95 void AwBrowserContext::PreMainMessageLoopRun() {
96 cookie_store_ = CreateCookieStore(this);
97 DataReductionProxySettings::SetAllowed(true);
98 DataReductionProxySettings::SetPromoAllowed(false);
99 data_reduction_proxy_settings_.reset(
100 new DataReductionProxySettings());
101 data_reduction_proxy_settings_->set_fallback_allowed(false);
103 url_request_context_getter_ =
104 new AwURLRequestContextGetter(GetPath(), cookie_store_.get());
106 scoped_ptr<data_reduction_proxy::DataReductionProxyConfigurator>
107 configurator(new data_reduction_proxy::DataReductionProxyConfigTracker(
108 url_request_context_getter_->proxy_config_service(),
109 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)));
110 data_reduction_proxy_settings_->SetProxyConfigurator(configurator.Pass());
112 visitedlink_master_.reset(
113 new visitedlink::VisitedLinkMaster(this, this, false));
114 visitedlink_master_->Init();
116 form_database_service_.reset(
117 new AwFormDatabaseService(context_storage_path_));
120 void AwBrowserContext::AddVisitedURLs(const std::vector<GURL>& urls) {
121 DCHECK(visitedlink_master_);
122 visitedlink_master_->AddURLs(urls);
125 net::URLRequestContextGetter* AwBrowserContext::CreateRequestContext(
126 content::ProtocolHandlerMap* protocol_handlers) {
127 // This function cannot actually create the request context because
128 // there is a reentrant dependency on GetResourceContext() via
129 // content::StoragePartitionImplMap::Create(). This is not fixable
130 // until http://crbug.com/159193. Until then, assert that the context
131 // has already been allocated and just handle setting the protocol_handlers.
132 DCHECK(url_request_context_getter_);
133 url_request_context_getter_->SetProtocolHandlers(protocol_handlers);
134 return url_request_context_getter_;
137 net::URLRequestContextGetter*
138 AwBrowserContext::CreateRequestContextForStoragePartition(
139 const base::FilePath& partition_path,
141 content::ProtocolHandlerMap* protocol_handlers) {
146 AwQuotaManagerBridge* AwBrowserContext::GetQuotaManagerBridge() {
147 if (!quota_manager_bridge_.get()) {
148 quota_manager_bridge_ = native_factory_->CreateAwQuotaManagerBridge(this);
150 return quota_manager_bridge_.get();
153 AwFormDatabaseService* AwBrowserContext::GetFormDatabaseService() {
154 return form_database_service_.get();
157 DataReductionProxySettings* AwBrowserContext::GetDataReductionProxySettings() {
158 return data_reduction_proxy_settings_.get();
161 // Create user pref service for autofill functionality.
162 void AwBrowserContext::CreateUserPrefServiceIfNecessary() {
163 if (user_pref_service_)
166 PrefRegistrySimple* pref_registry = new PrefRegistrySimple();
167 // We only use the autocomplete feature of the Autofill, which is
168 // controlled via the manager_delegate. We don't use the rest
169 // of autofill, which is why it is hardcoded as disabled here.
170 pref_registry->RegisterBooleanPref(
171 autofill::prefs::kAutofillEnabled, false);
172 pref_registry->RegisterDoublePref(
173 autofill::prefs::kAutofillPositiveUploadRate, 0.0);
174 pref_registry->RegisterDoublePref(
175 autofill::prefs::kAutofillNegativeUploadRate, 0.0);
176 data_reduction_proxy::RegisterSimpleProfilePrefs(pref_registry);
177 data_reduction_proxy::RegisterPrefs(pref_registry);
179 base::PrefServiceFactory pref_service_factory;
180 pref_service_factory.set_user_prefs(make_scoped_refptr(new AwPrefStore()));
181 pref_service_factory.set_read_error_callback(base::Bind(&HandleReadError));
182 user_pref_service_ = pref_service_factory.Create(pref_registry).Pass();
184 user_prefs::UserPrefs::Set(this, user_pref_service_.get());
186 data_reduction_proxy_settings_->InitDataReductionProxySettings(
187 user_pref_service_.get(),
188 user_pref_service_.get(),
189 GetRequestContext());
191 data_reduction_proxy_settings_->SetDataReductionProxyEnabled(
192 data_reduction_proxy_enabled_);
195 base::FilePath AwBrowserContext::GetPath() const {
196 return context_storage_path_;
199 bool AwBrowserContext::IsOffTheRecord() const {
200 // Android WebView does not support off the record profile yet.
204 net::URLRequestContextGetter* AwBrowserContext::GetRequestContext() {
205 return GetDefaultStoragePartition(this)->GetURLRequestContext();
208 net::URLRequestContextGetter*
209 AwBrowserContext::GetRequestContextForRenderProcess(
210 int renderer_child_id) {
211 return GetRequestContext();
214 net::URLRequestContextGetter* AwBrowserContext::GetMediaRequestContext() {
215 return GetRequestContext();
218 void AwBrowserContext::RequestMidiSysExPermission(
219 int render_process_id,
222 const GURL& requesting_frame,
224 const MidiSysExPermissionCallback& callback) {
225 // TODO(toyoshim): Android WebView is not supported yet.
226 // See http://crbug.com/339767.
230 void AwBrowserContext::CancelMidiSysExPermissionRequest(
231 int render_process_id,
234 const GURL& requesting_frame) {
237 void AwBrowserContext::RequestProtectedMediaIdentifierPermission(
238 int render_process_id,
242 const GURL& requesting_frame,
243 const ProtectedMediaIdentifierPermissionCallback& callback) {
248 void AwBrowserContext::CancelProtectedMediaIdentifierPermissionRequests(
253 net::URLRequestContextGetter*
254 AwBrowserContext::GetMediaRequestContextForRenderProcess(
255 int renderer_child_id) {
256 return GetRequestContext();
259 net::URLRequestContextGetter*
260 AwBrowserContext::GetMediaRequestContextForStoragePartition(
261 const base::FilePath& partition_path,
267 content::ResourceContext* AwBrowserContext::GetResourceContext() {
268 if (!resource_context_) {
269 resource_context_.reset(
270 new AwResourceContext(url_request_context_getter_.get()));
272 return resource_context_.get();
275 content::DownloadManagerDelegate*
276 AwBrowserContext::GetDownloadManagerDelegate() {
277 return &download_manager_delegate_;
280 content::GeolocationPermissionContext*
281 AwBrowserContext::GetGeolocationPermissionContext() {
282 if (!geolocation_permission_context_.get()) {
283 geolocation_permission_context_ =
284 native_factory_->CreateGeolocationPermission(this);
286 return geolocation_permission_context_.get();
289 content::BrowserPluginGuestManagerDelegate*
290 AwBrowserContext::GetGuestManagerDelegate() {
294 quota::SpecialStoragePolicy* AwBrowserContext::GetSpecialStoragePolicy() {
295 // Intentionally returning NULL as 'Extensions' and 'Apps' not supported.
299 void AwBrowserContext::RebuildTable(
300 const scoped_refptr<URLEnumerator>& enumerator) {
301 // Android WebView rebuilds from WebChromeClient.getVisitedHistory. The client
302 // can change in the lifetime of this WebView and may not yet be set here.
303 // Therefore this initialization path is not used.
304 enumerator->OnComplete(true);
307 } // namespace android_webview