| 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_daemon_client.h
" | 5 #include "device/media_transfer_protocol/media_transfer_protocol_daemon_client.h
" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/memory/weak_ptr.h" | 8 #include "base/memory/weak_ptr.h" |
| 9 #include "dbus/bus.h" | 9 #include "dbus/bus.h" |
| 10 #include "dbus/message.h" | 10 #include "dbus/message.h" |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 writer.AppendString(handle); | 89 writer.AppendString(handle); |
| 90 proxy_->CallMethod( | 90 proxy_->CallMethod( |
| 91 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 91 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 92 base::Bind(&MediaTransferProtocolDaemonClientImpl::OnCloseStorage, | 92 base::Bind(&MediaTransferProtocolDaemonClientImpl::OnCloseStorage, |
| 93 weak_ptr_factory_.GetWeakPtr(), | 93 weak_ptr_factory_.GetWeakPtr(), |
| 94 callback, | 94 callback, |
| 95 error_callback)); | 95 error_callback)); |
| 96 } | 96 } |
| 97 | 97 |
| 98 // MediaTransferProtocolDaemonClient override. | 98 // MediaTransferProtocolDaemonClient override. |
| 99 virtual void ReadDirectoryByPath( | |
| 100 const std::string& handle, | |
| 101 const std::string& path, | |
| 102 const ReadDirectoryCallback& callback, | |
| 103 const ErrorCallback& error_callback) OVERRIDE { | |
| 104 dbus::MethodCall method_call(mtpd::kMtpdInterface, | |
| 105 mtpd::kReadDirectoryByPath); | |
| 106 dbus::MessageWriter writer(&method_call); | |
| 107 writer.AppendString(handle); | |
| 108 writer.AppendString(path); | |
| 109 proxy_->CallMethod( | |
| 110 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 111 base::Bind(&MediaTransferProtocolDaemonClientImpl::OnReadDirectory, | |
| 112 weak_ptr_factory_.GetWeakPtr(), | |
| 113 callback, | |
| 114 error_callback)); | |
| 115 } | |
| 116 | |
| 117 // MediaTransferProtocolDaemonClient override. | |
| 118 virtual void ReadDirectoryById( | 99 virtual void ReadDirectoryById( |
| 119 const std::string& handle, | 100 const std::string& handle, |
| 120 uint32 file_id, | 101 uint32 file_id, |
| 121 const ReadDirectoryCallback& callback, | 102 const ReadDirectoryCallback& callback, |
| 122 const ErrorCallback& error_callback) OVERRIDE { | 103 const ErrorCallback& error_callback) OVERRIDE { |
| 123 dbus::MethodCall method_call(mtpd::kMtpdInterface, | 104 dbus::MethodCall method_call(mtpd::kMtpdInterface, |
| 124 mtpd::kReadDirectoryById); | 105 mtpd::kReadDirectoryById); |
| 125 dbus::MessageWriter writer(&method_call); | 106 dbus::MessageWriter writer(&method_call); |
| 126 writer.AppendString(handle); | 107 writer.AppendString(handle); |
| 127 writer.AppendUint32(file_id); | 108 writer.AppendUint32(file_id); |
| 128 proxy_->CallMethod( | 109 proxy_->CallMethod( |
| 129 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 110 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 130 base::Bind(&MediaTransferProtocolDaemonClientImpl::OnReadDirectory, | 111 base::Bind(&MediaTransferProtocolDaemonClientImpl::OnReadDirectory, |
| 131 weak_ptr_factory_.GetWeakPtr(), | 112 weak_ptr_factory_.GetWeakPtr(), |
| 132 callback, | 113 callback, |
| 133 error_callback)); | 114 error_callback)); |
| 134 } | 115 } |
| 135 | 116 |
| 136 // MediaTransferProtocolDaemonClient override. | 117 // MediaTransferProtocolDaemonClient override. |
| 137 virtual void ReadFileChunkByPath( | |
| 138 const std::string& handle, | |
| 139 const std::string& path, | |
| 140 uint32 offset, | |
| 141 uint32 bytes_to_read, | |
| 142 const ReadFileCallback& callback, | |
| 143 const ErrorCallback& error_callback) OVERRIDE { | |
| 144 DCHECK_LE(bytes_to_read, kMaxChunkSize); | |
| 145 dbus::MethodCall method_call(mtpd::kMtpdInterface, | |
| 146 mtpd::kReadFileChunkByPath); | |
| 147 dbus::MessageWriter writer(&method_call); | |
| 148 writer.AppendString(handle); | |
| 149 writer.AppendString(path); | |
| 150 writer.AppendUint32(offset); | |
| 151 writer.AppendUint32(bytes_to_read); | |
| 152 proxy_->CallMethod( | |
| 153 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 154 base::Bind(&MediaTransferProtocolDaemonClientImpl::OnReadFile, | |
| 155 weak_ptr_factory_.GetWeakPtr(), | |
| 156 callback, | |
| 157 error_callback)); | |
| 158 } | |
| 159 | |
| 160 // MediaTransferProtocolDaemonClient override. | |
| 161 virtual void ReadFileChunkById(const std::string& handle, | 118 virtual void ReadFileChunkById(const std::string& handle, |
| 162 uint32 file_id, | 119 uint32 file_id, |
| 163 uint32 offset, | 120 uint32 offset, |
| 164 uint32 bytes_to_read, | 121 uint32 bytes_to_read, |
| 165 const ReadFileCallback& callback, | 122 const ReadFileCallback& callback, |
| 166 const ErrorCallback& error_callback) OVERRIDE { | 123 const ErrorCallback& error_callback) OVERRIDE { |
| 167 DCHECK_LE(bytes_to_read, kMaxChunkSize); | 124 DCHECK_LE(bytes_to_read, kMaxChunkSize); |
| 168 dbus::MethodCall method_call(mtpd::kMtpdInterface, | 125 dbus::MethodCall method_call(mtpd::kMtpdInterface, |
| 169 mtpd::kReadFileChunkById); | 126 mtpd::kReadFileChunkById); |
| 170 dbus::MessageWriter writer(&method_call); | 127 dbus::MessageWriter writer(&method_call); |
| 171 writer.AppendString(handle); | 128 writer.AppendString(handle); |
| 172 writer.AppendUint32(file_id); | 129 writer.AppendUint32(file_id); |
| 173 writer.AppendUint32(offset); | 130 writer.AppendUint32(offset); |
| 174 writer.AppendUint32(bytes_to_read); | 131 writer.AppendUint32(bytes_to_read); |
| 175 proxy_->CallMethod( | 132 proxy_->CallMethod( |
| 176 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 133 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 177 base::Bind(&MediaTransferProtocolDaemonClientImpl::OnReadFile, | 134 base::Bind(&MediaTransferProtocolDaemonClientImpl::OnReadFile, |
| 178 weak_ptr_factory_.GetWeakPtr(), | 135 weak_ptr_factory_.GetWeakPtr(), |
| 179 callback, | 136 callback, |
| 180 error_callback)); | 137 error_callback)); |
| 181 } | 138 } |
| 182 | 139 |
| 183 // MediaTransferProtocolDaemonClient override. | 140 // MediaTransferProtocolDaemonClient override. |
| 184 virtual void GetFileInfoByPath(const std::string& handle, | |
| 185 const std::string& path, | |
| 186 const GetFileInfoCallback& callback, | |
| 187 const ErrorCallback& error_callback) OVERRIDE { | |
| 188 dbus::MethodCall method_call(mtpd::kMtpdInterface, | |
| 189 mtpd::kGetFileInfoByPath); | |
| 190 dbus::MessageWriter writer(&method_call); | |
| 191 writer.AppendString(handle); | |
| 192 writer.AppendString(path); | |
| 193 proxy_->CallMethod( | |
| 194 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 195 base::Bind(&MediaTransferProtocolDaemonClientImpl::OnGetFileInfo, | |
| 196 weak_ptr_factory_.GetWeakPtr(), | |
| 197 callback, | |
| 198 error_callback)); | |
| 199 } | |
| 200 | |
| 201 // MediaTransferProtocolDaemonClient override. | |
| 202 virtual void GetFileInfoById(const std::string& handle, | 141 virtual void GetFileInfoById(const std::string& handle, |
| 203 uint32 file_id, | 142 uint32 file_id, |
| 204 const GetFileInfoCallback& callback, | 143 const GetFileInfoCallback& callback, |
| 205 const ErrorCallback& error_callback) OVERRIDE { | 144 const ErrorCallback& error_callback) OVERRIDE { |
| 206 dbus::MethodCall method_call(mtpd::kMtpdInterface, mtpd::kGetFileInfoById); | 145 dbus::MethodCall method_call(mtpd::kMtpdInterface, mtpd::kGetFileInfoById); |
| 207 dbus::MessageWriter writer(&method_call); | 146 dbus::MessageWriter writer(&method_call); |
| 208 writer.AppendString(handle); | 147 writer.AppendString(handle); |
| 209 writer.AppendUint32(file_id); | 148 writer.AppendUint32(file_id); |
| 210 proxy_->CallMethod( | 149 proxy_->CallMethod( |
| 211 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 150 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 } | 205 } |
| 267 callback.Run(storage_names); | 206 callback.Run(storage_names); |
| 268 } | 207 } |
| 269 | 208 |
| 270 // Handles the result of GetStorageInfo and calls |callback| or | 209 // Handles the result of GetStorageInfo and calls |callback| or |
| 271 // |error_callback|. | 210 // |error_callback|. |
| 272 void OnGetStorageInfo(const std::string& storage_name, | 211 void OnGetStorageInfo(const std::string& storage_name, |
| 273 const GetStorageInfoCallback& callback, | 212 const GetStorageInfoCallback& callback, |
| 274 const ErrorCallback& error_callback, | 213 const ErrorCallback& error_callback, |
| 275 dbus::Response* response) { | 214 dbus::Response* response) { |
| 215 LOG(ERROR) << "Client OnGetStorageInfo " << storage_name; |
| 276 if (!response) { | 216 if (!response) { |
| 277 error_callback.Run(); | 217 error_callback.Run(); |
| 278 return; | 218 return; |
| 279 } | 219 } |
| 280 | 220 |
| 281 dbus::MessageReader reader(response); | 221 dbus::MessageReader reader(response); |
| 282 MtpStorageInfo protobuf; | 222 MtpStorageInfo protobuf; |
| 283 if (!reader.PopArrayOfBytesAsProto(&protobuf)) { | 223 if (!reader.PopArrayOfBytesAsProto(&protobuf)) { |
| 284 LOG(ERROR) << kInvalidResponseMsg << response->ToString(); | 224 LOG(ERROR) << kInvalidResponseMsg << response->ToString(); |
| 285 error_callback.Run(); | 225 error_callback.Run(); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 311 void OnCloseStorage(const CloseStorageCallback& callback, | 251 void OnCloseStorage(const CloseStorageCallback& callback, |
| 312 const ErrorCallback& error_callback, | 252 const ErrorCallback& error_callback, |
| 313 dbus::Response* response) { | 253 dbus::Response* response) { |
| 314 if (!response) { | 254 if (!response) { |
| 315 error_callback.Run(); | 255 error_callback.Run(); |
| 316 return; | 256 return; |
| 317 } | 257 } |
| 318 callback.Run(); | 258 callback.Run(); |
| 319 } | 259 } |
| 320 | 260 |
| 321 // Handles the result of ReadDirectoryByPath/Id and calls |callback| or | 261 // Handles the result of ReadDirectoryById and calls |callback| or |
| 322 // |error_callback|. | 262 // |error_callback|. |
| 323 void OnReadDirectory(const ReadDirectoryCallback& callback, | 263 void OnReadDirectory(const ReadDirectoryCallback& callback, |
| 324 const ErrorCallback& error_callback, | 264 const ErrorCallback& error_callback, |
| 325 dbus::Response* response) { | 265 dbus::Response* response) { |
| 326 if (!response) { | 266 if (!response) { |
| 327 error_callback.Run(); | 267 error_callback.Run(); |
| 328 return; | 268 return; |
| 329 } | 269 } |
| 330 | 270 |
| 331 std::vector<MtpFileEntry> file_entries; | 271 std::vector<MtpFileEntry> file_entries; |
| 332 dbus::MessageReader reader(response); | 272 dbus::MessageReader reader(response); |
| 333 MtpFileEntries entries_protobuf; | 273 MtpFileEntries entries_protobuf; |
| 334 if (!reader.PopArrayOfBytesAsProto(&entries_protobuf)) { | 274 if (!reader.PopArrayOfBytesAsProto(&entries_protobuf)) { |
| 335 LOG(ERROR) << kInvalidResponseMsg << response->ToString(); | 275 LOG(ERROR) << kInvalidResponseMsg << response->ToString(); |
| 336 error_callback.Run(); | 276 error_callback.Run(); |
| 337 return; | 277 return; |
| 338 } | 278 } |
| 339 | 279 |
| 340 for (int i = 0; i < entries_protobuf.file_entries_size(); ++i) | 280 for (int i = 0; i < entries_protobuf.file_entries_size(); ++i) |
| 341 file_entries.push_back(entries_protobuf.file_entries(i)); | 281 file_entries.push_back(entries_protobuf.file_entries(i)); |
| 342 callback.Run(file_entries); | 282 callback.Run(file_entries); |
| 343 } | 283 } |
| 344 | 284 |
| 345 // Handles the result of ReadFileChunkByPath/Id and calls |callback| or | 285 // Handles the result of ReadFileChunkById and calls |callback| or |
| 346 // |error_callback|. | 286 // |error_callback|. |
| 347 void OnReadFile(const ReadFileCallback& callback, | 287 void OnReadFile(const ReadFileCallback& callback, |
| 348 const ErrorCallback& error_callback, | 288 const ErrorCallback& error_callback, |
| 349 dbus::Response* response) { | 289 dbus::Response* response) { |
| 350 if (!response) { | 290 if (!response) { |
| 351 error_callback.Run(); | 291 error_callback.Run(); |
| 352 return; | 292 return; |
| 353 } | 293 } |
| 354 | 294 |
| 355 const uint8* data_bytes = NULL; | 295 const uint8* data_bytes = NULL; |
| 356 size_t data_length = 0; | 296 size_t data_length = 0; |
| 357 dbus::MessageReader reader(response); | 297 dbus::MessageReader reader(response); |
| 358 if (!reader.PopArrayOfBytes(&data_bytes, &data_length)) { | 298 if (!reader.PopArrayOfBytes(&data_bytes, &data_length)) { |
| 359 error_callback.Run(); | 299 error_callback.Run(); |
| 360 return; | 300 return; |
| 361 } | 301 } |
| 362 std::string data(reinterpret_cast<const char*>(data_bytes), data_length); | 302 std::string data(reinterpret_cast<const char*>(data_bytes), data_length); |
| 363 callback.Run(data); | 303 callback.Run(data); |
| 364 } | 304 } |
| 365 | 305 |
| 366 // Handles the result of GetFileInfoByPath/Id and calls |callback| or | 306 // Handles the result of GetFileInfoById and calls |callback| or |
| 367 // |error_callback|. | 307 // |error_callback|. |
| 368 void OnGetFileInfo(const GetFileInfoCallback& callback, | 308 void OnGetFileInfo(const GetFileInfoCallback& callback, |
| 369 const ErrorCallback& error_callback, | 309 const ErrorCallback& error_callback, |
| 370 dbus::Response* response) { | 310 dbus::Response* response) { |
| 371 if (!response) { | 311 if (!response) { |
| 372 error_callback.Run(); | 312 error_callback.Run(); |
| 373 return; | 313 return; |
| 374 } | 314 } |
| 375 | 315 |
| 376 dbus::MessageReader reader(response); | 316 dbus::MessageReader reader(response); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 426 | 366 |
| 427 MediaTransferProtocolDaemonClient::~MediaTransferProtocolDaemonClient() {} | 367 MediaTransferProtocolDaemonClient::~MediaTransferProtocolDaemonClient() {} |
| 428 | 368 |
| 429 // static | 369 // static |
| 430 MediaTransferProtocolDaemonClient* MediaTransferProtocolDaemonClient::Create( | 370 MediaTransferProtocolDaemonClient* MediaTransferProtocolDaemonClient::Create( |
| 431 dbus::Bus* bus) { | 371 dbus::Bus* bus) { |
| 432 return new MediaTransferProtocolDaemonClientImpl(bus); | 372 return new MediaTransferProtocolDaemonClientImpl(bus); |
| 433 } | 373 } |
| 434 | 374 |
| 435 } // namespace device | 375 } // namespace device |
| OLD | NEW |