Home | History | Annotate | Download | only in media_transfer_protocol
      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 DEVICE_MEDIA_TRANSFER_PROTOCOL_MEDIA_TRANSFER_PROTOCOL_MANAGER_H_
      6 #define DEVICE_MEDIA_TRANSFER_PROTOCOL_MEDIA_TRANSFER_PROTOCOL_MANAGER_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/callback.h"
     12 #include "base/memory/ref_counted.h"
     13 #include "build/build_config.h"
     14 
     15 #if !defined(OS_LINUX)
     16 #error "Only used on Linux and ChromeOS"
     17 #endif
     18 
     19 class MtpFileEntry;
     20 class MtpStorageInfo;
     21 
     22 namespace base {
     23 class SequencedTaskRunner;
     24 }
     25 
     26 namespace device {
     27 
     28 // This class handles the interaction with mtpd.
     29 // Other classes can add themselves as observers.
     30 class MediaTransferProtocolManager {
     31  public:
     32   // A callback to handle the result of OpenStorage.
     33   // The first argument is the returned handle.
     34   // The second argument is true if there was an error.
     35   typedef base::Callback<void(const std::string& handle,
     36                               bool error)> OpenStorageCallback;
     37 
     38   // A callback to handle the result of CloseStorage.
     39   // The argument is true if there was an error.
     40   typedef base::Callback<void(bool error)> CloseStorageCallback;
     41 
     42   // A callback to handle the result of ReadDirectoryByPath/Id.
     43   // The first argument is a vector of file entries.
     44   // The second argument is true if there was an error.
     45   typedef base::Callback<void(const std::vector<MtpFileEntry>& file_entries,
     46                               bool error)> ReadDirectoryCallback;
     47 
     48   // A callback to handle the result of ReadFileChunkByPath/Id.
     49   // The first argument is a string containing the file data.
     50   // The second argument is true if there was an error.
     51   typedef base::Callback<void(const std::string& data,
     52                               bool error)> ReadFileCallback;
     53 
     54   // A callback to handle the result of GetFileInfoByPath/Id.
     55   // The first argument is a file entry.
     56   // The second argument is true if there was an error.
     57   typedef base::Callback<void(const MtpFileEntry& file_entry,
     58                               bool error)> GetFileInfoCallback;
     59 
     60   // Implement this interface to be notified about MTP storage
     61   // attachment / detachment events.
     62   class Observer {
     63    public:
     64     virtual ~Observer() {}
     65 
     66     // A function called after a MTP storage has been attached / detached.
     67     virtual void StorageChanged(bool is_attached,
     68                                 const std::string& storage_name) = 0;
     69   };
     70 
     71   virtual ~MediaTransferProtocolManager() {}
     72 
     73   // Adds an observer.
     74   virtual void AddObserver(Observer* observer) = 0;
     75 
     76   // Removes an observer.
     77   virtual void RemoveObserver(Observer* observer) = 0;
     78 
     79   // Returns a vector of available MTP storages.
     80   virtual const std::vector<std::string> GetStorages() const = 0;
     81 
     82   // On success, returns the the metadata for |storage_name|.
     83   // Otherwise returns NULL.
     84   virtual const MtpStorageInfo* GetStorageInfo(
     85       const std::string& storage_name) const = 0;
     86 
     87   // Opens |storage_name| in |mode| and runs |callback|.
     88   virtual void OpenStorage(const std::string& storage_name,
     89                            const std::string& mode,
     90                            const OpenStorageCallback& callback) = 0;
     91 
     92   // Close |storage_handle| and runs |callback|.
     93   virtual void CloseStorage(const std::string& storage_handle,
     94                             const CloseStorageCallback& callback) = 0;
     95 
     96   // Reads directory entries from |path| on |storage_handle| and runs
     97   // |callback|.
     98   virtual void ReadDirectoryByPath(const std::string& storage_handle,
     99                                    const std::string& path,
    100                                    const ReadDirectoryCallback& callback) = 0;
    101 
    102   // Reads directory entries from |file_id| on |storage_handle| and runs
    103   // |callback|.
    104   virtual void ReadDirectoryById(const std::string& storage_handle,
    105                                  uint32 file_id,
    106                                  const ReadDirectoryCallback& callback) = 0;
    107 
    108   // Reads file data from |path| on |storage_handle| and runs |callback|.
    109   // Reads |count| bytes of data starting at |offset|.
    110   virtual void ReadFileChunkByPath(const std::string& storage_handle,
    111                                    const std::string& path,
    112                                    uint32 offset,
    113                                    uint32 count,
    114                                    const ReadFileCallback& callback) = 0;
    115 
    116   // Reads file data from |file_id| on |storage_handle| and runs |callback|.
    117   // Reads |count| bytes of data starting at |offset|.
    118   virtual void ReadFileChunkById(const std::string& storage_handle,
    119                                  uint32 file_id,
    120                                  uint32 offset,
    121                                  uint32 count,
    122                                  const ReadFileCallback& callback) = 0;
    123 
    124   // Gets the file metadata for |path| on |storage_handle| and runs |callback|.
    125   virtual void GetFileInfoByPath(const std::string& storage_handle,
    126                                  const std::string& path,
    127                                  const GetFileInfoCallback& callback) = 0;
    128 
    129   // Gets the file metadata for |file_id| on |storage_handle| and runs
    130   // |callback|.
    131   virtual void GetFileInfoById(const std::string& storage_handle,
    132                                uint32 file_id,
    133                                const GetFileInfoCallback& callback) = 0;
    134 
    135   // Creates and returns the global MediaTransferProtocolManager instance.
    136   // On Linux, |task_runner| specifies the task runner to process asynchronous
    137   // operations.
    138   // On ChromeOS, |task_runner| should just be set to NULL because ChromeOS
    139   // already has a dedicated message loop proxy.
    140   static MediaTransferProtocolManager* Initialize(
    141       scoped_refptr<base::SequencedTaskRunner> task_runner);
    142 };
    143 
    144 }  // namespace device
    145 
    146 #endif  // DEVICE_MEDIA_TRANSFER_PROTOCOL_MEDIA_TRANSFER_PROTOCOL_MANAGER_H_
    147