Upstream version 7.36.149.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 void WorkerWebKitPlatformSupportImpl::createMessageChannel(
146     blink::WebMessagePortChannel** channel1,
147     blink::WebMessagePortChannel** channel2) {
148   WebMessagePortChannelImpl::CreatePair(
149       child_thread_loop_.get(), channel1, channel2);
150 }
151
152 void WorkerWebKitPlatformSupportImpl::setCookies(
153     const WebURL& url,
154     const WebURL& first_party_for_cookies,
155     const WebString& value) {
156   NOTREACHED();
157 }
158
159 WebString WorkerWebKitPlatformSupportImpl::cookies(
160     const WebURL& url, const WebURL& first_party_for_cookies) {
161   // WebSocketHandshake may access cookies in worker process.
162   return WebString();
163 }
164
165 WebString WorkerWebKitPlatformSupportImpl::defaultLocale() {
166   NOTREACHED();
167   return WebString();
168 }
169
170 WebStorageNamespace*
171 WorkerWebKitPlatformSupportImpl::createLocalStorageNamespace() {
172   NOTREACHED();
173   return 0;
174 }
175
176 void WorkerWebKitPlatformSupportImpl::dispatchStorageEvent(
177     const WebString& key, const WebString& old_value,
178     const WebString& new_value, const WebString& origin,
179     const blink::WebURL& url, bool is_local_storage) {
180   NOTREACHED();
181 }
182
183 Platform::FileHandle
184 WorkerWebKitPlatformSupportImpl::databaseOpenFile(
185     const WebString& vfs_file_name, int desired_flags) {
186   return DatabaseUtil::DatabaseOpenFile(
187       vfs_file_name, desired_flags, sync_message_filter_.get());
188 }
189
190 int WorkerWebKitPlatformSupportImpl::databaseDeleteFile(
191     const WebString& vfs_file_name, bool sync_dir) {
192   return DatabaseUtil::DatabaseDeleteFile(
193       vfs_file_name, sync_dir, sync_message_filter_.get());
194 }
195
196 long WorkerWebKitPlatformSupportImpl::databaseGetFileAttributes(
197     const WebString& vfs_file_name) {
198   return DatabaseUtil::DatabaseGetFileAttributes(vfs_file_name,
199                                                  sync_message_filter_.get());
200 }
201
202 long long WorkerWebKitPlatformSupportImpl::databaseGetFileSize(
203     const WebString& vfs_file_name) {
204   return DatabaseUtil::DatabaseGetFileSize(vfs_file_name,
205                                            sync_message_filter_.get());
206 }
207
208 long long WorkerWebKitPlatformSupportImpl::databaseGetSpaceAvailableForOrigin(
209     const WebString& origin_identifier) {
210   return DatabaseUtil::DatabaseGetSpaceAvailable(origin_identifier,
211                                                  sync_message_filter_.get());
212 }
213
214 blink::WebIDBFactory* WorkerWebKitPlatformSupportImpl::idbFactory() {
215   if (!web_idb_factory_)
216     web_idb_factory_.reset(new WebIDBFactoryImpl(thread_safe_sender_.get()));
217   return web_idb_factory_.get();
218 }
219
220 blink::WebDatabaseObserver*
221 WorkerWebKitPlatformSupportImpl::databaseObserver() {
222   return web_database_observer_impl_.get();
223 }
224
225 WebMimeRegistry::SupportsType
226 WorkerWebKitPlatformSupportImpl::supportsMIMEType(
227     const WebString&) {
228   return WebMimeRegistry::IsSupported;
229 }
230
231 WebMimeRegistry::SupportsType
232 WorkerWebKitPlatformSupportImpl::supportsImageMIMEType(
233     const WebString&) {
234   NOTREACHED();
235   return WebMimeRegistry::IsSupported;
236 }
237
238 WebMimeRegistry::SupportsType
239 WorkerWebKitPlatformSupportImpl::supportsJavaScriptMIMEType(const WebString&) {
240   NOTREACHED();
241   return WebMimeRegistry::IsSupported;
242 }
243
244 WebMimeRegistry::SupportsType
245 WorkerWebKitPlatformSupportImpl::supportsMediaMIMEType(
246     const WebString&, const WebString&, const WebString&) {
247   NOTREACHED();
248   return WebMimeRegistry::IsSupported;
249 }
250
251 bool WorkerWebKitPlatformSupportImpl::supportsMediaSourceMIMEType(
252     const blink::WebString& mimeType, const blink::WebString& codecs) {
253   NOTREACHED();
254   return false;
255 }
256
257 bool WorkerWebKitPlatformSupportImpl::supportsEncryptedMediaMIMEType(
258     const blink::WebString& key_system,
259     const blink::WebString& mime_type,
260     const blink::WebString& codecs) {
261   NOTREACHED();
262   return false;
263 }
264
265 WebMimeRegistry::SupportsType
266 WorkerWebKitPlatformSupportImpl::supportsNonImageMIMEType(
267     const WebString&) {
268   NOTREACHED();
269   return WebMimeRegistry::IsSupported;
270 }
271
272 WebString WorkerWebKitPlatformSupportImpl::mimeTypeForExtension(
273     const WebString& file_extension) {
274   std::string mime_type;
275   thread_safe_sender_->Send(new MimeRegistryMsg_GetMimeTypeFromExtension(
276       base::FilePath::FromUTF16Unsafe(file_extension).value(), &mime_type));
277   return base::ASCIIToUTF16(mime_type);
278 }
279
280 WebString WorkerWebKitPlatformSupportImpl::wellKnownMimeTypeForExtension(
281     const WebString& file_extension) {
282   std::string mime_type;
283   net::GetWellKnownMimeTypeFromExtension(
284       base::FilePath::FromUTF16Unsafe(file_extension).value(), &mime_type);
285   return base::ASCIIToUTF16(mime_type);
286 }
287
288 WebString WorkerWebKitPlatformSupportImpl::mimeTypeFromFile(
289     const WebString& file_path) {
290   std::string mime_type;
291   thread_safe_sender_->Send(
292       new MimeRegistryMsg_GetMimeTypeFromFile(
293           base::FilePath::FromUTF16Unsafe(file_path),
294           &mime_type));
295   return base::ASCIIToUTF16(mime_type);
296 }
297
298 WebBlobRegistry* WorkerWebKitPlatformSupportImpl::blobRegistry() {
299   return blob_registry_.get();
300 }
301
302 void WorkerWebKitPlatformSupportImpl::queryStorageUsageAndQuota(
303     const blink::WebURL& storage_partition,
304     blink::WebStorageQuotaType type,
305     blink::WebStorageQuotaCallbacks callbacks) {
306   if (!thread_safe_sender_.get() || !quota_message_filter_.get())
307     return;
308   QuotaDispatcher::ThreadSpecificInstance(
309       thread_safe_sender_.get(),
310       quota_message_filter_.get())->QueryStorageUsageAndQuota(
311           storage_partition,
312           static_cast<quota::StorageType>(type),
313           QuotaDispatcher::CreateWebStorageQuotaCallbacksWrapper(callbacks));
314 }
315
316 }  // namespace content