1 // Copyright (c) 2013 Intel Corporation. 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 "xwalk/runtime/browser/runtime_context.h"
11 #include "base/command_line.h"
12 #include "base/logging.h"
13 #include "base/path_service.h"
14 #include "components/visitedlink/browser/visitedlink_master.h"
15 #include "content/public/browser/browser_thread.h"
16 #include "content/public/browser/render_process_host.h"
17 #include "content/public/browser/resource_context.h"
18 #include "content/public/browser/storage_partition.h"
19 #include "content/public/browser/web_contents.h"
20 #include "content/public/common/content_switches.h"
21 #include "xwalk/application/browser/application.h"
22 #include "xwalk/application/browser/application_protocols.h"
23 #include "xwalk/application/browser/application_service.h"
24 #include "xwalk/application/browser/application_system.h"
25 #include "xwalk/application/common/constants.h"
26 #include "xwalk/runtime/browser/runtime_download_manager_delegate.h"
27 #include "xwalk/runtime/browser/runtime_geolocation_permission_context.h"
28 #include "xwalk/runtime/browser/runtime_url_request_context_getter.h"
29 #include "xwalk/runtime/browser/xwalk_runner.h"
30 #include "xwalk/runtime/common/xwalk_paths.h"
31 #include "xwalk/runtime/common/xwalk_switches.h"
33 #if defined(OS_ANDROID)
34 #include "base/strings/string_split.h"
37 using content::BrowserThread;
38 using content::DownloadManager;
42 class RuntimeContext::RuntimeResourceContext : public content::ResourceContext {
44 RuntimeResourceContext() : getter_(NULL) {}
45 virtual ~RuntimeResourceContext() {}
47 // ResourceContext implementation:
48 virtual net::HostResolver* GetHostResolver() OVERRIDE {
50 return getter_->host_resolver();
52 virtual net::URLRequestContext* GetRequestContext() OVERRIDE {
54 return getter_->GetURLRequestContext();
57 virtual bool AllowMicAccess(const GURL& origin) OVERRIDE { return false; }
58 virtual bool AllowCameraAccess(const GURL& origin) OVERRIDE { return false; }
60 void set_url_request_context_getter(RuntimeURLRequestContextGetter* getter) {
65 RuntimeURLRequestContextGetter* getter_;
67 DISALLOW_COPY_AND_ASSIGN(RuntimeResourceContext);
70 RuntimeContext::RuntimeContext()
71 : resource_context_(new RuntimeResourceContext) {
73 #if defined(OS_ANDROID)
74 InitVisitedLinkMaster();
78 RuntimeContext::~RuntimeContext() {
79 if (resource_context_) {
80 BrowserThread::DeleteSoon(
81 BrowserThread::IO, FROM_HERE, resource_context_.release());
86 RuntimeContext* RuntimeContext::FromWebContents(
87 content::WebContents* web_contents) {
88 // This is safe; this is the only implementation of the browser context.
89 return static_cast<RuntimeContext*>(web_contents->GetBrowserContext());
92 void RuntimeContext::InitWhileIOAllowed() {
93 CommandLine* cmd_line = CommandLine::ForCurrentProcess();
94 if (cmd_line->HasSwitch(switches::kXWalkDataPath)) {
96 cmd_line->GetSwitchValuePath(switches::kXWalkDataPath);
97 PathService::OverrideAndCreateIfNeeded(
98 xwalk::DIR_DATA_PATH, path, false, true);
102 base::FilePath RuntimeContext::GetPath() const {
103 base::FilePath result;
104 #if defined(OS_ANDROID)
105 CHECK(PathService::Get(base::DIR_ANDROID_APP_DATA, &result));
107 CHECK(PathService::Get(xwalk::DIR_DATA_PATH, &result));
112 bool RuntimeContext::IsOffTheRecord() const {
113 // We don't consider off the record scenario.
117 content::DownloadManagerDelegate* RuntimeContext::GetDownloadManagerDelegate() {
118 content::DownloadManager* manager = BrowserContext::GetDownloadManager(this);
120 if (!download_manager_delegate_) {
121 download_manager_delegate_ = new RuntimeDownloadManagerDelegate();
122 download_manager_delegate_->SetDownloadManager(manager);
125 return download_manager_delegate_.get();
128 net::URLRequestContextGetter* RuntimeContext::GetRequestContext() {
129 return GetDefaultStoragePartition(this)->GetURLRequestContext();
132 net::URLRequestContextGetter*
133 RuntimeContext::GetRequestContextForRenderProcess(
134 int renderer_child_id) {
135 #if defined(OS_ANDROID)
136 return GetRequestContext();
138 content::RenderProcessHost* rph =
139 content::RenderProcessHost::FromID(renderer_child_id);
140 return rph->GetStoragePartition()->GetURLRequestContext();
144 net::URLRequestContextGetter* RuntimeContext::GetMediaRequestContext() {
145 return GetRequestContext();
148 net::URLRequestContextGetter*
149 RuntimeContext::GetMediaRequestContextForRenderProcess(
150 int renderer_child_id) {
151 #if defined(OS_ANDROID)
152 return GetRequestContext();
154 content::RenderProcessHost* rph =
155 content::RenderProcessHost::FromID(renderer_child_id);
156 return rph->GetStoragePartition()->GetURLRequestContext();
160 net::URLRequestContextGetter*
161 RuntimeContext::GetMediaRequestContextForStoragePartition(
162 const base::FilePath& partition_path,
164 #if defined(OS_ANDROID)
165 return GetRequestContext();
167 PartitionPathContextGetterMap::iterator iter =
168 context_getters_.find(partition_path.value());
169 CHECK(iter != context_getters_.end());
170 return iter->second.get();
174 content::ResourceContext* RuntimeContext::GetResourceContext() {
175 return resource_context_.get();
178 content::GeolocationPermissionContext*
179 RuntimeContext::GetGeolocationPermissionContext() {
180 #if defined(OS_ANDROID) || defined(OS_TIZEN)
181 if (!geolocation_permission_context_) {
182 geolocation_permission_context_ =
183 RuntimeGeolocationPermissionContext::Create(this);
186 // TODO(yongsheng): Create geolcation permission context for other platforms.
187 return geolocation_permission_context_.get();
190 content::BrowserPluginGuestManagerDelegate*
191 RuntimeContext::GetGuestManagerDelegate() {
195 quota::SpecialStoragePolicy* RuntimeContext::GetSpecialStoragePolicy() {
199 void RuntimeContext::RequestProtectedMediaIdentifierPermission(
200 int render_process_id,
204 const GURL& requesting_frame,
205 const ProtectedMediaIdentifierPermissionCallback& callback) {
210 void RuntimeContext::CancelProtectedMediaIdentifierPermissionRequests(
215 net::URLRequestContextGetter* RuntimeContext::CreateRequestContext(
216 content::ProtocolHandlerMap* protocol_handlers,
217 content::ProtocolHandlerScopedVector protocol_interceptors) {
218 DCHECK(!url_request_getter_);
220 application::ApplicationService* service =
221 XWalkRunner::GetInstance()->app_system()->application_service();
222 protocol_handlers->insert(std::pair<std::string,
223 linked_ptr<net::URLRequestJobFactory::ProtocolHandler> >(
224 application::kApplicationScheme,
225 application::CreateApplicationProtocolHandler(service)));
227 url_request_getter_ = new RuntimeURLRequestContextGetter(
228 false, /* ignore_certificate_error = false */
230 BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::IO),
231 BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::FILE),
232 protocol_handlers, protocol_interceptors.Pass());
233 resource_context_->set_url_request_context_getter(url_request_getter_.get());
234 return url_request_getter_.get();
237 net::URLRequestContextGetter*
238 RuntimeContext::CreateRequestContextForStoragePartition(
239 const base::FilePath& partition_path,
241 content::ProtocolHandlerMap* protocol_handlers,
242 content::ProtocolHandlerScopedVector protocol_interceptors) {
243 #if defined(OS_ANDROID)
246 PartitionPathContextGetterMap::iterator iter =
247 context_getters_.find(partition_path.value());
248 if (iter != context_getters_.end())
249 return iter->second.get();
251 application::ApplicationService* service =
252 XWalkRunner::GetInstance()->app_system()->application_service();
253 protocol_handlers->insert(std::pair<std::string,
254 linked_ptr<net::URLRequestJobFactory::ProtocolHandler> >(
255 application::kApplicationScheme,
256 application::CreateApplicationProtocolHandler(service)));
258 scoped_refptr<RuntimeURLRequestContextGetter>
259 context_getter = new RuntimeURLRequestContextGetter(
260 false, /* ignore_certificate_error = false */
262 BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::IO),
263 BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::FILE),
264 protocol_handlers, protocol_interceptors.Pass());
266 context_getters_.insert(
267 std::make_pair(partition_path.value(), context_getter));
268 return context_getter.get();
272 #if defined(OS_ANDROID)
273 void RuntimeContext::SetCSPString(const std::string& csp) {
274 // Check format of csp string.
275 std::vector<std::string> policies;
276 base::SplitString(csp, ';', &policies);
277 for (size_t i = 0; i < policies.size(); ++i) {
278 size_t found = policies[i].find(' ');
279 if (found == std::string::npos) {
280 LOG(INFO) << "Invalid value of directive: " << policies[i];
287 std::string RuntimeContext::GetCSPString() const {
291 void RuntimeContext::InitVisitedLinkMaster() {
292 visitedlink_master_.reset(
293 new visitedlink::VisitedLinkMaster(this, this, false));
294 visitedlink_master_->Init();
297 void RuntimeContext::AddVisitedURLs(const std::vector<GURL>& urls) {
298 DCHECK(visitedlink_master_);
299 visitedlink_master_->AddURLs(urls);
302 void RuntimeContext::RebuildTable(
303 const scoped_refptr<URLEnumerator>& enumerator) {
304 // XWalkView rebuilds from XWalkWebChromeClient.getVisitedHistory. The client
305 // can change in the lifetime of this XWalkView and may not yet be set here.
306 // Therefore this initialization path is not used.
307 enumerator->OnComplete(true);