e1c7bad432f12b7ed28edc1f0ef6afc7c82fd8d5
[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 "content/public/browser/browser_thread.h"
15 #include "content/public/browser/resource_context.h"
16 #include "content/public/browser/storage_partition.h"
17 #include "content/public/browser/web_contents.h"
18 #include "content/public/common/content_switches.h"
19 #include "xwalk/application/browser/application.h"
20 #include "xwalk/application/browser/application_protocols.h"
21 #include "xwalk/application/browser/application_service.h"
22 #include "xwalk/application/browser/application_system.h"
23 #include "xwalk/application/common/constants.h"
24 #include "xwalk/runtime/browser/runtime_download_manager_delegate.h"
25 #include "xwalk/runtime/browser/runtime_geolocation_permission_context.h"
26 #include "xwalk/runtime/browser/runtime_url_request_context_getter.h"
27 #include "xwalk/runtime/browser/xwalk_runner.h"
28 #include "xwalk/runtime/common/xwalk_paths.h"
29 #include "xwalk/runtime/common/xwalk_switches.h"
30
31 #if defined(OS_ANDROID)
32 #include "base/strings/string_split.h"
33 #endif
34
35 using content::BrowserThread;
36 using content::DownloadManager;
37
38 namespace xwalk {
39
40 class RuntimeContext::RuntimeResourceContext : public content::ResourceContext {
41  public:
42   RuntimeResourceContext() : getter_(NULL) {}
43   virtual ~RuntimeResourceContext() {}
44
45   // ResourceContext implementation:
46   virtual net::HostResolver* GetHostResolver() OVERRIDE {
47     CHECK(getter_);
48     return getter_->host_resolver();
49   }
50   virtual net::URLRequestContext* GetRequestContext() OVERRIDE {
51     CHECK(getter_);
52     return getter_->GetURLRequestContext();
53   }
54
55   virtual bool AllowMicAccess(const GURL& origin) OVERRIDE { return false; }
56   virtual bool AllowCameraAccess(const GURL& origin) OVERRIDE { return false; }
57
58   void set_url_request_context_getter(RuntimeURLRequestContextGetter* getter) {
59     getter_ = getter;
60   }
61
62  private:
63   RuntimeURLRequestContextGetter* getter_;
64
65   DISALLOW_COPY_AND_ASSIGN(RuntimeResourceContext);
66 };
67
68 RuntimeContext::RuntimeContext()
69   : resource_context_(new RuntimeResourceContext) {
70   InitWhileIOAllowed();
71 }
72
73 RuntimeContext::~RuntimeContext() {
74   if (resource_context_) {
75     BrowserThread::DeleteSoon(
76         BrowserThread::IO, FROM_HERE, resource_context_.release());
77   }
78 }
79
80 // static
81 RuntimeContext* RuntimeContext::FromWebContents(
82     content::WebContents* web_contents) {
83   // This is safe; this is the only implementation of the browser context.
84   return static_cast<RuntimeContext*>(web_contents->GetBrowserContext());
85 }
86
87 void RuntimeContext::InitWhileIOAllowed() {
88   CommandLine* cmd_line = CommandLine::ForCurrentProcess();
89   if (cmd_line->HasSwitch(switches::kXWalkDataPath)) {
90     base::FilePath path =
91         cmd_line->GetSwitchValuePath(switches::kXWalkDataPath);
92     PathService::OverrideAndCreateIfNeeded(xwalk::DIR_DATA_PATH, path, true);
93   }
94 }
95
96 base::FilePath RuntimeContext::GetPath() const {
97   base::FilePath result;
98 #if defined(OS_ANDROID)
99   CHECK(PathService::Get(base::DIR_ANDROID_APP_DATA, &result));
100 #else
101   CHECK(PathService::Get(xwalk::DIR_DATA_PATH, &result));
102 #endif
103   return result;
104 }
105
106 bool RuntimeContext::IsOffTheRecord() const {
107   // We don't consider off the record scenario.
108   return false;
109 }
110
111 content::DownloadManagerDelegate* RuntimeContext::GetDownloadManagerDelegate() {
112   content::DownloadManager* manager = BrowserContext::GetDownloadManager(this);
113
114   if (!download_manager_delegate_) {
115     download_manager_delegate_ = new RuntimeDownloadManagerDelegate();
116     download_manager_delegate_->SetDownloadManager(manager);
117   }
118
119   return download_manager_delegate_.get();
120 }
121
122 net::URLRequestContextGetter* RuntimeContext::GetRequestContext() {
123   return GetDefaultStoragePartition(this)->GetURLRequestContext();
124 }
125
126 net::URLRequestContextGetter*
127     RuntimeContext::GetRequestContextForRenderProcess(
128         int renderer_child_id)  {
129   return GetRequestContext();
130 }
131
132 net::URLRequestContextGetter* RuntimeContext::GetMediaRequestContext()  {
133   return GetRequestContext();
134 }
135
136 net::URLRequestContextGetter*
137     RuntimeContext::GetMediaRequestContextForRenderProcess(
138         int renderer_child_id)  {
139   return GetRequestContext();
140 }
141
142 net::URLRequestContextGetter*
143     RuntimeContext::GetMediaRequestContextForStoragePartition(
144         const base::FilePath& partition_path,
145         bool in_memory) {
146   return GetRequestContext();
147 }
148
149 content::ResourceContext* RuntimeContext::GetResourceContext()  {
150   return resource_context_.get();
151 }
152
153 content::GeolocationPermissionContext*
154     RuntimeContext::GetGeolocationPermissionContext()  {
155 #if defined(OS_ANDROID) || defined(OS_TIZEN)
156   if (!geolocation_permission_context_) {
157     geolocation_permission_context_ =
158         RuntimeGeolocationPermissionContext::Create(this);
159   }
160 #endif
161   // TODO(yongsheng): Create geolcation permission context for other platforms.
162   return geolocation_permission_context_.get();
163 }
164
165 quota::SpecialStoragePolicy* RuntimeContext::GetSpecialStoragePolicy() {
166   return NULL;
167 }
168
169 void RuntimeContext::RequestProtectedMediaIdentifierPermission(
170     int render_process_id,
171     int render_view_id,
172     int bridge_id,
173     int group_id,
174     const GURL& requesting_frame,
175     const ProtectedMediaIdentifierPermissionCallback& callback) {
176   NOTIMPLEMENTED();
177   callback.Run(false);
178 }
179
180 void RuntimeContext::CancelProtectedMediaIdentifierPermissionRequests(
181     int group_id) {
182   NOTIMPLEMENTED();
183 }
184
185 net::URLRequestContextGetter* RuntimeContext::CreateRequestContext(
186     content::ProtocolHandlerMap* protocol_handlers) {
187   DCHECK(!url_request_getter_);
188
189   application::ApplicationService* service =
190       XWalkRunner::GetInstance()->app_system()->application_service();
191   protocol_handlers->insert(std::pair<std::string,
192         linked_ptr<net::URLRequestJobFactory::ProtocolHandler> >(
193           application::kApplicationScheme,
194           application::CreateApplicationProtocolHandler(service)));
195
196   url_request_getter_ = new RuntimeURLRequestContextGetter(
197       false, /* ignore_certificate_error = false */
198       GetPath(),
199       BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::IO),
200       BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::FILE),
201       protocol_handlers);
202   resource_context_->set_url_request_context_getter(url_request_getter_.get());
203   return url_request_getter_.get();
204 }
205
206 net::URLRequestContextGetter*
207     RuntimeContext::CreateRequestContextForStoragePartition(
208         const base::FilePath& partition_path,
209         bool in_memory,
210         content::ProtocolHandlerMap* protocol_handlers) {
211   return NULL;
212 }
213
214 #if defined(OS_ANDROID)
215 void RuntimeContext::SetCSPString(const std::string& csp) {
216   // Check format of csp string.
217   std::vector<std::string> policies;
218   base::SplitString(csp, ';', &policies);
219   for (size_t i = 0; i < policies.size(); ++i) {
220     size_t found = policies[i].find(' ');
221     if (found == std::string::npos) {
222       LOG(INFO) << "Invalid value of directive: " << policies[i];
223       return;
224     }
225   }
226   csp_ = csp;
227 }
228
229 std::string RuntimeContext::GetCSPString() const {
230   return csp_;
231 }
232 #endif
233
234 }  // namespace xwalk