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.
5 #include "content/worker/worker_webkitplatformsupport_impl.h"
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"
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
;
49 // TODO(kinuko): Probably this could be consolidated into
50 // RendererWebKitPlatformSupportImpl::FileUtilities.
51 class WorkerWebKitPlatformSupportImpl::FileUtilities
52 : public WebFileUtilitiesImpl
{
54 explicit FileUtilities(ThreadSafeSender
* sender
)
55 : thread_safe_sender_(sender
) {}
56 virtual bool getFileInfo(const WebString
& path
, WebFileInfo
& result
);
58 scoped_refptr
<ThreadSafeSender
> thread_safe_sender_
;
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
) {
72 FileInfoToWebFileInfo(file_info
, &web_file_info
);
73 web_file_info
.platformPath
= path
;
77 //------------------------------------------------------------------------------
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
) {
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
));
95 WorkerWebKitPlatformSupportImpl::~WorkerWebKitPlatformSupportImpl() {
96 WebFileSystemImpl::DeleteThreadSpecificInstance();
99 WebClipboard
* WorkerWebKitPlatformSupportImpl::clipboard() {
104 WebMimeRegistry
* WorkerWebKitPlatformSupportImpl::mimeRegistry() {
108 WebFileSystem
* WorkerWebKitPlatformSupportImpl::fileSystem() {
109 return WebFileSystemImpl::ThreadSpecificInstance(child_thread_loop_
.get());
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());
117 return file_utilities_
.get();
120 WebSandboxSupport
* WorkerWebKitPlatformSupportImpl::sandboxSupport() {
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
132 unsigned long long WorkerWebKitPlatformSupportImpl::visitedLinkHash(
133 const char* canonical_url
,
139 bool WorkerWebKitPlatformSupportImpl::isLinkVisited(
140 unsigned long long link_hash
) {
145 void WorkerWebKitPlatformSupportImpl::createMessageChannel(
146 blink::WebMessagePortChannel
** channel1
,
147 blink::WebMessagePortChannel
** channel2
) {
148 WebMessagePortChannelImpl::CreatePair(
149 child_thread_loop_
.get(), channel1
, channel2
);
152 void WorkerWebKitPlatformSupportImpl::setCookies(
154 const WebURL
& first_party_for_cookies
,
155 const WebString
& value
) {
159 WebString
WorkerWebKitPlatformSupportImpl::cookies(
160 const WebURL
& url
, const WebURL
& first_party_for_cookies
) {
161 // WebSocketHandshake may access cookies in worker process.
165 WebString
WorkerWebKitPlatformSupportImpl::defaultLocale() {
171 WorkerWebKitPlatformSupportImpl::createLocalStorageNamespace() {
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
) {
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());
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());
196 long WorkerWebKitPlatformSupportImpl::databaseGetFileAttributes(
197 const WebString
& vfs_file_name
) {
198 return DatabaseUtil::DatabaseGetFileAttributes(vfs_file_name
,
199 sync_message_filter_
.get());
202 long long WorkerWebKitPlatformSupportImpl::databaseGetFileSize(
203 const WebString
& vfs_file_name
) {
204 return DatabaseUtil::DatabaseGetFileSize(vfs_file_name
,
205 sync_message_filter_
.get());
208 long long WorkerWebKitPlatformSupportImpl::databaseGetSpaceAvailableForOrigin(
209 const WebString
& origin_identifier
) {
210 return DatabaseUtil::DatabaseGetSpaceAvailable(origin_identifier
,
211 sync_message_filter_
.get());
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();
220 blink::WebDatabaseObserver
*
221 WorkerWebKitPlatformSupportImpl::databaseObserver() {
222 return web_database_observer_impl_
.get();
225 WebMimeRegistry::SupportsType
226 WorkerWebKitPlatformSupportImpl::supportsMIMEType(
228 return WebMimeRegistry::IsSupported
;
231 WebMimeRegistry::SupportsType
232 WorkerWebKitPlatformSupportImpl::supportsImageMIMEType(
235 return WebMimeRegistry::IsSupported
;
238 WebMimeRegistry::SupportsType
239 WorkerWebKitPlatformSupportImpl::supportsJavaScriptMIMEType(const WebString
&) {
241 return WebMimeRegistry::IsSupported
;
244 WebMimeRegistry::SupportsType
245 WorkerWebKitPlatformSupportImpl::supportsMediaMIMEType(
246 const WebString
&, const WebString
&, const WebString
&) {
248 return WebMimeRegistry::IsSupported
;
251 bool WorkerWebKitPlatformSupportImpl::supportsMediaSourceMIMEType(
252 const blink::WebString
& mimeType
, const blink::WebString
& codecs
) {
257 bool WorkerWebKitPlatformSupportImpl::supportsEncryptedMediaMIMEType(
258 const blink::WebString
& key_system
,
259 const blink::WebString
& mime_type
,
260 const blink::WebString
& codecs
) {
265 WebMimeRegistry::SupportsType
266 WorkerWebKitPlatformSupportImpl::supportsNonImageMIMEType(
269 return WebMimeRegistry::IsSupported
;
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
);
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
);
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
),
295 return base::ASCIIToUTF16(mime_type
);
298 WebBlobRegistry
* WorkerWebKitPlatformSupportImpl::blobRegistry() {
299 return blob_registry_
.get();
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())
308 QuotaDispatcher::ThreadSpecificInstance(
309 thread_safe_sender_
.get(),
310 quota_message_filter_
.get())->QueryStorageUsageAndQuota(
312 static_cast<quota::StorageType
>(type
),
313 QuotaDispatcher::CreateWebStorageQuotaCallbacksWrapper(callbacks
));
316 } // namespace content