Upstream version 10.38.210.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / browser / runtime_context.cc
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.
5
6 #include "xwalk/runtime/browser/runtime_context.h"
7
8 #include <string>
9 #include <utility>
10 #include <vector>
11
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"
34
35 #if defined(OS_ANDROID)
36 #include "base/strings/string_split.h"
37 #endif
38
39 using content::BrowserThread;
40 using content::DownloadManager;
41
42 namespace xwalk {
43
44 class RuntimeContext::RuntimeResourceContext : public content::ResourceContext {
45  public:
46   RuntimeResourceContext() : getter_(NULL) {}
47   virtual ~RuntimeResourceContext() {}
48
49   // ResourceContext implementation:
50   virtual net::HostResolver* GetHostResolver() OVERRIDE {
51     CHECK(getter_);
52     return getter_->host_resolver();
53   }
54   virtual net::URLRequestContext* GetRequestContext() OVERRIDE {
55     CHECK(getter_);
56     return getter_->GetURLRequestContext();
57   }
58
59   virtual bool AllowMicAccess(const GURL& origin) OVERRIDE { return false; }
60   virtual bool AllowCameraAccess(const GURL& origin) OVERRIDE { return false; }
61
62   void set_url_request_context_getter(RuntimeURLRequestContextGetter* getter) {
63     getter_ = getter;
64   }
65
66  private:
67   RuntimeURLRequestContextGetter* getter_;
68
69   DISALLOW_COPY_AND_ASSIGN(RuntimeResourceContext);
70 };
71
72 RuntimeContext::RuntimeContext()
73   : resource_context_(new RuntimeResourceContext) {
74   InitWhileIOAllowed();
75 #if defined(OS_ANDROID)
76   InitVisitedLinkMaster();
77 #endif
78 }
79
80 RuntimeContext::~RuntimeContext() {
81   if (resource_context_.get()) {
82     BrowserThread::DeleteSoon(
83         BrowserThread::IO, FROM_HERE, resource_context_.release());
84   }
85 }
86
87 // static
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());
92 }
93
94 void RuntimeContext::InitWhileIOAllowed() {
95   CommandLine* cmd_line = CommandLine::ForCurrentProcess();
96   if (cmd_line->HasSwitch(switches::kXWalkDataPath)) {
97     base::FilePath path =
98         cmd_line->GetSwitchValuePath(switches::kXWalkDataPath);
99     PathService::OverrideAndCreateIfNeeded(
100         xwalk::DIR_DATA_PATH, path, false, true);
101   }
102 }
103
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));
112 #else
113   CHECK(PathService::Get(xwalk::DIR_DATA_PATH, &result));
114 #endif
115   return result;
116 }
117
118 bool RuntimeContext::IsOffTheRecord() const {
119   // We don't consider off the record scenario.
120   return false;
121 }
122
123 content::DownloadManagerDelegate* RuntimeContext::GetDownloadManagerDelegate() {
124   content::DownloadManager* manager = BrowserContext::GetDownloadManager(this);
125
126   if (!download_manager_delegate_.get()) {
127     download_manager_delegate_ = new RuntimeDownloadManagerDelegate();
128     download_manager_delegate_->SetDownloadManager(manager);
129   }
130
131   return download_manager_delegate_.get();
132 }
133
134 net::URLRequestContextGetter* RuntimeContext::GetRequestContext() {
135   return GetDefaultStoragePartition(this)->GetURLRequestContext();
136 }
137
138 net::URLRequestContextGetter*
139     RuntimeContext::GetRequestContextForRenderProcess(
140         int renderer_child_id) {
141 #if defined(OS_ANDROID)
142   return GetRequestContext();
143 #else
144   content::RenderProcessHost* rph =
145       content::RenderProcessHost::FromID(renderer_child_id);
146   return rph->GetStoragePartition()->GetURLRequestContext();
147 #endif
148 }
149
150 net::URLRequestContextGetter* RuntimeContext::GetMediaRequestContext() {
151   return GetRequestContext();
152 }
153
154 net::URLRequestContextGetter*
155     RuntimeContext::GetMediaRequestContextForRenderProcess(
156         int renderer_child_id) {
157 #if defined(OS_ANDROID)
158   return GetRequestContext();
159 #else
160   content::RenderProcessHost* rph =
161       content::RenderProcessHost::FromID(renderer_child_id);
162   return rph->GetStoragePartition()->GetURLRequestContext();
163 #endif
164 }
165
166 net::URLRequestContextGetter*
167     RuntimeContext::GetMediaRequestContextForStoragePartition(
168         const base::FilePath& partition_path,
169         bool in_memory) {
170 #if defined(OS_ANDROID)
171   return GetRequestContext();
172 #else
173   PartitionPathContextGetterMap::iterator iter =
174       context_getters_.find(partition_path.value());
175   CHECK(iter != context_getters_.end());
176   return iter->second.get();
177 #endif
178 }
179
180 content::ResourceContext* RuntimeContext::GetResourceContext()  {
181   return resource_context_.get();
182 }
183
184 content::BrowserPluginGuestManager*
185 RuntimeContext::GetGuestManager() {
186   return NULL;
187 }
188
189 quota::SpecialStoragePolicy* RuntimeContext::GetSpecialStoragePolicy() {
190   return NULL;
191 }
192
193 content::PushMessagingService* RuntimeContext::GetPushMessagingService() {
194   return NULL;
195 }
196
197 content::SSLHostStateDelegate* RuntimeContext::GetSSLHostStateDelegate() {
198   return NULL;
199 }
200
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();
207   }
208   return 0;
209 }
210
211 net::URLRequestContextGetter* RuntimeContext::CreateRequestContext(
212     content::ProtocolHandlerMap* protocol_handlers,
213     content::URLRequestInterceptorScopedVector request_interceptors) {
214   DCHECK(!url_request_getter_.get());
215
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)));
222
223   url_request_getter_ = new RuntimeURLRequestContextGetter(
224       false, /* ignore_certificate_error = false */
225       GetPath(),
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();
231 }
232
233 net::URLRequestContextGetter*
234   RuntimeContext::CreateRequestContextForStoragePartition(
235       const base::FilePath& partition_path,
236       bool in_memory,
237       content::ProtocolHandlerMap* protocol_handlers,
238       content::URLRequestInterceptorScopedVector request_interceptors) {
239 #if defined(OS_ANDROID)
240     return NULL;
241 #else
242   PartitionPathContextGetterMap::iterator iter =
243     context_getters_.find(partition_path.value());
244   if (iter != context_getters_.end())
245     return iter->second.get();
246
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)));
253
254   scoped_refptr<RuntimeURLRequestContextGetter>
255   context_getter = new RuntimeURLRequestContextGetter(
256       false, /* ignore_certificate_error = false */
257       partition_path,
258       BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::IO),
259       BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::FILE),
260       protocol_handlers, request_interceptors.Pass());
261
262   context_getters_.insert(
263       std::make_pair(partition_path.value(), context_getter));
264   return context_getter.get();
265 #endif
266 }
267
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];
277       return;
278     }
279   }
280   csp_ = csp;
281 }
282
283 std::string RuntimeContext::GetCSPString() const {
284   return csp_;
285 }
286
287 void RuntimeContext::InitVisitedLinkMaster() {
288   visitedlink_master_.reset(
289       new visitedlink::VisitedLinkMaster(this, this, false));
290   visitedlink_master_->Init();
291 }
292
293 void RuntimeContext::AddVisitedURLs(const std::vector<GURL>& urls) {
294   DCHECK(visitedlink_master_.get());
295   visitedlink_master_->AddURLs(urls);
296 }
297
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);
304 }
305 #endif
306
307 }  // namespace xwalk