Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / browser / xwalk_browser_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/xwalk_browser_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 XWalkBrowserContext::RuntimeResourceContext :
45     public content::ResourceContext {
46  public:
47   RuntimeResourceContext() : getter_(NULL) {}
48   virtual ~RuntimeResourceContext() {}
49
50   // ResourceContext implementation:
51   net::HostResolver* GetHostResolver() override {
52     CHECK(getter_);
53     return getter_->host_resolver();
54   }
55   net::URLRequestContext* GetRequestContext() override {
56     CHECK(getter_);
57     return getter_->GetURLRequestContext();
58   }
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 XWalkBrowserContext::XWalkBrowserContext()
71   : resource_context_(new RuntimeResourceContext) {
72   InitWhileIOAllowed();
73 #if defined(OS_ANDROID)
74   InitVisitedLinkMaster();
75 #endif
76 }
77
78 XWalkBrowserContext::~XWalkBrowserContext() {
79   if (resource_context_.get()) {
80     BrowserThread::DeleteSoon(
81         BrowserThread::IO, FROM_HERE, resource_context_.release());
82   }
83 }
84
85 // static
86 XWalkBrowserContext* XWalkBrowserContext::FromWebContents(
87     content::WebContents* web_contents) {
88   // This is safe; this is the only implementation of the browser context.
89   return static_cast<XWalkBrowserContext*>(
90       web_contents->GetBrowserContext());
91 }
92
93 void XWalkBrowserContext::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         DIR_DATA_PATH, path, false, true);
100   }
101 }
102
103 base::FilePath XWalkBrowserContext::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(DIR_DATA_PATH, &result));
113 #endif
114   return result;
115 }
116
117 bool XWalkBrowserContext::IsOffTheRecord() const {
118   // We don't consider off the record scenario.
119   return false;
120 }
121
122 content::DownloadManagerDelegate*
123 XWalkBrowserContext::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* XWalkBrowserContext::GetRequestContext() {
135   return GetDefaultStoragePartition(this)->GetURLRequestContext();
136 }
137
138 net::URLRequestContextGetter*
139     XWalkBrowserContext::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* XWalkBrowserContext::GetMediaRequestContext() {
151   return GetRequestContext();
152 }
153
154 net::URLRequestContextGetter*
155     XWalkBrowserContext::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     XWalkBrowserContext::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* XWalkBrowserContext::GetResourceContext()  {
181   return resource_context_.get();
182 }
183
184 content::BrowserPluginGuestManager*
185 XWalkBrowserContext::GetGuestManager() {
186   return NULL;
187 }
188
189 storage::SpecialStoragePolicy* XWalkBrowserContext::GetSpecialStoragePolicy() {
190   return NULL;
191 }
192
193 content::PushMessagingService* XWalkBrowserContext::GetPushMessagingService() {
194   return NULL;
195 }
196
197 content::SSLHostStateDelegate* XWalkBrowserContext::GetSSLHostStateDelegate() {
198   return NULL;
199 }
200
201 RuntimeURLRequestContextGetter*
202 XWalkBrowserContext::GetURLRequestContextGetterById(
203     const std::string& pkg_id) {
204   for (PartitionPathContextGetterMap::iterator it = context_getters_.begin();
205        it != context_getters_.end(); ++it) {
206 #if defined(OS_WIN)
207     if (it->first.find(base::UTF8ToWide(pkg_id)))
208 #else
209     if (it->first.find(pkg_id))
210 #endif
211       return it->second.get();
212   }
213   return 0;
214 }
215
216 net::URLRequestContextGetter* XWalkBrowserContext::CreateRequestContext(
217     content::ProtocolHandlerMap* protocol_handlers,
218     content::URLRequestInterceptorScopedVector request_interceptors) {
219   DCHECK(!url_request_getter_.get());
220
221   application::ApplicationService* service =
222       XWalkRunner::GetInstance()->app_system()->application_service();
223   protocol_handlers->insert(std::pair<std::string,
224         linked_ptr<net::URLRequestJobFactory::ProtocolHandler> >(
225           application::kApplicationScheme,
226           application::CreateApplicationProtocolHandler(service)));
227
228   url_request_getter_ = new RuntimeURLRequestContextGetter(
229       false, /* ignore_certificate_error = false */
230       GetPath(),
231       BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::IO),
232       BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::FILE),
233       protocol_handlers, request_interceptors.Pass());
234   resource_context_->set_url_request_context_getter(url_request_getter_.get());
235   return url_request_getter_.get();
236 }
237
238 net::URLRequestContextGetter*
239   XWalkBrowserContext::CreateRequestContextForStoragePartition(
240       const base::FilePath& partition_path,
241       bool in_memory,
242       content::ProtocolHandlerMap* protocol_handlers,
243       content::URLRequestInterceptorScopedVector request_interceptors) {
244 #if defined(OS_ANDROID)
245     return NULL;
246 #else
247   PartitionPathContextGetterMap::iterator iter =
248     context_getters_.find(partition_path.value());
249   if (iter != context_getters_.end())
250     return iter->second.get();
251
252   application::ApplicationService* service =
253       XWalkRunner::GetInstance()->app_system()->application_service();
254   protocol_handlers->insert(std::pair<std::string,
255         linked_ptr<net::URLRequestJobFactory::ProtocolHandler> >(
256           application::kApplicationScheme,
257           application::CreateApplicationProtocolHandler(service)));
258
259   scoped_refptr<RuntimeURLRequestContextGetter>
260   context_getter = new RuntimeURLRequestContextGetter(
261       false, /* ignore_certificate_error = false */
262       partition_path,
263       BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::IO),
264       BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::FILE),
265       protocol_handlers, request_interceptors.Pass());
266
267   context_getters_.insert(
268       std::make_pair(partition_path.value(), context_getter));
269   // Make sure that the default url request getter has been initialized,
270   // please refer to https://crosswalk-project.org/jira/browse/XWALK-2890
271   // for more details.
272   if (!url_request_getter_.get())
273     CreateRequestContext(protocol_handlers, request_interceptors.Pass());
274
275   return context_getter.get();
276 #endif
277 }
278
279 #if defined(OS_ANDROID)
280 void XWalkBrowserContext::SetCSPString(const std::string& csp) {
281   // Check format of csp string.
282   std::vector<std::string> policies;
283   base::SplitString(csp, ';', &policies);
284   for (size_t i = 0; i < policies.size(); ++i) {
285     size_t found = policies[i].find(' ');
286     if (found == std::string::npos) {
287       LOG(INFO) << "Invalid value of directive: " << policies[i];
288       return;
289     }
290   }
291   csp_ = csp;
292 }
293
294 std::string XWalkBrowserContext::GetCSPString() const {
295   return csp_;
296 }
297
298 void XWalkBrowserContext::InitVisitedLinkMaster() {
299   visitedlink_master_.reset(
300       new visitedlink::VisitedLinkMaster(this, this, false));
301   visitedlink_master_->Init();
302 }
303
304 void XWalkBrowserContext::AddVisitedURLs(const std::vector<GURL>& urls) {
305   DCHECK(visitedlink_master_.get());
306   visitedlink_master_->AddURLs(urls);
307 }
308
309 void XWalkBrowserContext::RebuildTable(
310     const scoped_refptr<URLEnumerator>& enumerator) {
311   // XWalkView rebuilds from XWalkWebChromeClient.getVisitedHistory. The client
312   // can change in the lifetime of this XWalkView and may not yet be set here.
313   // Therefore this initialization path is not used.
314   enumerator->OnComplete(true);
315 }
316 #endif
317
318 }  // namespace xwalk