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