Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / android_webview / browser / aw_browser_context.cc
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_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"
27
28 using base::FilePath;
29 using content::BrowserThread;
30 using data_reduction_proxy::DataReductionProxySettings;
31
32 namespace android_webview {
33
34 namespace {
35
36 // Shows notifications which correspond to PersistentPrefStore's reading errors.
37 void HandleReadError(PersistentPrefStore::PrefReadError error) {
38 }
39
40 AwBrowserContext* g_browser_context = NULL;
41
42 }  // namespace
43
44 // Data reduction proxy is disabled by default.
45 bool AwBrowserContext::data_reduction_proxy_enabled_ = false;
46
47 AwBrowserContext::AwBrowserContext(
48     const FilePath path,
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;
54
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.
58 }
59
60 AwBrowserContext::~AwBrowserContext() {
61   DCHECK(g_browser_context == this);
62   g_browser_context = NULL;
63 }
64
65 // static
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;
70 }
71
72 // static
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());
77 }
78
79 // static
80 void AwBrowserContext::SetDataReductionProxyEnabled(bool enabled) {
81   // Cache the setting value. It is possible that data reduction proxy is
82   // not created yet.
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)
87     return;
88   DataReductionProxySettings* proxy_settings =
89       context->GetDataReductionProxySettings();
90   if (proxy_settings == NULL)
91     return;
92   proxy_settings->SetDataReductionProxyEnabled(data_reduction_proxy_enabled_);
93 }
94
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);
102
103   url_request_context_getter_ =
104       new AwURLRequestContextGetter(GetPath(), cookie_store_.get());
105
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());
111
112   visitedlink_master_.reset(
113       new visitedlink::VisitedLinkMaster(this, this, false));
114   visitedlink_master_->Init();
115
116   form_database_service_.reset(
117       new AwFormDatabaseService(context_storage_path_));
118 }
119
120 void AwBrowserContext::AddVisitedURLs(const std::vector<GURL>& urls) {
121   DCHECK(visitedlink_master_);
122   visitedlink_master_->AddURLs(urls);
123 }
124
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_;
135 }
136
137 net::URLRequestContextGetter*
138 AwBrowserContext::CreateRequestContextForStoragePartition(
139     const base::FilePath& partition_path,
140     bool in_memory,
141     content::ProtocolHandlerMap* protocol_handlers) {
142   NOTREACHED();
143   return NULL;
144 }
145
146 AwQuotaManagerBridge* AwBrowserContext::GetQuotaManagerBridge() {
147   if (!quota_manager_bridge_.get()) {
148     quota_manager_bridge_ = native_factory_->CreateAwQuotaManagerBridge(this);
149   }
150   return quota_manager_bridge_.get();
151 }
152
153 AwFormDatabaseService* AwBrowserContext::GetFormDatabaseService() {
154   return form_database_service_.get();
155 }
156
157 DataReductionProxySettings* AwBrowserContext::GetDataReductionProxySettings() {
158   return data_reduction_proxy_settings_.get();
159 }
160
161 // Create user pref service for autofill functionality.
162 void AwBrowserContext::CreateUserPrefServiceIfNecessary() {
163   if (user_pref_service_)
164     return;
165
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);
178
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();
183
184   user_prefs::UserPrefs::Set(this, user_pref_service_.get());
185
186   data_reduction_proxy_settings_->InitDataReductionProxySettings(
187       user_pref_service_.get(),
188       user_pref_service_.get(),
189       GetRequestContext());
190
191   data_reduction_proxy_settings_->SetDataReductionProxyEnabled(
192       data_reduction_proxy_enabled_);
193 }
194
195 base::FilePath AwBrowserContext::GetPath() const {
196   return context_storage_path_;
197 }
198
199 bool AwBrowserContext::IsOffTheRecord() const {
200   // Android WebView does not support off the record profile yet.
201   return false;
202 }
203
204 net::URLRequestContextGetter* AwBrowserContext::GetRequestContext() {
205   return GetDefaultStoragePartition(this)->GetURLRequestContext();
206 }
207
208 net::URLRequestContextGetter*
209 AwBrowserContext::GetRequestContextForRenderProcess(
210     int renderer_child_id) {
211   return GetRequestContext();
212 }
213
214 net::URLRequestContextGetter* AwBrowserContext::GetMediaRequestContext() {
215   return GetRequestContext();
216 }
217
218 void AwBrowserContext::RequestMidiSysExPermission(
219       int render_process_id,
220       int render_view_id,
221       int bridge_id,
222       const GURL& requesting_frame,
223       bool user_gesture,
224       const MidiSysExPermissionCallback& callback) {
225   // TODO(toyoshim): Android WebView is not supported yet.
226   // See http://crbug.com/339767.
227   callback.Run(false);
228 }
229
230 void AwBrowserContext::CancelMidiSysExPermissionRequest(
231     int render_process_id,
232     int render_view_id,
233     int bridge_id,
234     const GURL& requesting_frame) {
235 }
236
237 void AwBrowserContext::RequestProtectedMediaIdentifierPermission(
238     int render_process_id,
239     int render_view_id,
240     int bridge_id,
241     int group_id,
242     const GURL& requesting_frame,
243     const ProtectedMediaIdentifierPermissionCallback& callback) {
244   NOTIMPLEMENTED();
245   callback.Run(false);
246 }
247
248 void AwBrowserContext::CancelProtectedMediaIdentifierPermissionRequests(
249     int group_id) {
250   NOTIMPLEMENTED();
251 }
252
253 net::URLRequestContextGetter*
254 AwBrowserContext::GetMediaRequestContextForRenderProcess(
255     int renderer_child_id) {
256   return GetRequestContext();
257 }
258
259 net::URLRequestContextGetter*
260 AwBrowserContext::GetMediaRequestContextForStoragePartition(
261     const base::FilePath& partition_path,
262     bool in_memory) {
263   NOTREACHED();
264   return NULL;
265 }
266
267 content::ResourceContext* AwBrowserContext::GetResourceContext() {
268   if (!resource_context_) {
269     resource_context_.reset(
270         new AwResourceContext(url_request_context_getter_.get()));
271   }
272   return resource_context_.get();
273 }
274
275 content::DownloadManagerDelegate*
276 AwBrowserContext::GetDownloadManagerDelegate() {
277   return &download_manager_delegate_;
278 }
279
280 content::GeolocationPermissionContext*
281 AwBrowserContext::GetGeolocationPermissionContext() {
282   if (!geolocation_permission_context_.get()) {
283     geolocation_permission_context_ =
284         native_factory_->CreateGeolocationPermission(this);
285   }
286   return geolocation_permission_context_.get();
287 }
288
289 content::BrowserPluginGuestManagerDelegate*
290 AwBrowserContext::GetGuestManagerDelegate() {
291   return NULL;
292 }
293
294 quota::SpecialStoragePolicy* AwBrowserContext::GetSpecialStoragePolicy() {
295   // Intentionally returning NULL as 'Extensions' and 'Apps' not supported.
296   return NULL;
297 }
298
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);
305 }
306
307 }  // namespace android_webview