Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / content / worker / worker_webkitplatformsupport_impl.cc
1 // Copyright (c) 2012 The Chromium Authors. 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 "content/worker/worker_webkitplatformsupport_impl.h"
6
7 #include "base/files/file_path.h"
8 #include "base/logging.h"
9 #include "base/message_loop/message_loop_proxy.h"
10 #include "base/platform_file.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "content/child/blink_glue.h"
13 #include "content/child/database_util.h"
14 #include "content/child/fileapi/webfilesystem_impl.h"
15 #include "content/child/indexed_db/webidbfactory_impl.h"
16 #include "content/child/quota_dispatcher.h"
17 #include "content/child/quota_message_filter.h"
18 #include "content/child/thread_safe_sender.h"
19 #include "content/child/web_database_observer_impl.h"
20 #include "content/child/webblobregistry_impl.h"
21 #include "content/child/webfileutilities_impl.h"
22 #include "content/child/webmessageportchannel_impl.h"
23 #include "content/common/file_utilities_messages.h"
24 #include "content/common/mime_registry_messages.h"
25 #include "content/worker/worker_thread.h"
26 #include "ipc/ipc_sync_message_filter.h"
27 #include "net/base/mime_util.h"
28 #include "third_party/WebKit/public/platform/WebBlobRegistry.h"
29 #include "third_party/WebKit/public/platform/WebFileInfo.h"
30 #include "third_party/WebKit/public/platform/WebString.h"
31 #include "third_party/WebKit/public/platform/WebURL.h"
32 #include "webkit/common/quota/quota_types.h"
33
34 using blink::Platform;
35 using blink::WebBlobRegistry;
36 using blink::WebClipboard;
37 using blink::WebFileInfo;
38 using blink::WebFileSystem;
39 using blink::WebFileUtilities;
40 using blink::WebMessagePortChannel;
41 using blink::WebMimeRegistry;
42 using blink::WebSandboxSupport;
43 using blink::WebStorageNamespace;
44 using blink::WebString;
45 using blink::WebURL;
46
47 namespace content {
48
49 // TODO(kinuko): Probably this could be consolidated into
50 // RendererWebKitPlatformSupportImpl::FileUtilities.
51 class WorkerWebKitPlatformSupportImpl::FileUtilities
52     : public WebFileUtilitiesImpl {
53  public:
54   explicit FileUtilities(ThreadSafeSender* sender)
55       : thread_safe_sender_(sender) {}
56   virtual bool getFileInfo(const WebString& path, WebFileInfo& result);
57  private:
58   scoped_refptr<ThreadSafeSender> thread_safe_sender_;
59 };
60
61 bool WorkerWebKitPlatformSupportImpl::FileUtilities::getFileInfo(
62     const WebString& path,
63     WebFileInfo& web_file_info) {
64   base::File::Info file_info;
65   base::File::Error status;
66   if (!thread_safe_sender_.get() ||
67       !thread_safe_sender_->Send(new FileUtilitiesMsg_GetFileInfo(
68            base::FilePath::FromUTF16Unsafe(path), &file_info, &status)) ||
69       status != base::File::FILE_OK) {
70     return false;
71   }
72   FileInfoToWebFileInfo(file_info, &web_file_info);
73   web_file_info.platformPath = path;
74   return true;
75 }
76
77 //------------------------------------------------------------------------------
78
79 WorkerWebKitPlatformSupportImpl::WorkerWebKitPlatformSupportImpl(
80     ThreadSafeSender* sender,
81     IPC::SyncMessageFilter* sync_message_filter,
82     QuotaMessageFilter* quota_message_filter)
83     : thread_safe_sender_(sender),
84       child_thread_loop_(base::MessageLoopProxy::current()),
85       sync_message_filter_(sync_message_filter),
86       quota_message_filter_(quota_message_filter) {
87   if (sender) {
88     blob_registry_.reset(new WebBlobRegistryImpl(sender));
89     web_idb_factory_.reset(new WebIDBFactoryImpl(sender));
90     web_database_observer_impl_.reset(
91         new WebDatabaseObserverImpl(sync_message_filter));
92   }
93 }
94
95 WorkerWebKitPlatformSupportImpl::~WorkerWebKitPlatformSupportImpl() {
96   WebFileSystemImpl::DeleteThreadSpecificInstance();
97 }
98
99 WebClipboard* WorkerWebKitPlatformSupportImpl::clipboard() {
100   NOTREACHED();
101   return NULL;
102 }
103
104 WebMimeRegistry* WorkerWebKitPlatformSupportImpl::mimeRegistry() {
105   return this;
106 }
107
108 WebFileSystem* WorkerWebKitPlatformSupportImpl::fileSystem() {
109   return WebFileSystemImpl::ThreadSpecificInstance(child_thread_loop_.get());
110 }
111
112 WebFileUtilities* WorkerWebKitPlatformSupportImpl::fileUtilities() {
113   if (!file_utilities_) {
114     file_utilities_.reset(new FileUtilities(thread_safe_sender_.get()));
115     file_utilities_->set_sandbox_enabled(sandboxEnabled());
116   }
117   return file_utilities_.get();
118 }
119
120 WebSandboxSupport* WorkerWebKitPlatformSupportImpl::sandboxSupport() {
121   NOTREACHED();
122   return NULL;
123 }
124
125 bool WorkerWebKitPlatformSupportImpl::sandboxEnabled() {
126   // Always return true because WebKit should always act as though the Sandbox
127   // is enabled for workers.  See the comment in WebKitPlatformSupport for
128   // more info.
129   return true;
130 }
131
132 unsigned long long WorkerWebKitPlatformSupportImpl::visitedLinkHash(
133     const char* canonical_url,
134     size_t length) {
135   NOTREACHED();
136   return 0;
137 }
138
139 bool WorkerWebKitPlatformSupportImpl::isLinkVisited(
140     unsigned long long link_hash) {
141   NOTREACHED();
142   return false;
143 }
144
145 WebMessagePortChannel*
146 WorkerWebKitPlatformSupportImpl::createMessagePortChannel() {
147   return new WebMessagePortChannelImpl(child_thread_loop_.get());
148 }
149
150 void WorkerWebKitPlatformSupportImpl::setCookies(
151     const WebURL& url,
152     const WebURL& first_party_for_cookies,
153     const WebString& value) {
154   NOTREACHED();
155 }
156
157 WebString WorkerWebKitPlatformSupportImpl::cookies(
158     const WebURL& url, const WebURL& first_party_for_cookies) {
159   // WebSocketHandshake may access cookies in worker process.
160   return WebString();
161 }
162
163 WebString WorkerWebKitPlatformSupportImpl::defaultLocale() {
164   NOTREACHED();
165   return WebString();
166 }
167
168 WebStorageNamespace*
169 WorkerWebKitPlatformSupportImpl::createLocalStorageNamespace() {
170   NOTREACHED();
171   return 0;
172 }
173
174 void WorkerWebKitPlatformSupportImpl::dispatchStorageEvent(
175     const WebString& key, const WebString& old_value,
176     const WebString& new_value, const WebString& origin,
177     const blink::WebURL& url, bool is_local_storage) {
178   NOTREACHED();
179 }
180
181 Platform::FileHandle
182 WorkerWebKitPlatformSupportImpl::databaseOpenFile(
183     const WebString& vfs_file_name, int desired_flags) {
184   return DatabaseUtil::DatabaseOpenFile(
185       vfs_file_name, desired_flags, sync_message_filter_.get());
186 }
187
188 int WorkerWebKitPlatformSupportImpl::databaseDeleteFile(
189     const WebString& vfs_file_name, bool sync_dir) {
190   return DatabaseUtil::DatabaseDeleteFile(
191       vfs_file_name, sync_dir, sync_message_filter_.get());
192 }
193
194 long WorkerWebKitPlatformSupportImpl::databaseGetFileAttributes(
195     const WebString& vfs_file_name) {
196   return DatabaseUtil::DatabaseGetFileAttributes(vfs_file_name,
197                                                  sync_message_filter_.get());
198 }
199
200 long long WorkerWebKitPlatformSupportImpl::databaseGetFileSize(
201     const WebString& vfs_file_name) {
202   return DatabaseUtil::DatabaseGetFileSize(vfs_file_name,
203                                            sync_message_filter_.get());
204 }
205
206 long long WorkerWebKitPlatformSupportImpl::databaseGetSpaceAvailableForOrigin(
207     const WebString& origin_identifier) {
208   return DatabaseUtil::DatabaseGetSpaceAvailable(origin_identifier,
209                                                  sync_message_filter_.get());
210 }
211
212 blink::WebIDBFactory* WorkerWebKitPlatformSupportImpl::idbFactory() {
213   if (!web_idb_factory_)
214     web_idb_factory_.reset(new WebIDBFactoryImpl(thread_safe_sender_.get()));
215   return web_idb_factory_.get();
216 }
217
218 blink::WebDatabaseObserver*
219 WorkerWebKitPlatformSupportImpl::databaseObserver() {
220   return web_database_observer_impl_.get();
221 }
222
223 WebMimeRegistry::SupportsType
224 WorkerWebKitPlatformSupportImpl::supportsMIMEType(
225     const WebString&) {
226   return WebMimeRegistry::IsSupported;
227 }
228
229 WebMimeRegistry::SupportsType
230 WorkerWebKitPlatformSupportImpl::supportsImageMIMEType(
231     const WebString&) {
232   NOTREACHED();
233   return WebMimeRegistry::IsSupported;
234 }
235
236 WebMimeRegistry::SupportsType
237 WorkerWebKitPlatformSupportImpl::supportsJavaScriptMIMEType(const WebString&) {
238   NOTREACHED();
239   return WebMimeRegistry::IsSupported;
240 }
241
242 WebMimeRegistry::SupportsType
243 WorkerWebKitPlatformSupportImpl::supportsMediaMIMEType(
244     const WebString&, const WebString&, const WebString&) {
245   NOTREACHED();
246   return WebMimeRegistry::IsSupported;
247 }
248
249 bool WorkerWebKitPlatformSupportImpl::supportsMediaSourceMIMEType(
250     const blink::WebString& mimeType, const blink::WebString& codecs) {
251   NOTREACHED();
252   return false;
253 }
254
255 WebMimeRegistry::SupportsType
256 WorkerWebKitPlatformSupportImpl::supportsNonImageMIMEType(
257     const WebString&) {
258   NOTREACHED();
259   return WebMimeRegistry::IsSupported;
260 }
261
262 WebString WorkerWebKitPlatformSupportImpl::mimeTypeForExtension(
263     const WebString& file_extension) {
264   std::string mime_type;
265   thread_safe_sender_->Send(new MimeRegistryMsg_GetMimeTypeFromExtension(
266       base::FilePath::FromUTF16Unsafe(file_extension).value(), &mime_type));
267   return base::ASCIIToUTF16(mime_type);
268 }
269
270 WebString WorkerWebKitPlatformSupportImpl::wellKnownMimeTypeForExtension(
271     const WebString& file_extension) {
272   std::string mime_type;
273   net::GetWellKnownMimeTypeFromExtension(
274       base::FilePath::FromUTF16Unsafe(file_extension).value(), &mime_type);
275   return base::ASCIIToUTF16(mime_type);
276 }
277
278 WebString WorkerWebKitPlatformSupportImpl::mimeTypeFromFile(
279     const WebString& file_path) {
280   std::string mime_type;
281   thread_safe_sender_->Send(
282       new MimeRegistryMsg_GetMimeTypeFromFile(
283           base::FilePath::FromUTF16Unsafe(file_path),
284           &mime_type));
285   return base::ASCIIToUTF16(mime_type);
286 }
287
288 WebBlobRegistry* WorkerWebKitPlatformSupportImpl::blobRegistry() {
289   return blob_registry_.get();
290 }
291
292 void WorkerWebKitPlatformSupportImpl::queryStorageUsageAndQuota(
293     const blink::WebURL& storage_partition,
294     blink::WebStorageQuotaType type,
295     blink::WebStorageQuotaCallbacks callbacks) {
296   if (!thread_safe_sender_.get() || !quota_message_filter_.get())
297     return;
298   QuotaDispatcher::ThreadSpecificInstance(
299       thread_safe_sender_.get(),
300       quota_message_filter_.get())->QueryStorageUsageAndQuota(
301           storage_partition,
302           static_cast<quota::StorageType>(type),
303           QuotaDispatcher::CreateWebStorageQuotaCallbacksWrapper(callbacks));
304 }
305
306 }  // namespace content