1 // Copyright (c) 2013 Intel Corporation. All rights reserved.
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
6 #include "xwalk/runtime/browser/runtime_context.h"
12 #include "base/command_line.h"
13 #include "base/logging.h"
14 #include "base/path_service.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "components/visitedlink/browser/visitedlink_master.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "content/public/browser/render_process_host.h"
19 #include "content/public/browser/resource_context.h"
20 #include "content/public/browser/storage_partition.h"
21 #include "content/public/browser/web_contents.h"
22 #include "content/public/common/content_switches.h"
23 #include "xwalk/application/browser/application.h"
24 #include "xwalk/application/browser/application_protocols.h"
25 #include "xwalk/application/browser/application_service.h"
26 #include "xwalk/application/browser/application_system.h"
27 #include "xwalk/application/common/constants.h"
28 #include "xwalk/runtime/browser/runtime_download_manager_delegate.h"
29 #include "xwalk/runtime/browser/runtime_geolocation_permission_context.h"
30 #include "xwalk/runtime/browser/runtime_url_request_context_getter.h"
31 #include "xwalk/runtime/browser/xwalk_runner.h"
32 #include "xwalk/runtime/common/xwalk_paths.h"
33 #include "xwalk/runtime/common/xwalk_switches.h"
35 #if defined(OS_ANDROID)
36 #include "base/strings/string_split.h"
39 using content::BrowserThread;
40 using content::DownloadManager;
44 class RuntimeContext::RuntimeResourceContext : public content::ResourceContext {
46 RuntimeResourceContext() : getter_(NULL) {}
47 virtual ~RuntimeResourceContext() {}
49 // ResourceContext implementation:
50 virtual net::HostResolver* GetHostResolver() OVERRIDE {
52 return getter_->host_resolver();
54 virtual net::URLRequestContext* GetRequestContext() OVERRIDE {
56 return getter_->GetURLRequestContext();
59 virtual bool AllowMicAccess(const GURL& origin) OVERRIDE { return false; }
60 virtual bool AllowCameraAccess(const GURL& origin) OVERRIDE { return false; }
62 void set_url_request_context_getter(RuntimeURLRequestContextGetter* getter) {
67 RuntimeURLRequestContextGetter* getter_;
69 DISALLOW_COPY_AND_ASSIGN(RuntimeResourceContext);
72 RuntimeContext::RuntimeContext()
73 : resource_context_(new RuntimeResourceContext) {
75 #if defined(OS_ANDROID)
76 InitVisitedLinkMaster();
80 RuntimeContext::~RuntimeContext() {
81 if (resource_context_.get()) {
82 BrowserThread::DeleteSoon(
83 BrowserThread::IO, FROM_HERE, resource_context_.release());
88 RuntimeContext* RuntimeContext::FromWebContents(
89 content::WebContents* web_contents) {
90 // This is safe; this is the only implementation of the browser context.
91 return static_cast<RuntimeContext*>(web_contents->GetBrowserContext());
94 void RuntimeContext::InitWhileIOAllowed() {
95 CommandLine* cmd_line = CommandLine::ForCurrentProcess();
96 if (cmd_line->HasSwitch(switches::kXWalkDataPath)) {
98 cmd_line->GetSwitchValuePath(switches::kXWalkDataPath);
99 PathService::OverrideAndCreateIfNeeded(
100 xwalk::DIR_DATA_PATH, path, false, true);
104 base::FilePath RuntimeContext::GetPath() const {
105 base::FilePath result;
106 #if defined(OS_ANDROID)
107 CHECK(PathService::Get(base::DIR_ANDROID_APP_DATA, &result));
108 CommandLine* cmd_line = CommandLine::ForCurrentProcess();
109 if (cmd_line->HasSwitch(switches::kXWalkProfileName))
110 result = result.Append(
111 cmd_line->GetSwitchValuePath(switches::kXWalkProfileName));
113 CHECK(PathService::Get(xwalk::DIR_DATA_PATH, &result));
118 bool RuntimeContext::IsOffTheRecord() const {
119 // We don't consider off the record scenario.
123 content::DownloadManagerDelegate* RuntimeContext::GetDownloadManagerDelegate() {
124 content::DownloadManager* manager = BrowserContext::GetDownloadManager(this);
126 if (!download_manager_delegate_.get()) {
127 download_manager_delegate_ = new RuntimeDownloadManagerDelegate();
128 download_manager_delegate_->SetDownloadManager(manager);
131 return download_manager_delegate_.get();
134 net::URLRequestContextGetter* RuntimeContext::GetRequestContext() {
135 return GetDefaultStoragePartition(this)->GetURLRequestContext();
138 net::URLRequestContextGetter*
139 RuntimeContext::GetRequestContextForRenderProcess(
140 int renderer_child_id) {
141 #if defined(OS_ANDROID)
142 return GetRequestContext();
144 content::RenderProcessHost* rph =
145 content::RenderProcessHost::FromID(renderer_child_id);
146 return rph->GetStoragePartition()->GetURLRequestContext();
150 net::URLRequestContextGetter* RuntimeContext::GetMediaRequestContext() {
151 return GetRequestContext();
154 net::URLRequestContextGetter*
155 RuntimeContext::GetMediaRequestContextForRenderProcess(
156 int renderer_child_id) {
157 #if defined(OS_ANDROID)
158 return GetRequestContext();
160 content::RenderProcessHost* rph =
161 content::RenderProcessHost::FromID(renderer_child_id);
162 return rph->GetStoragePartition()->GetURLRequestContext();
166 net::URLRequestContextGetter*
167 RuntimeContext::GetMediaRequestContextForStoragePartition(
168 const base::FilePath& partition_path,
170 #if defined(OS_ANDROID)
171 return GetRequestContext();
173 PartitionPathContextGetterMap::iterator iter =
174 context_getters_.find(partition_path.value());
175 CHECK(iter != context_getters_.end());
176 return iter->second.get();
180 content::ResourceContext* RuntimeContext::GetResourceContext() {
181 return resource_context_.get();
184 content::BrowserPluginGuestManager*
185 RuntimeContext::GetGuestManager() {
189 quota::SpecialStoragePolicy* RuntimeContext::GetSpecialStoragePolicy() {
193 content::PushMessagingService* RuntimeContext::GetPushMessagingService() {
197 content::SSLHostStateDelegate* RuntimeContext::GetSSLHostStateDelegate() {
201 RuntimeURLRequestContextGetter* RuntimeContext::GetURLRequestContextGetterById(
202 const std::string& pkg_id) {
203 for (PartitionPathContextGetterMap::iterator it = context_getters_.begin();
204 it != context_getters_.end(); ++it) {
205 if (it->first.find(pkg_id))
206 return it->second.get();
211 net::URLRequestContextGetter* RuntimeContext::CreateRequestContext(
212 content::ProtocolHandlerMap* protocol_handlers,
213 content::URLRequestInterceptorScopedVector request_interceptors) {
214 DCHECK(!url_request_getter_.get());
216 application::ApplicationService* service =
217 XWalkRunner::GetInstance()->app_system()->application_service();
218 protocol_handlers->insert(std::pair<std::string,
219 linked_ptr<net::URLRequestJobFactory::ProtocolHandler> >(
220 application::kApplicationScheme,
221 application::CreateApplicationProtocolHandler(service)));
223 url_request_getter_ = new RuntimeURLRequestContextGetter(
224 false, /* ignore_certificate_error = false */
226 BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::IO),
227 BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::FILE),
228 protocol_handlers, request_interceptors.Pass());
229 resource_context_->set_url_request_context_getter(url_request_getter_.get());
230 return url_request_getter_.get();
233 net::URLRequestContextGetter*
234 RuntimeContext::CreateRequestContextForStoragePartition(
235 const base::FilePath& partition_path,
237 content::ProtocolHandlerMap* protocol_handlers,
238 content::URLRequestInterceptorScopedVector request_interceptors) {
239 #if defined(OS_ANDROID)
242 PartitionPathContextGetterMap::iterator iter =
243 context_getters_.find(partition_path.value());
244 if (iter != context_getters_.end())
245 return iter->second.get();
247 application::ApplicationService* service =
248 XWalkRunner::GetInstance()->app_system()->application_service();
249 protocol_handlers->insert(std::pair<std::string,
250 linked_ptr<net::URLRequestJobFactory::ProtocolHandler> >(
251 application::kApplicationScheme,
252 application::CreateApplicationProtocolHandler(service)));
254 scoped_refptr<RuntimeURLRequestContextGetter>
255 context_getter = new RuntimeURLRequestContextGetter(
256 false, /* ignore_certificate_error = false */
258 BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::IO),
259 BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::FILE),
260 protocol_handlers, request_interceptors.Pass());
262 context_getters_.insert(
263 std::make_pair(partition_path.value(), context_getter));
264 return context_getter.get();
268 #if defined(OS_ANDROID)
269 void RuntimeContext::SetCSPString(const std::string& csp) {
270 // Check format of csp string.
271 std::vector<std::string> policies;
272 base::SplitString(csp, ';', &policies);
273 for (size_t i = 0; i < policies.size(); ++i) {
274 size_t found = policies[i].find(' ');
275 if (found == std::string::npos) {
276 LOG(INFO) << "Invalid value of directive: " << policies[i];
283 std::string RuntimeContext::GetCSPString() const {
287 void RuntimeContext::InitVisitedLinkMaster() {
288 visitedlink_master_.reset(
289 new visitedlink::VisitedLinkMaster(this, this, false));
290 visitedlink_master_->Init();
293 void RuntimeContext::AddVisitedURLs(const std::vector<GURL>& urls) {
294 DCHECK(visitedlink_master_.get());
295 visitedlink_master_->AddURLs(urls);
298 void RuntimeContext::RebuildTable(
299 const scoped_refptr<URLEnumerator>& enumerator) {
300 // XWalkView rebuilds from XWalkWebChromeClient.getVisitedHistory. The client
301 // can change in the lifetime of this XWalkView and may not yet be set here.
302 // Therefore this initialization path is not used.
303 enumerator->OnComplete(true);