Home | History | Annotate | Download | only in linux
      1 // Copyright 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_LINUX_MTP_DEVICE_TASK_HELPER_H_
      6 #define CHROME_BROWSER_MEDIA_GALLERIES_LINUX_MTP_DEVICE_TASK_HELPER_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/callback.h"
     12 #include "base/memory/scoped_ptr.h"
     13 #include "base/memory/weak_ptr.h"
     14 #include "base/platform_file.h"
     15 #include "chrome/browser/media_galleries/fileapi/mtp_device_async_delegate.h"
     16 #include "device/media_transfer_protocol/mtp_file_entry.pb.h"
     17 #include "webkit/browser/fileapi/async_file_util.h"
     18 
     19 class MTPReadFileWorker;
     20 struct SnapshotRequestInfo;
     21 
     22 // MTPDeviceTaskHelper dispatches the media transfer protocol (MTP) device
     23 // operation requests (such as GetFileInfo, ReadDirectory, CreateSnapshotFile,
     24 // OpenStorage and CloseStorage) to the MediaTransferProtocolManager.
     25 // MTPDeviceTaskHelper lives on the UI thread. MTPDeviceTaskHelperMapService
     26 // owns the MTPDeviceTaskHelper objects. MTPDeviceTaskHelper is instantiated per
     27 // MTP device storage.
     28 class MTPDeviceTaskHelper {
     29  public:
     30   typedef base::Callback<void(bool succeeded)> OpenStorageCallback;
     31 
     32   typedef MTPDeviceAsyncDelegate::GetFileInfoSuccessCallback
     33       GetFileInfoSuccessCallback;
     34 
     35   typedef base::Callback<void(const fileapi::AsyncFileUtil::EntryList&)>
     36       ReadDirectorySuccessCallback;
     37 
     38   typedef MTPDeviceAsyncDelegate::ErrorCallback ErrorCallback;
     39 
     40   MTPDeviceTaskHelper();
     41   ~MTPDeviceTaskHelper();
     42 
     43   // Dispatches the request to the MediaTransferProtocolManager to open the MTP
     44   // storage for communication.
     45   //
     46   // |storage_name| specifies the name of the storage device.
     47   // |callback| is called when the OpenStorage request completes. |callback|
     48   // runs on the IO thread.
     49   void OpenStorage(const std::string& storage_name,
     50                    const OpenStorageCallback& callback);
     51 
     52   // Dispatches the GetFileInfoByPath request to the
     53   // MediaTransferProtocolManager.
     54   //
     55   // |file_path| specifies the relative of the file whose details are requested.
     56   //
     57   // If the file details are fetched successfully, |success_callback| is invoked
     58   // on the IO thread to notify the caller about the file details.
     59   //
     60   // If there is an error, |error_callback| is invoked on the IO thread to
     61   // notify the caller about the file error.
     62   void GetFileInfoByPath(
     63       const std::string& file_path,
     64       const GetFileInfoSuccessCallback& success_callback,
     65       const ErrorCallback& error_callback);
     66 
     67   // Dispatches the read directory request to the MediaTransferProtocolManager.
     68   //
     69   // |dir_path| specifies the directory file path.
     70   //
     71   // If the directory file entries are enumerated successfully,
     72   // |success_callback| is invoked on the IO thread to notify the caller about
     73   // the directory file entries.
     74   //
     75   // If there is an error, |error_callback| is invoked on the IO thread to
     76   // notify the caller about the file error.
     77   void ReadDirectoryByPath(const std::string& dir_path,
     78                            const ReadDirectorySuccessCallback& success_callback,
     79                            const ErrorCallback& error_callback);
     80 
     81   // Forwards the WriteDataIntoSnapshotFile request to the MTPReadFileWorker
     82   // object.
     83   //
     84   // |request_info| specifies the snapshot file request params.
     85   // |snapshot_file_info| specifies the metadata of the snapshot file.
     86   void WriteDataIntoSnapshotFile(
     87       const SnapshotRequestInfo& request_info,
     88       const base::PlatformFileInfo& snapshot_file_info);
     89 
     90   // Dispatches the CloseStorage request to the MediaTransferProtocolManager.
     91   void CloseStorage() const;
     92 
     93  private:
     94   // Query callback for OpenStorage() to run |callback| on the IO thread.
     95   //
     96   // If OpenStorage request succeeds, |error| is set to false and
     97   // |device_handle| contains the handle to communicate with the MTP device.
     98   //
     99   // If OpenStorage request fails, |error| is set to true and |device_handle| is
    100   // set to an empty string.
    101   void OnDidOpenStorage(const OpenStorageCallback& callback,
    102                         const std::string& device_handle,
    103                         bool error);
    104 
    105   // Query callback for GetFileInfo().
    106   //
    107   // If there is no error, |file_entry| will contain the
    108   // requested media device file details and |error| is set to false.
    109   // |success_callback| is invoked on the IO thread to notify the caller.
    110   //
    111   // If there is an error, |file_entry| is invalid and |error| is
    112   // set to true. |error_callback| is invoked on the IO thread to notify the
    113   // caller.
    114   void OnGetFileInfo(const GetFileInfoSuccessCallback& success_callback,
    115                      const ErrorCallback& error_callback,
    116                      const MtpFileEntry& file_entry,
    117                      bool error) const;
    118 
    119   // Query callback for ReadDirectoryByPath().
    120   //
    121   // If there is no error, |error| is set to false, |file_entries| has the
    122   // directory file entries and |success_callback| is invoked on the IO thread
    123   // to notify the caller.
    124   //
    125   // If there is an error, |error| is set to true, |file_entries| is empty
    126   // and |error_callback| is invoked on the IO thread to notify the caller.
    127   void OnDidReadDirectoryByPath(
    128       const ReadDirectorySuccessCallback& success_callback,
    129       const ErrorCallback& error_callback,
    130       const std::vector<MtpFileEntry>& file_entries,
    131       bool error) const;
    132 
    133   // Called when the device is uninitialized.
    134   //
    135   // Runs |error_callback| on the IO thread to notify the caller about the
    136   // device |error|.
    137   void HandleDeviceError(const ErrorCallback& error_callback,
    138                          base::PlatformFileError error) const;
    139 
    140   // Handle to communicate with the MTP device.
    141   std::string device_handle_;
    142 
    143   // Used to handle WriteDataInfoSnapshotFile request.
    144   scoped_ptr<MTPReadFileWorker> read_file_worker_;
    145 
    146   // For callbacks that may run after destruction.
    147   base::WeakPtrFactory<MTPDeviceTaskHelper> weak_ptr_factory_;
    148 
    149   DISALLOW_COPY_AND_ASSIGN(MTPDeviceTaskHelper);
    150 };
    151 
    152 #endif  // CHROME_BROWSER_MEDIA_GALLERIES_LINUX_MTP_DEVICE_TASK_HELPER_H_
    153