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 "content/public/browser/browser_thread.h"
15 #include "content/public/browser/resource_context.h"
16 #include "content/public/browser/storage_partition.h"
17 #include "content/public/browser/web_contents.h"
18 #include "content/public/common/content_switches.h"
19 #include "xwalk/application/browser/application.h"
20 #include "xwalk/application/browser/application_protocols.h"
21 #include "xwalk/application/browser/application_service.h"
22 #include "xwalk/application/browser/application_system.h"
23 #include "xwalk/application/common/constants.h"
24 #include "xwalk/runtime/browser/runtime_download_manager_delegate.h"
25 #include "xwalk/runtime/browser/runtime_geolocation_permission_context.h"
26 #include "xwalk/runtime/browser/runtime_url_request_context_getter.h"
27 #include "xwalk/runtime/browser/xwalk_runner.h"
28 #include "xwalk/runtime/common/xwalk_paths.h"
29 #include "xwalk/runtime/common/xwalk_switches.h"
31 #if defined(OS_ANDROID)
32 #include "base/strings/string_split.h"
35 using content::BrowserThread;
36 using content::DownloadManager;
40 class RuntimeContext::RuntimeResourceContext : public content::ResourceContext {
42 RuntimeResourceContext() : getter_(NULL) {}
43 virtual ~RuntimeResourceContext() {}
45 // ResourceContext implementation:
46 virtual net::HostResolver* GetHostResolver() OVERRIDE {
48 return getter_->host_resolver();
50 virtual net::URLRequestContext* GetRequestContext() OVERRIDE {
52 return getter_->GetURLRequestContext();
55 virtual bool AllowMicAccess(const GURL& origin) OVERRIDE { return false; }
56 virtual bool AllowCameraAccess(const GURL& origin) OVERRIDE { return false; }
58 void set_url_request_context_getter(RuntimeURLRequestContextGetter* getter) {
63 RuntimeURLRequestContextGetter* getter_;
65 DISALLOW_COPY_AND_ASSIGN(RuntimeResourceContext);
68 RuntimeContext::RuntimeContext()
69 : resource_context_(new RuntimeResourceContext) {
73 RuntimeContext::~RuntimeContext() {
74 if (resource_context_) {
75 BrowserThread::DeleteSoon(
76 BrowserThread::IO, FROM_HERE, resource_context_.release());
81 RuntimeContext* RuntimeContext::FromWebContents(
82 content::WebContents* web_contents) {
83 // This is safe; this is the only implementation of the browser context.
84 return static_cast<RuntimeContext*>(web_contents->GetBrowserContext());
87 void RuntimeContext::InitWhileIOAllowed() {
88 CommandLine* cmd_line = CommandLine::ForCurrentProcess();
89 if (cmd_line->HasSwitch(switches::kXWalkDataPath)) {
91 cmd_line->GetSwitchValuePath(switches::kXWalkDataPath);
92 PathService::OverrideAndCreateIfNeeded(xwalk::DIR_DATA_PATH, path, true);
96 base::FilePath RuntimeContext::GetPath() const {
97 base::FilePath result;
98 #if defined(OS_ANDROID)
99 CHECK(PathService::Get(base::DIR_ANDROID_APP_DATA, &result));
101 CHECK(PathService::Get(xwalk::DIR_DATA_PATH, &result));
106 bool RuntimeContext::IsOffTheRecord() const {
107 // We don't consider off the record scenario.
111 content::DownloadManagerDelegate* RuntimeContext::GetDownloadManagerDelegate() {
112 content::DownloadManager* manager = BrowserContext::GetDownloadManager(this);
114 if (!download_manager_delegate_) {
115 download_manager_delegate_ = new RuntimeDownloadManagerDelegate();
116 download_manager_delegate_->SetDownloadManager(manager);
119 return download_manager_delegate_.get();
122 net::URLRequestContextGetter* RuntimeContext::GetRequestContext() {
123 return GetDefaultStoragePartition(this)->GetURLRequestContext();
126 net::URLRequestContextGetter*
127 RuntimeContext::GetRequestContextForRenderProcess(
128 int renderer_child_id) {
129 return GetRequestContext();
132 net::URLRequestContextGetter* RuntimeContext::GetMediaRequestContext() {
133 return GetRequestContext();
136 net::URLRequestContextGetter*
137 RuntimeContext::GetMediaRequestContextForRenderProcess(
138 int renderer_child_id) {
139 return GetRequestContext();
142 net::URLRequestContextGetter*
143 RuntimeContext::GetMediaRequestContextForStoragePartition(
144 const base::FilePath& partition_path,
146 return GetRequestContext();
149 content::ResourceContext* RuntimeContext::GetResourceContext() {
150 return resource_context_.get();
153 content::GeolocationPermissionContext*
154 RuntimeContext::GetGeolocationPermissionContext() {
155 #if defined(OS_ANDROID) || defined(OS_TIZEN)
156 if (!geolocation_permission_context_) {
157 geolocation_permission_context_ =
158 RuntimeGeolocationPermissionContext::Create(this);
161 // TODO(yongsheng): Create geolcation permission context for other platforms.
162 return geolocation_permission_context_.get();
165 quota::SpecialStoragePolicy* RuntimeContext::GetSpecialStoragePolicy() {
169 void RuntimeContext::RequestProtectedMediaIdentifierPermission(
170 int render_process_id,
174 const GURL& requesting_frame,
175 const ProtectedMediaIdentifierPermissionCallback& callback) {
180 void RuntimeContext::CancelProtectedMediaIdentifierPermissionRequests(
185 net::URLRequestContextGetter* RuntimeContext::CreateRequestContext(
186 content::ProtocolHandlerMap* protocol_handlers) {
187 DCHECK(!url_request_getter_);
189 application::ApplicationService* service =
190 XWalkRunner::GetInstance()->app_system()->application_service();
191 protocol_handlers->insert(std::pair<std::string,
192 linked_ptr<net::URLRequestJobFactory::ProtocolHandler> >(
193 application::kApplicationScheme,
194 application::CreateApplicationProtocolHandler(service)));
196 url_request_getter_ = new RuntimeURLRequestContextGetter(
197 false, /* ignore_certificate_error = false */
199 BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::IO),
200 BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::FILE),
202 resource_context_->set_url_request_context_getter(url_request_getter_.get());
203 return url_request_getter_.get();
206 net::URLRequestContextGetter*
207 RuntimeContext::CreateRequestContextForStoragePartition(
208 const base::FilePath& partition_path,
210 content::ProtocolHandlerMap* protocol_handlers) {
214 #if defined(OS_ANDROID)
215 void RuntimeContext::SetCSPString(const std::string& csp) {
216 // Check format of csp string.
217 std::vector<std::string> policies;
218 base::SplitString(csp, ';', &policies);
219 for (size_t i = 0; i < policies.size(); ++i) {
220 size_t found = policies[i].find(' ');
221 if (found == std::string::npos) {
222 LOG(INFO) << "Invalid value of directive: " << policies[i];
229 std::string RuntimeContext::GetCSPString() const {