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 "base/strings/utf_string_conversions.h"
15 #include "components/visitedlink/browser/visitedlink_master.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "content/public/browser/render_process_host.h"
18 #include "content/public/browser/resource_context.h"
19 #include "content/public/browser/storage_partition.h"
20 #include "content/public/browser/web_contents.h"
21 #include "content/public/common/content_switches.h"
22 #include "xwalk/application/browser/application.h"
23 #include "xwalk/application/browser/application_protocols.h"
24 #include "xwalk/application/browser/application_service.h"
25 #include "xwalk/application/browser/application_system.h"
26 #include "xwalk/application/common/constants.h"
27 #include "xwalk/runtime/browser/runtime_download_manager_delegate.h"
28 #include "xwalk/runtime/browser/runtime_geolocation_permission_context.h"
29 #include "xwalk/runtime/browser/runtime_url_request_context_getter.h"
30 #include "xwalk/runtime/browser/xwalk_runner.h"
31 #include "xwalk/runtime/common/xwalk_paths.h"
32 #include "xwalk/runtime/common/xwalk_switches.h"
34 #if defined(OS_ANDROID)
35 #include "base/strings/string_split.h"
38 using content::BrowserThread;
39 using content::DownloadManager;
43 class RuntimeContext::RuntimeResourceContext : public content::ResourceContext {
45 RuntimeResourceContext() : getter_(NULL) {}
46 virtual ~RuntimeResourceContext() {}
48 // ResourceContext implementation:
49 virtual net::HostResolver* GetHostResolver() OVERRIDE {
51 return getter_->host_resolver();
53 virtual net::URLRequestContext* GetRequestContext() OVERRIDE {
55 return getter_->GetURLRequestContext();
58 virtual bool AllowMicAccess(const GURL& origin) OVERRIDE { return false; }
59 virtual bool AllowCameraAccess(const GURL& origin) OVERRIDE { return false; }
61 void set_url_request_context_getter(RuntimeURLRequestContextGetter* getter) {
66 RuntimeURLRequestContextGetter* getter_;
68 DISALLOW_COPY_AND_ASSIGN(RuntimeResourceContext);
71 RuntimeContext::RuntimeContext()
72 : resource_context_(new RuntimeResourceContext) {
74 #if defined(OS_ANDROID)
75 InitVisitedLinkMaster();
79 RuntimeContext::~RuntimeContext() {
80 if (resource_context_.get()) {
81 BrowserThread::DeleteSoon(
82 BrowserThread::IO, FROM_HERE, resource_context_.release());
87 RuntimeContext* RuntimeContext::FromWebContents(
88 content::WebContents* web_contents) {
89 // This is safe; this is the only implementation of the browser context.
90 return static_cast<RuntimeContext*>(web_contents->GetBrowserContext());
93 void RuntimeContext::InitWhileIOAllowed() {
94 CommandLine* cmd_line = CommandLine::ForCurrentProcess();
95 if (cmd_line->HasSwitch(switches::kXWalkDataPath)) {
97 cmd_line->GetSwitchValuePath(switches::kXWalkDataPath);
98 PathService::OverrideAndCreateIfNeeded(
99 xwalk::DIR_DATA_PATH, path, false, true);
103 base::FilePath RuntimeContext::GetPath() const {
104 base::FilePath result;
105 #if defined(OS_ANDROID)
106 CHECK(PathService::Get(base::DIR_ANDROID_APP_DATA, &result));
107 CommandLine* cmd_line = CommandLine::ForCurrentProcess();
108 if (cmd_line->HasSwitch(switches::kXWalkProfileName))
109 result = result.Append(
110 cmd_line->GetSwitchValuePath(switches::kXWalkProfileName));
112 CHECK(PathService::Get(xwalk::DIR_DATA_PATH, &result));
117 bool RuntimeContext::IsOffTheRecord() const {
118 // We don't consider off the record scenario.
122 content::DownloadManagerDelegate* RuntimeContext::GetDownloadManagerDelegate() {
123 content::DownloadManager* manager = BrowserContext::GetDownloadManager(this);
125 if (!download_manager_delegate_.get()) {
126 download_manager_delegate_ = new RuntimeDownloadManagerDelegate();
127 download_manager_delegate_->SetDownloadManager(manager);
130 return download_manager_delegate_.get();
133 net::URLRequestContextGetter* RuntimeContext::GetRequestContext() {
134 return GetDefaultStoragePartition(this)->GetURLRequestContext();
137 net::URLRequestContextGetter*
138 RuntimeContext::GetRequestContextForRenderProcess(
139 int renderer_child_id) {
140 #if defined(OS_ANDROID)
141 return GetRequestContext();
143 content::RenderProcessHost* rph =
144 content::RenderProcessHost::FromID(renderer_child_id);
145 return rph->GetStoragePartition()->GetURLRequestContext();
149 net::URLRequestContextGetter* RuntimeContext::GetMediaRequestContext() {
150 return GetRequestContext();
153 net::URLRequestContextGetter*
154 RuntimeContext::GetMediaRequestContextForRenderProcess(
155 int renderer_child_id) {
156 #if defined(OS_ANDROID)
157 return GetRequestContext();
159 content::RenderProcessHost* rph =
160 content::RenderProcessHost::FromID(renderer_child_id);
161 return rph->GetStoragePartition()->GetURLRequestContext();
165 net::URLRequestContextGetter*
166 RuntimeContext::GetMediaRequestContextForStoragePartition(
167 const base::FilePath& partition_path,
169 #if defined(OS_ANDROID)
170 return GetRequestContext();
172 PartitionPathContextGetterMap::iterator iter =
173 context_getters_.find(partition_path.value());
174 CHECK(iter != context_getters_.end());
175 return iter->second.get();
179 content::ResourceContext* RuntimeContext::GetResourceContext() {
180 return resource_context_.get();
183 content::BrowserPluginGuestManager*
184 RuntimeContext::GetGuestManager() {
188 quota::SpecialStoragePolicy* RuntimeContext::GetSpecialStoragePolicy() {
192 content::PushMessagingService* RuntimeContext::GetPushMessagingService() {
196 content::SSLHostStateDelegate* RuntimeContext::GetSSLHostStateDelegate() {
200 net::URLRequestContextGetter* RuntimeContext::CreateRequestContext(
201 content::ProtocolHandlerMap* protocol_handlers,
202 content::URLRequestInterceptorScopedVector request_interceptors) {
203 DCHECK(!url_request_getter_.get());
205 application::ApplicationService* service =
206 XWalkRunner::GetInstance()->app_system()->application_service();
207 protocol_handlers->insert(std::pair<std::string,
208 linked_ptr<net::URLRequestJobFactory::ProtocolHandler> >(
209 application::kApplicationScheme,
210 application::CreateApplicationProtocolHandler(service)));
212 url_request_getter_ = new RuntimeURLRequestContextGetter(
213 false, /* ignore_certificate_error = false */
215 BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::IO),
216 BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::FILE),
217 protocol_handlers, request_interceptors.Pass());
218 resource_context_->set_url_request_context_getter(url_request_getter_.get());
219 return url_request_getter_.get();
222 net::URLRequestContextGetter*
223 RuntimeContext::CreateRequestContextForStoragePartition(
224 const base::FilePath& partition_path,
226 content::ProtocolHandlerMap* protocol_handlers,
227 content::URLRequestInterceptorScopedVector request_interceptors) {
228 #if defined(OS_ANDROID)
231 PartitionPathContextGetterMap::iterator iter =
232 context_getters_.find(partition_path.value());
233 if (iter != context_getters_.end())
234 return iter->second.get();
236 application::ApplicationService* service =
237 XWalkRunner::GetInstance()->app_system()->application_service();
238 protocol_handlers->insert(std::pair<std::string,
239 linked_ptr<net::URLRequestJobFactory::ProtocolHandler> >(
240 application::kApplicationScheme,
241 application::CreateApplicationProtocolHandler(service)));
243 scoped_refptr<RuntimeURLRequestContextGetter>
244 context_getter = new RuntimeURLRequestContextGetter(
245 false, /* ignore_certificate_error = false */
247 BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::IO),
248 BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::FILE),
249 protocol_handlers, request_interceptors.Pass());
251 context_getters_.insert(
252 std::make_pair(partition_path.value(), context_getter));
253 return context_getter.get();
257 #if defined(OS_ANDROID)
258 void RuntimeContext::SetCSPString(const std::string& csp) {
259 // Check format of csp string.
260 std::vector<std::string> policies;
261 base::SplitString(csp, ';', &policies);
262 for (size_t i = 0; i < policies.size(); ++i) {
263 size_t found = policies[i].find(' ');
264 if (found == std::string::npos) {
265 LOG(INFO) << "Invalid value of directive: " << policies[i];
272 std::string RuntimeContext::GetCSPString() const {
276 void RuntimeContext::InitVisitedLinkMaster() {
277 visitedlink_master_.reset(
278 new visitedlink::VisitedLinkMaster(this, this, false));
279 visitedlink_master_->Init();
282 void RuntimeContext::AddVisitedURLs(const std::vector<GURL>& urls) {
283 DCHECK(visitedlink_master_.get());
284 visitedlink_master_->AddURLs(urls);
287 void RuntimeContext::RebuildTable(
288 const scoped_refptr<URLEnumerator>& enumerator) {
289 // XWalkView rebuilds from XWalkWebChromeClient.getVisitedHistory. The client
290 // can change in the lifetime of this XWalkView and may not yet be set here.
291 // Therefore this initialization path is not used.
292 enumerator->OnComplete(true);