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 ReadDirectory.
     43   // The first argument is a vector of file entries.
     44   // The second argument is true if there are more file entries.
     45   // The third argument is true if there was an error.
     46   typedef base::Callback<void(const std::vector<MtpFileEntry>& file_entries,
     47                               bool has_more,
     48                               bool error)> ReadDirectoryCallback;
     49 
     50   // A callback to handle the result of ReadFileChunk.
     51   // The first argument is a string containing the file data.
     52   // The second argument is true if there was an error.
     53   typedef base::Callback<void(const std::string& data,
     54                               bool error)> ReadFileCallback;
     55 
     56   // A callback to handle the result of GetFileInfo.
     57   // The first argument is a file entry.
     58   // The second argument is true if there was an error.
     59   typedef base::Callback<void(const MtpFileEntry& file_entry,
     60                               bool error)> GetFileInfoCallback;
     61 
     62   // Implement this interface to be notified about MTP storage
     63   // attachment / detachment events.
     64   class Observer {
     65    public:
     66     virtual ~Observer() {}
     67 
     68     // A function called after a MTP storage has been attached / detached.
     69     virtual void StorageChanged(bool is_attached,
     70                                 const std::string& storage_name) = 0;
     71   };
     72 
     73   virtual ~MediaTransferProtocolManager() {}
     74 
     75   // Adds an observer.
     76   virtual void AddObserver(Observer* observer) = 0;
     77 
     78   // Removes an observer.
     79   virtual void RemoveObserver(Observer* observer) = 0;
     80 
     81   // Returns a vector of available MTP storages.
     82   virtual const std::vector<std::string> GetStorages() const = 0;
     83 
     84   // On success, returns the the metadata for |storage_name|.
     85   // Otherwise returns NULL.
     86   virtual const MtpStorageInfo* GetStorageInfo(
     87       const std::string& storage_name) const = 0;
     88 
     89   // Opens |storage_name| in |mode| and runs |callback|.
     90   virtual void OpenStorage(const std::string& storage_name,
     91                            const std::string& mode,
     92                            const OpenStorageCallback& callback) = 0;
     93 
     94   // Close |storage_handle| and runs |callback|.
     95   virtual void CloseStorage(const std::string& storage_handle,
     96                             const CloseStorageCallback& callback) = 0;
     97 
     98   // Reads directory entries from |file_id| on |storage_handle| and runs
     99   // |callback|.
    100   virtual void ReadDirectory(const std::string& storage_handle,
    101                              uint32 file_id,
    102                              const ReadDirectoryCallback& callback) = 0;
    103 
    104   // Reads file data from |file_id| on |storage_handle| and runs |callback|.
    105   // Reads |count| bytes of data starting at |offset|.
    106   virtual void ReadFileChunk(const std::string& storage_handle,
    107                              uint32 file_id,
    108                              uint32 offset,
    109                              uint32 count,
    110                              const ReadFileCallback& callback) = 0;
    111 
    112   // Gets the file metadata for |file_id| on |storage_handle| and runs
    113   // |callback|.
    114   virtual void GetFileInfo(const std::string& storage_handle,
    115                            uint32 file_id,
    116                            const GetFileInfoCallback& callback) = 0;
    117 
    118   // Creates and returns the global MediaTransferProtocolManager instance.
    119   // On Linux, |task_runner| specifies the task runner to process asynchronous
    120   // operations.
    121   // On ChromeOS, |task_runner| should just be set to NULL because ChromeOS
    122   // already has a dedicated message loop proxy.
    123   static MediaTransferProtocolManager* Initialize(
    124       scoped_refptr<base::SequencedTaskRunner> task_runner);
    125 };
    126 
    127 }  // namespace device
    128 
    129 #endif  // DEVICE_MEDIA_TRANSFER_PROTOCOL_MEDIA_TRANSFER_PROTOCOL_MANAGER_H_
    130