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

Side by Side Diff: webkit/plugins/ppapi/ppb_file_ref_impl.cc

Issue 19744007: Create a public API around webkit::ppapi::PluginInstance and use it in chrome. After this, webkit/p… (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 7 years, 5 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "webkit/plugins/ppapi/ppb_file_ref_impl.h" 5 #include "webkit/plugins/ppapi/ppb_file_ref_impl.h"
6 6
7 #include "base/files/file_util_proxy.h" 7 #include "base/files/file_util_proxy.h"
8 #include "base/platform_file.h" 8 #include "base/platform_file.h"
9 #include "base/strings/string_util.h" 9 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h" 10 #include "base/strings/utf_string_conversions.h"
11 #include "net/base/escape.h" 11 #include "net/base/escape.h"
12 #include "ppapi/c/pp_errors.h" 12 #include "ppapi/c/pp_errors.h"
13 #include "ppapi/shared_impl/file_type_conversion.h" 13 #include "ppapi/shared_impl/file_type_conversion.h"
14 #include "ppapi/shared_impl/time_conversion.h" 14 #include "ppapi/shared_impl/time_conversion.h"
15 #include "ppapi/shared_impl/var.h" 15 #include "ppapi/shared_impl/var.h"
16 #include "ppapi/thunk/enter.h" 16 #include "ppapi/thunk/enter.h"
17 #include "ppapi/thunk/ppb_file_system_api.h" 17 #include "ppapi/thunk/ppb_file_system_api.h"
18 #include "url/gurl.h" 18 #include "url/gurl.h"
19 #include "webkit/common/fileapi/directory_entry.h" 19 #include "webkit/common/fileapi/directory_entry.h"
20 #include "webkit/common/fileapi/file_system_util.h" 20 #include "webkit/common/fileapi/file_system_util.h"
21 #include "webkit/plugins/ppapi/common.h" 21 #include "webkit/plugins/ppapi/common.h"
22 #include "webkit/plugins/ppapi/plugin_delegate.h" 22 #include "webkit/plugins/ppapi/plugin_delegate.h"
23 #include "webkit/plugins/ppapi/plugin_module.h" 23 #include "webkit/plugins/ppapi/plugin_module.h"
24 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" 24 #include "webkit/plugins/ppapi/ppapi_plugin_instance_impl.h"
25 #include "webkit/plugins/ppapi/resource_helper.h" 25 #include "webkit/plugins/ppapi/resource_helper.h"
26 26
27 using ppapi::HostResource; 27 using ppapi::HostResource;
28 using ppapi::PPB_FileRef_CreateInfo; 28 using ppapi::PPB_FileRef_CreateInfo;
29 using ppapi::PPTimeToTime; 29 using ppapi::PPTimeToTime;
30 using ppapi::StringVar; 30 using ppapi::StringVar;
31 using ppapi::TrackedCallback; 31 using ppapi::TrackedCallback;
32 using ppapi::thunk::EnterResourceNoLock; 32 using ppapi::thunk::EnterResourceNoLock;
33 using ppapi::thunk::PPB_FileRef_API; 33 using ppapi::thunk::PPB_FileRef_API;
34 using ppapi::thunk::PPB_FileSystem_API; 34 using ppapi::thunk::PPB_FileSystem_API;
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 external_file_system_path_(external_file_path) { 225 external_file_system_path_(external_file_path) {
226 } 226 }
227 227
228 PPB_FileRef_Impl::~PPB_FileRef_Impl() { 228 PPB_FileRef_Impl::~PPB_FileRef_Impl() {
229 } 229 }
230 230
231 // static 231 // static
232 PPB_FileRef_Impl* PPB_FileRef_Impl::CreateInternal(PP_Instance instance, 232 PPB_FileRef_Impl* PPB_FileRef_Impl::CreateInternal(PP_Instance instance,
233 PP_Resource pp_file_system, 233 PP_Resource pp_file_system,
234 const std::string& path) { 234 const std::string& path) {
235 PluginInstance* plugin_instance = 235 PluginInstanceImpl* plugin_instance =
236 ResourceHelper::PPInstanceToPluginInstance(instance); 236 ResourceHelper::PPInstanceToPluginInstance(instance);
237 if (!plugin_instance || !plugin_instance->delegate()) 237 if (!plugin_instance || !plugin_instance->delegate())
238 return 0; 238 return 0;
239 239
240 PP_FileSystemType type = 240 PP_FileSystemType type =
241 plugin_instance->delegate()->GetFileSystemType(instance, pp_file_system); 241 plugin_instance->delegate()->GetFileSystemType(instance, pp_file_system);
242 if (type != PP_FILESYSTEMTYPE_LOCALPERSISTENT && 242 if (type != PP_FILESYSTEMTYPE_LOCALPERSISTENT &&
243 type != PP_FILESYSTEMTYPE_LOCALTEMPORARY && 243 type != PP_FILESYSTEMTYPE_LOCALTEMPORARY &&
244 type != PP_FILESYSTEMTYPE_EXTERNAL && 244 type != PP_FILESYSTEMTYPE_EXTERNAL &&
245 type != PP_FILESYSTEMTYPE_ISOLATED) 245 type != PP_FILESYSTEMTYPE_ISOLATED)
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
302 return 0; 302 return 0;
303 return parent_ref->GetReference(); 303 return parent_ref->GetReference();
304 } 304 }
305 305
306 int32_t PPB_FileRef_Impl::MakeDirectory( 306 int32_t PPB_FileRef_Impl::MakeDirectory(
307 PP_Bool make_ancestors, 307 PP_Bool make_ancestors,
308 scoped_refptr<TrackedCallback> callback) { 308 scoped_refptr<TrackedCallback> callback) {
309 if (!IsValidNonExternalFileSystem()) 309 if (!IsValidNonExternalFileSystem())
310 return PP_ERROR_NOACCESS; 310 return PP_ERROR_NOACCESS;
311 311
312 PluginInstance* plugin_instance = ResourceHelper::GetPluginInstance(this); 312 PluginInstanceImpl* plugin_instance = ResourceHelper::GetPluginInstance(this);
313 if (!plugin_instance) 313 if (!plugin_instance)
314 return PP_ERROR_FAILED; 314 return PP_ERROR_FAILED;
315 plugin_instance->delegate()->MakeDirectory( 315 plugin_instance->delegate()->MakeDirectory(
316 GetFileSystemURL(), PP_ToBool(make_ancestors), 316 GetFileSystemURL(), PP_ToBool(make_ancestors),
317 base::Bind(&DidFinishFileOperation, callback)); 317 base::Bind(&DidFinishFileOperation, callback));
318 return PP_OK_COMPLETIONPENDING; 318 return PP_OK_COMPLETIONPENDING;
319 } 319 }
320 320
321 int32_t PPB_FileRef_Impl::Touch(PP_Time last_access_time, 321 int32_t PPB_FileRef_Impl::Touch(PP_Time last_access_time,
322 PP_Time last_modified_time, 322 PP_Time last_modified_time,
323 scoped_refptr<TrackedCallback> callback) { 323 scoped_refptr<TrackedCallback> callback) {
324 if (!IsValidNonExternalFileSystem()) 324 if (!IsValidNonExternalFileSystem())
325 return PP_ERROR_NOACCESS; 325 return PP_ERROR_NOACCESS;
326 326
327 PluginInstance* plugin_instance = ResourceHelper::GetPluginInstance(this); 327 PluginInstanceImpl* plugin_instance = ResourceHelper::GetPluginInstance(this);
328 if (!plugin_instance) 328 if (!plugin_instance)
329 return PP_ERROR_FAILED; 329 return PP_ERROR_FAILED;
330 plugin_instance->delegate()->Touch( 330 plugin_instance->delegate()->Touch(
331 GetFileSystemURL(), 331 GetFileSystemURL(),
332 PPTimeToTime(last_access_time), 332 PPTimeToTime(last_access_time),
333 PPTimeToTime(last_modified_time), 333 PPTimeToTime(last_modified_time),
334 base::Bind(&DidFinishFileOperation, callback)); 334 base::Bind(&DidFinishFileOperation, callback));
335 return PP_OK_COMPLETIONPENDING; 335 return PP_OK_COMPLETIONPENDING;
336 } 336 }
337 337
338 int32_t PPB_FileRef_Impl::Delete(scoped_refptr<TrackedCallback> callback) { 338 int32_t PPB_FileRef_Impl::Delete(scoped_refptr<TrackedCallback> callback) {
339 if (!IsValidNonExternalFileSystem()) 339 if (!IsValidNonExternalFileSystem())
340 return PP_ERROR_NOACCESS; 340 return PP_ERROR_NOACCESS;
341 341
342 PluginInstance* plugin_instance = ResourceHelper::GetPluginInstance(this); 342 PluginInstanceImpl* plugin_instance = ResourceHelper::GetPluginInstance(this);
343 if (!plugin_instance) 343 if (!plugin_instance)
344 return PP_ERROR_FAILED; 344 return PP_ERROR_FAILED;
345 plugin_instance->delegate()->Delete( 345 plugin_instance->delegate()->Delete(
346 GetFileSystemURL(), 346 GetFileSystemURL(),
347 base::Bind(&DidFinishFileOperation, callback)); 347 base::Bind(&DidFinishFileOperation, callback));
348 return PP_OK_COMPLETIONPENDING; 348 return PP_OK_COMPLETIONPENDING;
349 } 349 }
350 350
351 int32_t PPB_FileRef_Impl::Rename(PP_Resource new_pp_file_ref, 351 int32_t PPB_FileRef_Impl::Rename(PP_Resource new_pp_file_ref,
352 scoped_refptr<TrackedCallback> callback) { 352 scoped_refptr<TrackedCallback> callback) {
353 EnterResourceNoLock<PPB_FileRef_API> enter(new_pp_file_ref, true); 353 EnterResourceNoLock<PPB_FileRef_API> enter(new_pp_file_ref, true);
354 if (enter.failed()) 354 if (enter.failed())
355 return PP_ERROR_BADRESOURCE; 355 return PP_ERROR_BADRESOURCE;
356 PPB_FileRef_Impl* new_file_ref = 356 PPB_FileRef_Impl* new_file_ref =
357 static_cast<PPB_FileRef_Impl*>(enter.object()); 357 static_cast<PPB_FileRef_Impl*>(enter.object());
358 358
359 if (!IsValidNonExternalFileSystem() || 359 if (!IsValidNonExternalFileSystem() ||
360 file_system_ != new_file_ref->file_system_) 360 file_system_ != new_file_ref->file_system_)
361 return PP_ERROR_NOACCESS; 361 return PP_ERROR_NOACCESS;
362 362
363 // TODO(viettrungluu): Also cancel when the new file ref is destroyed? 363 // TODO(viettrungluu): Also cancel when the new file ref is destroyed?
364 // http://crbug.com/67624 364 // http://crbug.com/67624
365 PluginInstance* plugin_instance = ResourceHelper::GetPluginInstance(this); 365 PluginInstanceImpl* plugin_instance = ResourceHelper::GetPluginInstance(this);
366 if (!plugin_instance) 366 if (!plugin_instance)
367 return PP_ERROR_FAILED; 367 return PP_ERROR_FAILED;
368 plugin_instance->delegate()->Rename( 368 plugin_instance->delegate()->Rename(
369 GetFileSystemURL(), new_file_ref->GetFileSystemURL(), 369 GetFileSystemURL(), new_file_ref->GetFileSystemURL(),
370 base::Bind(&DidFinishFileOperation, callback)); 370 base::Bind(&DidFinishFileOperation, callback));
371 return PP_OK_COMPLETIONPENDING; 371 return PP_OK_COMPLETIONPENDING;
372 } 372 }
373 373
374 PP_Var PPB_FileRef_Impl::GetAbsolutePath() { 374 PP_Var PPB_FileRef_Impl::GetAbsolutePath() {
375 if (GetFileSystemType() != PP_FILESYSTEMTYPE_EXTERNAL) 375 if (GetFileSystemType() != PP_FILESYSTEMTYPE_EXTERNAL)
(...skipping 22 matching lines...) Expand all
398 return GURL(); 398 return GURL();
399 } 399 }
400 400
401 const std::string& virtual_path = GetCreateInfo().path; 401 const std::string& virtual_path = GetCreateInfo().path;
402 CHECK(!virtual_path.empty()); // Should always be at least "/". 402 CHECK(!virtual_path.empty()); // Should always be at least "/".
403 403
404 // Since |virtual_path_| starts with a '/', it looks like an absolute path. 404 // Since |virtual_path_| starts with a '/', it looks like an absolute path.
405 // We need to trim off the '/' before calling Resolve, as FileSystem URLs 405 // We need to trim off the '/' before calling Resolve, as FileSystem URLs
406 // start with a storage type identifier that looks like a path segment. 406 // start with a storage type identifier that looks like a path segment.
407 407
408 PluginInstance* plugin_instance = ResourceHelper::GetPluginInstance(this); 408 PluginInstanceImpl* plugin_instance = ResourceHelper::GetPluginInstance(this);
409 PluginDelegate* delegate = 409 PluginDelegate* delegate =
410 plugin_instance ? plugin_instance->delegate() : NULL; 410 plugin_instance ? plugin_instance->delegate() : NULL;
411 if (!delegate) 411 if (!delegate)
412 return GURL(); 412 return GURL();
413 return GURL(delegate->GetFileSystemRootUrl(pp_instance(), file_system_)) 413 return GURL(delegate->GetFileSystemRootUrl(pp_instance(), file_system_))
414 .Resolve(net::EscapePath(virtual_path.substr(1))); 414 .Resolve(net::EscapePath(virtual_path.substr(1)));
415 } 415 }
416 416
417 bool PPB_FileRef_Impl::IsValidNonExternalFileSystem() const { 417 bool PPB_FileRef_Impl::IsValidNonExternalFileSystem() const {
418 PluginInstance* plugin_instance = ResourceHelper::GetPluginInstance(this); 418 PluginInstanceImpl* plugin_instance = ResourceHelper::GetPluginInstance(this);
419 PluginDelegate* delegate = 419 PluginDelegate* delegate =
420 plugin_instance ? plugin_instance->delegate() : NULL; 420 plugin_instance ? plugin_instance->delegate() : NULL;
421 return delegate && 421 return delegate &&
422 delegate->IsFileSystemOpened(pp_instance(), file_system_) && 422 delegate->IsFileSystemOpened(pp_instance(), file_system_) &&
423 delegate->GetFileSystemType(pp_instance(), file_system_) != 423 delegate->GetFileSystemType(pp_instance(), file_system_) !=
424 PP_FILESYSTEMTYPE_EXTERNAL; 424 PP_FILESYSTEMTYPE_EXTERNAL;
425 } 425 }
426 426
427 bool PPB_FileRef_Impl::HasValidFileSystem() const { 427 bool PPB_FileRef_Impl::HasValidFileSystem() const {
428 PluginInstance* plugin_instance = ResourceHelper::GetPluginInstance(this); 428 PluginInstanceImpl* plugin_instance = ResourceHelper::GetPluginInstance(this);
429 PluginDelegate* delegate = 429 PluginDelegate* delegate =
430 plugin_instance ? plugin_instance->delegate() : NULL; 430 plugin_instance ? plugin_instance->delegate() : NULL;
431 return delegate && delegate->IsFileSystemOpened(pp_instance(), file_system_); 431 return delegate && delegate->IsFileSystemOpened(pp_instance(), file_system_);
432 } 432 }
433 433
434 int32_t PPB_FileRef_Impl::Query(PP_FileInfo* info, 434 int32_t PPB_FileRef_Impl::Query(PP_FileInfo* info,
435 scoped_refptr<TrackedCallback> callback) { 435 scoped_refptr<TrackedCallback> callback) {
436 NOTREACHED(); 436 NOTREACHED();
437 return PP_ERROR_FAILED; 437 return PP_ERROR_FAILED;
438 } 438 }
439 439
440 int32_t PPB_FileRef_Impl::QueryInHost( 440 int32_t PPB_FileRef_Impl::QueryInHost(
441 linked_ptr<PP_FileInfo> info, 441 linked_ptr<PP_FileInfo> info,
442 scoped_refptr<TrackedCallback> callback) { 442 scoped_refptr<TrackedCallback> callback) {
443 scoped_refptr<PluginInstance> plugin_instance = 443 scoped_refptr<PluginInstanceImpl> plugin_instance =
444 ResourceHelper::GetPluginInstance(this); 444 ResourceHelper::GetPluginInstance(this);
445 if (!plugin_instance.get()) 445 if (!plugin_instance.get())
446 return PP_ERROR_FAILED; 446 return PP_ERROR_FAILED;
447 447
448 if (!file_system_) { 448 if (!file_system_) {
449 // External file system 449 // External file system
450 // We have to do something totally different for external file systems. 450 // We have to do something totally different for external file systems.
451 451
452 // TODO(teravest): Use the SequencedWorkerPool instead. 452 // TODO(teravest): Use the SequencedWorkerPool instead.
453 scoped_refptr<base::TaskRunner> task_runner = 453 scoped_refptr<base::TaskRunner> task_runner =
454 plugin_instance->delegate()->GetFileThreadMessageLoopProxy(); 454 plugin_instance->delegate()->GetFileThreadMessageLoopProxy();
455 if (!plugin_instance->delegate()->AsyncOpenFile( 455 if (!plugin_instance->delegate()->AsyncOpenFile(
456 GetSystemPath(), 456 GetSystemPath(),
457 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, 457 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ,
458 base::Bind(&QueryCallback, task_runner, info, callback))) 458 base::Bind(&QueryCallback, task_runner, info, callback)))
459 return PP_ERROR_FAILED; 459 return PP_ERROR_FAILED;
460 } else { 460 } else {
461 // Non-external file system 461 // Non-external file system
462 if (!HasValidFileSystem()) 462 if (!HasValidFileSystem())
463 return PP_ERROR_NOACCESS; 463 return PP_ERROR_NOACCESS;
464 464
465 PluginInstance* plugin_instance = ResourceHelper::GetPluginInstance(this); 465 PluginInstanceImpl* plugin_instance =
466 ResourceHelper::GetPluginInstance(this);
466 PluginDelegate* delegate = 467 PluginDelegate* delegate =
467 plugin_instance ? plugin_instance->delegate() : NULL; 468 plugin_instance ? plugin_instance->delegate() : NULL;
468 if (!delegate) 469 if (!delegate)
469 return PP_ERROR_FAILED; 470 return PP_ERROR_FAILED;
470 471
471 PP_FileSystemType file_system_type = 472 PP_FileSystemType file_system_type =
472 delegate->GetFileSystemType(pp_instance(), file_system_); 473 delegate->GetFileSystemType(pp_instance(), file_system_);
473 plugin_instance->delegate()->Query( 474 plugin_instance->delegate()->Query(
474 GetFileSystemURL(), 475 GetFileSystemURL(),
475 base::Bind(&DidReadMetadata, callback, info, file_system_type), 476 base::Bind(&DidReadMetadata, callback, info, file_system_type),
476 base::Bind(&DidFinishFileOperation, callback)); 477 base::Bind(&DidFinishFileOperation, callback));
477 } 478 }
478 return PP_OK_COMPLETIONPENDING; 479 return PP_OK_COMPLETIONPENDING;
479 } 480 }
480 481
481 int32_t PPB_FileRef_Impl::ReadDirectoryEntries( 482 int32_t PPB_FileRef_Impl::ReadDirectoryEntries(
482 const PP_ArrayOutput& output, 483 const PP_ArrayOutput& output,
483 scoped_refptr<TrackedCallback> callback) { 484 scoped_refptr<TrackedCallback> callback) {
484 NOTREACHED(); 485 NOTREACHED();
485 return PP_ERROR_FAILED; 486 return PP_ERROR_FAILED;
486 } 487 }
487 488
488 int32_t PPB_FileRef_Impl::ReadDirectoryEntriesInHost( 489 int32_t PPB_FileRef_Impl::ReadDirectoryEntriesInHost(
489 linked_ptr<std::vector< ::ppapi::PPB_FileRef_CreateInfo> > files, 490 linked_ptr<std::vector< ::ppapi::PPB_FileRef_CreateInfo> > files,
490 linked_ptr<std::vector<PP_FileType> > file_types, 491 linked_ptr<std::vector<PP_FileType> > file_types,
491 scoped_refptr<TrackedCallback> callback) { 492 scoped_refptr<TrackedCallback> callback) {
492 if (!IsValidNonExternalFileSystem()) 493 if (!IsValidNonExternalFileSystem())
493 return PP_ERROR_NOACCESS; 494 return PP_ERROR_NOACCESS;
494 495
495 PluginInstance* plugin_instance = ResourceHelper::GetPluginInstance(this); 496 PluginInstanceImpl* plugin_instance = ResourceHelper::GetPluginInstance(this);
496 if (!plugin_instance) 497 if (!plugin_instance)
497 return PP_ERROR_FAILED; 498 return PP_ERROR_FAILED;
498 499
499 // TODO(yzshen): Passing base::Unretained(this) to the callback could 500 // TODO(yzshen): Passing base::Unretained(this) to the callback could
500 // be dangerous. 501 // be dangerous.
501 plugin_instance->delegate()->ReadDirectoryEntries( 502 plugin_instance->delegate()->ReadDirectoryEntries(
502 GetFileSystemURL(), 503 GetFileSystemURL(),
503 base::Bind(&DidReadDirectory, 504 base::Bind(&DidReadDirectory,
504 callback, base::Unretained(this), files, file_types), 505 callback, base::Unretained(this), files, file_types),
505 base::Bind(&DidFinishFileOperation, callback)); 506 base::Bind(&DidFinishFileOperation, callback));
506 return PP_OK_COMPLETIONPENDING; 507 return PP_OK_COMPLETIONPENDING;
507 } 508 }
508 509
509 } // namespace ppapi 510 } // namespace ppapi
510 } // namespace webkit 511 } // namespace webkit
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698