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/strings/utf_string_conversions.h"
11 #include "content/child/blink_glue.h"
12 #include "content/child/database_util.h"
13 #include "content/child/fileapi/webfilesystem_impl.h"
14 #include "content/child/indexed_db/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/web_database_observer_impl.h"
19 #include "content/child/webblobregistry_impl.h"
20 #include "content/child/webfileutilities_impl.h"
21 #include "content/child/webmessageportchannel_impl.h"
22 #include "content/common/file_utilities_messages.h"
23 #include "content/common/mime_registry_messages.h"
24 #include "content/worker/worker_thread.h"
25 #include "ipc/ipc_sync_message_filter.h"
26 #include "net/base/mime_util.h"
27 #include "third_party/WebKit/public/platform/WebBlobRegistry.h"
28 #include "third_party/WebKit/public/platform/WebFileInfo.h"
29 #include "third_party/WebKit/public/platform/WebString.h"
30 #include "third_party/WebKit/public/platform/WebURL.h"
31 #include "webkit/common/quota/quota_types.h"
33 using blink::Platform
;
34 using blink::WebBlobRegistry
;
35 using blink::WebClipboard
;
36 using blink::WebFileInfo
;
37 using blink::WebFileSystem
;
38 using blink::WebFileUtilities
;
39 using blink::WebMessagePortChannel
;
40 using blink::WebMimeRegistry
;
41 using blink::WebSandboxSupport
;
42 using blink::WebStorageNamespace
;
43 using blink::WebString
;
48 // TODO(kinuko): Probably this could be consolidated into
49 // RendererWebKitPlatformSupportImpl::FileUtilities.
50 class WorkerWebKitPlatformSupportImpl::FileUtilities
51 : public WebFileUtilitiesImpl
{
53 explicit FileUtilities(ThreadSafeSender
* sender
)
54 : thread_safe_sender_(sender
) {}
55 virtual bool getFileInfo(const WebString
& path
, WebFileInfo
& result
);
57 scoped_refptr
<ThreadSafeSender
> thread_safe_sender_
;
60 bool WorkerWebKitPlatformSupportImpl::FileUtilities::getFileInfo(
61 const WebString
& path
,
62 WebFileInfo
& web_file_info
) {
63 base::File::Info file_info
;
64 base::File::Error 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::File::FILE_OK
) {
71 FileInfoToWebFileInfo(file_info
, &web_file_info
);
72 web_file_info
.platformPath
= path
;
76 //------------------------------------------------------------------------------
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
) {
87 blob_registry_
.reset(new WebBlobRegistryImpl(sender
));
88 web_idb_factory_
.reset(new WebIDBFactoryImpl(sender
));
89 web_database_observer_impl_
.reset(
90 new WebDatabaseObserverImpl(sync_message_filter
));
94 WorkerWebKitPlatformSupportImpl::~WorkerWebKitPlatformSupportImpl() {
95 WebFileSystemImpl::DeleteThreadSpecificInstance();
98 WebClipboard
* WorkerWebKitPlatformSupportImpl::clipboard() {
103 WebMimeRegistry
* WorkerWebKitPlatformSupportImpl::mimeRegistry() {
107 WebFileSystem
* WorkerWebKitPlatformSupportImpl::fileSystem() {
108 return WebFileSystemImpl::ThreadSpecificInstance(child_thread_loop_
.get());
111 WebFileUtilities
* WorkerWebKitPlatformSupportImpl::fileUtilities() {
112 if (!file_utilities_
) {
113 file_utilities_
.reset(new FileUtilities(thread_safe_sender_
.get()));
114 file_utilities_
->set_sandbox_enabled(sandboxEnabled());
116 return file_utilities_
.get();
119 WebSandboxSupport
* WorkerWebKitPlatformSupportImpl::sandboxSupport() {
124 bool WorkerWebKitPlatformSupportImpl::sandboxEnabled() {
125 // Always return true because WebKit should always act as though the Sandbox
126 // is enabled for workers. See the comment in WebKitPlatformSupport for
131 unsigned long long WorkerWebKitPlatformSupportImpl::visitedLinkHash(
132 const char* canonical_url
,
138 bool WorkerWebKitPlatformSupportImpl::isLinkVisited(
139 unsigned long long link_hash
) {
144 void WorkerWebKitPlatformSupportImpl::createMessageChannel(
145 blink::WebMessagePortChannel
** channel1
,
146 blink::WebMessagePortChannel
** channel2
) {
147 WebMessagePortChannelImpl::CreatePair(
148 child_thread_loop_
.get(), channel1
, channel2
);
151 void WorkerWebKitPlatformSupportImpl::setCookies(
153 const WebURL
& first_party_for_cookies
,
154 const WebString
& value
) {
158 WebString
WorkerWebKitPlatformSupportImpl::cookies(
159 const WebURL
& url
, const WebURL
& first_party_for_cookies
) {
160 // WebSocketHandshake may access cookies in worker process.
164 WebString
WorkerWebKitPlatformSupportImpl::defaultLocale() {
170 WorkerWebKitPlatformSupportImpl::createLocalStorageNamespace() {
175 void WorkerWebKitPlatformSupportImpl::dispatchStorageEvent(
176 const WebString
& key
, const WebString
& old_value
,
177 const WebString
& new_value
, const WebString
& origin
,
178 const blink::WebURL
& url
, bool is_local_storage
) {
183 WorkerWebKitPlatformSupportImpl::databaseOpenFile(
184 const WebString
& vfs_file_name
, int desired_flags
) {
185 return DatabaseUtil::DatabaseOpenFile(
186 vfs_file_name
, desired_flags
, sync_message_filter_
.get());
189 int WorkerWebKitPlatformSupportImpl::databaseDeleteFile(
190 const WebString
& vfs_file_name
, bool sync_dir
) {
191 return DatabaseUtil::DatabaseDeleteFile(
192 vfs_file_name
, sync_dir
, sync_message_filter_
.get());
195 long WorkerWebKitPlatformSupportImpl::databaseGetFileAttributes(
196 const WebString
& vfs_file_name
) {
197 return DatabaseUtil::DatabaseGetFileAttributes(vfs_file_name
,
198 sync_message_filter_
.get());
201 long long WorkerWebKitPlatformSupportImpl::databaseGetFileSize(
202 const WebString
& vfs_file_name
) {
203 return DatabaseUtil::DatabaseGetFileSize(vfs_file_name
,
204 sync_message_filter_
.get());
207 long long WorkerWebKitPlatformSupportImpl::databaseGetSpaceAvailableForOrigin(
208 const WebString
& origin_identifier
) {
209 return DatabaseUtil::DatabaseGetSpaceAvailable(origin_identifier
,
210 sync_message_filter_
.get());
213 blink::WebIDBFactory
* WorkerWebKitPlatformSupportImpl::idbFactory() {
214 if (!web_idb_factory_
)
215 web_idb_factory_
.reset(new WebIDBFactoryImpl(thread_safe_sender_
.get()));
216 return web_idb_factory_
.get();
219 blink::WebDatabaseObserver
*
220 WorkerWebKitPlatformSupportImpl::databaseObserver() {
221 return web_database_observer_impl_
.get();
224 WebMimeRegistry::SupportsType
225 WorkerWebKitPlatformSupportImpl::supportsMIMEType(
227 return WebMimeRegistry::IsSupported
;
230 WebMimeRegistry::SupportsType
231 WorkerWebKitPlatformSupportImpl::supportsImageMIMEType(
234 return WebMimeRegistry::IsSupported
;
237 WebMimeRegistry::SupportsType
238 WorkerWebKitPlatformSupportImpl::supportsJavaScriptMIMEType(const WebString
&) {
240 return WebMimeRegistry::IsSupported
;
243 WebMimeRegistry::SupportsType
244 WorkerWebKitPlatformSupportImpl::supportsMediaMIMEType(
245 const WebString
&, const WebString
&, const WebString
&) {
247 return WebMimeRegistry::IsSupported
;
250 bool WorkerWebKitPlatformSupportImpl::supportsMediaSourceMIMEType(
251 const blink::WebString
& mimeType
, const blink::WebString
& codecs
) {
256 bool WorkerWebKitPlatformSupportImpl::supportsEncryptedMediaMIMEType(
257 const blink::WebString
& key_system
,
258 const blink::WebString
& mime_type
,
259 const blink::WebString
& codecs
) {
264 WebMimeRegistry::SupportsType
265 WorkerWebKitPlatformSupportImpl::supportsNonImageMIMEType(
268 return WebMimeRegistry::IsSupported
;
271 WebString
WorkerWebKitPlatformSupportImpl::mimeTypeForExtension(
272 const WebString
& file_extension
) {
273 std::string mime_type
;
274 thread_safe_sender_
->Send(new MimeRegistryMsg_GetMimeTypeFromExtension(
275 base::FilePath::FromUTF16Unsafe(file_extension
).value(), &mime_type
));
276 return base::ASCIIToUTF16(mime_type
);
279 WebString
WorkerWebKitPlatformSupportImpl::wellKnownMimeTypeForExtension(
280 const WebString
& file_extension
) {
281 std::string mime_type
;
282 net::GetWellKnownMimeTypeFromExtension(
283 base::FilePath::FromUTF16Unsafe(file_extension
).value(), &mime_type
);
284 return base::ASCIIToUTF16(mime_type
);
287 WebString
WorkerWebKitPlatformSupportImpl::mimeTypeFromFile(
288 const WebString
& file_path
) {
289 std::string mime_type
;
290 thread_safe_sender_
->Send(
291 new MimeRegistryMsg_GetMimeTypeFromFile(
292 base::FilePath::FromUTF16Unsafe(file_path
),
294 return base::ASCIIToUTF16(mime_type
);
297 WebBlobRegistry
* WorkerWebKitPlatformSupportImpl::blobRegistry() {
298 return blob_registry_
.get();
301 void WorkerWebKitPlatformSupportImpl::queryStorageUsageAndQuota(
302 const blink::WebURL
& storage_partition
,
303 blink::WebStorageQuotaType type
,
304 blink::WebStorageQuotaCallbacks callbacks
) {
305 if (!thread_safe_sender_
.get() || !quota_message_filter_
.get())
307 QuotaDispatcher::ThreadSpecificInstance(
308 thread_safe_sender_
.get(),
309 quota_message_filter_
.get())->QueryStorageUsageAndQuota(
311 static_cast<quota::StorageType
>(type
),
312 QuotaDispatcher::CreateWebStorageQuotaCallbacksWrapper(callbacks
));
315 } // namespace content