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 CHROME_BROWSER_MEDIA_GALLERIES_MAC_MTP_DEVICE_DELEGATE_IMPL_MAC_H_
6#define CHROME_BROWSER_MEDIA_GALLERIES_MAC_MTP_DEVICE_DELEGATE_IMPL_MAC_H_
7
8#include <list>
9#include <map>
10#include <vector>
11
12#include "base/containers/hash_tables.h"
13#include "base/files/file_path.h"
14#include "base/memory/weak_ptr.h"
15#include "base/platform_file.h"
16#include "chrome/browser/media_galleries/fileapi/mtp_device_async_delegate.h"
17
18// Delegate for presenting an Image Capture device through the filesystem
19// API. The synthetic filesystem will be rooted at the constructed location,
20// and names of all files notified through the ItemAdded call will be
21// all appear as children of that directory. (ItemAdded calls with directories
22// will be ignored.)
23// Note on thread management: This class is thread-compatible: it can be created
24// on any thread, but then mutates all state on the UI thread. The async
25// delegate interface can be invoked on any thread, as it simply forwards calls
26// to the UI thread.
27class MTPDeviceDelegateImplMac : public MTPDeviceAsyncDelegate {
28 public:
29  MTPDeviceDelegateImplMac(const std::string& device_id,
30                           const base::FilePath::StringType& synthetic_path);
31
32  // MTPDeviceAsyncDelegate implementation. These functions are called on the
33  // IO thread by the async filesystem file util. They forward to
34  // similarly-named methods on the UI thread.
35  virtual void GetFileInfo(
36      const base::FilePath& file_path,
37      const GetFileInfoSuccessCallback& success_callback,
38      const ErrorCallback& error_callback) OVERRIDE;
39
40  // Note: passed absolute paths, but expects relative paths in reply.
41  virtual void ReadDirectory(
42      const base::FilePath& root,
43      const ReadDirectorySuccessCallback& success_callback,
44      const ErrorCallback& error_callback) OVERRIDE;
45
46  // Note: passed absolute paths.
47  virtual void CreateSnapshotFile(
48      const base::FilePath& device_file_path,
49      const base::FilePath& local_path,
50      const CreateSnapshotFileSuccessCallback& success_callback,
51      const ErrorCallback& error_callback) OVERRIDE;
52  virtual void CancelPendingTasksAndDeleteDelegate() OVERRIDE;
53
54  // Forward delegates for ImageCaptureDeviceListener. These are
55  // invoked in callbacks of the ImageCapture library on the UI thread.
56  virtual void ItemAdded(const std::string& name,
57                         const base::PlatformFileInfo& info);
58  virtual void NoMoreItems();
59  virtual void DownloadedFile(const std::string& name,
60                              base::PlatformFileError error);
61
62  // Scheduled when early directory reads are requested. The
63  // timeout will signal an ABORT error to the caller if the
64  // device metadata cannot be read.
65  void ReadDirectoryTimeout(const base::FilePath& root);
66
67 private:
68  class DeviceListener;
69
70  virtual ~MTPDeviceDelegateImplMac();
71
72  // Delegate for GetFileInfo, called on the UI thread.
73  void GetFileInfoImpl(const base::FilePath& file_path,
74                       base::PlatformFileInfo* file_info,
75                       base::PlatformFileError* error);
76
77  // Delegate for ReadDirectory, called on the UI thread.
78  void ReadDirectoryImpl(
79      const base::FilePath& root,
80      const ReadDirectorySuccessCallback& success_callback,
81      const ErrorCallback& error_callback);
82
83  // Delegate for CreateSnapshotFile, called on the UI thread.
84  void DownloadFile(
85      const base::FilePath& device_file_path,
86      const base::FilePath& local_path,
87      const CreateSnapshotFileSuccessCallback& success_callback,
88      const ErrorCallback& error_callback);
89
90  // Public for closures; should not be called except by
91  // CancelTasksAndDeleteDelegate.
92  void CancelAndDelete();
93
94  // Cancels any outstanding downloads.
95  void CancelDownloads();
96
97  // If necessary, notifies the ReadDirectory callback that all data
98  // has been read.
99  void NotifyReadDir();
100
101  std::string device_id_;
102  base::FilePath root_path_;
103
104  // Interface object for the camera underlying this MTP session.
105  scoped_ptr<DeviceListener> camera_interface_;
106
107  // Stores a map from filename to file metadata received from the camera.
108  base::hash_map<base::FilePath::StringType,
109                 base::PlatformFileInfo> file_info_;
110
111  // List of filenames received from the camera.
112  std::vector<base::FilePath> file_paths_;
113
114  // Set to true when all file metadata has been received from the camera.
115  bool received_all_files_;
116
117  struct ReadFileRequest {
118    ReadFileRequest();
119    ReadFileRequest(const std::string& request_file,
120                    const base::FilePath& snapshot_filename,
121                    CreateSnapshotFileSuccessCallback success_cb,
122                    ErrorCallback error_cb);
123    ~ReadFileRequest();
124
125    std::string request_file;
126    base::FilePath snapshot_file;
127    CreateSnapshotFileSuccessCallback success_callback;
128    ErrorCallback error_callback;
129  };
130
131  typedef std::list<ReadFileRequest> ReadFileTransactionList;
132
133  struct ReadDirectoryRequest {
134    ReadDirectoryRequest(const base::FilePath& dir,
135                         ReadDirectorySuccessCallback success_cb,
136                         ErrorCallback error_cb);
137    ~ReadDirectoryRequest();
138
139    base::FilePath directory;
140    ReadDirectorySuccessCallback success_callback;
141    ErrorCallback error_callback;
142  };
143
144  typedef std::list<ReadDirectoryRequest> ReadDirTransactionList;
145
146  ReadFileTransactionList read_file_transactions_;
147  ReadDirTransactionList read_dir_transactions_;
148
149  base::WeakPtrFactory<MTPDeviceDelegateImplMac> weak_factory_;
150
151  DISALLOW_COPY_AND_ASSIGN(MTPDeviceDelegateImplMac);
152};
153
154#endif  // CHROME_BROWSER_MEDIA_GALLERIES_MAC_MTP_DEVICE_DELEGATE_IMPL_MAC_H_
155