098ce73ea4e15025166e93268a27fe7c55158ac8
[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);
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_EQ(this, g_browser_context);
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   data_reduction_proxy_settings_.reset(
103       new DataReductionProxySettings(
104           new data_reduction_proxy::DataReductionProxyParams(
105               data_reduction_proxy::DataReductionProxyParams::kAllowed)));
106   scoped_ptr<DataReductionProxyConfigService>
107       data_reduction_proxy_config_service(
108           new DataReductionProxyConfigService(
109               scoped_ptr<net::ProxyConfigService>(
110                   net::ProxyService::CreateSystemProxyConfigService(
111                       BrowserThread::GetMessageLoopProxyForThread(
112                           BrowserThread::IO),
113                           NULL /* Ignored on Android */)).Pass()));
114   if (data_reduction_proxy_settings_.get()) {
115       data_reduction_proxy_configurator_.reset(
116           new data_reduction_proxy::DataReductionProxyConfigTracker(
117               base::Bind(&DataReductionProxyConfigService::UpdateProxyConfig,
118                          base::Unretained(
119                              data_reduction_proxy_config_service.get())),
120             BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)));
121     data_reduction_proxy_settings_->SetProxyConfigurator(
122         data_reduction_proxy_configurator_.get());
123   }
124
125   url_request_context_getter_ =
126       new AwURLRequestContextGetter(GetPath(),
127                                     cookie_store_.get(),
128                                     data_reduction_proxy_config_service.Pass());
129
130   visitedlink_master_.reset(
131       new visitedlink::VisitedLinkMaster(this, this, false));
132   visitedlink_master_->Init();
133
134   form_database_service_.reset(
135       new AwFormDatabaseService(context_storage_path_));
136 }
137
138 void AwBrowserContext::AddVisitedURLs(const std::vector<GURL>& urls) {
139   DCHECK(visitedlink_master_);
140   visitedlink_master_->AddURLs(urls);
141 }
142
143 net::URLRequestContextGetter* AwBrowserContext::CreateRequestContext(
144     content::ProtocolHandlerMap* protocol_handlers,
145     content::URLRequestInterceptorScopedVector request_interceptors) {
146   // This function cannot actually create the request context because
147   // there is a reentrant dependency on GetResourceContext() via
148   // content::StoragePartitionImplMap::Create(). This is not fixable
149   // until http://crbug.com/159193. Until then, assert that the context
150   // has already been allocated and just handle setting the protocol_handlers.
151   DCHECK(url_request_context_getter_);
152   url_request_context_getter_->SetHandlersAndInterceptors(
153       protocol_handlers, request_interceptors.Pass());
154   return url_request_context_getter_;
155 }
156
157 net::URLRequestContextGetter*
158 AwBrowserContext::CreateRequestContextForStoragePartition(
159     const base::FilePath& partition_path,
160     bool in_memory,
161     content::ProtocolHandlerMap* protocol_handlers,
162     content::URLRequestInterceptorScopedVector request_interceptors) {
163   NOTREACHED();
164   return NULL;
165 }
166
167 AwQuotaManagerBridge* AwBrowserContext::GetQuotaManagerBridge() {
168   if (!quota_manager_bridge_.get()) {
169     quota_manager_bridge_ = native_factory_->CreateAwQuotaManagerBridge(this);
170   }
171   return quota_manager_bridge_.get();
172 }
173
174 AwFormDatabaseService* AwBrowserContext::GetFormDatabaseService() {
175   return form_database_service_.get();
176 }
177
178 DataReductionProxySettings* AwBrowserContext::GetDataReductionProxySettings() {
179   return data_reduction_proxy_settings_.get();
180 }
181
182 AwURLRequestContextGetter* AwBrowserContext::GetAwURLRequestContext() {
183   return url_request_context_getter_.get();
184 }
185
186 // Create user pref service for autofill functionality.
187 void AwBrowserContext::CreateUserPrefServiceIfNecessary() {
188   if (user_pref_service_)
189     return;
190
191   PrefRegistrySimple* pref_registry = new PrefRegistrySimple();
192   // We only use the autocomplete feature of the Autofill, which is
193   // controlled via the manager_delegate. We don't use the rest
194   // of autofill, which is why it is hardcoded as disabled here.
195   pref_registry->RegisterBooleanPref(
196       autofill::prefs::kAutofillEnabled, false);
197   pref_registry->RegisterDoublePref(
198       autofill::prefs::kAutofillPositiveUploadRate, 0.0);
199   pref_registry->RegisterDoublePref(
200       autofill::prefs::kAutofillNegativeUploadRate, 0.0);
201   data_reduction_proxy::RegisterSimpleProfilePrefs(pref_registry);
202   data_reduction_proxy::RegisterPrefs(pref_registry);
203
204   base::PrefServiceFactory pref_service_factory;
205   pref_service_factory.set_user_prefs(make_scoped_refptr(new AwPrefStore()));
206   pref_service_factory.set_read_error_callback(base::Bind(&HandleReadError));
207   user_pref_service_ = pref_service_factory.Create(pref_registry).Pass();
208
209   user_prefs::UserPrefs::Set(this, user_pref_service_.get());
210
211   if (data_reduction_proxy_settings_.get()) {
212     data_reduction_proxy_settings_->InitDataReductionProxySettings(
213         user_pref_service_.get(),
214         GetRequestContext());
215
216     data_reduction_proxy_settings_->SetDataReductionProxyEnabled(
217         data_reduction_proxy_enabled_);
218   }
219 }
220
221 base::FilePath AwBrowserContext::GetPath() const {
222   return context_storage_path_;
223 }
224
225 bool AwBrowserContext::IsOffTheRecord() const {
226   // Android WebView does not support off the record profile yet.
227   return false;
228 }
229
230 net::URLRequestContextGetter* AwBrowserContext::GetRequestContext() {
231   return GetDefaultStoragePartition(this)->GetURLRequestContext();
232 }
233
234 net::URLRequestContextGetter*
235 AwBrowserContext::GetRequestContextForRenderProcess(
236     int renderer_child_id) {
237   return GetRequestContext();
238 }
239
240 net::URLRequestContextGetter* AwBrowserContext::GetMediaRequestContext() {
241   return GetRequestContext();
242 }
243
244 net::URLRequestContextGetter*
245 AwBrowserContext::GetMediaRequestContextForRenderProcess(
246     int renderer_child_id) {
247   return GetRequestContext();
248 }
249
250 net::URLRequestContextGetter*
251 AwBrowserContext::GetMediaRequestContextForStoragePartition(
252     const base::FilePath& partition_path,
253     bool in_memory) {
254   NOTREACHED();
255   return NULL;
256 }
257
258 content::ResourceContext* AwBrowserContext::GetResourceContext() {
259   if (!resource_context_) {
260     resource_context_.reset(
261         new AwResourceContext(url_request_context_getter_.get()));
262   }
263   return resource_context_.get();
264 }
265
266 content::DownloadManagerDelegate*
267 AwBrowserContext::GetDownloadManagerDelegate() {
268   return &download_manager_delegate_;
269 }
270
271 content::BrowserPluginGuestManager* AwBrowserContext::GetGuestManager() {
272   return NULL;
273 }
274
275 storage::SpecialStoragePolicy* AwBrowserContext::GetSpecialStoragePolicy() {
276   // Intentionally returning NULL as 'Extensions' and 'Apps' not supported.
277   return NULL;
278 }
279
280 content::PushMessagingService* AwBrowserContext::GetPushMessagingService() {
281   // TODO(johnme): Support push messaging in WebView.
282   return NULL;
283 }
284
285 content::SSLHostStateDelegate* AwBrowserContext::GetSSLHostStateDelegate() {
286   return NULL;
287 }
288
289 void AwBrowserContext::RebuildTable(
290     const scoped_refptr<URLEnumerator>& enumerator) {
291   // Android WebView rebuilds from WebChromeClient.getVisitedHistory. The client
292   // can change in the lifetime of this WebView and may not yet be set here.
293   // Therefore this initialization path is not used.
294   enumerator->OnComplete(true);
295 }
296
297 }  // namespace android_webview