OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_MEDIA_GALLERIES_LINUX_MTP_DEVICE_DELEGATE_IMPL_LINUX_H_ | 5 #ifndef CHROME_BROWSER_MEDIA_GALLERIES_LINUX_MTP_DEVICE_DELEGATE_IMPL_LINUX_H_ |
6 #define CHROME_BROWSER_MEDIA_GALLERIES_LINUX_MTP_DEVICE_DELEGATE_IMPL_LINUX_H_ | 6 #define CHROME_BROWSER_MEDIA_GALLERIES_LINUX_MTP_DEVICE_DELEGATE_IMPL_LINUX_H_ |
7 | 7 |
8 #include <map> | |
8 #include <queue> | 9 #include <queue> |
10 #include <set> | |
11 #include <string> | |
9 | 12 |
10 #include "base/callback.h" | 13 #include "base/callback.h" |
14 #include "base/containers/scoped_ptr_hash_map.h" | |
11 #include "base/location.h" | 15 #include "base/location.h" |
12 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
13 #include "base/memory/weak_ptr.h" | 17 #include "base/memory/weak_ptr.h" |
14 #include "chrome/browser/media_galleries/fileapi/mtp_device_async_delegate.h" | 18 #include "chrome/browser/media_galleries/fileapi/mtp_device_async_delegate.h" |
15 #include "webkit/browser/fileapi/async_file_util.h" | 19 #include "webkit/browser/fileapi/async_file_util.h" |
16 | 20 |
17 namespace base { | 21 namespace base { |
18 class FilePath; | 22 class FilePath; |
19 } | 23 } |
20 | 24 |
(...skipping 20 matching lines...) Expand all Loading... | |
41 // Used to represent pending task details. | 45 // Used to represent pending task details. |
42 struct PendingTaskInfo { | 46 struct PendingTaskInfo { |
43 PendingTaskInfo(const tracked_objects::Location& location, | 47 PendingTaskInfo(const tracked_objects::Location& location, |
44 const base::Closure& task); | 48 const base::Closure& task); |
45 ~PendingTaskInfo(); | 49 ~PendingTaskInfo(); |
46 | 50 |
47 const tracked_objects::Location location; | 51 const tracked_objects::Location location; |
48 const base::Closure task; | 52 const base::Closure task; |
49 }; | 53 }; |
50 | 54 |
55 class MTPFileNode; | |
tommycli
2014/07/09 21:48:31
Either remove fwd decl or move inner class decl to
Lei Zhang
2014/07/14 23:46:33
Done.
| |
56 | |
57 // Maps file ids to file nodes. | |
58 typedef std::map<uint32, MTPFileNode*> FileIdToMTPFileNodeMap; | |
59 | |
60 // Container for holding a node's children. | |
61 typedef base::ScopedPtrHashMap<std::string, MTPFileNode> ChildNodes; | |
tommycli
2014/07/09 21:48:31
Since this is only used in the inner class, it sho
Lei Zhang
2014/07/14 23:46:33
Done.
| |
62 | |
63 // Represents a file on the MTP device. | |
64 class MTPFileNode { | |
65 public: | |
66 MTPFileNode(uint32 file_id, | |
67 MTPFileNode* parent, | |
68 FileIdToMTPFileNodeMap* file_id_to_node_map); | |
69 ~MTPFileNode(); | |
70 | |
71 const MTPFileNode* GetChild(const std::string& name) const; | |
72 | |
73 void AddChild(const std::string& name, uint32 id); | |
74 | |
75 // Clears all the children, except those in |children_to_keep|. | |
76 void ClearChildren(const std::set<std::string>& children_to_keep); | |
77 | |
78 bool DeleteChild(uint32 file_id); | |
79 | |
80 uint32 file_id() const { return file_id_; } | |
81 MTPFileNode* parent() { return parent_; } | |
82 | |
83 private: | |
84 const uint32 file_id_; | |
85 ChildNodes children_; | |
86 MTPFileNode* const parent_; | |
87 FileIdToMTPFileNodeMap* file_id_to_node_map_; | |
88 | |
89 DISALLOW_COPY_AND_ASSIGN(MTPFileNode); | |
90 }; | |
91 | |
51 // Should only be called by CreateMTPDeviceAsyncDelegate() factory call. | 92 // Should only be called by CreateMTPDeviceAsyncDelegate() factory call. |
52 // Defer the device initializations until the first file operation request. | 93 // Defer the device initializations until the first file operation request. |
53 // Do all the initializations in EnsureInitAndRunTask() function. | 94 // Do all the initializations in EnsureInitAndRunTask() function. |
54 explicit MTPDeviceDelegateImplLinux(const std::string& device_location); | 95 explicit MTPDeviceDelegateImplLinux(const std::string& device_location); |
55 | 96 |
56 // Destructed via CancelPendingTasksAndDeleteDelegate(). | 97 // Destructed via CancelPendingTasksAndDeleteDelegate(). |
57 virtual ~MTPDeviceDelegateImplLinux(); | 98 virtual ~MTPDeviceDelegateImplLinux(); |
58 | 99 |
59 // MTPDeviceAsyncDelegate: | 100 // MTPDeviceAsyncDelegate: |
60 virtual void GetFileInfo(const base::FilePath& file_path, | 101 virtual void GetFileInfo(const base::FilePath& file_path, |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
104 // If the device is successfully initialized, runs the next pending task. | 145 // If the device is successfully initialized, runs the next pending task. |
105 void OnInitCompleted(bool succeeded); | 146 void OnInitCompleted(bool succeeded); |
106 | 147 |
107 // Called when GetFileInfo() succeeds. |file_info| specifies the | 148 // Called when GetFileInfo() succeeds. |file_info| specifies the |
108 // requested file details. |success_callback| is invoked to notify the caller | 149 // requested file details. |success_callback| is invoked to notify the caller |
109 // about the requested file details. | 150 // about the requested file details. |
110 void OnDidGetFileInfo(const GetFileInfoSuccessCallback& success_callback, | 151 void OnDidGetFileInfo(const GetFileInfoSuccessCallback& success_callback, |
111 const base::File::Info& file_info); | 152 const base::File::Info& file_info); |
112 | 153 |
113 // Called when GetFileInfo() succeeds. GetFileInfo() is invoked to | 154 // Called when GetFileInfo() succeeds. GetFileInfo() is invoked to |
114 // get the |root| directory metadata details. |file_info| specifies the |root| | 155 // get the |dir_id| directory metadata details. |file_info| specifies the |
115 // directory details. | 156 // |dir_id| directory details. |
116 // | 157 // |
117 // If |root| is a directory, post a task on the UI thread to read the |root| | 158 // If |dir_id| is a directory, post a task on the UI thread to read the |
118 // directory file entries. | 159 // |dir_id| directory file entries. |
119 // | 160 // |
120 // If |root| is not a directory, |error_callback| is invoked to notify the | 161 // If |dir_id| is not a directory, |error_callback| is invoked to notify the |
121 // caller about the file error and process the next pending request. | 162 // caller about the file error and process the next pending request. |
122 void OnDidGetFileInfoToReadDirectory( | 163 void OnDidGetFileInfoToReadDirectory( |
123 const std::string& root, | 164 uint32 dir_id, |
124 const ReadDirectorySuccessCallback& success_callback, | 165 const ReadDirectorySuccessCallback& success_callback, |
125 const ErrorCallback& error_callback, | 166 const ErrorCallback& error_callback, |
126 const base::File::Info& file_info); | 167 const base::File::Info& file_info); |
127 | 168 |
128 // Called when GetFileInfo() succeeds. GetFileInfo() is invoked to | 169 // Called when GetFileInfo() succeeds. GetFileInfo() is invoked to |
129 // create the snapshot file of |snapshot_request_info.device_file_path|. | 170 // create the snapshot file of |snapshot_request_info.device_file_path|. |
130 // |file_info| specifies the device file metadata details. | 171 // |file_info| specifies the device file metadata details. |
131 // | 172 // |
132 // Posts a task on the UI thread to copy the data contents of the device file | 173 // Posts a task on the UI thread to copy the data contents of the device file |
133 // to the snapshot file. | 174 // to the snapshot file. |
134 void OnDidGetFileInfoToCreateSnapshotFile( | 175 void OnDidGetFileInfoToCreateSnapshotFile( |
135 scoped_ptr<SnapshotRequestInfo> snapshot_request_info, | 176 scoped_ptr<SnapshotRequestInfo> snapshot_request_info, |
136 const base::File::Info& file_info); | 177 const base::File::Info& file_info); |
137 | 178 |
138 // Called when ReadDirectory() succeeds. | 179 // Called when ReadDirectory() succeeds. |
139 // | 180 // |
140 // |file_list| contains the directory file entries. | 181 // |dir_id| is the directory read. |
182 // |file_list| contains the directory file entries with their file ids. | |
141 // |success_callback| is invoked to notify the caller about the directory | 183 // |success_callback| is invoked to notify the caller about the directory |
142 // file entries. | 184 // file entries. |
143 void OnDidReadDirectory(const ReadDirectorySuccessCallback& success_callback, | 185 void OnDidReadDirectory(uint32 dir_id, |
186 const ReadDirectorySuccessCallback& success_callback, | |
144 const fileapi::AsyncFileUtil::EntryList& file_list); | 187 const fileapi::AsyncFileUtil::EntryList& file_list); |
145 | 188 |
146 // Called when WriteDataIntoSnapshotFile() succeeds. | 189 // Called when WriteDataIntoSnapshotFile() succeeds. |
147 // | 190 // |
148 // |snapshot_file_info| specifies the snapshot file metadata details. | 191 // |snapshot_file_info| specifies the snapshot file metadata details. |
149 // | 192 // |
150 // |current_snapshot_request_info_.success_callback| is invoked to notify the | 193 // |current_snapshot_request_info_.success_callback| is invoked to notify the |
151 // caller about |snapshot_file_info|. | 194 // caller about |snapshot_file_info|. |
152 void OnDidWriteDataIntoSnapshotFile( | 195 void OnDidWriteDataIntoSnapshotFile( |
153 const base::File::Info& snapshot_file_info, | 196 const base::File::Info& snapshot_file_info, |
154 const base::FilePath& snapshot_file_path); | 197 const base::FilePath& snapshot_file_path); |
155 | 198 |
156 // Called when WriteDataIntoSnapshotFile() fails. | 199 // Called when WriteDataIntoSnapshotFile() fails. |
157 // | 200 // |
158 // |error| specifies the file error code. | 201 // |error| specifies the file error code. |
159 // | 202 // |
160 // |current_snapshot_request_info_.error_callback| is invoked to notify the | 203 // |current_snapshot_request_info_.error_callback| is invoked to notify the |
161 // caller about |error|. | 204 // caller about |error|. |
162 void OnWriteDataIntoSnapshotFileError(base::File::Error error); | 205 void OnWriteDataIntoSnapshotFileError(base::File::Error error); |
163 | 206 |
164 // Called when ReadBytes() succeeds. | 207 // Called when ReadBytes() succeeds. |
165 // | 208 // |
166 // |success_callback| is invoked to notify the caller about the read bytes. | 209 // |success_callback| is invoked to notify the caller about the read bytes. |
167 // |bytes_read| is the number of bytes read. | 210 // |bytes_read| is the number of bytes read. |
168 void OnDidReadBytes(const ReadBytesSuccessCallback& success_callback, | 211 void OnDidReadBytes(const ReadBytesSuccessCallback& success_callback, |
169 const base::File::Info& file_info, int bytes_read); | 212 const base::File::Info& file_info, int bytes_read); |
170 | 213 |
171 // Handles the device file |error|. |error_callback| is invoked to notify the | 214 // Handles the device file |error| while operating on |file_id|. |
172 // caller about the file error. | 215 // |error_callback| is invoked to notify the caller about the file error. |
173 void HandleDeviceFileError(const ErrorCallback& error_callback, | 216 void HandleDeviceFileError(const ErrorCallback& error_callback, |
217 uint32 file_id, | |
174 base::File::Error error); | 218 base::File::Error error); |
175 | 219 |
220 // Given a full path, return a non-empty sub-path that needs to be read into | |
221 // the cache if such an uncached path exists. In which case, also write the | |
222 // last cached file id to |cached_file_id|. | |
223 base::FilePath NextUncachedPathComponent(const base::FilePath& path, | |
224 uint32* cached_file_id) const; | |
225 | |
226 // Fills the file cache using the results from NextUncachedPathComponent(). | |
227 void FillFileCache(const base::FilePath& uncached_path, | |
228 uint32 cached_file_id, | |
229 const base::Closure success_callback, | |
230 const ErrorCallback& error_callback); | |
231 | |
232 // Given a full path, if it exists in the cache, write the file's id to |id| | |
233 // and return true. | |
234 bool CachedPathToId(const base::FilePath& path, uint32* id) const; | |
tommycli
2014/07/09 21:48:31
Since every caller really just wants a "EnsureCach
| |
235 | |
176 // MTP device initialization state. | 236 // MTP device initialization state. |
177 InitializationState init_state_; | 237 InitializationState init_state_; |
178 | 238 |
179 // Used to make sure only one task is in progress at any time. | 239 // Used to make sure only one task is in progress at any time. |
180 bool task_in_progress_; | 240 bool task_in_progress_; |
181 | 241 |
182 // Registered file system device path. This path does not | 242 // Registered file system device path. This path does not |
183 // correspond to a real device path (e.g. "/usb:2,2:81282"). | 243 // correspond to a real device path (e.g. "/usb:2,2:81282"). |
184 const base::FilePath device_path_; | 244 const base::FilePath device_path_; |
185 | 245 |
186 // MTP device storage name (e.g. "usb:2,2:81282"). | 246 // MTP device storage name (e.g. "usb:2,2:81282"). |
187 std::string storage_name_; | 247 std::string storage_name_; |
188 | 248 |
189 // A list of pending tasks that needs to be run when the device is | 249 // A list of pending tasks that needs to be run when the device is |
190 // initialized or when the current task in progress is complete. | 250 // initialized or when the current task in progress is complete. |
191 std::queue<PendingTaskInfo> pending_tasks_; | 251 std::queue<PendingTaskInfo> pending_tasks_; |
192 | 252 |
193 // Used to track the current snapshot file request. A snapshot file is created | 253 // Used to track the current snapshot file request. A snapshot file is created |
194 // incrementally. CreateSnapshotFile request reads the device file and writes | 254 // incrementally. CreateSnapshotFile request reads the device file and writes |
195 // to the snapshot file in chunks. In order to retain the order of the | 255 // to the snapshot file in chunks. In order to retain the order of the |
196 // snapshot file requests, make sure there is only one active snapshot file | 256 // snapshot file requests, make sure there is only one active snapshot file |
197 // request at any time. | 257 // request at any time. |
198 scoped_ptr<SnapshotRequestInfo> current_snapshot_request_info_; | 258 scoped_ptr<SnapshotRequestInfo> current_snapshot_request_info_; |
199 | 259 |
260 // A mapping for quick lookups into the |root_node_| tree structure. Since | |
261 // |root_node_| contains pointers to this map, it must be declared after this | |
262 // so destruction happens in the right order. | |
263 FileIdToMTPFileNodeMap file_id_to_node_map_; | |
264 | |
265 // The root node of a tree-structure that caches the directory structure of | |
266 // the MTP device. | |
267 MTPFileNode root_node_; | |
268 | |
200 // For callbacks that may run after destruction. | 269 // For callbacks that may run after destruction. |
201 base::WeakPtrFactory<MTPDeviceDelegateImplLinux> weak_ptr_factory_; | 270 base::WeakPtrFactory<MTPDeviceDelegateImplLinux> weak_ptr_factory_; |
202 | 271 |
203 DISALLOW_COPY_AND_ASSIGN(MTPDeviceDelegateImplLinux); | 272 DISALLOW_COPY_AND_ASSIGN(MTPDeviceDelegateImplLinux); |
204 }; | 273 }; |
205 | 274 |
206 #endif // CHROME_BROWSER_MEDIA_GALLERIES_LINUX_MTP_DEVICE_DELEGATE_IMPL_LINUX_H _ | 275 #endif // CHROME_BROWSER_MEDIA_GALLERIES_LINUX_MTP_DEVICE_DELEGATE_IMPL_LINUX_H _ |
OLD | NEW |