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/user_prefs/user_prefs.h"
19#include "components/visitedlink/browser/visitedlink_master.h"
20#include "content/public/browser/browser_thread.h"
21#include "content/public/browser/storage_partition.h"
22#include "content/public/browser/web_contents.h"
23#include "net/cookies/cookie_store.h"
24
25using base::FilePath;
26using content::BrowserThread;
27
28namespace android_webview {
29
30namespace {
31
32// Shows notifications which correspond to PersistentPrefStore's reading errors.
33void HandleReadError(PersistentPrefStore::PrefReadError error) {
34}
35
36AwBrowserContext* g_browser_context = NULL;
37
38}  // namespace
39
40AwBrowserContext::AwBrowserContext(
41    const FilePath path,
42    JniDependencyFactory* native_factory)
43    : context_storage_path_(path),
44      native_factory_(native_factory) {
45  DCHECK(g_browser_context == NULL);
46  g_browser_context = this;
47
48  // This constructor is entered during the creation of ContentBrowserClient,
49  // before browser threads are created. Therefore any checks to enforce
50  // threading (such as BrowserThread::CurrentlyOn()) will fail here.
51}
52
53AwBrowserContext::~AwBrowserContext() {
54  DCHECK(g_browser_context == this);
55  g_browser_context = NULL;
56}
57
58// static
59AwBrowserContext* AwBrowserContext::GetDefault() {
60  // TODO(joth): rather than store in a global here, lookup this instance
61  // from the Java-side peer.
62  return g_browser_context;
63}
64
65// static
66AwBrowserContext* AwBrowserContext::FromWebContents(
67    content::WebContents* web_contents) {
68  // This is safe; this is the only implementation of the browser context.
69  return static_cast<AwBrowserContext*>(web_contents->GetBrowserContext());
70}
71
72void AwBrowserContext::PreMainMessageLoopRun() {
73  cookie_store_ = CreateCookieStore(this);
74  url_request_context_getter_ =
75      new AwURLRequestContextGetter(GetPath(), cookie_store_.get());
76
77  visitedlink_master_.reset(
78      new visitedlink::VisitedLinkMaster(this, this, false));
79  visitedlink_master_->Init();
80
81  form_database_service_.reset(
82      new AwFormDatabaseService(context_storage_path_));
83}
84
85void AwBrowserContext::AddVisitedURLs(const std::vector<GURL>& urls) {
86  DCHECK(visitedlink_master_);
87  visitedlink_master_->AddURLs(urls);
88}
89
90net::URLRequestContextGetter* AwBrowserContext::CreateRequestContext(
91    content::ProtocolHandlerMap* protocol_handlers) {
92  // This function cannot actually create the request context because
93  // there is a reentrant dependency on GetResourceContext() via
94  // content::StoragePartitionImplMap::Create(). This is not fixable
95  // until http://crbug.com/159193. Until then, assert that the context
96  // has already been allocated and just handle setting the protocol_handlers.
97  DCHECK(url_request_context_getter_);
98  url_request_context_getter_->SetProtocolHandlers(protocol_handlers);
99  return url_request_context_getter_;
100}
101
102net::URLRequestContextGetter*
103AwBrowserContext::CreateRequestContextForStoragePartition(
104    const base::FilePath& partition_path,
105    bool in_memory,
106    content::ProtocolHandlerMap* protocol_handlers) {
107  NOTREACHED();
108  return NULL;
109}
110
111AwQuotaManagerBridge* AwBrowserContext::GetQuotaManagerBridge() {
112  if (!quota_manager_bridge_.get()) {
113    quota_manager_bridge_ = native_factory_->CreateAwQuotaManagerBridge(this);
114  }
115  return quota_manager_bridge_.get();
116}
117
118AwFormDatabaseService* AwBrowserContext::GetFormDatabaseService() {
119  return form_database_service_.get();
120}
121
122// Create user pref service for autofill functionality.
123void AwBrowserContext::CreateUserPrefServiceIfNecessary() {
124  if (user_pref_service_)
125    return;
126
127  PrefRegistrySimple* pref_registry = new PrefRegistrySimple();
128  // We only use the autocomplete feature of the Autofill, which is
129  // controlled via the manager_delegate. We don't use the rest
130  // of autofill, which is why it is hardcoded as disabled here.
131  pref_registry->RegisterBooleanPref(
132      autofill::prefs::kAutofillEnabled, false);
133  pref_registry->RegisterDoublePref(
134      autofill::prefs::kAutofillPositiveUploadRate, 0.0);
135  pref_registry->RegisterDoublePref(
136      autofill::prefs::kAutofillNegativeUploadRate, 0.0);
137
138  base::PrefServiceFactory pref_service_factory;
139  pref_service_factory.set_user_prefs(make_scoped_refptr(new AwPrefStore()));
140  pref_service_factory.set_read_error_callback(base::Bind(&HandleReadError));
141  user_pref_service_ = pref_service_factory.Create(pref_registry).Pass();
142
143  user_prefs::UserPrefs::Set(this, user_pref_service_.get());
144}
145
146base::FilePath AwBrowserContext::GetPath() const {
147  return context_storage_path_;
148}
149
150bool AwBrowserContext::IsOffTheRecord() const {
151  // Android WebView does not support off the record profile yet.
152  return false;
153}
154
155net::URLRequestContextGetter* AwBrowserContext::GetRequestContext() {
156  return GetDefaultStoragePartition(this)->GetURLRequestContext();
157}
158
159net::URLRequestContextGetter*
160AwBrowserContext::GetRequestContextForRenderProcess(
161    int renderer_child_id) {
162  return GetRequestContext();
163}
164
165net::URLRequestContextGetter* AwBrowserContext::GetMediaRequestContext() {
166  return GetRequestContext();
167}
168
169void AwBrowserContext::RequestMIDISysExPermission(
170      int render_process_id,
171      int render_view_id,
172      int bridge_id,
173      const GURL& requesting_frame,
174      const MIDISysExPermissionCallback& callback) {
175  // TODO(toyoshim): Android is not supported yet.
176  callback.Run(false);
177}
178
179void AwBrowserContext::CancelMIDISysExPermissionRequest(
180    int render_process_id,
181    int render_view_id,
182    int bridge_id,
183    const GURL& requesting_frame) {
184}
185
186net::URLRequestContextGetter*
187AwBrowserContext::GetMediaRequestContextForRenderProcess(
188    int renderer_child_id) {
189  return GetRequestContext();
190}
191
192net::URLRequestContextGetter*
193AwBrowserContext::GetMediaRequestContextForStoragePartition(
194    const base::FilePath& partition_path,
195    bool in_memory) {
196  NOTREACHED();
197  return NULL;
198}
199
200content::ResourceContext* AwBrowserContext::GetResourceContext() {
201  if (!resource_context_) {
202    resource_context_.reset(
203        new AwResourceContext(url_request_context_getter_.get()));
204  }
205  return resource_context_.get();
206}
207
208content::DownloadManagerDelegate*
209AwBrowserContext::GetDownloadManagerDelegate() {
210  return &download_manager_delegate_;
211}
212
213content::GeolocationPermissionContext*
214AwBrowserContext::GetGeolocationPermissionContext() {
215  if (!geolocation_permission_context_.get()) {
216    geolocation_permission_context_ =
217        native_factory_->CreateGeolocationPermission(this);
218  }
219  return geolocation_permission_context_.get();
220}
221
222quota::SpecialStoragePolicy* AwBrowserContext::GetSpecialStoragePolicy() {
223  // Intentionally returning NULL as 'Extensions' and 'Apps' not supported.
224  return NULL;
225}
226
227void AwBrowserContext::RebuildTable(
228    const scoped_refptr<URLEnumerator>& enumerator) {
229  // Android WebView rebuilds from WebChromeClient.getVisitedHistory. The client
230  // can change in the lifetime of this WebView and may not yet be set here.
231  // Therefore this initialization path is not used.
232  enumerator->OnComplete(true);
233}
234
235}  // namespace android_webview
236