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

Side by Side Diff: device/media_transfer_protocol/media_transfer_protocol_manager.cc

Issue 12092074: Remove 'content' dependency from device/media_transfer_protocol (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: '' Created 7 years, 10 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
OLDNEW
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 #include "device/media_transfer_protocol/media_transfer_protocol_manager.h" 5 #include "device/media_transfer_protocol/media_transfer_protocol_manager.h"
6 6
7 #include <map> 7 #include <map>
8 #include <queue> 8 #include <queue>
9 #include <set> 9 #include <set>
10 #include <utility> 10 #include <utility>
11 11
12 #include "base/bind.h" 12 #include "base/bind.h"
13 #include "base/command_line.h" 13 #include "base/command_line.h"
14 #include "base/memory/weak_ptr.h" 14 #include "base/memory/weak_ptr.h"
15 #include "base/message_loop_proxy.h"
15 #include "base/observer_list.h" 16 #include "base/observer_list.h"
16 #include "base/stl_util.h" 17 #include "base/stl_util.h"
18 #include "base/threading/thread_checker.h"
17 #include "chrome/common/chrome_switches.h" 19 #include "chrome/common/chrome_switches.h"
18 #include "content/public/browser/browser_thread.h"
19 #include "device/media_transfer_protocol/media_transfer_protocol_daemon_client.h " 20 #include "device/media_transfer_protocol/media_transfer_protocol_daemon_client.h "
20 #include "device/media_transfer_protocol/mtp_file_entry.pb.h" 21 #include "device/media_transfer_protocol/mtp_file_entry.pb.h"
21 #include "device/media_transfer_protocol/mtp_storage_info.pb.h" 22 #include "device/media_transfer_protocol/mtp_storage_info.pb.h"
22 23
23 #if defined(OS_CHROMEOS) 24 #if defined(OS_CHROMEOS)
24 #include "chromeos/dbus/dbus_thread_manager.h" 25 #include "chromeos/dbus/dbus_thread_manager.h"
25 #else 26 #else
26 #include "dbus/bus.h" 27 #include "dbus/bus.h"
27 #endif 28 #endif
28 29
29 using content::BrowserThread;
30
31 namespace device { 30 namespace device {
32 31
33 namespace { 32 namespace {
34 33
35 MediaTransferProtocolManager* g_media_transfer_protocol_manager = NULL; 34 MediaTransferProtocolManager* g_media_transfer_protocol_manager = NULL;
36 35
37 // The MediaTransferProtocolManager implementation. 36 // The MediaTransferProtocolManager implementation.
38 class MediaTransferProtocolManagerImpl : public MediaTransferProtocolManager { 37 class MediaTransferProtocolManagerImpl : public MediaTransferProtocolManager {
39 public: 38 public:
40 MediaTransferProtocolManagerImpl() : weak_ptr_factory_(this) { 39 MediaTransferProtocolManagerImpl(
40 scoped_refptr<base::MessageLoopProxy> loop_proxy)
41 : weak_ptr_factory_(this) {
41 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType)) 42 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType))
42 return; 43 return;
43 44
44 dbus::Bus* bus = NULL; 45 dbus::Bus* bus = NULL;
45 #if defined(OS_CHROMEOS) 46 #if defined(OS_CHROMEOS)
47 DCHECK(!loop_proxy.get());
46 chromeos::DBusThreadManager* dbus_thread_manager = 48 chromeos::DBusThreadManager* dbus_thread_manager =
47 chromeos::DBusThreadManager::Get(); 49 chromeos::DBusThreadManager::Get();
48 bus = dbus_thread_manager->GetSystemBus(); 50 bus = dbus_thread_manager->GetSystemBus();
49 if (!bus) 51 if (!bus)
50 return; 52 return;
51 #else 53 #else
54 DCHECK(loop_proxy.get());
52 dbus::Bus::Options options; 55 dbus::Bus::Options options;
53 options.bus_type = dbus::Bus::SYSTEM; 56 options.bus_type = dbus::Bus::SYSTEM;
54 options.connection_type = dbus::Bus::PRIVATE; 57 options.connection_type = dbus::Bus::PRIVATE;
55 options.dbus_thread_message_loop_proxy = 58 options.dbus_thread_message_loop_proxy = loop_proxy;
56 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE);
57 session_bus_ = new dbus::Bus(options); 59 session_bus_ = new dbus::Bus(options);
58 bus = session_bus_.get(); 60 bus = session_bus_.get();
59 #endif 61 #endif
60 62
61 DCHECK(bus); 63 DCHECK(bus);
62 mtp_client_.reset( 64 mtp_client_.reset(
63 MediaTransferProtocolDaemonClient::Create(bus, false /* not stub */)); 65 MediaTransferProtocolDaemonClient::Create(bus, false /* not stub */));
64 66
65 // Set up signals and start initializing |storage_info_map_|. 67 // Set up signals and start initializing |storage_info_map_|.
66 mtp_client_->SetUpConnections( 68 mtp_client_->SetUpConnections(
(...skipping 13 matching lines...) Expand all
80 observers_.AddObserver(observer); 82 observers_.AddObserver(observer);
81 } 83 }
82 84
83 // MediaTransferProtocolManager override. 85 // MediaTransferProtocolManager override.
84 virtual void RemoveObserver(Observer* observer) OVERRIDE { 86 virtual void RemoveObserver(Observer* observer) OVERRIDE {
85 observers_.RemoveObserver(observer); 87 observers_.RemoveObserver(observer);
86 } 88 }
87 89
88 // MediaTransferProtocolManager override. 90 // MediaTransferProtocolManager override.
89 const std::vector<std::string> GetStorages() const OVERRIDE { 91 const std::vector<std::string> GetStorages() const OVERRIDE {
90 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 92 DCHECK(thread_checker_.CalledOnValidThread());
91 std::vector<std::string> storages; 93 std::vector<std::string> storages;
92 for (StorageInfoMap::const_iterator it = storage_info_map_.begin(); 94 for (StorageInfoMap::const_iterator it = storage_info_map_.begin();
93 it != storage_info_map_.end(); 95 it != storage_info_map_.end();
94 ++it) { 96 ++it) {
95 storages.push_back(it->first); 97 storages.push_back(it->first);
96 } 98 }
97 return storages; 99 return storages;
98 } 100 }
99 101
100 // MediaTransferProtocolManager override. 102 // MediaTransferProtocolManager override.
101 virtual const MtpStorageInfo* GetStorageInfo( 103 virtual const MtpStorageInfo* GetStorageInfo(
102 const std::string& storage_name) const OVERRIDE { 104 const std::string& storage_name) const OVERRIDE {
103 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 105 DCHECK(thread_checker_.CalledOnValidThread());
104 StorageInfoMap::const_iterator it = storage_info_map_.find(storage_name); 106 StorageInfoMap::const_iterator it = storage_info_map_.find(storage_name);
105 if (it == storage_info_map_.end()) 107 if (it == storage_info_map_.end())
106 return NULL; 108 return NULL;
107 return &it->second; 109 return &it->second;
108 } 110 }
109 111
110 // MediaTransferProtocolManager override. 112 // MediaTransferProtocolManager override.
111 virtual void OpenStorage(const std::string& storage_name, 113 virtual void OpenStorage(const std::string& storage_name,
112 const std::string& mode, 114 const std::string& mode,
113 const OpenStorageCallback& callback) OVERRIDE { 115 const OpenStorageCallback& callback) OVERRIDE {
114 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 116 DCHECK(thread_checker_.CalledOnValidThread());
115 if (!ContainsKey(storage_info_map_, storage_name)) { 117 if (!ContainsKey(storage_info_map_, storage_name)) {
116 callback.Run("", true); 118 callback.Run("", true);
117 return; 119 return;
118 } 120 }
119 open_storage_callbacks_.push(callback); 121 open_storage_callbacks_.push(callback);
120 mtp_client_->OpenStorage( 122 mtp_client_->OpenStorage(
121 storage_name, 123 storage_name,
122 mode, 124 mode,
123 base::Bind(&MediaTransferProtocolManagerImpl::OnOpenStorage, 125 base::Bind(&MediaTransferProtocolManagerImpl::OnOpenStorage,
124 weak_ptr_factory_.GetWeakPtr()), 126 weak_ptr_factory_.GetWeakPtr()),
125 base::Bind(&MediaTransferProtocolManagerImpl::OnOpenStorageError, 127 base::Bind(&MediaTransferProtocolManagerImpl::OnOpenStorageError,
126 weak_ptr_factory_.GetWeakPtr())); 128 weak_ptr_factory_.GetWeakPtr()));
127 } 129 }
128 130
129 // MediaTransferProtocolManager override. 131 // MediaTransferProtocolManager override.
130 virtual void CloseStorage(const std::string& storage_handle, 132 virtual void CloseStorage(const std::string& storage_handle,
131 const CloseStorageCallback& callback) OVERRIDE { 133 const CloseStorageCallback& callback) OVERRIDE {
132 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 134 DCHECK(thread_checker_.CalledOnValidThread());
133 if (!ContainsKey(handles_, storage_handle)) { 135 if (!ContainsKey(handles_, storage_handle)) {
134 callback.Run(true); 136 callback.Run(true);
135 return; 137 return;
136 } 138 }
137 close_storage_callbacks_.push(std::make_pair(callback, storage_handle)); 139 close_storage_callbacks_.push(std::make_pair(callback, storage_handle));
138 mtp_client_->CloseStorage( 140 mtp_client_->CloseStorage(
139 storage_handle, 141 storage_handle,
140 base::Bind(&MediaTransferProtocolManagerImpl::OnCloseStorage, 142 base::Bind(&MediaTransferProtocolManagerImpl::OnCloseStorage,
141 weak_ptr_factory_.GetWeakPtr()), 143 weak_ptr_factory_.GetWeakPtr()),
142 base::Bind(&MediaTransferProtocolManagerImpl::OnCloseStorageError, 144 base::Bind(&MediaTransferProtocolManagerImpl::OnCloseStorageError,
143 weak_ptr_factory_.GetWeakPtr())); 145 weak_ptr_factory_.GetWeakPtr()));
144 } 146 }
145 147
146 // MediaTransferProtocolManager override. 148 // MediaTransferProtocolManager override.
147 virtual void ReadDirectoryByPath( 149 virtual void ReadDirectoryByPath(
148 const std::string& storage_handle, 150 const std::string& storage_handle,
149 const std::string& path, 151 const std::string& path,
150 const ReadDirectoryCallback& callback) OVERRIDE { 152 const ReadDirectoryCallback& callback) OVERRIDE {
151 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 153 DCHECK(thread_checker_.CalledOnValidThread());
152 if (!ContainsKey(handles_, storage_handle)) { 154 if (!ContainsKey(handles_, storage_handle)) {
153 callback.Run(std::vector<MtpFileEntry>(), true); 155 callback.Run(std::vector<MtpFileEntry>(), true);
154 return; 156 return;
155 } 157 }
156 read_directory_callbacks_.push(callback); 158 read_directory_callbacks_.push(callback);
157 mtp_client_->ReadDirectoryByPath( 159 mtp_client_->ReadDirectoryByPath(
158 storage_handle, 160 storage_handle,
159 path, 161 path,
160 base::Bind(&MediaTransferProtocolManagerImpl::OnReadDirectory, 162 base::Bind(&MediaTransferProtocolManagerImpl::OnReadDirectory,
161 weak_ptr_factory_.GetWeakPtr()), 163 weak_ptr_factory_.GetWeakPtr()),
162 base::Bind(&MediaTransferProtocolManagerImpl::OnReadDirectoryError, 164 base::Bind(&MediaTransferProtocolManagerImpl::OnReadDirectoryError,
163 weak_ptr_factory_.GetWeakPtr())); 165 weak_ptr_factory_.GetWeakPtr()));
164 } 166 }
165 167
166 // MediaTransferProtocolManager override. 168 // MediaTransferProtocolManager override.
167 virtual void ReadDirectoryById( 169 virtual void ReadDirectoryById(
168 const std::string& storage_handle, 170 const std::string& storage_handle,
169 uint32 file_id, 171 uint32 file_id,
170 const ReadDirectoryCallback& callback) OVERRIDE { 172 const ReadDirectoryCallback& callback) OVERRIDE {
171 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 173 DCHECK(thread_checker_.CalledOnValidThread());
172 if (!ContainsKey(handles_, storage_handle)) { 174 if (!ContainsKey(handles_, storage_handle)) {
173 callback.Run(std::vector<MtpFileEntry>(), true); 175 callback.Run(std::vector<MtpFileEntry>(), true);
174 return; 176 return;
175 } 177 }
176 read_directory_callbacks_.push(callback); 178 read_directory_callbacks_.push(callback);
177 mtp_client_->ReadDirectoryById( 179 mtp_client_->ReadDirectoryById(
178 storage_handle, 180 storage_handle,
179 file_id, 181 file_id,
180 base::Bind(&MediaTransferProtocolManagerImpl::OnReadDirectory, 182 base::Bind(&MediaTransferProtocolManagerImpl::OnReadDirectory,
181 weak_ptr_factory_.GetWeakPtr()), 183 weak_ptr_factory_.GetWeakPtr()),
182 base::Bind(&MediaTransferProtocolManagerImpl::OnReadDirectoryError, 184 base::Bind(&MediaTransferProtocolManagerImpl::OnReadDirectoryError,
183 weak_ptr_factory_.GetWeakPtr())); 185 weak_ptr_factory_.GetWeakPtr()));
184 } 186 }
185 187
186 // MediaTransferProtocolManager override. 188 // MediaTransferProtocolManager override.
187 virtual void ReadFileChunkByPath(const std::string& storage_handle, 189 virtual void ReadFileChunkByPath(const std::string& storage_handle,
188 const std::string& path, 190 const std::string& path,
189 uint32 offset, 191 uint32 offset,
190 uint32 count, 192 uint32 count,
191 const ReadFileCallback& callback) OVERRIDE { 193 const ReadFileCallback& callback) OVERRIDE {
192 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 194 DCHECK(thread_checker_.CalledOnValidThread());
193 if (!ContainsKey(handles_, storage_handle)) { 195 if (!ContainsKey(handles_, storage_handle)) {
194 callback.Run(std::string(), true); 196 callback.Run(std::string(), true);
195 return; 197 return;
196 } 198 }
197 read_file_callbacks_.push(callback); 199 read_file_callbacks_.push(callback);
198 mtp_client_->ReadFileChunkByPath( 200 mtp_client_->ReadFileChunkByPath(
199 storage_handle, path, offset, count, 201 storage_handle, path, offset, count,
200 base::Bind(&MediaTransferProtocolManagerImpl::OnReadFile, 202 base::Bind(&MediaTransferProtocolManagerImpl::OnReadFile,
201 weak_ptr_factory_.GetWeakPtr()), 203 weak_ptr_factory_.GetWeakPtr()),
202 base::Bind(&MediaTransferProtocolManagerImpl::OnReadFileError, 204 base::Bind(&MediaTransferProtocolManagerImpl::OnReadFileError,
203 weak_ptr_factory_.GetWeakPtr())); 205 weak_ptr_factory_.GetWeakPtr()));
204 } 206 }
205 207
206 // MediaTransferProtocolManager override. 208 // MediaTransferProtocolManager override.
207 virtual void ReadFileChunkById(const std::string& storage_handle, 209 virtual void ReadFileChunkById(const std::string& storage_handle,
208 uint32 file_id, 210 uint32 file_id,
209 uint32 offset, 211 uint32 offset,
210 uint32 count, 212 uint32 count,
211 const ReadFileCallback& callback) OVERRIDE { 213 const ReadFileCallback& callback) OVERRIDE {
212 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 214 DCHECK(thread_checker_.CalledOnValidThread());
213 if (!ContainsKey(handles_, storage_handle)) { 215 if (!ContainsKey(handles_, storage_handle)) {
214 callback.Run(std::string(), true); 216 callback.Run(std::string(), true);
215 return; 217 return;
216 } 218 }
217 read_file_callbacks_.push(callback); 219 read_file_callbacks_.push(callback);
218 mtp_client_->ReadFileChunkById( 220 mtp_client_->ReadFileChunkById(
219 storage_handle, file_id, offset, count, 221 storage_handle, file_id, offset, count,
220 base::Bind(&MediaTransferProtocolManagerImpl::OnReadFile, 222 base::Bind(&MediaTransferProtocolManagerImpl::OnReadFile,
221 weak_ptr_factory_.GetWeakPtr()), 223 weak_ptr_factory_.GetWeakPtr()),
222 base::Bind(&MediaTransferProtocolManagerImpl::OnReadFileError, 224 base::Bind(&MediaTransferProtocolManagerImpl::OnReadFileError,
223 weak_ptr_factory_.GetWeakPtr())); 225 weak_ptr_factory_.GetWeakPtr()));
224 } 226 }
225 227
226 virtual void GetFileInfoByPath(const std::string& storage_handle, 228 virtual void GetFileInfoByPath(const std::string& storage_handle,
227 const std::string& path, 229 const std::string& path,
228 const GetFileInfoCallback& callback) OVERRIDE { 230 const GetFileInfoCallback& callback) OVERRIDE {
229 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 231 DCHECK(thread_checker_.CalledOnValidThread());
230 if (!ContainsKey(handles_, storage_handle)) { 232 if (!ContainsKey(handles_, storage_handle)) {
231 callback.Run(MtpFileEntry(), true); 233 callback.Run(MtpFileEntry(), true);
232 return; 234 return;
233 } 235 }
234 get_file_info_callbacks_.push(callback); 236 get_file_info_callbacks_.push(callback);
235 mtp_client_->GetFileInfoByPath( 237 mtp_client_->GetFileInfoByPath(
236 storage_handle, 238 storage_handle,
237 path, 239 path,
238 base::Bind(&MediaTransferProtocolManagerImpl::OnGetFileInfo, 240 base::Bind(&MediaTransferProtocolManagerImpl::OnGetFileInfo,
239 weak_ptr_factory_.GetWeakPtr()), 241 weak_ptr_factory_.GetWeakPtr()),
240 base::Bind(&MediaTransferProtocolManagerImpl::OnGetFileInfoError, 242 base::Bind(&MediaTransferProtocolManagerImpl::OnGetFileInfoError,
241 weak_ptr_factory_.GetWeakPtr())); 243 weak_ptr_factory_.GetWeakPtr()));
242 } 244 }
243 245
244 virtual void GetFileInfoById(const std::string& storage_handle, 246 virtual void GetFileInfoById(const std::string& storage_handle,
245 uint32 file_id, 247 uint32 file_id,
246 const GetFileInfoCallback& callback) OVERRIDE { 248 const GetFileInfoCallback& callback) OVERRIDE {
247 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 249 DCHECK(thread_checker_.CalledOnValidThread());
248 if (!ContainsKey(handles_, storage_handle)) { 250 if (!ContainsKey(handles_, storage_handle)) {
249 callback.Run(MtpFileEntry(), true); 251 callback.Run(MtpFileEntry(), true);
250 return; 252 return;
251 } 253 }
252 get_file_info_callbacks_.push(callback); 254 get_file_info_callbacks_.push(callback);
253 mtp_client_->GetFileInfoById( 255 mtp_client_->GetFileInfoById(
254 storage_handle, 256 storage_handle,
255 file_id, 257 file_id,
256 base::Bind(&MediaTransferProtocolManagerImpl::OnGetFileInfo, 258 base::Bind(&MediaTransferProtocolManagerImpl::OnGetFileInfo,
257 weak_ptr_factory_.GetWeakPtr()), 259 weak_ptr_factory_.GetWeakPtr()),
258 base::Bind(&MediaTransferProtocolManagerImpl::OnGetFileInfoError, 260 base::Bind(&MediaTransferProtocolManagerImpl::OnGetFileInfoError,
259 weak_ptr_factory_.GetWeakPtr())); 261 weak_ptr_factory_.GetWeakPtr()));
260 } 262 }
261 263
262 private: 264 private:
263 // Map of storage names to storage info. 265 // Map of storage names to storage info.
264 typedef std::map<std::string, MtpStorageInfo> StorageInfoMap; 266 typedef std::map<std::string, MtpStorageInfo> StorageInfoMap;
265 // Callback queues - DBus communication is in-order, thus callbacks are 267 // Callback queues - DBus communication is in-order, thus callbacks are
266 // received in the same order as the requests. 268 // received in the same order as the requests.
267 typedef std::queue<OpenStorageCallback> OpenStorageCallbackQueue; 269 typedef std::queue<OpenStorageCallback> OpenStorageCallbackQueue;
268 // (callback, handle) 270 // (callback, handle)
269 typedef std::queue<std::pair<CloseStorageCallback, std::string> 271 typedef std::queue<std::pair<CloseStorageCallback, std::string>
270 > CloseStorageCallbackQueue; 272 > CloseStorageCallbackQueue;
271 typedef std::queue<ReadDirectoryCallback> ReadDirectoryCallbackQueue; 273 typedef std::queue<ReadDirectoryCallback> ReadDirectoryCallbackQueue;
272 typedef std::queue<ReadFileCallback> ReadFileCallbackQueue; 274 typedef std::queue<ReadFileCallback> ReadFileCallbackQueue;
273 typedef std::queue<GetFileInfoCallback> GetFileInfoCallbackQueue; 275 typedef std::queue<GetFileInfoCallback> GetFileInfoCallbackQueue;
274 276
275 void OnStorageChanged(bool is_attach, const std::string& storage_name) { 277 void OnStorageChanged(bool is_attach, const std::string& storage_name) {
276 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 278 DCHECK(thread_checker_.CalledOnValidThread());
277 if (is_attach) { 279 if (is_attach) {
278 mtp_client_->GetStorageInfo( 280 mtp_client_->GetStorageInfo(
279 storage_name, 281 storage_name,
280 base::Bind(&MediaTransferProtocolManagerImpl::OnGetStorageInfo, 282 base::Bind(&MediaTransferProtocolManagerImpl::OnGetStorageInfo,
281 weak_ptr_factory_.GetWeakPtr()), 283 weak_ptr_factory_.GetWeakPtr()),
282 base::Bind(&base::DoNothing)); 284 base::Bind(&base::DoNothing));
283 return; 285 return;
284 } 286 }
285 287
286 // Detach case. 288 // Detach case.
287 StorageInfoMap::iterator it = storage_info_map_.find(storage_name); 289 StorageInfoMap::iterator it = storage_info_map_.find(storage_name);
288 if (it == storage_info_map_.end()) { 290 if (it == storage_info_map_.end()) {
289 // This might happen during initialization when |storage_info_map_| has 291 // This might happen during initialization when |storage_info_map_| has
290 // not been fully populated yet? 292 // not been fully populated yet?
291 return; 293 return;
292 } 294 }
293 storage_info_map_.erase(it); 295 storage_info_map_.erase(it);
294 FOR_EACH_OBSERVER(Observer, 296 FOR_EACH_OBSERVER(Observer,
295 observers_, 297 observers_,
296 StorageChanged(false /* detach */, storage_name)); 298 StorageChanged(false /* detach */, storage_name));
297 } 299 }
298 300
299 void OnEnumerateStorages(const std::vector<std::string>& storage_names) { 301 void OnEnumerateStorages(const std::vector<std::string>& storage_names) {
302 DCHECK(thread_checker_.CalledOnValidThread());
300 for (size_t i = 0; i < storage_names.size(); ++i) { 303 for (size_t i = 0; i < storage_names.size(); ++i) {
301 mtp_client_->GetStorageInfo( 304 mtp_client_->GetStorageInfo(
302 storage_names[i], 305 storage_names[i],
303 base::Bind(&MediaTransferProtocolManagerImpl::OnGetStorageInfo, 306 base::Bind(&MediaTransferProtocolManagerImpl::OnGetStorageInfo,
304 weak_ptr_factory_.GetWeakPtr()), 307 weak_ptr_factory_.GetWeakPtr()),
305 base::Bind(&base::DoNothing)); 308 base::Bind(&base::DoNothing));
306 } 309 }
307 } 310 }
308 311
309 void OnGetStorageInfo(const MtpStorageInfo& storage_info) { 312 void OnGetStorageInfo(const MtpStorageInfo& storage_info) {
310 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 313 DCHECK(thread_checker_.CalledOnValidThread());
311 const std::string& storage_name = storage_info.storage_name(); 314 const std::string& storage_name = storage_info.storage_name();
312 if (ContainsKey(storage_info_map_, storage_name)) { 315 if (ContainsKey(storage_info_map_, storage_name)) {
313 // This should not happen, since MediaTransferProtocolManagerImpl should 316 // This should not happen, since MediaTransferProtocolManagerImpl should
314 // only call EnumerateStorages() once, which populates |storage_info_map_| 317 // only call EnumerateStorages() once, which populates |storage_info_map_|
315 // with the already-attached devices. 318 // with the already-attached devices.
316 // After that, all incoming signals are either for new storage 319 // After that, all incoming signals are either for new storage
317 // attachments, which should not be in |storage_info_map_|, or for 320 // attachments, which should not be in |storage_info_map_|, or for
318 // storage detachements, which do not add to |storage_info_map_|. 321 // storage detachements, which do not add to |storage_info_map_|.
319 NOTREACHED(); 322 NOTREACHED();
320 return; 323 return;
321 } 324 }
322 325
323 // New storage. Add it and let the observers know. 326 // New storage. Add it and let the observers know.
324 storage_info_map_.insert(std::make_pair(storage_name, storage_info)); 327 storage_info_map_.insert(std::make_pair(storage_name, storage_info));
325 FOR_EACH_OBSERVER(Observer, 328 FOR_EACH_OBSERVER(Observer,
326 observers_, 329 observers_,
327 StorageChanged(true /* is attach */, storage_name)); 330 StorageChanged(true /* is attach */, storage_name));
328 } 331 }
329 332
330 void OnOpenStorage(const std::string& handle) { 333 void OnOpenStorage(const std::string& handle) {
334 DCHECK(thread_checker_.CalledOnValidThread());
331 if (!ContainsKey(handles_, handle)) { 335 if (!ContainsKey(handles_, handle)) {
332 handles_.insert(handle); 336 handles_.insert(handle);
333 open_storage_callbacks_.front().Run(handle, false); 337 open_storage_callbacks_.front().Run(handle, false);
334 } else { 338 } else {
335 NOTREACHED(); 339 NOTREACHED();
336 open_storage_callbacks_.front().Run("", true); 340 open_storage_callbacks_.front().Run("", true);
337 } 341 }
338 open_storage_callbacks_.pop(); 342 open_storage_callbacks_.pop();
339 } 343 }
340 344
341 void OnOpenStorageError() { 345 void OnOpenStorageError() {
342 open_storage_callbacks_.front().Run("", true); 346 open_storage_callbacks_.front().Run("", true);
343 open_storage_callbacks_.pop(); 347 open_storage_callbacks_.pop();
344 } 348 }
345 349
346 void OnCloseStorage() { 350 void OnCloseStorage() {
351 DCHECK(thread_checker_.CalledOnValidThread());
347 const std::string& handle = close_storage_callbacks_.front().second; 352 const std::string& handle = close_storage_callbacks_.front().second;
348 if (ContainsKey(handles_, handle)) { 353 if (ContainsKey(handles_, handle)) {
349 handles_.erase(handle); 354 handles_.erase(handle);
350 close_storage_callbacks_.front().first.Run(false); 355 close_storage_callbacks_.front().first.Run(false);
351 } else { 356 } else {
352 NOTREACHED(); 357 NOTREACHED();
353 close_storage_callbacks_.front().first.Run(true); 358 close_storage_callbacks_.front().first.Run(true);
354 } 359 }
355 close_storage_callbacks_.pop(); 360 close_storage_callbacks_.pop();
356 } 361 }
357 362
358 void OnCloseStorageError() { 363 void OnCloseStorageError() {
364 DCHECK(thread_checker_.CalledOnValidThread());
359 close_storage_callbacks_.front().first.Run(true); 365 close_storage_callbacks_.front().first.Run(true);
360 close_storage_callbacks_.pop(); 366 close_storage_callbacks_.pop();
361 } 367 }
362 368
363 void OnReadDirectory(const std::vector<MtpFileEntry>& file_entries) { 369 void OnReadDirectory(const std::vector<MtpFileEntry>& file_entries) {
370 DCHECK(thread_checker_.CalledOnValidThread());
364 read_directory_callbacks_.front().Run(file_entries, false); 371 read_directory_callbacks_.front().Run(file_entries, false);
365 read_directory_callbacks_.pop(); 372 read_directory_callbacks_.pop();
366 } 373 }
367 374
368 void OnReadDirectoryError() { 375 void OnReadDirectoryError() {
376 DCHECK(thread_checker_.CalledOnValidThread());
369 read_directory_callbacks_.front().Run(std::vector<MtpFileEntry>(), true); 377 read_directory_callbacks_.front().Run(std::vector<MtpFileEntry>(), true);
370 read_directory_callbacks_.pop(); 378 read_directory_callbacks_.pop();
371 } 379 }
372 380
373 void OnReadFile(const std::string& data) { 381 void OnReadFile(const std::string& data) {
382 DCHECK(thread_checker_.CalledOnValidThread());
374 read_file_callbacks_.front().Run(data, false); 383 read_file_callbacks_.front().Run(data, false);
375 read_file_callbacks_.pop(); 384 read_file_callbacks_.pop();
376 } 385 }
377 386
378 void OnReadFileError() { 387 void OnReadFileError() {
388 DCHECK(thread_checker_.CalledOnValidThread());
379 read_file_callbacks_.front().Run(std::string(), true); 389 read_file_callbacks_.front().Run(std::string(), true);
380 read_file_callbacks_.pop(); 390 read_file_callbacks_.pop();
381 } 391 }
382 392
383 void OnGetFileInfo(const MtpFileEntry& entry) { 393 void OnGetFileInfo(const MtpFileEntry& entry) {
394 DCHECK(thread_checker_.CalledOnValidThread());
384 get_file_info_callbacks_.front().Run(entry, false); 395 get_file_info_callbacks_.front().Run(entry, false);
385 get_file_info_callbacks_.pop(); 396 get_file_info_callbacks_.pop();
386 } 397 }
387 398
388 void OnGetFileInfoError() { 399 void OnGetFileInfoError() {
400 DCHECK(thread_checker_.CalledOnValidThread());
389 get_file_info_callbacks_.front().Run(MtpFileEntry(), true); 401 get_file_info_callbacks_.front().Run(MtpFileEntry(), true);
390 get_file_info_callbacks_.pop(); 402 get_file_info_callbacks_.pop();
391 } 403 }
392 404
393 // Mtpd DBus client. 405 // Mtpd DBus client.
394 scoped_ptr<MediaTransferProtocolDaemonClient> mtp_client_; 406 scoped_ptr<MediaTransferProtocolDaemonClient> mtp_client_;
395 407
396 #if !defined(OS_CHROMEOS) 408 #if !defined(OS_CHROMEOS)
397 // And a D-Bus session for talking to mtpd. 409 // And a D-Bus session for talking to mtpd.
398 scoped_refptr<dbus::Bus> session_bus_; 410 scoped_refptr<dbus::Bus> session_bus_;
(...skipping 12 matching lines...) Expand all
411 // Set of open storage handles. 423 // Set of open storage handles.
412 std::set<std::string> handles_; 424 std::set<std::string> handles_;
413 425
414 // Queued callbacks. 426 // Queued callbacks.
415 OpenStorageCallbackQueue open_storage_callbacks_; 427 OpenStorageCallbackQueue open_storage_callbacks_;
416 CloseStorageCallbackQueue close_storage_callbacks_; 428 CloseStorageCallbackQueue close_storage_callbacks_;
417 ReadDirectoryCallbackQueue read_directory_callbacks_; 429 ReadDirectoryCallbackQueue read_directory_callbacks_;
418 ReadFileCallbackQueue read_file_callbacks_; 430 ReadFileCallbackQueue read_file_callbacks_;
419 GetFileInfoCallbackQueue get_file_info_callbacks_; 431 GetFileInfoCallbackQueue get_file_info_callbacks_;
420 432
433 base::ThreadChecker thread_checker_;
434
421 DISALLOW_COPY_AND_ASSIGN(MediaTransferProtocolManagerImpl); 435 DISALLOW_COPY_AND_ASSIGN(MediaTransferProtocolManagerImpl);
422 }; 436 };
423 437
424 } // namespace 438 } // namespace
425 439
426 // static 440 // static
427 void MediaTransferProtocolManager::Initialize() { 441 void MediaTransferProtocolManager::Initialize(
442 scoped_refptr<base::MessageLoopProxy> loop_proxy) {
428 if (g_media_transfer_protocol_manager) { 443 if (g_media_transfer_protocol_manager) {
429 LOG(WARNING) << "MediaTransferProtocolManager was already initialized"; 444 LOG(WARNING) << "MediaTransferProtocolManager was already initialized";
430 return; 445 return;
431 } 446 }
432 g_media_transfer_protocol_manager = new MediaTransferProtocolManagerImpl(); 447 g_media_transfer_protocol_manager = new MediaTransferProtocolManagerImpl(
Lei Zhang 2013/01/31 04:01:31 nit: I find it a bit more readable if you wrap the
kmadhusu 2013/01/31 18:12:38 Done.
448 loop_proxy);
433 VLOG(1) << "MediaTransferProtocolManager initialized"; 449 VLOG(1) << "MediaTransferProtocolManager initialized";
434 } 450 }
435 451
436 // static 452 // static
437 void MediaTransferProtocolManager::Shutdown() { 453 void MediaTransferProtocolManager::Shutdown() {
438 if (!g_media_transfer_protocol_manager) { 454 if (!g_media_transfer_protocol_manager) {
439 LOG(WARNING) << "MediaTransferProtocolManager::Shutdown() called with " 455 LOG(WARNING) << "MediaTransferProtocolManager::Shutdown() called with "
440 << "NULL manager"; 456 << "NULL manager";
441 return; 457 return;
442 } 458 }
443 delete g_media_transfer_protocol_manager; 459 delete g_media_transfer_protocol_manager;
444 g_media_transfer_protocol_manager = NULL; 460 g_media_transfer_protocol_manager = NULL;
445 VLOG(1) << "MediaTransferProtocolManager Shutdown completed"; 461 VLOG(1) << "MediaTransferProtocolManager Shutdown completed";
446 } 462 }
447 463
448 // static 464 // static
449 MediaTransferProtocolManager* MediaTransferProtocolManager::GetInstance() { 465 MediaTransferProtocolManager* MediaTransferProtocolManager::GetInstance() {
450 return g_media_transfer_protocol_manager; 466 return g_media_transfer_protocol_manager;
451 } 467 }
452 468
453 } // namespace device 469 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698