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/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/webmessageportchannel_impl.h"
21 #include "content/common/file_utilities_messages.h"
22 #include "content/common/mime_registry_messages.h"
23 #include "content/worker/worker_thread.h"
24 #include "ipc/ipc_sync_message_filter.h"
25 #include "net/base/mime_util.h"
26 #include "third_party/WebKit/public/platform/WebBlobRegistry.h"
27 #include "third_party/WebKit/public/platform/WebFileInfo.h"
28 #include "third_party/WebKit/public/platform/WebString.h"
29 #include "third_party/WebKit/public/platform/WebURL.h"
30 #include "webkit/common/quota/quota_types.h"
31 #include "webkit/glue/webfileutilities_impl.h"
32 #include "webkit/glue/webkit_glue.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 webkit_glue::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::PlatformFileInfo file_info
;
65 base::PlatformFileError 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::PLATFORM_FILE_OK
) {
72 webkit_glue::PlatformFileInfoToWebFileInfo(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 WebMessagePortChannel
*
146 WorkerWebKitPlatformSupportImpl::createMessagePortChannel() {
147 return new WebMessagePortChannelImpl(child_thread_loop_
.get());
150 void WorkerWebKitPlatformSupportImpl::setCookies(
152 const WebURL
& first_party_for_cookies
,
153 const WebString
& value
) {
157 WebString
WorkerWebKitPlatformSupportImpl::cookies(
158 const WebURL
& url
, const WebURL
& first_party_for_cookies
) {
159 // WebSocketHandshake may access cookies in worker process.
163 WebString
WorkerWebKitPlatformSupportImpl::defaultLocale() {
169 WorkerWebKitPlatformSupportImpl::createLocalStorageNamespace() {
174 void WorkerWebKitPlatformSupportImpl::dispatchStorageEvent(
175 const WebString
& key
, const WebString
& old_value
,
176 const WebString
& new_value
, const WebString
& origin
,
177 const blink::WebURL
& url
, bool is_local_storage
) {
182 WorkerWebKitPlatformSupportImpl::databaseOpenFile(
183 const WebString
& vfs_file_name
, int desired_flags
) {
184 return DatabaseUtil::DatabaseOpenFile(
185 vfs_file_name
, desired_flags
, sync_message_filter_
.get());
188 int WorkerWebKitPlatformSupportImpl::databaseDeleteFile(
189 const WebString
& vfs_file_name
, bool sync_dir
) {
190 return DatabaseUtil::DatabaseDeleteFile(
191 vfs_file_name
, sync_dir
, sync_message_filter_
.get());
194 long WorkerWebKitPlatformSupportImpl::databaseGetFileAttributes(
195 const WebString
& vfs_file_name
) {
196 return DatabaseUtil::DatabaseGetFileAttributes(vfs_file_name
,
197 sync_message_filter_
.get());
200 long long WorkerWebKitPlatformSupportImpl::databaseGetFileSize(
201 const WebString
& vfs_file_name
) {
202 return DatabaseUtil::DatabaseGetFileSize(vfs_file_name
,
203 sync_message_filter_
.get());
206 long long WorkerWebKitPlatformSupportImpl::databaseGetSpaceAvailableForOrigin(
207 const WebString
& origin_identifier
) {
208 return DatabaseUtil::DatabaseGetSpaceAvailable(origin_identifier
,
209 sync_message_filter_
.get());
212 blink::WebIDBFactory
* WorkerWebKitPlatformSupportImpl::idbFactory() {
213 if (!web_idb_factory_
)
214 web_idb_factory_
.reset(new WebIDBFactoryImpl(thread_safe_sender_
.get()));
215 return web_idb_factory_
.get();
218 blink::WebDatabaseObserver
*
219 WorkerWebKitPlatformSupportImpl::databaseObserver() {
220 return web_database_observer_impl_
.get();
223 WebMimeRegistry::SupportsType
224 WorkerWebKitPlatformSupportImpl::supportsMIMEType(
226 return WebMimeRegistry::IsSupported
;
229 WebMimeRegistry::SupportsType
230 WorkerWebKitPlatformSupportImpl::supportsImageMIMEType(
233 return WebMimeRegistry::IsSupported
;
236 WebMimeRegistry::SupportsType
237 WorkerWebKitPlatformSupportImpl::supportsJavaScriptMIMEType(const WebString
&) {
239 return WebMimeRegistry::IsSupported
;
242 WebMimeRegistry::SupportsType
243 WorkerWebKitPlatformSupportImpl::supportsMediaMIMEType(
244 const WebString
&, const WebString
&, const WebString
&) {
246 return WebMimeRegistry::IsSupported
;
249 bool WorkerWebKitPlatformSupportImpl::supportsMediaSourceMIMEType(
250 const blink::WebString
& mimeType
, const blink::WebString
& codecs
) {
255 WebMimeRegistry::SupportsType
256 WorkerWebKitPlatformSupportImpl::supportsNonImageMIMEType(
259 return WebMimeRegistry::IsSupported
;
262 WebString
WorkerWebKitPlatformSupportImpl::mimeTypeForExtension(
263 const WebString
& file_extension
) {
264 std::string mime_type
;
265 thread_safe_sender_
->Send(new MimeRegistryMsg_GetMimeTypeFromExtension(
266 base::FilePath::FromUTF16Unsafe(file_extension
).value(), &mime_type
));
267 return base::ASCIIToUTF16(mime_type
);
270 WebString
WorkerWebKitPlatformSupportImpl::wellKnownMimeTypeForExtension(
271 const WebString
& file_extension
) {
272 std::string mime_type
;
273 net::GetWellKnownMimeTypeFromExtension(
274 base::FilePath::FromUTF16Unsafe(file_extension
).value(), &mime_type
);
275 return base::ASCIIToUTF16(mime_type
);
278 WebString
WorkerWebKitPlatformSupportImpl::mimeTypeFromFile(
279 const WebString
& file_path
) {
280 std::string mime_type
;
281 thread_safe_sender_
->Send(
282 new MimeRegistryMsg_GetMimeTypeFromFile(
283 base::FilePath::FromUTF16Unsafe(file_path
),
285 return base::ASCIIToUTF16(mime_type
);
288 WebBlobRegistry
* WorkerWebKitPlatformSupportImpl::blobRegistry() {
289 return blob_registry_
.get();
292 void WorkerWebKitPlatformSupportImpl::queryStorageUsageAndQuota(
293 const blink::WebURL
& storage_partition
,
294 blink::WebStorageQuotaType type
,
295 blink::WebStorageQuotaCallbacks
* callbacks
) {
296 if (!thread_safe_sender_
.get() || !quota_message_filter_
.get())
298 QuotaDispatcher::ThreadSpecificInstance(
299 thread_safe_sender_
.get(),
300 quota_message_filter_
.get())->QueryStorageUsageAndQuota(
302 static_cast<quota::StorageType
>(type
),
303 QuotaDispatcher::CreateWebStorageQuotaCallbacksWrapper(callbacks
));
306 } // namespace content