Home | History | Annotate | Download | only in pepper
      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 #ifndef CONTENT_RENDERER_PEPPER_PPB_FILE_REF_IMPL_H_
      6 #define CONTENT_RENDERER_PEPPER_PPB_FILE_REF_IMPL_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/callback_forward.h"
     12 #include "base/files/file_path.h"
     13 #include "base/id_map.h"
     14 #include "base/memory/linked_ptr.h"
     15 #include "base/platform_file.h"
     16 #include "ipc/ipc_listener.h"
     17 #include "ipc/ipc_platform_file.h"
     18 #include "ppapi/c/pp_file_info.h"
     19 #include "ppapi/c/ppb_file_ref.h"
     20 #include "ppapi/shared_impl/ppb_file_ref_shared.h"
     21 #include "ppapi/shared_impl/scoped_pp_resource.h"
     22 #include "ppapi/shared_impl/var.h"
     23 #include "url/gurl.h"
     24 
     25 namespace ppapi {
     26 namespace host {
     27 class ResourceHost;
     28 }
     29 }
     30 
     31 namespace content {
     32 
     33 using ppapi::StringVar;
     34 
     35 class PepperFileSystemHost;
     36 
     37 class PPB_FileRef_Impl : public ppapi::PPB_FileRef_Shared,
     38                          public IPC::Listener {
     39  public:
     40   PPB_FileRef_Impl(const ppapi::PPB_FileRef_CreateInfo& info,
     41                    PP_Resource file_system);
     42   PPB_FileRef_Impl(const ppapi::PPB_FileRef_CreateInfo& info,
     43                    const base::FilePath& external_file_path);
     44 
     45   // The returned object will have a refcount of 0 (just like "new").
     46   static PPB_FileRef_Impl* CreateInternal(PP_Instance instance,
     47                                           PP_Resource pp_file_system,
     48                                           const std::string& path);
     49 
     50   // The returned object will have a refcount of 0 (just like "new").
     51   static PPB_FileRef_Impl* CreateExternal(
     52       PP_Instance instance,
     53       const base::FilePath& external_file_path,
     54       const std::string& display_name);
     55 
     56   // PPB_FileRef_API implementation (not provided by PPB_FileRef_Shared).
     57   virtual PP_Resource GetParent() OVERRIDE;
     58   virtual int32_t MakeDirectory(
     59       PP_Bool make_ancestors,
     60       scoped_refptr<ppapi::TrackedCallback> callback) OVERRIDE;
     61   virtual int32_t Touch(
     62       PP_Time last_access_time,
     63       PP_Time last_modified_time,
     64       scoped_refptr<ppapi::TrackedCallback> callback) OVERRIDE;
     65   virtual int32_t Delete(
     66       scoped_refptr<ppapi::TrackedCallback> callback) OVERRIDE;
     67   virtual int32_t Rename(
     68       PP_Resource new_file_ref,
     69       scoped_refptr<ppapi::TrackedCallback> callback) OVERRIDE;
     70   virtual int32_t Query(
     71       PP_FileInfo* info,
     72       scoped_refptr<ppapi::TrackedCallback> callback) OVERRIDE;
     73   virtual int32_t ReadDirectoryEntries(
     74       const PP_ArrayOutput& output,
     75       scoped_refptr<ppapi::TrackedCallback> callback) OVERRIDE;
     76   virtual int32_t QueryInHost(
     77       linked_ptr<PP_FileInfo> info,
     78       scoped_refptr<ppapi::TrackedCallback> callback) OVERRIDE;
     79   virtual int32_t ReadDirectoryEntriesInHost(
     80       linked_ptr<std::vector<ppapi::PPB_FileRef_CreateInfo> > files,
     81       linked_ptr<std::vector<PP_FileType> > file_types,
     82       scoped_refptr<ppapi::TrackedCallback> callback) OVERRIDE;
     83   virtual PP_Var GetAbsolutePath() OVERRIDE;
     84 
     85   PP_Resource file_system_resource() const { return file_system_; }
     86 
     87   // Returns the system path corresponding to this file. Valid only for
     88   // external filesystems.
     89   base::FilePath GetSystemPath() const;
     90 
     91   // Returns the FileSystem API URL corresponding to this file.
     92   GURL GetFileSystemURL() const;
     93 
     94   // Checks if file ref has file system instance and if the instance is opened.
     95   bool HasValidFileSystem() const;
     96 
     97   void AddFileSystemRefCount() {
     98     file_system_ref_ = file_system_;
     99   }
    100 
    101  private:
    102   virtual ~PPB_FileRef_Impl();
    103 
    104   // IPC::Listener implementation.
    105   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
    106 
    107   void OnAsyncFileOpened(
    108       base::PlatformFileError error_code,
    109       IPC::PlatformFileForTransit file_for_transit,
    110       int message_id);
    111 
    112   // Many mutation functions are allow only to non-external filesystems, This
    113   // function returns true if the filesystem is opened and isn't external as an
    114   // access check for these functions.
    115   bool IsValidNonExternalFileSystem() const;
    116 
    117   PepperFileSystemHost* GetFileSystemHost() const;
    118   static PepperFileSystemHost* GetFileSystemHostInternal(
    119       PP_Instance instance, PP_Resource resource);
    120 
    121   // 0 for external filesystems.  This is a plugin side resource that we don't
    122   // hold a reference here, so file_system_ could be destroyed earlier than
    123   // this object.  Right now we checked the existance in plugin delegate before
    124   // use.  But it's better to hold a reference once we migrate FileRef to the
    125   // new design.
    126   PP_Resource file_system_;
    127 
    128   // Holds a reference of FileSystem when running in process.  See
    129   // PPB_FileRef_Proxy for corresponding code for out-of-process mode.  Note
    130   // that this ScopedPPResource is only expected to be used when running in
    131   // process (since PPB_FileRef_Proxy takes care of out-of-process case).
    132   // Also note that this workaround will be no longer needed after FileRef
    133   // refactoring.
    134   ppapi::ScopedPPResource file_system_ref_;
    135 
    136   // Used only for external filesystems.
    137   base::FilePath external_file_system_path_;
    138 
    139   // Lazily initialized var created from the external path. This is so we can
    140   // return the identical string object every time it is requested.
    141   scoped_refptr<StringVar> external_path_var_;
    142 
    143   int routing_id_;
    144 
    145   typedef base::Callback<void (base::PlatformFileError, base::PassPlatformFile)>
    146       AsyncOpenFileCallback;
    147 
    148   IDMap<AsyncOpenFileCallback> pending_async_open_files_;
    149 
    150   DISALLOW_COPY_AND_ASSIGN(PPB_FileRef_Impl);
    151 };
    152 
    153 }  // namespace content
    154 
    155 #endif  // CONTENT_RENDERER_PEPPER_PPB_FILE_REF_IMPL_H_
    156