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
33using WebKit::Platform;
34using WebKit::WebBlobRegistry;
35using WebKit::WebClipboard;
36using WebKit::WebFileInfo;
37using WebKit::WebFileSystem;
38using WebKit::WebFileUtilities;
39using WebKit::WebMessagePortChannel;
40using WebKit::WebMimeRegistry;
41using WebKit::WebSandboxSupport;
42using WebKit::WebStorageNamespace;
43using WebKit::WebString;
44using WebKit::WebURL;
45
46namespace content {
47
48// TODO(kinuko): Probably this could be consolidated into
49// RendererWebKitPlatformSupportImpl::FileUtilities.
50class 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
60bool 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
78WorkerWebKitPlatformSupportImpl::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}
87
88WorkerWebKitPlatformSupportImpl::~WorkerWebKitPlatformSupportImpl() {
89}
90
91WebClipboard* WorkerWebKitPlatformSupportImpl::clipboard() {
92  NOTREACHED();
93  return NULL;
94}
95
96WebMimeRegistry* WorkerWebKitPlatformSupportImpl::mimeRegistry() {
97  return this;
98}
99
100WebFileSystem* WorkerWebKitPlatformSupportImpl::fileSystem() {
101  if (!web_file_system_)
102    web_file_system_.reset(new WebFileSystemImpl(child_thread_loop_.get()));
103  return web_file_system_.get();
104}
105
106WebFileUtilities* WorkerWebKitPlatformSupportImpl::fileUtilities() {
107  if (!file_utilities_) {
108    file_utilities_.reset(new FileUtilities(thread_safe_sender_.get()));
109    file_utilities_->set_sandbox_enabled(sandboxEnabled());
110  }
111  return file_utilities_.get();
112}
113
114WebSandboxSupport* WorkerWebKitPlatformSupportImpl::sandboxSupport() {
115  NOTREACHED();
116  return NULL;
117}
118
119bool WorkerWebKitPlatformSupportImpl::sandboxEnabled() {
120  // Always return true because WebKit should always act as though the Sandbox
121  // is enabled for workers.  See the comment in WebKitPlatformSupport for
122  // more info.
123  return true;
124}
125
126unsigned long long WorkerWebKitPlatformSupportImpl::visitedLinkHash(
127    const char* canonical_url,
128    size_t length) {
129  NOTREACHED();
130  return 0;
131}
132
133bool WorkerWebKitPlatformSupportImpl::isLinkVisited(
134    unsigned long long link_hash) {
135  NOTREACHED();
136  return false;
137}
138
139WebMessagePortChannel*
140WorkerWebKitPlatformSupportImpl::createMessagePortChannel() {
141  return new WebMessagePortChannelImpl(child_thread_loop_.get());
142}
143
144void WorkerWebKitPlatformSupportImpl::setCookies(
145    const WebURL& url,
146    const WebURL& first_party_for_cookies,
147    const WebString& value) {
148  NOTREACHED();
149}
150
151WebString WorkerWebKitPlatformSupportImpl::cookies(
152    const WebURL& url, const WebURL& first_party_for_cookies) {
153  // WebSocketHandshake may access cookies in worker process.
154  return WebString();
155}
156
157WebString WorkerWebKitPlatformSupportImpl::defaultLocale() {
158  NOTREACHED();
159  return WebString();
160}
161
162WebStorageNamespace*
163WorkerWebKitPlatformSupportImpl::createLocalStorageNamespace() {
164  NOTREACHED();
165  return 0;
166}
167
168void WorkerWebKitPlatformSupportImpl::dispatchStorageEvent(
169    const WebString& key, const WebString& old_value,
170    const WebString& new_value, const WebString& origin,
171    const WebKit::WebURL& url, bool is_local_storage) {
172  NOTREACHED();
173}
174
175Platform::FileHandle
176WorkerWebKitPlatformSupportImpl::databaseOpenFile(
177    const WebString& vfs_file_name, int desired_flags) {
178  return DatabaseUtil::DatabaseOpenFile(
179      vfs_file_name, desired_flags, sync_message_filter_.get());
180}
181
182int WorkerWebKitPlatformSupportImpl::databaseDeleteFile(
183    const WebString& vfs_file_name, bool sync_dir) {
184  return DatabaseUtil::DatabaseDeleteFile(
185      vfs_file_name, sync_dir, sync_message_filter_.get());
186}
187
188long WorkerWebKitPlatformSupportImpl::databaseGetFileAttributes(
189    const WebString& vfs_file_name) {
190  return DatabaseUtil::DatabaseGetFileAttributes(vfs_file_name,
191                                                 sync_message_filter_.get());
192}
193
194long long WorkerWebKitPlatformSupportImpl::databaseGetFileSize(
195    const WebString& vfs_file_name) {
196  return DatabaseUtil::DatabaseGetFileSize(vfs_file_name,
197                                           sync_message_filter_.get());
198}
199
200long long WorkerWebKitPlatformSupportImpl::databaseGetSpaceAvailableForOrigin(
201    const WebString& origin_identifier) {
202  return DatabaseUtil::DatabaseGetSpaceAvailable(origin_identifier,
203                                                 sync_message_filter_.get());
204}
205
206WebKit::WebIDBFactory* WorkerWebKitPlatformSupportImpl::idbFactory() {
207  if (!web_idb_factory_)
208    web_idb_factory_.reset(
209        new RendererWebIDBFactoryImpl(thread_safe_sender_.get()));
210  return web_idb_factory_.get();
211}
212
213WebMimeRegistry::SupportsType
214WorkerWebKitPlatformSupportImpl::supportsMIMEType(
215    const WebString&) {
216  return WebMimeRegistry::IsSupported;
217}
218
219WebMimeRegistry::SupportsType
220WorkerWebKitPlatformSupportImpl::supportsImageMIMEType(
221    const WebString&) {
222  NOTREACHED();
223  return WebMimeRegistry::IsSupported;
224}
225
226WebMimeRegistry::SupportsType
227WorkerWebKitPlatformSupportImpl::supportsJavaScriptMIMEType(const WebString&) {
228  NOTREACHED();
229  return WebMimeRegistry::IsSupported;
230}
231
232WebMimeRegistry::SupportsType
233WorkerWebKitPlatformSupportImpl::supportsMediaMIMEType(
234    const WebString&, const WebString&) {
235  NOTREACHED();
236  return WebMimeRegistry::IsSupported;
237}
238
239WebMimeRegistry::SupportsType
240WorkerWebKitPlatformSupportImpl::supportsMediaMIMEType(
241    const WebString&, const WebString&, const WebString&) {
242  NOTREACHED();
243  return WebMimeRegistry::IsSupported;
244}
245
246bool WorkerWebKitPlatformSupportImpl::supportsMediaSourceMIMEType(
247    const WebKit::WebString& mimeType, const WebKit::WebString& codecs) {
248  NOTREACHED();
249  return false;
250}
251
252WebMimeRegistry::SupportsType
253WorkerWebKitPlatformSupportImpl::supportsNonImageMIMEType(
254    const WebString&) {
255  NOTREACHED();
256  return WebMimeRegistry::IsSupported;
257}
258
259WebString WorkerWebKitPlatformSupportImpl::mimeTypeForExtension(
260    const WebString& file_extension) {
261  std::string mime_type;
262  thread_safe_sender_->Send(new MimeRegistryMsg_GetMimeTypeFromExtension(
263      base::FilePath::FromUTF16Unsafe(file_extension).value(), &mime_type));
264  return ASCIIToUTF16(mime_type);
265}
266
267WebString WorkerWebKitPlatformSupportImpl::wellKnownMimeTypeForExtension(
268    const WebString& file_extension) {
269  std::string mime_type;
270  net::GetWellKnownMimeTypeFromExtension(
271      base::FilePath::FromUTF16Unsafe(file_extension).value(), &mime_type);
272  return ASCIIToUTF16(mime_type);
273}
274
275WebString WorkerWebKitPlatformSupportImpl::mimeTypeFromFile(
276    const WebString& file_path) {
277  std::string mime_type;
278  thread_safe_sender_->Send(
279      new MimeRegistryMsg_GetMimeTypeFromFile(
280          base::FilePath::FromUTF16Unsafe(file_path),
281          &mime_type));
282  return ASCIIToUTF16(mime_type);
283}
284
285WebString WorkerWebKitPlatformSupportImpl::preferredExtensionForMIMEType(
286    const WebString& mime_type) {
287  base::FilePath::StringType file_extension;
288  thread_safe_sender_->Send(
289      new MimeRegistryMsg_GetPreferredExtensionForMimeType(
290          UTF16ToASCII(mime_type), &file_extension));
291  return base::FilePath(file_extension).AsUTF16Unsafe();
292}
293
294WebBlobRegistry* WorkerWebKitPlatformSupportImpl::blobRegistry() {
295  if (!blob_registry_.get() && thread_safe_sender_.get())
296    blob_registry_.reset(new WebBlobRegistryImpl(thread_safe_sender_.get()));
297  return blob_registry_.get();
298}
299
300void WorkerWebKitPlatformSupportImpl::queryStorageUsageAndQuota(
301    const WebKit::WebURL& storage_partition,
302    WebKit::WebStorageQuotaType type,
303    WebKit::WebStorageQuotaCallbacks* callbacks) {
304  if (!thread_safe_sender_.get() || !quota_message_filter_.get())
305    return;
306  QuotaDispatcher::ThreadSpecificInstance(
307      thread_safe_sender_.get(),
308      quota_message_filter_.get())->QueryStorageUsageAndQuota(
309          storage_partition,
310          static_cast<quota::StorageType>(type),
311          QuotaDispatcher::CreateWebStorageQuotaCallbacksWrapper(callbacks));
312}
313
314}  // namespace content
315