Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(533)

Side by Side Diff: chrome/browser/chromeos/mtp/media_transfer_protocol_manager.cc

Issue 10854048: ChromeOS: Implement MediaTransferProtocolManager. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
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 #include "chrome/browser/chromeos/mtp/media_transfer_protocol_manager.h"
6
7 #include <map>
8 #include <queue>
9 #include <set>
10 #include <utility>
11
12 #include "base/bind.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/observer_list.h"
15 #include "base/stl_util.h"
16 #include "chromeos/dbus/dbus_thread_manager.h"
17 #include "content/public/browser/browser_thread.h"
18
19 using content::BrowserThread;
20
21 namespace chromeos {
22 namespace mtp {
23
24 namespace {
25
26 MediaTransferProtocolManager* g_media_transfer_protocol_manager = NULL;
27
28 // The MediaTransferProtocolManager implementation.
29 class MediaTransferProtocolManagerImpl : public MediaTransferProtocolManager {
30 public:
31 MediaTransferProtocolManagerImpl() : weak_ptr_factory_(this) {
32 DBusThreadManager* dbus_thread_manager = DBusThreadManager::Get();
33 mtp_client_ = dbus_thread_manager->GetMediaTransferProtocolDaemonClient();
34
35 // Set up signals and start initializing |storage_info_map_|.
36 mtp_client_->SetUpConnections(
37 base::Bind(&MediaTransferProtocolManagerImpl::OnStorageChanged,
38 weak_ptr_factory_.GetWeakPtr()));
39 mtp_client_->EnumerateStorage(
40 base::Bind(&MediaTransferProtocolManagerImpl::OnEnumerateStorage,
41 weak_ptr_factory_.GetWeakPtr()),
42 base::Bind(&base::DoNothing));
43 }
44
45 virtual ~MediaTransferProtocolManagerImpl() {
46 }
47
48 // MediaTransferProtocolManager override.
49 virtual void AddObserver(Observer* observer) OVERRIDE {
50 observers_.AddObserver(observer);
51 }
52
53 // MediaTransferProtocolManager override.
54 virtual void RemoveObserver(Observer* observer) OVERRIDE {
55 observers_.RemoveObserver(observer);
56 }
57
58 // MediaTransferProtocolManager override.
59 const std::vector<std::string> GetStorages() const OVERRIDE {
60 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
61 std::vector<std::string> ret;
tbarzic 2012/08/29 19:52:27 what do you think about "result" instead of "ret"
Lei Zhang 2012/08/30 01:32:00 s/ret/storages/
62 for (StorageInfoMap::const_iterator it = storage_info_map_.begin();
63 it != storage_info_map_.end();
64 ++it) {
65 ret.push_back(it->first);
66 }
67 return ret;
68 }
69
70 // MediaTransferProtocolManager override.
71 virtual bool GetStorageInfo(const std::string& storage_name,
72 StorageInfo* info) const OVERRIDE {
tbarzic 2012/08/29 19:52:27 Personally, I'd prefer to return Storage info cons
Lei Zhang 2012/08/30 01:32:00 Done.
73 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
74 StorageInfoMap::const_iterator it = storage_info_map_.find(storage_name);
75 if (it == storage_info_map_.end())
76 return false;
77 *info = it->second;
78 return true;
79 }
80
81 // MediaTransferProtocolManager override.
82 virtual void OpenStorage(const std::string& storage_name,
83 OpenStorageMode mode,
84 const OpenStorageCallback& callback) OVERRIDE {
85 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
86 if (!ContainsKey(storage_info_map_, storage_name)) {
87 callback.Run("", true);
88 return;
89 }
90 open_storage_callbacks_.push(callback);
tbarzic 2012/08/29 19:52:27 I guess we can assume that open storage callbacks
Lei Zhang 2012/08/30 01:32:00 Done. Where the queue's are typedef'd.
toni.barzic 2012/08/30 01:46:34 yeah, makes sense..
91 mtp_client_->OpenStorage(
92 storage_name,
93 mode,
94 base::Bind(&MediaTransferProtocolManagerImpl::OnOpenStorage,
95 weak_ptr_factory_.GetWeakPtr()),
96 base::Bind(&MediaTransferProtocolManagerImpl::OnOpenStorageError,
97 weak_ptr_factory_.GetWeakPtr()));
98 }
99
100 // MediaTransferProtocolManager override.
101 virtual void CloseStorage(const std::string& storage_handle,
102 const CloseStorageCallback& callback) OVERRIDE {
103 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
104 if (!ContainsKey(handles_, storage_handle)) {
105 callback.Run(true);
106 return;
107 }
108 close_storage_callbacks_.push(std::make_pair(callback, storage_handle));
109 mtp_client_->CloseStorage(
110 storage_handle,
111 base::Bind(&MediaTransferProtocolManagerImpl::OnCloseStorage,
112 weak_ptr_factory_.GetWeakPtr()),
113 base::Bind(&MediaTransferProtocolManagerImpl::OnCloseStorageError,
114 weak_ptr_factory_.GetWeakPtr()));
115 }
116
117 // MediaTransferProtocolManager override.
118 virtual void ReadDirectoryByPath(
119 const std::string& storage_handle,
120 const std::string& path,
121 const ReadDirectoryCallback& callback) OVERRIDE {
122 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
123 if (!ContainsKey(handles_, storage_handle)) {
124 callback.Run(std::vector<FileEntry>(), true);
125 return;
126 }
127 read_directory_callbacks_.push(callback);
128 mtp_client_->ReadDirectoryByPath(
129 storage_handle,
130 path,
131 base::Bind(&MediaTransferProtocolManagerImpl::OnReadDirectory,
132 weak_ptr_factory_.GetWeakPtr()),
133 base::Bind(&MediaTransferProtocolManagerImpl::OnReadDirectoryError,
134 weak_ptr_factory_.GetWeakPtr()));
135 }
136
137 // MediaTransferProtocolManager override.
138 virtual void ReadDirectoryById(
139 const std::string& storage_handle,
140 uint32 file_id,
141 const ReadDirectoryCallback& callback) OVERRIDE {
142 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
143 if (!ContainsKey(handles_, storage_handle)) {
144 callback.Run(std::vector<FileEntry>(), true);
145 return;
146 }
147 read_directory_callbacks_.push(callback);
148 mtp_client_->ReadDirectoryById(
149 storage_handle,
150 file_id,
151 base::Bind(&MediaTransferProtocolManagerImpl::OnReadDirectory,
152 weak_ptr_factory_.GetWeakPtr()),
153 base::Bind(&MediaTransferProtocolManagerImpl::OnReadDirectoryError,
154 weak_ptr_factory_.GetWeakPtr()));
155 }
156
157 // MediaTransferProtocolManager override.
158 virtual void ReadFileByPath(const std::string& storage_handle,
159 const std::string& path,
160 const ReadFileCallback& callback) OVERRIDE {
161 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
162 if (!ContainsKey(handles_, storage_handle)) {
163 callback.Run(std::string(), true);
164 return;
165 }
166 read_file_callbacks_.push(callback);
167 mtp_client_->ReadFileByPath(
168 storage_handle,
169 path,
170 base::Bind(&MediaTransferProtocolManagerImpl::OnReadFile,
171 weak_ptr_factory_.GetWeakPtr()),
172 base::Bind(&MediaTransferProtocolManagerImpl::OnReadFileError,
173 weak_ptr_factory_.GetWeakPtr()));
174 }
175
176 // MediaTransferProtocolManager override.
177 virtual void ReadFileById(const std::string& storage_handle,
178 uint32 file_id,
179 const ReadFileCallback& callback) OVERRIDE {
180 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
181 if (!ContainsKey(handles_, storage_handle)) {
182 callback.Run(std::string(), true);
183 return;
184 }
185 read_file_callbacks_.push(callback);
186 mtp_client_->ReadFileById(
187 storage_handle,
188 file_id,
189 base::Bind(&MediaTransferProtocolManagerImpl::OnReadFile,
190 weak_ptr_factory_.GetWeakPtr()),
191 base::Bind(&MediaTransferProtocolManagerImpl::OnReadFileError,
192 weak_ptr_factory_.GetWeakPtr()));
193 }
194
195 virtual void GetFileInfoByPath(const std::string& storage_handle,
196 const std::string& path,
197 const GetFileInfoCallback& callback) OVERRIDE {
198 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
199 if (!ContainsKey(handles_, storage_handle)) {
200 callback.Run(FileEntry(), true);
201 return;
202 }
203 get_file_info_callbacks_.push(callback);
204 mtp_client_->GetFileInfoByPath(
205 storage_handle,
206 path,
207 base::Bind(&MediaTransferProtocolManagerImpl::OnGetFileInfo,
208 weak_ptr_factory_.GetWeakPtr()),
209 base::Bind(&MediaTransferProtocolManagerImpl::OnGetFileInfoError,
210 weak_ptr_factory_.GetWeakPtr()));
211 }
212
213 virtual void GetFileInfoById(const std::string& storage_handle,
214 uint32 file_id,
215 const GetFileInfoCallback& callback) OVERRIDE {
216 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
217 if (!ContainsKey(handles_, storage_handle)) {
218 callback.Run(FileEntry(), true);
219 return;
220 }
221 get_file_info_callbacks_.push(callback);
222 mtp_client_->GetFileInfoById(
223 storage_handle,
224 file_id,
225 base::Bind(&MediaTransferProtocolManagerImpl::OnGetFileInfo,
226 weak_ptr_factory_.GetWeakPtr()),
227 base::Bind(&MediaTransferProtocolManagerImpl::OnGetFileInfoError,
228 weak_ptr_factory_.GetWeakPtr()));
229 }
230
231 private:
232 // Map of storage names to storage info.
233 typedef std::map<std::string, StorageInfo> StorageInfoMap;
234 // Callback queues
235 typedef std::queue<OpenStorageCallback> OpenStorageCallbackQueue;
236 // (callback, handle)
237 typedef std::queue<std::pair<CloseStorageCallback, std::string>
238 > CloseStorageCallbackQueue;
239 typedef std::queue<ReadDirectoryCallback> ReadDirectoryCallbackQueue;
240 typedef std::queue<ReadFileCallback> ReadFileCallbackQueue;
241 typedef std::queue<GetFileInfoCallback> GetFileInfoCallbackQueue;
242
243 void OnStorageChanged(bool is_attach, const std::string& storage_name) {
244 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
245 if (is_attach) {
246 mtp_client_->GetStorageInfo(
247 storage_name,
248 base::Bind(&MediaTransferProtocolManagerImpl::OnGetStorageInfo,
249 weak_ptr_factory_.GetWeakPtr()),
250 base::Bind(&base::DoNothing));
251 return;
252 }
253
254 // Detach case.
255 StorageInfoMap::iterator it = storage_info_map_.find(storage_name);
256 if (it == storage_info_map_.end()) {
257 // This might happen during initialization when |storage_info_map_| has
258 // not been fully populated yet?
259 return;
260 }
261 storage_info_map_.erase(it);
262 FOR_EACH_OBSERVER(Observer,
263 observers_,
264 StorageChanged(false /* detach */, storage_name));
265 }
266
267 void OnEnumerateStorage(const std::vector<std::string>& storage_names) {
268 for (size_t i = 0; i < storage_names.size(); ++i) {
269 mtp_client_->GetStorageInfo(
270 storage_names[i],
271 base::Bind(&MediaTransferProtocolManagerImpl::OnGetStorageInfo,
272 weak_ptr_factory_.GetWeakPtr()),
273 base::Bind(&base::DoNothing));
274 }
275 }
276
277 void OnGetStorageInfo(const StorageInfo& storage_info) {
278 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
279 const std::string& storage_name = storage_info.storage_name();
280 if (ContainsKey(storage_info_map_, storage_name)) {
281 // This should not happen, since MediaTransferProtocolManagerImpl should
282 // only call EnumerateStorage() once. After that, all incoming signals
283 // should be for new storages, or storage detachments.
tbarzic 2012/08/29 19:52:27 I don't see this being called on storage detachmen
Lei Zhang 2012/08/30 01:32:00 There's no need to call OnGetStorageInfo() during
toni.barzic 2012/08/30 01:46:34 My comment was about not necessary needing "or sto
284 NOTREACHED();
285 return;
286 }
287
288 // New storage. Add it and let the observers know.
289 storage_info_map_.insert(std::make_pair(storage_name, storage_info));
290 FOR_EACH_OBSERVER(Observer,
291 observers_,
292 StorageChanged(true /* is attach */, storage_name));
293 }
294
295 void OnOpenStorage(const std::string& handle) {
296 if (!ContainsKey(handles_, handle)) {
297 handles_.insert(handle);
298 open_storage_callbacks_.front().Run(handle, false);
299 } else {
300 NOTREACHED();
301 open_storage_callbacks_.front().Run("", true);
302 }
303 open_storage_callbacks_.pop();
304 }
305
306 void OnOpenStorageError() {
307 open_storage_callbacks_.front().Run("", true);
308 open_storage_callbacks_.pop();
309 }
310
311 void OnCloseStorage() {
312 const std::string& handle = close_storage_callbacks_.front().second;
313 if (ContainsKey(handles_, handle)) {
314 handles_.erase(handle);
315 close_storage_callbacks_.front().first.Run(false);
316 } else {
317 NOTREACHED();
318 close_storage_callbacks_.front().first.Run(true);
319 }
320 close_storage_callbacks_.pop();
321 }
322
323 void OnCloseStorageError() {
324 close_storage_callbacks_.front().first.Run(true);
325 close_storage_callbacks_.pop();
326 }
327
328 void OnReadDirectory(const std::vector<FileEntry>& file_entries) {
329 read_directory_callbacks_.front().Run(file_entries, false);
330 read_directory_callbacks_.pop();
331 }
332
333 void OnReadDirectoryError() {
334 read_directory_callbacks_.front().Run(std::vector<FileEntry>(), true);
335 read_directory_callbacks_.pop();
336 }
337
338 void OnReadFile(const std::string& data) {
339 read_file_callbacks_.front().Run(data, false);
340 read_file_callbacks_.pop();
341 }
342
343 void OnReadFileError() {
344 read_file_callbacks_.front().Run(std::string(), true);
345 read_file_callbacks_.pop();
346 }
347
348 void OnGetFileInfo(const FileEntry& entry) {
349 get_file_info_callbacks_.front().Run(entry, false);
350 get_file_info_callbacks_.pop();
351 }
352
353 void OnGetFileInfoError() {
354 get_file_info_callbacks_.front().Run(FileEntry(), true);
355 get_file_info_callbacks_.pop();
356 }
357
358 // Mtpd DBus client.
359 MediaTransferProtocolDaemonClient* mtp_client_;
360
361 // Device attachment / detachment observers.
362 ObserverList<Observer> observers_;
363
364 base::WeakPtrFactory<MediaTransferProtocolManagerImpl> weak_ptr_factory_;
365
366 // Everything below is only accessed on the UI thread.
367
368 // Map to keep track of attached storages.
tbarzic 2012/08/29 19:52:27 "Map to keep track of attached storaged by name."
Lei Zhang 2012/08/30 01:32:00 Done.
369 StorageInfoMap storage_info_map_;
370
371 // Set of open storage handles.
372 std::set<std::string> handles_;
373
374 // Queued callbacks.
375 OpenStorageCallbackQueue open_storage_callbacks_;
376 CloseStorageCallbackQueue close_storage_callbacks_;
377 ReadDirectoryCallbackQueue read_directory_callbacks_;
378 ReadFileCallbackQueue read_file_callbacks_;
379 GetFileInfoCallbackQueue get_file_info_callbacks_;
380
381 DISALLOW_COPY_AND_ASSIGN(MediaTransferProtocolManagerImpl);
382 };
383
384 } // namespace
385
386 // static
387 void MediaTransferProtocolManager::Initialize() {
388 if (g_media_transfer_protocol_manager) {
389 LOG(WARNING) << "MediaTransferProtocolManager was already initialized";
390 return;
391 }
392 g_media_transfer_protocol_manager = new MediaTransferProtocolManagerImpl();
393 VLOG(1) << "MediaTransferProtocolManager initialized";
394 }
395
396 // static
397 void MediaTransferProtocolManager::Shutdown() {
398 if (!g_media_transfer_protocol_manager) {
399 LOG(WARNING) << "MediaTransferProtocolManager::Shutdown() called with "
400 << "NULL manager";
401 return;
402 }
403 delete g_media_transfer_protocol_manager;
404 g_media_transfer_protocol_manager = NULL;
405 VLOG(1) << "MediaTransferProtocolManager Shutdown completed";
406 }
407
408 // static
409 MediaTransferProtocolManager* MediaTransferProtocolManager::GetInstance() {
410 return g_media_transfer_protocol_manager;
411 }
412
413 } // namespace mtp
414 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698