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