Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(675)

Side by Side Diff: content/renderer/pepper/ppb_file_ref_impl.cc

Issue 21966004: Pepper: Move FileRef to the "new" resource proxy. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix bug in GetAbsolutePath (fixes FlashDRM test failure) Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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/renderer/pepper/ppb_file_ref_impl.h"
6
7 #include "base/files/file_util_proxy.h"
8 #include "base/platform_file.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "content/child/fileapi/file_system_dispatcher.h"
12 #include "content/common/view_messages.h"
13 #include "content/renderer/pepper/common.h"
14 #include "content/renderer/pepper/pepper_file_system_host.h"
15 #include "content/renderer/pepper/pepper_helper_impl.h"
16 #include "content/renderer/pepper/plugin_module.h"
17 #include "content/renderer/pepper/renderer_ppapi_host_impl.h"
18 #include "content/renderer/render_thread_impl.h"
19 #include "net/base/escape.h"
20 #include "ppapi/c/pp_errors.h"
21 #include "ppapi/c/ppb_file_io.h"
22 #include "ppapi/host/ppapi_host.h"
23 #include "ppapi/shared_impl/file_type_conversion.h"
24 #include "ppapi/shared_impl/time_conversion.h"
25 #include "ppapi/shared_impl/var.h"
26 #include "ppapi/thunk/enter.h"
27 #include "ppapi/thunk/ppb_file_system_api.h"
28 #include "url/gurl.h"
29 #include "webkit/common/fileapi/directory_entry.h"
30 #include "webkit/common/fileapi/file_system_util.h"
31
32 using ppapi::HostResource;
33 using ppapi::PPB_FileRef_CreateInfo;
34 using ppapi::PPTimeToTime;
35 using ppapi::StringVar;
36 using ppapi::TrackedCallback;
37 using ppapi::thunk::EnterResourceNoLock;
38 using ppapi::thunk::PPB_FileRef_API;
39 using ppapi::thunk::PPB_FileSystem_API;
40
41 namespace content {
42
43 namespace {
44
45 bool IsValidLocalPath(const std::string& path) {
46 // The path must start with '/'
47 if (path.empty() || path[0] != '/')
48 return false;
49
50 // The path must contain valid UTF-8 characters.
51 if (!IsStringUTF8(path))
52 return false;
53
54 #if defined(OS_WIN)
55 base::FilePath::StringType path_win(path.begin(), path.end());
56 base::FilePath file_path(path_win);
57 #else
58 base::FilePath file_path(path);
59 #endif
60 if (file_path.ReferencesParent())
61 return false;
62
63 return true;
64 }
65
66 void TrimTrailingSlash(std::string* path) {
67 // If this path ends with a slash, then normalize it away unless path is the
68 // root path.
69 if (path->size() > 1 && path->at(path->size() - 1) == '/')
70 path->erase(path->size() - 1, 1);
71 }
72
73 std::string GetNameForExternalFilePath(const base::FilePath& in_path) {
74 const base::FilePath::StringType& path = in_path.value();
75 size_t pos = path.rfind(base::FilePath::kSeparators[0]);
76 CHECK(pos != base::FilePath::StringType::npos);
77 #if defined(OS_WIN)
78 return WideToUTF8(path.substr(pos + 1));
79 #elif defined(OS_POSIX)
80 return path.substr(pos + 1);
81 #else
82 #error "Unsupported platform."
83 #endif
84 }
85
86 std::string GetNameForVirtualFilePath(const std::string& path) {
87 if (path.size() == 1 && path[0] == '/')
88 return path;
89
90 // There should always be a leading slash at least!
91 size_t pos = path.rfind('/');
92 CHECK(pos != std::string::npos);
93 return path.substr(pos + 1);
94 }
95
96 void IgnoreCloseCallback(base::PlatformFileError error_code) {
97 }
98
99 void PlatformFileInfoToPPFileInfo(
100 const base::PlatformFileInfo& file_info,
101 PP_FileSystemType file_system_type,
102 PP_FileInfo* info) {
103 DCHECK(info);
104 ::ppapi::PlatformFileInfoToPepperFileInfo(file_info, file_system_type, info);
105 }
106
107 void GetFileInfoCallback(
108 scoped_refptr<base::TaskRunner> task_runner,
109 base::PlatformFile file,
110 linked_ptr<PP_FileInfo> info,
111 scoped_refptr<TrackedCallback> callback,
112 base::PlatformFileError error_code,
113 const base::PlatformFileInfo& file_info) {
114 base::FileUtilProxy::Close(
115 task_runner.get(), file, base::Bind(&IgnoreCloseCallback));
116
117 if (!TrackedCallback::IsPending(callback))
118 return;
119
120 int32_t pp_error = ::ppapi::PlatformFileErrorToPepperError(error_code);
121 if (pp_error != PP_OK) {
122 callback->Run(pp_error);
123 return;
124 }
125
126 PlatformFileInfoToPPFileInfo(
127 file_info, PP_FILESYSTEMTYPE_EXTERNAL, info.get());
128
129 callback->Run(PP_OK);
130 }
131
132 void QueryCallback(scoped_refptr<base::TaskRunner> task_runner,
133 linked_ptr<PP_FileInfo> info,
134 scoped_refptr<TrackedCallback> callback,
135 base::PlatformFileError error_code,
136 base::PassPlatformFile passed_file) {
137 if (!TrackedCallback::IsPending(callback))
138 return;
139
140 int32_t pp_error = ::ppapi::PlatformFileErrorToPepperError(error_code);
141 if (pp_error != PP_OK) {
142 callback->Run(pp_error);
143 return;
144 }
145 base::PlatformFile file = passed_file.ReleaseValue();
146
147 if (!base::FileUtilProxy::GetFileInfoFromPlatformFile(
148 task_runner.get(),
149 file,
150 base::Bind(
151 &GetFileInfoCallback, task_runner, file, info, callback))) {
152 base::FileUtilProxy::Close(
153 task_runner.get(), file, base::Bind(&IgnoreCloseCallback));
154 callback->Run(PP_ERROR_FAILED);
155 }
156 }
157
158 void DidReadMetadata(
159 scoped_refptr< ::ppapi::TrackedCallback> callback,
160 linked_ptr<PP_FileInfo> info,
161 PP_FileSystemType file_system_type,
162 const base::PlatformFileInfo& file_info) {
163 if (!TrackedCallback::IsPending(callback))
164 return;
165
166 PlatformFileInfoToPPFileInfo(file_info, file_system_type, info.get());
167 callback->Run(PP_OK);
168 }
169
170 void DidReadDirectory(
171 scoped_refptr< ::ppapi::TrackedCallback> callback,
172 PPB_FileRef_Impl* dir_ref,
173 linked_ptr<std::vector< ::ppapi::PPB_FileRef_CreateInfo> > dir_files,
174 linked_ptr<std::vector<PP_FileType> > dir_file_types,
175 const std::vector<fileapi::DirectoryEntry>& entries,
176 bool has_more) {
177 if (!TrackedCallback::IsPending(callback))
178 return;
179
180 // The current filesystem backend always returns false.
181 DCHECK(!has_more);
182
183 DCHECK(dir_ref);
184 DCHECK(dir_files.get());
185 DCHECK(dir_file_types.get());
186
187 std::string dir_path = dir_ref->GetCreateInfo().path;
188 if (dir_path.empty() || dir_path[dir_path.size() - 1] != '/')
189 dir_path += '/';
190
191 for (size_t i = 0; i < entries.size(); ++i) {
192 const fileapi::DirectoryEntry& entry = entries[i];
193 scoped_refptr<PPB_FileRef_Impl> file_ref(PPB_FileRef_Impl::CreateInternal(
194 dir_ref->pp_instance(),
195 dir_ref->file_system_resource(),
196 dir_path + fileapi::FilePathToString(base::FilePath(entry.name))));
197 dir_files->push_back(file_ref->GetCreateInfo());
198 dir_file_types->push_back(
199 entry.is_directory ? PP_FILETYPE_DIRECTORY : PP_FILETYPE_REGULAR);
200 // Add a ref count on behalf of the plugin side.
201 file_ref->GetReference();
202 }
203 CHECK_EQ(dir_files->size(), dir_file_types->size());
204
205 callback->Run(PP_OK);
206 }
207
208 void DidFinishFileOperation(
209 scoped_refptr< ::ppapi::TrackedCallback> callback,
210 base::PlatformFileError error_code) {
211 if (callback->completed())
212 return;
213 callback->Run(::ppapi::PlatformFileErrorToPepperError(error_code));
214 }
215
216 } // namespace
217
218 PPB_FileRef_Impl::PPB_FileRef_Impl(const PPB_FileRef_CreateInfo& info,
219 PP_Resource file_system)
220 : PPB_FileRef_Shared(::ppapi::OBJECT_IS_IMPL, info),
221 file_system_(file_system),
222 external_file_system_path_(),
223 routing_id_(MSG_ROUTING_NONE) {
224 if (RenderThreadImpl::current()) { // NULL in tests.
225 routing_id_ = RenderThreadImpl::current()->GenerateRoutingID();
226 ChildThread::current()->AddRoute(routing_id_, this);
227 }
228 }
229
230 PPB_FileRef_Impl::PPB_FileRef_Impl(const PPB_FileRef_CreateInfo& info,
231 const base::FilePath& external_file_path)
232 : PPB_FileRef_Shared(::ppapi::OBJECT_IS_IMPL, info),
233 file_system_(),
234 external_file_system_path_(external_file_path),
235 routing_id_(MSG_ROUTING_NONE) {
236 if (RenderThreadImpl::current()) { // NULL in tests.
237 routing_id_ = RenderThreadImpl::current()->GenerateRoutingID();
238 ChildThread::current()->AddRoute(routing_id_, this);
239 }
240 }
241
242 PPB_FileRef_Impl::~PPB_FileRef_Impl() {
243 if (RenderThreadImpl::current())
244 ChildThread::current()->RemoveRoute(routing_id_);
245 }
246
247 // static
248 PPB_FileRef_Impl* PPB_FileRef_Impl::CreateInternal(PP_Instance instance,
249 PP_Resource pp_file_system,
250 const std::string& path) {
251 PepperFileSystemHost* fs_host = GetFileSystemHostInternal(
252 instance, pp_file_system);
253 if (!fs_host)
254 return 0;
255
256 PP_FileSystemType type = fs_host->GetType();
257 if (type != PP_FILESYSTEMTYPE_LOCALPERSISTENT &&
258 type != PP_FILESYSTEMTYPE_LOCALTEMPORARY &&
259 type != PP_FILESYSTEMTYPE_EXTERNAL &&
260 type != PP_FILESYSTEMTYPE_ISOLATED)
261 return 0;
262
263 PPB_FileRef_CreateInfo info;
264 info.resource = HostResource::MakeInstanceOnly(instance);
265 info.file_system_plugin_resource = pp_file_system;
266 info.file_system_type = type;
267
268 // Validate the path.
269 info.path = path;
270 if (!IsValidLocalPath(info.path))
271 return 0;
272 TrimTrailingSlash(&info.path);
273
274 info.name = GetNameForVirtualFilePath(info.path);
275
276 PPB_FileRef_Impl* file_ref = new PPB_FileRef_Impl(info, pp_file_system);
277
278 RendererPpapiHostImpl* renderer_host =
279 RendererPpapiHostImpl::GetForPPInstance(instance);
280 if (renderer_host && renderer_host->IsRunningInProcess())
281 file_ref->AddFileSystemRefCount();
282 return file_ref;
283 }
284
285 // static
286 PPB_FileRef_Impl* PPB_FileRef_Impl::CreateExternal(
287 PP_Instance instance,
288 const base::FilePath& external_file_path,
289 const std::string& display_name) {
290 PPB_FileRef_CreateInfo info;
291 info.resource = HostResource::MakeInstanceOnly(instance);
292 info.file_system_plugin_resource = 0;
293 info.file_system_type = PP_FILESYSTEMTYPE_EXTERNAL;
294 if (display_name.empty())
295 info.name = GetNameForExternalFilePath(external_file_path);
296 else
297 info.name = display_name;
298
299 return new PPB_FileRef_Impl(info, external_file_path);
300 }
301
302 PP_Resource PPB_FileRef_Impl::GetParent() {
303 if (GetFileSystemType() == PP_FILESYSTEMTYPE_EXTERNAL)
304 return 0;
305
306 const std::string& virtual_path = GetCreateInfo().path;
307
308 // There should always be a leading slash at least!
309 size_t pos = virtual_path.rfind('/');
310 CHECK(pos != std::string::npos);
311
312 // If the path is "/foo", then we want to include the slash.
313 if (pos == 0)
314 pos++;
315 std::string parent_path = virtual_path.substr(0, pos);
316
317 scoped_refptr<PPB_FileRef_Impl> parent_ref(
318 CreateInternal(pp_instance(), file_system_, parent_path));
319 if (!parent_ref.get())
320 return 0;
321 return parent_ref->GetReference();
322 }
323
324 int32_t PPB_FileRef_Impl::MakeDirectory(
325 PP_Bool make_ancestors,
326 scoped_refptr<TrackedCallback> callback) {
327 if (!IsValidNonExternalFileSystem())
328 return PP_ERROR_NOACCESS;
329
330 FileSystemDispatcher* file_system_dispatcher =
331 ChildThread::current()->file_system_dispatcher();
332 file_system_dispatcher->CreateDirectory(
333 GetFileSystemURL(), false /* exclusive */, PP_ToBool(make_ancestors),
334 base::Bind(&DidFinishFileOperation, callback));
335 return PP_OK_COMPLETIONPENDING;
336 }
337
338 int32_t PPB_FileRef_Impl::Touch(PP_Time last_access_time,
339 PP_Time last_modified_time,
340 scoped_refptr<TrackedCallback> callback) {
341 if (!IsValidNonExternalFileSystem())
342 return PP_ERROR_NOACCESS;
343
344 FileSystemDispatcher* file_system_dispatcher =
345 ChildThread::current()->file_system_dispatcher();
346 file_system_dispatcher->TouchFile(
347 GetFileSystemURL(),
348 PPTimeToTime(last_access_time),
349 PPTimeToTime(last_modified_time),
350 base::Bind(&DidFinishFileOperation, callback));
351 return PP_OK_COMPLETIONPENDING;
352 }
353
354 int32_t PPB_FileRef_Impl::Delete(scoped_refptr<TrackedCallback> callback) {
355 if (!IsValidNonExternalFileSystem())
356 return PP_ERROR_NOACCESS;
357
358 FileSystemDispatcher* file_system_dispatcher =
359 ChildThread::current()->file_system_dispatcher();
360 file_system_dispatcher->Remove(
361 GetFileSystemURL(),
362 false /* recursive */,
363 base::Bind(&DidFinishFileOperation, callback));
364 return PP_OK_COMPLETIONPENDING;
365 }
366
367 int32_t PPB_FileRef_Impl::Rename(PP_Resource new_pp_file_ref,
368 scoped_refptr<TrackedCallback> callback) {
369 EnterResourceNoLock<PPB_FileRef_API> enter(new_pp_file_ref, true);
370 if (enter.failed())
371 return PP_ERROR_BADRESOURCE;
372 PPB_FileRef_Impl* new_file_ref =
373 static_cast<PPB_FileRef_Impl*>(enter.object());
374
375 if (!IsValidNonExternalFileSystem() ||
376 file_system_ != new_file_ref->file_system_)
377 return PP_ERROR_NOACCESS;
378
379 // TODO(viettrungluu): Also cancel when the new file ref is destroyed?
380 // http://crbug.com/67624
381 FileSystemDispatcher* file_system_dispatcher =
382 ChildThread::current()->file_system_dispatcher();
383 file_system_dispatcher->Move(
384 GetFileSystemURL(), new_file_ref->GetFileSystemURL(),
385 base::Bind(&DidFinishFileOperation, callback));
386 return PP_OK_COMPLETIONPENDING;
387 }
388
389 PP_Var PPB_FileRef_Impl::GetAbsolutePath() {
390 if (GetFileSystemType() != PP_FILESYSTEMTYPE_EXTERNAL)
391 return GetPath();
392 if (!external_path_var_.get()) {
393 external_path_var_ =
394 new StringVar(external_file_system_path_.AsUTF8Unsafe());
395 }
396 return external_path_var_->GetPPVar();
397 }
398
399 base::FilePath PPB_FileRef_Impl::GetSystemPath() const {
400 if (GetFileSystemType() != PP_FILESYSTEMTYPE_EXTERNAL) {
401 NOTREACHED();
402 return base::FilePath();
403 }
404 return external_file_system_path_;
405 }
406
407 GURL PPB_FileRef_Impl::GetFileSystemURL() const {
408 if (GetFileSystemType() != PP_FILESYSTEMTYPE_LOCALPERSISTENT &&
409 GetFileSystemType() != PP_FILESYSTEMTYPE_LOCALTEMPORARY &&
410 GetFileSystemType() != PP_FILESYSTEMTYPE_EXTERNAL &&
411 GetFileSystemType() != PP_FILESYSTEMTYPE_ISOLATED) {
412 NOTREACHED();
413 return GURL();
414 }
415
416 const std::string& virtual_path = GetCreateInfo().path;
417 CHECK(!virtual_path.empty()); // Should always be at least "/".
418
419 // Since |virtual_path_| starts with a '/', it looks like an absolute path.
420 // We need to trim off the '/' before calling Resolve, as FileSystem URLs
421 // start with a storage type identifier that looks like a path segment.
422
423 PepperFileSystemHost* host = GetFileSystemHost();
424 if (!host)
425 return GURL();
426
427 return GURL(host->GetRootUrl()).Resolve(net::EscapePath(
428 virtual_path.substr(1)));
429 }
430
431 bool PPB_FileRef_Impl::OnMessageReceived(const IPC::Message& msg) {
432 bool handled = true;
433 IPC_BEGIN_MESSAGE_MAP(PPB_FileRef_Impl, msg)
434 IPC_MESSAGE_HANDLER(ViewMsg_AsyncOpenPepperFile_ACK, OnAsyncFileOpened)
435 IPC_MESSAGE_UNHANDLED(handled = false)
436 IPC_END_MESSAGE_MAP()
437 return handled;
438 }
439
440 void PPB_FileRef_Impl::OnAsyncFileOpened(
441 base::PlatformFileError error_code,
442 IPC::PlatformFileForTransit file_for_transit,
443 int message_id) {
444 AsyncOpenFileCallback* callback =
445 pending_async_open_files_.Lookup(message_id);
446 DCHECK(callback);
447 pending_async_open_files_.Remove(message_id);
448
449 base::PlatformFile file =
450 IPC::PlatformFileForTransitToPlatformFile(file_for_transit);
451 callback->Run(error_code, base::PassPlatformFile(&file));
452 // Make sure we won't leak file handle if the requester has died.
453 if (file != base::kInvalidPlatformFileValue) {
454 base::FileUtilProxy::Close(
455 RenderThreadImpl::current()->GetFileThreadMessageLoopProxy().get(),
456 file,
457 base::FileUtilProxy::StatusCallback());
458 }
459 delete callback;
460 }
461
462 bool PPB_FileRef_Impl::IsValidNonExternalFileSystem() const {
463 PepperFileSystemHost* host = GetFileSystemHost();
464 return HasValidFileSystem() && host &&
465 host->GetType() != PP_FILESYSTEMTYPE_EXTERNAL;
466 }
467
468 bool PPB_FileRef_Impl::HasValidFileSystem() const {
469 PepperFileSystemHost* host = GetFileSystemHost();
470 return host && host->IsOpened();
471 }
472
473 int32_t PPB_FileRef_Impl::Query(PP_FileInfo* info,
474 scoped_refptr<TrackedCallback> callback) {
475 NOTREACHED();
476 return PP_ERROR_FAILED;
477 }
478
479 int32_t PPB_FileRef_Impl::QueryInHost(
480 linked_ptr<PP_FileInfo> info,
481 scoped_refptr<TrackedCallback> callback) {
482 if (!file_system_) {
483 // External file system
484 // We have to do something totally different for external file systems.
485
486 // TODO(teravest): Use the SequencedWorkerPool instead.
487 scoped_refptr<base::TaskRunner> task_runner =
488 RenderThreadImpl::current()->GetFileThreadMessageLoopProxy();
489
490 int message_id = pending_async_open_files_.Add(new AsyncOpenFileCallback(
491 base::Bind(&QueryCallback, task_runner, info, callback)));
492 RenderThreadImpl::current()->Send(new ViewHostMsg_AsyncOpenPepperFile(
493 routing_id_,
494 GetSystemPath(),
495 PP_FILEOPENFLAG_READ,
496 message_id));
497 } else {
498 // Non-external file system
499 if (!HasValidFileSystem())
500 return PP_ERROR_NOACCESS;
501
502 PP_FileSystemType file_system_type = GetFileSystemHost()->GetType();
503 FileSystemDispatcher* file_system_dispatcher =
504 ChildThread::current()->file_system_dispatcher();
505 file_system_dispatcher->ReadMetadata(
506 GetFileSystemURL(),
507 base::Bind(&DidReadMetadata, callback, info, file_system_type),
508 base::Bind(&DidFinishFileOperation, callback));
509 }
510 return PP_OK_COMPLETIONPENDING;
511 }
512
513 int32_t PPB_FileRef_Impl::ReadDirectoryEntries(
514 const PP_ArrayOutput& output,
515 scoped_refptr<TrackedCallback> callback) {
516 NOTREACHED();
517 return PP_ERROR_FAILED;
518 }
519
520 int32_t PPB_FileRef_Impl::ReadDirectoryEntriesInHost(
521 linked_ptr<std::vector< ::ppapi::PPB_FileRef_CreateInfo> > files,
522 linked_ptr<std::vector<PP_FileType> > file_types,
523 scoped_refptr<TrackedCallback> callback) {
524 if (!IsValidNonExternalFileSystem())
525 return PP_ERROR_NOACCESS;
526
527 // TODO(yzshen): Passing base::Unretained(this) to the callback could
528 // be dangerous.
529 FileSystemDispatcher* file_system_dispatcher =
530 ChildThread::current()->file_system_dispatcher();
531 file_system_dispatcher->ReadDirectory(
532 GetFileSystemURL(),
533 base::Bind(&DidReadDirectory,
534 callback, base::Unretained(this), files, file_types),
535 base::Bind(&DidFinishFileOperation, callback));
536 return PP_OK_COMPLETIONPENDING;
537 }
538
539 PepperFileSystemHost* PPB_FileRef_Impl::GetFileSystemHost() const {
540 return GetFileSystemHostInternal(pp_instance(), file_system_);
541 }
542
543 PepperFileSystemHost* PPB_FileRef_Impl::GetFileSystemHostInternal(
544 PP_Instance instance, PP_Resource resource) {
545 const ppapi::host::PpapiHost* ppapi_host =
546 RendererPpapiHost::GetForPPInstance(instance)->GetPpapiHost();
547 if (!resource || !ppapi_host)
548 return NULL;
549 ppapi::host::ResourceHost* resource_host =
550 ppapi_host->GetResourceHost(resource);
551 if (resource_host && resource_host->IsFileSystemHost())
552 return static_cast<PepperFileSystemHost*>(resource_host);
553 return NULL;
554 }
555
556 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698