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