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