Roll blink 153402:153409
[chromium-blink-merge.git] / android_webview / browser / aw_browser_context.cc
blob06793fa217dada615a6aae52326524e9e1a86b4f
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/jni_dependency_factory.h"
11 #include "android_webview/browser/net/aw_url_request_context_getter.h"
12 #include "base/prefs/pref_registry_simple.h"
13 #include "base/prefs/pref_service.h"
14 #include "base/prefs/pref_service_builder.h"
15 #include "components/autofill/core/common/autofill_pref_names.h"
16 #include "components/user_prefs/user_prefs.h"
17 #include "components/visitedlink/browser/visitedlink_master.h"
18 #include "content/public/browser/browser_thread.h"
19 #include "content/public/browser/resource_context.h"
20 #include "content/public/browser/storage_partition.h"
21 #include "content/public/browser/web_contents.h"
22 #include "net/url_request/url_request_context.h"
24 namespace android_webview {
26 namespace {
28 // Shows notifications which correspond to PersistentPrefStore's reading errors.
29 void HandleReadError(PersistentPrefStore::PrefReadError error) {
32 class AwResourceContext : public content::ResourceContext {
33 public:
34 explicit AwResourceContext(net::URLRequestContextGetter* getter)
35 : getter_(getter) {}
36 virtual ~AwResourceContext() {}
38 // content::ResourceContext implementation.
39 virtual net::HostResolver* GetHostResolver() OVERRIDE {
40 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
41 return getter_->GetURLRequestContext()->host_resolver();
43 virtual net::URLRequestContext* GetRequestContext() OVERRIDE {
44 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
45 return getter_->GetURLRequestContext();
48 private:
49 net::URLRequestContextGetter* getter_;
51 DISALLOW_COPY_AND_ASSIGN(AwResourceContext);
54 AwBrowserContext* g_browser_context = NULL;
56 } // namespace
58 AwBrowserContext::AwBrowserContext(
59 const base::FilePath path,
60 JniDependencyFactory* native_factory)
61 : context_storage_path_(path),
62 native_factory_(native_factory),
63 user_pref_service_ready_(false) {
64 DCHECK(g_browser_context == NULL);
65 g_browser_context = this;
68 AwBrowserContext::~AwBrowserContext() {
69 DCHECK(g_browser_context == this);
70 g_browser_context = NULL;
73 // static
74 AwBrowserContext* AwBrowserContext::GetDefault() {
75 // TODO(joth): rather than store in a global here, lookup this instance
76 // from the Java-side peer.
77 return g_browser_context;
80 // static
81 AwBrowserContext* AwBrowserContext::FromWebContents(
82 content::WebContents* web_contents) {
83 // This is safe; this is the only implementation of the browser context.
84 return static_cast<AwBrowserContext*>(web_contents->GetBrowserContext());
87 void AwBrowserContext::InitializeBeforeThreadCreation() {
88 DCHECK(!url_request_context_getter_.get());
89 url_request_context_getter_ = new AwURLRequestContextGetter(this);
92 void AwBrowserContext::PreMainMessageLoopRun() {
93 visitedlink_master_.reset(
94 new visitedlink::VisitedLinkMaster(this, this, false));
95 visitedlink_master_->Init();
98 void AwBrowserContext::AddVisitedURLs(const std::vector<GURL>& urls) {
99 DCHECK(visitedlink_master_);
100 visitedlink_master_->AddURLs(urls);
103 net::URLRequestContextGetter* AwBrowserContext::CreateRequestContext(
104 content::ProtocolHandlerMap* protocol_handlers) {
105 CHECK(url_request_context_getter_.get());
106 url_request_context_getter_->SetProtocolHandlers(protocol_handlers);
107 return url_request_context_getter_.get();
110 net::URLRequestContextGetter*
111 AwBrowserContext::CreateRequestContextForStoragePartition(
112 const base::FilePath& partition_path,
113 bool in_memory,
114 content::ProtocolHandlerMap* protocol_handlers) {
115 CHECK(url_request_context_getter_.get());
116 return url_request_context_getter_.get();
119 AwQuotaManagerBridge* AwBrowserContext::GetQuotaManagerBridge() {
120 if (!quota_manager_bridge_) {
121 quota_manager_bridge_.reset(
122 native_factory_->CreateAwQuotaManagerBridge(this));
124 return quota_manager_bridge_.get();
127 AwFormDatabaseService* AwBrowserContext::GetFormDatabaseService() {
128 if (!form_database_service_) {
129 form_database_service_.reset(
130 new AwFormDatabaseService(context_storage_path_));
132 return form_database_service_.get();
135 // Create user pref service for autofill functionality.
136 void AwBrowserContext::CreateUserPrefServiceIfNecessary() {
137 if (user_pref_service_ready_)
138 return;
140 user_pref_service_ready_ = true;
141 PrefRegistrySimple* pref_registry = new PrefRegistrySimple();
142 // We only use the autocomplete feature of the Autofill, which is
143 // controlled via the manager_delegate. We don't use the rest
144 // of autofill, which is why it is hardcoded as disabled here.
145 pref_registry->RegisterBooleanPref(
146 autofill::prefs::kAutofillEnabled, false);
147 pref_registry->RegisterDoublePref(
148 autofill::prefs::kAutofillPositiveUploadRate, 0.0);
149 pref_registry->RegisterDoublePref(
150 autofill::prefs::kAutofillNegativeUploadRate, 0.0);
152 PrefServiceBuilder pref_service_builder;
153 pref_service_builder.WithUserPrefs(new AwPrefStore());
154 pref_service_builder.WithReadErrorCallback(base::Bind(&HandleReadError));
156 user_prefs::UserPrefs::Set(this,
157 pref_service_builder.Create(pref_registry));
160 base::FilePath AwBrowserContext::GetPath() {
161 return context_storage_path_;
164 bool AwBrowserContext::IsOffTheRecord() const {
165 // Android WebView does not support off the record profile yet.
166 return false;
169 net::URLRequestContextGetter* AwBrowserContext::GetRequestContext() {
170 return GetDefaultStoragePartition(this)->GetURLRequestContext();
173 net::URLRequestContextGetter*
174 AwBrowserContext::GetRequestContextForRenderProcess(
175 int renderer_child_id) {
176 return GetRequestContext();
179 net::URLRequestContextGetter* AwBrowserContext::GetMediaRequestContext() {
180 return GetRequestContext();
183 net::URLRequestContextGetter*
184 AwBrowserContext::GetMediaRequestContextForRenderProcess(
185 int renderer_child_id) {
186 return GetRequestContext();
189 net::URLRequestContextGetter*
190 AwBrowserContext::GetMediaRequestContextForStoragePartition(
191 const base::FilePath& partition_path,
192 bool in_memory) {
193 return GetRequestContext();
196 content::ResourceContext* AwBrowserContext::GetResourceContext() {
197 if (!resource_context_) {
198 CHECK(url_request_context_getter_.get());
199 resource_context_.reset(
200 new AwResourceContext(url_request_context_getter_.get()));
202 return resource_context_.get();
205 content::DownloadManagerDelegate*
206 AwBrowserContext::GetDownloadManagerDelegate() {
207 return &download_manager_delegate_;
210 content::GeolocationPermissionContext*
211 AwBrowserContext::GetGeolocationPermissionContext() {
212 if (!geolocation_permission_context_.get()) {
213 geolocation_permission_context_ =
214 native_factory_->CreateGeolocationPermission(this);
216 return geolocation_permission_context_.get();
219 content::SpeechRecognitionPreferences*
220 AwBrowserContext::GetSpeechRecognitionPreferences() {
221 // By default allows profanities in speech recognition if return NULL.
222 return NULL;
225 quota::SpecialStoragePolicy* AwBrowserContext::GetSpecialStoragePolicy() {
226 // TODO(boliu): Implement this so we are not relying on default behavior.
227 NOTIMPLEMENTED();
228 return NULL;
231 void AwBrowserContext::RebuildTable(
232 const scoped_refptr<URLEnumerator>& enumerator) {
233 // Android WebView rebuilds from WebChromeClient.getVisitedHistory. The client
234 // can change in the lifetime of this WebView and may not yet be set here.
235 // Therefore this initialization path is not used.
236 enumerator->OnComplete(true);
239 } // namespace android_webview