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/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"
32
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;
44 using blink::WebURL;
45
46 namespace content {
47
48 // TODO(kinuko): Probably this could be consolidated into
49 // RendererWebKitPlatformSupportImpl::FileUtilities.
50 class WorkerWebKitPlatformSupportImpl::FileUtilities
51 : public WebFileUtilitiesImpl {
52 public:
FileUtilities(ThreadSafeSender * sender)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
getFileInfo(const WebString & path,WebFileInfo & web_file_info)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) {
69 return false;
70 }
71 FileInfoToWebFileInfo(file_info, &web_file_info);
72 web_file_info.platformPath = path;
73 return true;
74 }
75
76 //------------------------------------------------------------------------------
77
WorkerWebKitPlatformSupportImpl(ThreadSafeSender * sender,IPC::SyncMessageFilter * sync_message_filter,QuotaMessageFilter * quota_message_filter)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) {
86 if (sender) {
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));
91 }
92 }
93
~WorkerWebKitPlatformSupportImpl()94 WorkerWebKitPlatformSupportImpl::~WorkerWebKitPlatformSupportImpl() {
95 WebFileSystemImpl::DeleteThreadSpecificInstance();
96 }
97
clipboard()98 WebClipboard* WorkerWebKitPlatformSupportImpl::clipboard() {
99 NOTREACHED();
100 return NULL;
101 }
102
mimeRegistry()103 WebMimeRegistry* WorkerWebKitPlatformSupportImpl::mimeRegistry() {
104 return this;
105 }
106
fileSystem()107 WebFileSystem* WorkerWebKitPlatformSupportImpl::fileSystem() {
108 return WebFileSystemImpl::ThreadSpecificInstance(child_thread_loop_.get());
109 }
110
fileUtilities()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());
115 }
116 return file_utilities_.get();
117 }
118
sandboxSupport()119 WebSandboxSupport* WorkerWebKitPlatformSupportImpl::sandboxSupport() {
120 NOTREACHED();
121 return NULL;
122 }
123
sandboxEnabled()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
127 // more info.
128 return true;
129 }
130
visitedLinkHash(const char * canonical_url,size_t length)131 unsigned long long WorkerWebKitPlatformSupportImpl::visitedLinkHash(
132 const char* canonical_url,
133 size_t length) {
134 NOTREACHED();
135 return 0;
136 }
137
isLinkVisited(unsigned long long link_hash)138 bool WorkerWebKitPlatformSupportImpl::isLinkVisited(
139 unsigned long long link_hash) {
140 NOTREACHED();
141 return false;
142 }
143
createMessageChannel(blink::WebMessagePortChannel ** channel1,blink::WebMessagePortChannel ** channel2)144 void WorkerWebKitPlatformSupportImpl::createMessageChannel(
145 blink::WebMessagePortChannel** channel1,
146 blink::WebMessagePortChannel** channel2) {
147 WebMessagePortChannelImpl::CreatePair(
148 child_thread_loop_.get(), channel1, channel2);
149 }
150
setCookies(const WebURL & url,const WebURL & first_party_for_cookies,const WebString & value)151 void WorkerWebKitPlatformSupportImpl::setCookies(
152 const WebURL& url,
153 const WebURL& first_party_for_cookies,
154 const WebString& value) {
155 NOTREACHED();
156 }
157
cookies(const WebURL & url,const WebURL & first_party_for_cookies)158 WebString WorkerWebKitPlatformSupportImpl::cookies(
159 const WebURL& url, const WebURL& first_party_for_cookies) {
160 // WebSocketHandshake may access cookies in worker process.
161 return WebString();
162 }
163
defaultLocale()164 WebString WorkerWebKitPlatformSupportImpl::defaultLocale() {
165 NOTREACHED();
166 return WebString();
167 }
168
169 WebStorageNamespace*
createLocalStorageNamespace()170 WorkerWebKitPlatformSupportImpl::createLocalStorageNamespace() {
171 NOTREACHED();
172 return 0;
173 }
174
dispatchStorageEvent(const WebString & key,const WebString & old_value,const WebString & new_value,const WebString & origin,const blink::WebURL & url,bool is_local_storage)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) {
179 NOTREACHED();
180 }
181
182 Platform::FileHandle
databaseOpenFile(const WebString & vfs_file_name,int desired_flags)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());
187 }
188
databaseDeleteFile(const WebString & vfs_file_name,bool sync_dir)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());
193 }
194
databaseGetFileAttributes(const WebString & vfs_file_name)195 long WorkerWebKitPlatformSupportImpl::databaseGetFileAttributes(
196 const WebString& vfs_file_name) {
197 return DatabaseUtil::DatabaseGetFileAttributes(vfs_file_name,
198 sync_message_filter_.get());
199 }
200
databaseGetFileSize(const WebString & vfs_file_name)201 long long WorkerWebKitPlatformSupportImpl::databaseGetFileSize(
202 const WebString& vfs_file_name) {
203 return DatabaseUtil::DatabaseGetFileSize(vfs_file_name,
204 sync_message_filter_.get());
205 }
206
databaseGetSpaceAvailableForOrigin(const WebString & origin_identifier)207 long long WorkerWebKitPlatformSupportImpl::databaseGetSpaceAvailableForOrigin(
208 const WebString& origin_identifier) {
209 return DatabaseUtil::DatabaseGetSpaceAvailable(origin_identifier,
210 sync_message_filter_.get());
211 }
212
idbFactory()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();
217 }
218
219 blink::WebDatabaseObserver*
databaseObserver()220 WorkerWebKitPlatformSupportImpl::databaseObserver() {
221 return web_database_observer_impl_.get();
222 }
223
224 WebMimeRegistry::SupportsType
supportsMIMEType(const WebString &)225 WorkerWebKitPlatformSupportImpl::supportsMIMEType(
226 const WebString&) {
227 return WebMimeRegistry::IsSupported;
228 }
229
230 WebMimeRegistry::SupportsType
supportsImageMIMEType(const WebString &)231 WorkerWebKitPlatformSupportImpl::supportsImageMIMEType(
232 const WebString&) {
233 NOTREACHED();
234 return WebMimeRegistry::IsSupported;
235 }
236
237 WebMimeRegistry::SupportsType
supportsJavaScriptMIMEType(const WebString &)238 WorkerWebKitPlatformSupportImpl::supportsJavaScriptMIMEType(const WebString&) {
239 NOTREACHED();
240 return WebMimeRegistry::IsSupported;
241 }
242
243 WebMimeRegistry::SupportsType
supportsMediaMIMEType(const WebString &,const WebString &,const WebString &)244 WorkerWebKitPlatformSupportImpl::supportsMediaMIMEType(
245 const WebString&, const WebString&, const WebString&) {
246 NOTREACHED();
247 return WebMimeRegistry::IsSupported;
248 }
249
supportsMediaSourceMIMEType(const blink::WebString & mimeType,const blink::WebString & codecs)250 bool WorkerWebKitPlatformSupportImpl::supportsMediaSourceMIMEType(
251 const blink::WebString& mimeType, const blink::WebString& codecs) {
252 NOTREACHED();
253 return false;
254 }
255
supportsEncryptedMediaMIMEType(const blink::WebString & key_system,const blink::WebString & mime_type,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) {
260 NOTREACHED();
261 return false;
262 }
263
264 WebMimeRegistry::SupportsType
supportsNonImageMIMEType(const WebString &)265 WorkerWebKitPlatformSupportImpl::supportsNonImageMIMEType(
266 const WebString&) {
267 NOTREACHED();
268 return WebMimeRegistry::IsSupported;
269 }
270
mimeTypeForExtension(const WebString & file_extension)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);
277 }
278
wellKnownMimeTypeForExtension(const WebString & file_extension)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);
285 }
286
mimeTypeFromFile(const WebString & file_path)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),
293 &mime_type));
294 return base::ASCIIToUTF16(mime_type);
295 }
296
blobRegistry()297 WebBlobRegistry* WorkerWebKitPlatformSupportImpl::blobRegistry() {
298 return blob_registry_.get();
299 }
300
queryStorageUsageAndQuota(const blink::WebURL & storage_partition,blink::WebStorageQuotaType type,blink::WebStorageQuotaCallbacks callbacks)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())
306 return;
307 QuotaDispatcher::ThreadSpecificInstance(
308 thread_safe_sender_.get(),
309 quota_message_filter_.get())->QueryStorageUsageAndQuota(
310 storage_partition,
311 static_cast<quota::StorageType>(type),
312 QuotaDispatcher::CreateWebStorageQuotaCallbacksWrapper(callbacks));
313 }
314
315 } // namespace content
316