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