Fix race condition in gyp/ninja builds.
[chromium-blink-merge.git] / android_webview / browser / aw_browser_context.cc
blob4287b02514445696ed74f8dba84bf9ef92daf4de
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/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_auth_request_handler.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/storage_partition.h"
27 #include "content/public/browser/web_contents.h"
28 #include "net/cookies/cookie_store.h"
30 using base::FilePath;
31 using content::BrowserThread;
32 using data_reduction_proxy::DataReductionProxyAuthRequestHandler;
33 using data_reduction_proxy::DataReductionProxySettings;
35 namespace android_webview {
37 namespace {
39 // Shows notifications which correspond to PersistentPrefStore's reading errors.
40 void HandleReadError(PersistentPrefStore::PrefReadError error) {
43 AwBrowserContext* g_browser_context = NULL;
45 } // namespace
47 // Data reduction proxy is disabled by default.
48 bool AwBrowserContext::data_reduction_proxy_enabled_ = false;
50 AwBrowserContext::AwBrowserContext(
51 const FilePath path,
52 JniDependencyFactory* native_factory)
53 : context_storage_path_(path),
54 native_factory_(native_factory) {
55 DCHECK(g_browser_context == NULL);
56 g_browser_context = this;
58 // This constructor is entered during the creation of ContentBrowserClient,
59 // before browser threads are created. Therefore any checks to enforce
60 // threading (such as BrowserThread::CurrentlyOn()) will fail here.
63 AwBrowserContext::~AwBrowserContext() {
64 DCHECK(g_browser_context == this);
65 g_browser_context = NULL;
68 // static
69 AwBrowserContext* AwBrowserContext::GetDefault() {
70 // TODO(joth): rather than store in a global here, lookup this instance
71 // from the Java-side peer.
72 return g_browser_context;
75 // static
76 AwBrowserContext* AwBrowserContext::FromWebContents(
77 content::WebContents* web_contents) {
78 // This is safe; this is the only implementation of the browser context.
79 return static_cast<AwBrowserContext*>(web_contents->GetBrowserContext());
82 // static
83 void AwBrowserContext::SetDataReductionProxyEnabled(bool enabled) {
84 // Cache the setting value. It is possible that data reduction proxy is
85 // not created yet.
86 data_reduction_proxy_enabled_ = enabled;
87 AwBrowserContext* context = AwBrowserContext::GetDefault();
88 // Can't enable Data reduction proxy if user pref service is not ready.
89 if (context == NULL || context->user_pref_service_.get() == NULL)
90 return;
91 DataReductionProxySettings* proxy_settings =
92 context->GetDataReductionProxySettings();
93 if (proxy_settings == NULL)
94 return;
95 proxy_settings->SetDataReductionProxyEnabled(data_reduction_proxy_enabled_);
98 void AwBrowserContext::PreMainMessageLoopRun() {
99 cookie_store_ = CreateCookieStore(this);
100 #if defined(SPDY_PROXY_AUTH_ORIGIN)
101 data_reduction_proxy_settings_.reset(
102 new DataReductionProxySettings(
103 new data_reduction_proxy::DataReductionProxyParams(
104 data_reduction_proxy::DataReductionProxyParams::kAllowed)));
105 data_reduction_proxy_auth_request_handler_.reset(
106 new DataReductionProxyAuthRequestHandler(
107 data_reduction_proxy_settings_->params()));
108 #endif
110 url_request_context_getter_ =
111 new AwURLRequestContextGetter(GetPath(), cookie_store_.get());
113 if (data_reduction_proxy_settings_.get()) {
114 scoped_ptr<data_reduction_proxy::DataReductionProxyConfigurator>
115 configurator(new data_reduction_proxy::DataReductionProxyConfigTracker(
116 url_request_context_getter_->proxy_config_service(),
117 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)));
118 data_reduction_proxy_settings_->SetProxyConfigurator(configurator.Pass());
120 visitedlink_master_.reset(
121 new visitedlink::VisitedLinkMaster(this, this, false));
122 visitedlink_master_->Init();
124 form_database_service_.reset(
125 new AwFormDatabaseService(context_storage_path_));
128 void AwBrowserContext::AddVisitedURLs(const std::vector<GURL>& urls) {
129 DCHECK(visitedlink_master_);
130 visitedlink_master_->AddURLs(urls);
133 net::URLRequestContextGetter* AwBrowserContext::CreateRequestContext(
134 content::ProtocolHandlerMap* protocol_handlers,
135 content::URLRequestInterceptorScopedVector request_interceptors) {
136 // This function cannot actually create the request context because
137 // there is a reentrant dependency on GetResourceContext() via
138 // content::StoragePartitionImplMap::Create(). This is not fixable
139 // until http://crbug.com/159193. Until then, assert that the context
140 // has already been allocated and just handle setting the protocol_handlers.
141 DCHECK(url_request_context_getter_);
142 url_request_context_getter_->SetHandlersAndInterceptors(
143 protocol_handlers, request_interceptors.Pass());
144 return url_request_context_getter_;
147 net::URLRequestContextGetter*
148 AwBrowserContext::CreateRequestContextForStoragePartition(
149 const base::FilePath& partition_path,
150 bool in_memory,
151 content::ProtocolHandlerMap* protocol_handlers,
152 content::URLRequestInterceptorScopedVector request_interceptors) {
153 NOTREACHED();
154 return NULL;
157 AwQuotaManagerBridge* AwBrowserContext::GetQuotaManagerBridge() {
158 if (!quota_manager_bridge_.get()) {
159 quota_manager_bridge_ = native_factory_->CreateAwQuotaManagerBridge(this);
161 return quota_manager_bridge_.get();
164 AwFormDatabaseService* AwBrowserContext::GetFormDatabaseService() {
165 return form_database_service_.get();
168 DataReductionProxySettings* AwBrowserContext::GetDataReductionProxySettings() {
169 return data_reduction_proxy_settings_.get();
172 DataReductionProxyAuthRequestHandler*
173 AwBrowserContext::GetDataReductionProxyAuthRequestHandler() {
174 return data_reduction_proxy_auth_request_handler_.get();
177 // Create user pref service for autofill functionality.
178 void AwBrowserContext::CreateUserPrefServiceIfNecessary() {
179 if (user_pref_service_)
180 return;
182 PrefRegistrySimple* pref_registry = new PrefRegistrySimple();
183 // We only use the autocomplete feature of the Autofill, which is
184 // controlled via the manager_delegate. We don't use the rest
185 // of autofill, which is why it is hardcoded as disabled here.
186 pref_registry->RegisterBooleanPref(
187 autofill::prefs::kAutofillEnabled, false);
188 pref_registry->RegisterDoublePref(
189 autofill::prefs::kAutofillPositiveUploadRate, 0.0);
190 pref_registry->RegisterDoublePref(
191 autofill::prefs::kAutofillNegativeUploadRate, 0.0);
192 data_reduction_proxy::RegisterSimpleProfilePrefs(pref_registry);
193 data_reduction_proxy::RegisterPrefs(pref_registry);
195 base::PrefServiceFactory pref_service_factory;
196 pref_service_factory.set_user_prefs(make_scoped_refptr(new AwPrefStore()));
197 pref_service_factory.set_read_error_callback(base::Bind(&HandleReadError));
198 user_pref_service_ = pref_service_factory.Create(pref_registry).Pass();
200 user_prefs::UserPrefs::Set(this, user_pref_service_.get());
202 if (data_reduction_proxy_settings_.get()) {
203 data_reduction_proxy_settings_->InitDataReductionProxySettings(
204 user_pref_service_.get(),
205 user_pref_service_.get(),
206 GetRequestContext());
208 data_reduction_proxy_settings_->SetDataReductionProxyEnabled(
209 data_reduction_proxy_enabled_);
213 base::FilePath AwBrowserContext::GetPath() const {
214 return context_storage_path_;
217 bool AwBrowserContext::IsOffTheRecord() const {
218 // Android WebView does not support off the record profile yet.
219 return false;
222 net::URLRequestContextGetter* AwBrowserContext::GetRequestContext() {
223 return GetDefaultStoragePartition(this)->GetURLRequestContext();
226 net::URLRequestContextGetter*
227 AwBrowserContext::GetRequestContextForRenderProcess(
228 int renderer_child_id) {
229 return GetRequestContext();
232 net::URLRequestContextGetter* AwBrowserContext::GetMediaRequestContext() {
233 return GetRequestContext();
236 net::URLRequestContextGetter*
237 AwBrowserContext::GetMediaRequestContextForRenderProcess(
238 int renderer_child_id) {
239 return GetRequestContext();
242 net::URLRequestContextGetter*
243 AwBrowserContext::GetMediaRequestContextForStoragePartition(
244 const base::FilePath& partition_path,
245 bool in_memory) {
246 NOTREACHED();
247 return NULL;
250 content::ResourceContext* AwBrowserContext::GetResourceContext() {
251 if (!resource_context_) {
252 resource_context_.reset(
253 new AwResourceContext(url_request_context_getter_.get()));
255 return resource_context_.get();
258 content::DownloadManagerDelegate*
259 AwBrowserContext::GetDownloadManagerDelegate() {
260 return &download_manager_delegate_;
263 content::BrowserPluginGuestManager* AwBrowserContext::GetGuestManager() {
264 return NULL;
267 quota::SpecialStoragePolicy* AwBrowserContext::GetSpecialStoragePolicy() {
268 // Intentionally returning NULL as 'Extensions' and 'Apps' not supported.
269 return NULL;
272 content::PushMessagingService* AwBrowserContext::GetPushMessagingService() {
273 // TODO(johnme): Support push messaging in WebView.
274 return NULL;
277 void AwBrowserContext::RebuildTable(
278 const scoped_refptr<URLEnumerator>& enumerator) {
279 // Android WebView rebuilds from WebChromeClient.getVisitedHistory. The client
280 // can change in the lifetime of this WebView and may not yet be set here.
281 // Therefore this initialization path is not used.
282 enumerator->OnComplete(true);
285 } // namespace android_webview