Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / browser / runtime_context.cc
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.
4
5 #include "xwalk/runtime/browser/runtime_context.h"
6
7 #include <string>
8 #include <utility>
9 #include <vector>
10
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"
32
33 #if defined(OS_ANDROID)
34 #include "base/strings/string_split.h"
35 #endif
36
37 using content::BrowserThread;
38 using content::DownloadManager;
39
40 namespace xwalk {
41
42 class RuntimeContext::RuntimeResourceContext : public content::ResourceContext {
43  public:
44   RuntimeResourceContext() : getter_(NULL) {}
45   virtual ~RuntimeResourceContext() {}
46
47   // ResourceContext implementation:
48   virtual net::HostResolver* GetHostResolver() OVERRIDE {
49     CHECK(getter_);
50     return getter_->host_resolver();
51   }
52   virtual net::URLRequestContext* GetRequestContext() OVERRIDE {
53     CHECK(getter_);
54     return getter_->GetURLRequestContext();
55   }
56
57   virtual bool AllowMicAccess(const GURL& origin) OVERRIDE { return false; }
58   virtual bool AllowCameraAccess(const GURL& origin) OVERRIDE { return false; }
59
60   void set_url_request_context_getter(RuntimeURLRequestContextGetter* getter) {
61     getter_ = getter;
62   }
63
64  private:
65   RuntimeURLRequestContextGetter* getter_;
66
67   DISALLOW_COPY_AND_ASSIGN(RuntimeResourceContext);
68 };
69
70 RuntimeContext::RuntimeContext()
71   : resource_context_(new RuntimeResourceContext) {
72   InitWhileIOAllowed();
73 #if defined(OS_ANDROID)
74   InitVisitedLinkMaster();
75 #endif
76 }
77
78 RuntimeContext::~RuntimeContext() {
79   if (resource_context_) {
80     BrowserThread::DeleteSoon(
81         BrowserThread::IO, FROM_HERE, resource_context_.release());
82   }
83 }
84
85 // static
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());
90 }
91
92 void RuntimeContext::InitWhileIOAllowed() {
93   CommandLine* cmd_line = CommandLine::ForCurrentProcess();
94   if (cmd_line->HasSwitch(switches::kXWalkDataPath)) {
95     base::FilePath path =
96         cmd_line->GetSwitchValuePath(switches::kXWalkDataPath);
97     PathService::OverrideAndCreateIfNeeded(
98         xwalk::DIR_DATA_PATH, path, false, true);
99   }
100 }
101
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));
106 #else
107   CHECK(PathService::Get(xwalk::DIR_DATA_PATH, &result));
108 #endif
109   return result;
110 }
111
112 bool RuntimeContext::IsOffTheRecord() const {
113   // We don't consider off the record scenario.
114   return false;
115 }
116
117 content::DownloadManagerDelegate* RuntimeContext::GetDownloadManagerDelegate() {
118   content::DownloadManager* manager = BrowserContext::GetDownloadManager(this);
119
120   if (!download_manager_delegate_) {
121     download_manager_delegate_ = new RuntimeDownloadManagerDelegate();
122     download_manager_delegate_->SetDownloadManager(manager);
123   }
124
125   return download_manager_delegate_.get();
126 }
127
128 net::URLRequestContextGetter* RuntimeContext::GetRequestContext() {
129   return GetDefaultStoragePartition(this)->GetURLRequestContext();
130 }
131
132 net::URLRequestContextGetter*
133     RuntimeContext::GetRequestContextForRenderProcess(
134         int renderer_child_id) {
135 #if defined(OS_ANDROID)
136   return GetRequestContext();
137 #else
138   content::RenderProcessHost* rph =
139       content::RenderProcessHost::FromID(renderer_child_id);
140   return rph->GetStoragePartition()->GetURLRequestContext();
141 #endif
142 }
143
144 net::URLRequestContextGetter* RuntimeContext::GetMediaRequestContext() {
145   return GetRequestContext();
146 }
147
148 net::URLRequestContextGetter*
149     RuntimeContext::GetMediaRequestContextForRenderProcess(
150         int renderer_child_id) {
151 #if defined(OS_ANDROID)
152   return GetRequestContext();
153 #else
154   content::RenderProcessHost* rph =
155       content::RenderProcessHost::FromID(renderer_child_id);
156   return rph->GetStoragePartition()->GetURLRequestContext();
157 #endif
158 }
159
160 net::URLRequestContextGetter*
161     RuntimeContext::GetMediaRequestContextForStoragePartition(
162         const base::FilePath& partition_path,
163         bool in_memory) {
164 #if defined(OS_ANDROID)
165   return GetRequestContext();
166 #else
167   PartitionPathContextGetterMap::iterator iter =
168       context_getters_.find(partition_path.value());
169   CHECK(iter != context_getters_.end());
170   return iter->second.get();
171 #endif
172 }
173
174 content::ResourceContext* RuntimeContext::GetResourceContext()  {
175   return resource_context_.get();
176 }
177
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);
184   }
185 #endif
186   // TODO(yongsheng): Create geolcation permission context for other platforms.
187   return geolocation_permission_context_.get();
188 }
189
190 content::BrowserPluginGuestManagerDelegate*
191 RuntimeContext::GetGuestManagerDelegate() {
192   return NULL;
193 }
194
195 quota::SpecialStoragePolicy* RuntimeContext::GetSpecialStoragePolicy() {
196   return NULL;
197 }
198
199 void RuntimeContext::RequestProtectedMediaIdentifierPermission(
200     int render_process_id,
201     int render_view_id,
202     int bridge_id,
203     int group_id,
204     const GURL& requesting_frame,
205     const ProtectedMediaIdentifierPermissionCallback& callback) {
206   NOTIMPLEMENTED();
207   callback.Run(false);
208 }
209
210 void RuntimeContext::CancelProtectedMediaIdentifierPermissionRequests(
211     int group_id) {
212   NOTIMPLEMENTED();
213 }
214
215 net::URLRequestContextGetter* RuntimeContext::CreateRequestContext(
216     content::ProtocolHandlerMap* protocol_handlers,
217     content::ProtocolHandlerScopedVector protocol_interceptors) {
218   DCHECK(!url_request_getter_);
219
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)));
226
227   url_request_getter_ = new RuntimeURLRequestContextGetter(
228       false, /* ignore_certificate_error = false */
229       GetPath(),
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();
235 }
236
237 net::URLRequestContextGetter*
238   RuntimeContext::CreateRequestContextForStoragePartition(
239       const base::FilePath& partition_path,
240       bool in_memory,
241       content::ProtocolHandlerMap* protocol_handlers,
242       content::ProtocolHandlerScopedVector protocol_interceptors) {
243 #if defined(OS_ANDROID)
244     return NULL;
245 #else
246   PartitionPathContextGetterMap::iterator iter =
247       context_getters_.find(partition_path.value());
248   if (iter != context_getters_.end())
249     return iter->second.get();
250
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)));
257
258   scoped_refptr<RuntimeURLRequestContextGetter>
259   context_getter = new RuntimeURLRequestContextGetter(
260       false, /* ignore_certificate_error = false */
261       partition_path,
262       BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::IO),
263       BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::FILE),
264       protocol_handlers, protocol_interceptors.Pass());
265
266   context_getters_.insert(
267       std::make_pair(partition_path.value(), context_getter));
268   return context_getter.get();
269 #endif
270 }
271
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];
281       return;
282     }
283   }
284   csp_ = csp;
285 }
286
287 std::string RuntimeContext::GetCSPString() const {
288   return csp_;
289 }
290
291 void RuntimeContext::InitVisitedLinkMaster() {
292   visitedlink_master_.reset(
293       new visitedlink::VisitedLinkMaster(this, this, false));
294   visitedlink_master_->Init();
295 }
296
297 void RuntimeContext::AddVisitedURLs(const std::vector<GURL>& urls) {
298   DCHECK(visitedlink_master_);
299   visitedlink_master_->AddURLs(urls);
300 }
301
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);
308 }
309 #endif
310
311 }  // namespace xwalk