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_quota_manager_bridge.h"
8 #include "android_webview/browser/jni_dependency_factory.h"
9 #include "android_webview/browser/net/aw_url_request_context_getter.h"
10 #include "components/visitedlink/browser/visitedlink_master.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "content/public/browser/resource_context.h"
13 #include "content/public/browser/storage_partition.h"
14 #include "content/public/browser/web_contents.h"
15 #include "net/url_request/url_request_context.h"
17 namespace android_webview
{
21 class AwResourceContext
: public content::ResourceContext
{
23 explicit AwResourceContext(net::URLRequestContextGetter
* getter
)
25 virtual ~AwResourceContext() {}
27 // content::ResourceContext implementation.
28 virtual net::HostResolver
* GetHostResolver() OVERRIDE
{
29 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO
));
30 return getter_
->GetURLRequestContext()->host_resolver();
32 virtual net::URLRequestContext
* GetRequestContext() OVERRIDE
{
33 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO
));
34 return getter_
->GetURLRequestContext();
38 net::URLRequestContextGetter
* getter_
;
40 DISALLOW_COPY_AND_ASSIGN(AwResourceContext
);
45 AwBrowserContext::AwBrowserContext(
46 const base::FilePath path
,
47 JniDependencyFactory
* native_factory
)
48 : context_storage_path_(path
),
49 native_factory_(native_factory
) {
52 AwBrowserContext::~AwBrowserContext() {
56 AwBrowserContext
* AwBrowserContext::FromWebContents(
57 content::WebContents
* web_contents
) {
58 // This is safe; this is the only implementation of the browser context.
59 return static_cast<AwBrowserContext
*>(web_contents
->GetBrowserContext());
62 void AwBrowserContext::InitializeBeforeThreadCreation() {
63 DCHECK(!url_request_context_getter_
);
64 url_request_context_getter_
= new AwURLRequestContextGetter(this);
67 void AwBrowserContext::PreMainMessageLoopRun() {
68 visitedlink_master_
.reset(
69 new components::VisitedLinkMaster(this, this, false));
70 visitedlink_master_
->Init();
73 void AwBrowserContext::AddVisitedURLs(const std::vector
<GURL
>& urls
) {
74 DCHECK(visitedlink_master_
);
75 visitedlink_master_
->AddURLs(urls
);
78 net::URLRequestContextGetter
* AwBrowserContext::CreateRequestContext(
79 content::ProtocolHandlerMap
* protocol_handlers
) {
80 CHECK(url_request_context_getter_
);
81 url_request_context_getter_
->SetProtocolHandlers(protocol_handlers
);
82 return url_request_context_getter_
.get();
85 net::URLRequestContextGetter
*
86 AwBrowserContext::CreateRequestContextForStoragePartition(
87 const base::FilePath
& partition_path
,
89 content::ProtocolHandlerMap
* protocol_handlers
) {
90 CHECK(url_request_context_getter_
);
91 return url_request_context_getter_
.get();
94 AwQuotaManagerBridge
* AwBrowserContext::GetQuotaManagerBridge() {
95 if (!quota_manager_bridge_
) {
96 quota_manager_bridge_
.reset(
97 native_factory_
->CreateAwQuotaManagerBridge(this));
99 return quota_manager_bridge_
.get();
102 base::FilePath
AwBrowserContext::GetPath() {
103 return context_storage_path_
;
106 bool AwBrowserContext::IsOffTheRecord() const {
107 // Android WebView does not support off the record profile yet.
111 net::URLRequestContextGetter
* AwBrowserContext::GetRequestContext() {
112 return GetDefaultStoragePartition(this)->GetURLRequestContext();
115 net::URLRequestContextGetter
*
116 AwBrowserContext::GetRequestContextForRenderProcess(
117 int renderer_child_id
) {
118 return GetRequestContext();
121 net::URLRequestContextGetter
* AwBrowserContext::GetMediaRequestContext() {
122 return GetRequestContext();
125 net::URLRequestContextGetter
*
126 AwBrowserContext::GetMediaRequestContextForRenderProcess(
127 int renderer_child_id
) {
128 return GetRequestContext();
131 net::URLRequestContextGetter
*
132 AwBrowserContext::GetMediaRequestContextForStoragePartition(
133 const base::FilePath
& partition_path
,
135 return GetRequestContext();
138 content::ResourceContext
* AwBrowserContext::GetResourceContext() {
139 if (!resource_context_
) {
140 CHECK(url_request_context_getter_
);
141 resource_context_
.reset(new AwResourceContext(
142 url_request_context_getter_
.get()));
144 return resource_context_
.get();
147 content::DownloadManagerDelegate
*
148 AwBrowserContext::GetDownloadManagerDelegate() {
149 return &download_manager_delegate_
;
152 content::GeolocationPermissionContext
*
153 AwBrowserContext::GetGeolocationPermissionContext() {
154 if (!geolocation_permission_context_
) {
155 geolocation_permission_context_
=
156 native_factory_
->CreateGeolocationPermission(this);
158 return geolocation_permission_context_
;
161 content::SpeechRecognitionPreferences
*
162 AwBrowserContext::GetSpeechRecognitionPreferences() {
163 // By default allows profanities in speech recognition if return NULL.
167 quota::SpecialStoragePolicy
* AwBrowserContext::GetSpecialStoragePolicy() {
168 // TODO(boliu): Implement this so we are not relying on default behavior.
173 void AwBrowserContext::RebuildTable(
174 const scoped_refptr
<URLEnumerator
>& enumerator
) {
175 // Android WebView rebuilds from WebChromeClient.getVisitedHistory. The client
176 // can change in the lifetime of this WebView and may not yet be set here.
177 // Therefore this initialization path is not used.
178 enumerator
->OnComplete(true);
181 } // namespace android_webview