Home | History | Annotate | Download | only in fileapi
      1 // Copyright (c) 2013 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 CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_MTP_DEVICE_ASYNC_DELEGATE_H_
      6 #define CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_MTP_DEVICE_ASYNC_DELEGATE_H_
      7 
      8 #include "base/callback.h"
      9 #include "base/files/file.h"
     10 #include "base/memory/ref_counted.h"
     11 #include "webkit/browser/fileapi/async_file_util.h"
     12 
     13 namespace base {
     14 class FilePath;
     15 }
     16 
     17 namespace net {
     18 class IOBuffer;
     19 }
     20 
     21 // Asynchronous delegate for media transfer protocol (MTP) device to perform
     22 // media device file system operations. Class that implements this
     23 // delegate does the actual communication with the MTP device.
     24 // The lifetime of the delegate is managed by the MTPDeviceMapService class.
     25 // Member functions and callbacks run on the IO thread.
     26 class MTPDeviceAsyncDelegate {
     27  public:
     28   // A callback to be called when GetFileInfo method call succeeds.
     29   typedef base::Callback<
     30       void(const base::File::Info& file_info)> GetFileInfoSuccessCallback;
     31 
     32   // A callback to be called when ReadDirectory method call succeeds.
     33   typedef base::Callback<
     34       void(const fileapi::AsyncFileUtil::EntryList& file_list,
     35            bool has_more)> ReadDirectorySuccessCallback;
     36 
     37   // A callback to be called when GetFileInfo/ReadDirectory/CreateSnapshot
     38   // method call fails.
     39   typedef base::Callback<void(base::File::Error error)> ErrorCallback;
     40 
     41   // A callback to be called when CreateSnapshotFile method call succeeds.
     42   typedef base::Callback<
     43       void(const base::File::Info& file_info,
     44            const base::FilePath& local_path)> CreateSnapshotFileSuccessCallback;
     45 
     46   // A callback to be called when ReadBytes method call succeeds.
     47   typedef base::Callback<
     48       void(const base::File::Info& file_info,
     49            int bytes_read)> ReadBytesSuccessCallback;
     50 
     51   struct ReadBytesRequest {
     52     ReadBytesRequest(const std::string& device_file_relative_path,
     53                      net::IOBuffer* buf, int64 offset, int buf_len,
     54                      const ReadBytesSuccessCallback& success_callback,
     55                      const ErrorCallback& error_callback);
     56     ~ReadBytesRequest();
     57 
     58     std::string device_file_relative_path;
     59     scoped_refptr<net::IOBuffer> buf;
     60     int64 offset;
     61     int buf_len;
     62     ReadBytesSuccessCallback success_callback;
     63     ErrorCallback error_callback;
     64   };
     65 
     66   // Gets information about the given |file_path| and invokes the appropriate
     67   // callback asynchronously when complete.
     68   virtual void GetFileInfo(
     69       const base::FilePath& file_path,
     70       const GetFileInfoSuccessCallback& success_callback,
     71       const ErrorCallback& error_callback) = 0;
     72 
     73   // Enumerates the |root| directory contents and invokes the appropriate
     74   // callback asynchronously when complete.
     75   virtual void ReadDirectory(
     76       const base::FilePath& root,
     77       const ReadDirectorySuccessCallback& success_callback,
     78       const ErrorCallback& error_callback) = 0;
     79 
     80   // Copy the contents of |device_file_path| to |local_path|. Invokes the
     81   // appropriate callback asynchronously when complete.
     82   virtual void CreateSnapshotFile(
     83       const base::FilePath& device_file_path,
     84       const base::FilePath& local_path,
     85       const CreateSnapshotFileSuccessCallback& success_callback,
     86       const ErrorCallback& error_callback) = 0;
     87 
     88   // Platform-specific implementations that are streaming don't create a local
     89   // snapshot file. Blobs are instead FileSystemURL backed and read in a stream.
     90   virtual bool IsStreaming() = 0;
     91 
     92   // Reads up to |buf_len| bytes from |device_file_path| into |buf|. Invokes the
     93   // appropriate callback asynchronously when complete. Only valid when
     94   // IsStreaming() is true.
     95   virtual void ReadBytes(
     96       const base::FilePath& device_file_path,
     97       net::IOBuffer* buf, int64 offset, int buf_len,
     98       const ReadBytesSuccessCallback& success_callback,
     99       const ErrorCallback& error_callback) = 0;
    100 
    101   // Called when the
    102   // (1) Browser application is in shutdown mode (or)
    103   // (2) Last extension using this MTP device is destroyed (or)
    104   // (3) Attached MTP device is removed (or)
    105   // (4) User revoked the MTP device gallery permission.
    106   // Ownership of |MTPDeviceAsyncDelegate| is handed off to the delegate
    107   // implementation class by this call. This function should take care of
    108   // cancelling all the pending tasks before deleting itself.
    109   virtual void CancelPendingTasksAndDeleteDelegate() = 0;
    110 
    111  protected:
    112   // Always destruct this object via CancelPendingTasksAndDeleteDelegate().
    113   virtual ~MTPDeviceAsyncDelegate() {}
    114 };
    115 
    116 typedef base::Callback<void(MTPDeviceAsyncDelegate*)>
    117     CreateMTPDeviceAsyncDelegateCallback;
    118 
    119 void CreateMTPDeviceAsyncDelegate(
    120     const base::FilePath::StringType& device_location,
    121     const CreateMTPDeviceAsyncDelegateCallback& callback);
    122 
    123 #endif  // CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_MTP_DEVICE_ASYNC_DELEGATE_H_
    124