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

Side by Side Diff: chrome/browser/media_galleries/linux/mtp_device_delegate_impl_linux.cc

Issue 947943002: Implement CopyFileFromLocal of MTPDeviceAsyncDelegate. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase. Created 5 years, 9 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
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 "chrome/browser/media_galleries/linux/mtp_device_delegate_impl_linux.h" 5 #include "chrome/browser/media_galleries/linux/mtp_device_delegate_impl_linux.h"
6 6
7 #include <fcntl.h>
7 #include <algorithm> 8 #include <algorithm>
8 #include <vector> 9 #include <vector>
9 10
10 #include "base/bind.h" 11 #include "base/bind.h"
11 #include "base/numerics/safe_conversions.h" 12 #include "base/numerics/safe_conversions.h"
13 #include "base/posix/eintr_wrapper.h"
12 #include "base/strings/string_number_conversions.h" 14 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_split.h" 15 #include "base/strings/string_split.h"
14 #include "base/strings/string_util.h" 16 #include "base/strings/string_util.h"
15 #include "chrome/browser/media_galleries/linux/mtp_device_task_helper.h" 17 #include "chrome/browser/media_galleries/linux/mtp_device_task_helper.h"
16 #include "chrome/browser/media_galleries/linux/mtp_device_task_helper_map_servic e.h" 18 #include "chrome/browser/media_galleries/linux/mtp_device_task_helper_map_servic e.h"
17 #include "chrome/browser/media_galleries/linux/snapshot_file_details.h" 19 #include "chrome/browser/media_galleries/linux/snapshot_file_details.h"
18 #include "net/base/io_buffer.h" 20 #include "net/base/io_buffer.h"
19 #include "third_party/cros_system_api/dbus/service_constants.h" 21 #include "third_party/cros_system_api/dbus/service_constants.h"
20 22
21 namespace { 23 namespace {
(...skipping 22 matching lines...) Expand all
44 result = relative_path.value(); 46 result = relative_path.value();
45 } 47 }
46 } 48 }
47 return result; 49 return result;
48 } 50 }
49 51
50 // Returns the MTPDeviceTaskHelper object associated with the MTP device 52 // Returns the MTPDeviceTaskHelper object associated with the MTP device
51 // storage. 53 // storage.
52 // 54 //
53 // |storage_name| specifies the name of the storage device. 55 // |storage_name| specifies the name of the storage device.
56 // |read_only| specifies the mode of the storage device.
54 // Returns NULL if the |storage_name| is no longer valid (e.g. because the 57 // Returns NULL if the |storage_name| is no longer valid (e.g. because the
55 // corresponding storage device is detached, etc). 58 // corresponding storage device is detached, etc).
56 MTPDeviceTaskHelper* GetDeviceTaskHelperForStorage( 59 MTPDeviceTaskHelper* GetDeviceTaskHelperForStorage(
57 const std::string& storage_name) { 60 const std::string& storage_name,
61 const bool read_only) {
58 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 62 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
59 return MTPDeviceTaskHelperMapService::GetInstance()->GetDeviceTaskHelper( 63 return MTPDeviceTaskHelperMapService::GetInstance()->GetDeviceTaskHelper(
60 storage_name); 64 storage_name,
65 read_only);
61 } 66 }
62 67
63 // Opens the storage device for communication. 68 // Opens the storage device for communication.
64 // 69 //
65 // Called on the UI thread to dispatch the request to the 70 // Called on the UI thread to dispatch the request to the
66 // MediaTransferProtocolManager. 71 // MediaTransferProtocolManager.
67 // 72 //
68 // |storage_name| specifies the name of the storage device. 73 // |storage_name| specifies the name of the storage device.
74 // |read_only| specifies the mode of the storage device.
69 // |reply_callback| is called when the OpenStorage request completes. 75 // |reply_callback| is called when the OpenStorage request completes.
70 // |reply_callback| runs on the IO thread. 76 // |reply_callback| runs on the IO thread.
71 void OpenStorageOnUIThread( 77 void OpenStorageOnUIThread(
72 const std::string& storage_name, 78 const std::string& storage_name,
79 const bool read_only,
73 const MTPDeviceTaskHelper::OpenStorageCallback& reply_callback) { 80 const MTPDeviceTaskHelper::OpenStorageCallback& reply_callback) {
74 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 81 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
75 MTPDeviceTaskHelper* task_helper = 82 MTPDeviceTaskHelper* task_helper =
76 GetDeviceTaskHelperForStorage(storage_name); 83 GetDeviceTaskHelperForStorage(storage_name, read_only);
77 if (!task_helper) { 84 if (!task_helper) {
78 task_helper = 85 task_helper =
79 MTPDeviceTaskHelperMapService::GetInstance()->CreateDeviceTaskHelper( 86 MTPDeviceTaskHelperMapService::GetInstance()->CreateDeviceTaskHelper(
80 storage_name); 87 storage_name, read_only);
81 } 88 }
82 task_helper->OpenStorage(storage_name, reply_callback); 89 task_helper->OpenStorage(storage_name, read_only, reply_callback);
83 } 90 }
84 91
85 // Enumerates the |dir_id| directory file entries. 92 // Enumerates the |dir_id| directory file entries.
86 // 93 //
87 // Called on the UI thread to dispatch the request to the 94 // Called on the UI thread to dispatch the request to the
88 // MediaTransferProtocolManager. 95 // MediaTransferProtocolManager.
89 // 96 //
90 // |storage_name| specifies the name of the storage device. 97 // |storage_name| specifies the name of the storage device.
98 // |read_only| specifies the mode of the storage device.
91 // |success_callback| is called when the ReadDirectory request succeeds. 99 // |success_callback| is called when the ReadDirectory request succeeds.
92 // |error_callback| is called when the ReadDirectory request fails. 100 // |error_callback| is called when the ReadDirectory request fails.
93 // |success_callback| and |error_callback| runs on the IO thread. 101 // |success_callback| and |error_callback| runs on the IO thread.
94 void ReadDirectoryOnUIThread( 102 void ReadDirectoryOnUIThread(
95 const std::string& storage_name, 103 const std::string& storage_name,
104 const bool read_only,
96 uint32 dir_id, 105 uint32 dir_id,
97 const MTPDeviceTaskHelper::ReadDirectorySuccessCallback& success_callback, 106 const MTPDeviceTaskHelper::ReadDirectorySuccessCallback& success_callback,
98 const MTPDeviceTaskHelper::ErrorCallback& error_callback) { 107 const MTPDeviceTaskHelper::ErrorCallback& error_callback) {
99 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 108 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
100 MTPDeviceTaskHelper* task_helper = 109 MTPDeviceTaskHelper* task_helper =
101 GetDeviceTaskHelperForStorage(storage_name); 110 GetDeviceTaskHelperForStorage(storage_name, read_only);
102 if (!task_helper) 111 if (!task_helper)
103 return; 112 return;
104 task_helper->ReadDirectory(dir_id, success_callback, error_callback); 113 task_helper->ReadDirectory(dir_id, success_callback, error_callback);
105 } 114 }
106 115
107 // Gets the |file_path| details. 116 // Gets the |file_path| details.
108 // 117 //
109 // Called on the UI thread to dispatch the request to the 118 // Called on the UI thread to dispatch the request to the
110 // MediaTransferProtocolManager. 119 // MediaTransferProtocolManager.
111 // 120 //
112 // |storage_name| specifies the name of the storage device. 121 // |storage_name| specifies the name of the storage device.
122 // |read_only| specifies the mode of the storage device.
113 // |success_callback| is called when the GetFileInfo request succeeds. 123 // |success_callback| is called when the GetFileInfo request succeeds.
114 // |error_callback| is called when the GetFileInfo request fails. 124 // |error_callback| is called when the GetFileInfo request fails.
115 // |success_callback| and |error_callback| runs on the IO thread. 125 // |success_callback| and |error_callback| runs on the IO thread.
116 void GetFileInfoOnUIThread( 126 void GetFileInfoOnUIThread(
117 const std::string& storage_name, 127 const std::string& storage_name,
128 const bool read_only,
118 uint32 file_id, 129 uint32 file_id,
119 const MTPDeviceTaskHelper::GetFileInfoSuccessCallback& success_callback, 130 const MTPDeviceTaskHelper::GetFileInfoSuccessCallback& success_callback,
120 const MTPDeviceTaskHelper::ErrorCallback& error_callback) { 131 const MTPDeviceTaskHelper::ErrorCallback& error_callback) {
121 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 132 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
122 MTPDeviceTaskHelper* task_helper = 133 MTPDeviceTaskHelper* task_helper =
123 GetDeviceTaskHelperForStorage(storage_name); 134 GetDeviceTaskHelperForStorage(storage_name, read_only);
124 if (!task_helper) 135 if (!task_helper)
125 return; 136 return;
126 task_helper->GetFileInfo(file_id, success_callback, error_callback); 137 task_helper->GetFileInfo(file_id, success_callback, error_callback);
127 } 138 }
128 139
129 // Copies the contents of |device_file_path| to |snapshot_file_path|. 140 // Copies the contents of |device_file_path| to |snapshot_file_path|.
130 // 141 //
131 // Called on the UI thread to dispatch the request to the 142 // Called on the UI thread to dispatch the request to the
132 // MediaTransferProtocolManager. 143 // MediaTransferProtocolManager.
133 // 144 //
134 // |storage_name| specifies the name of the storage device. 145 // |storage_name| specifies the name of the storage device.
146 // |read_only| specifies the mode of the storage device.
135 // |device_file_path| specifies the media device file path. 147 // |device_file_path| specifies the media device file path.
136 // |snapshot_file_path| specifies the platform path of the snapshot file. 148 // |snapshot_file_path| specifies the platform path of the snapshot file.
137 // |file_size| specifies the number of bytes that will be written to the 149 // |file_size| specifies the number of bytes that will be written to the
138 // snapshot file. 150 // snapshot file.
139 // |success_callback| is called when the copy operation succeeds. 151 // |success_callback| is called when the copy operation succeeds.
140 // |error_callback| is called when the copy operation fails. 152 // |error_callback| is called when the copy operation fails.
141 // |success_callback| and |error_callback| runs on the IO thread. 153 // |success_callback| and |error_callback| runs on the IO thread.
142 void WriteDataIntoSnapshotFileOnUIThread( 154 void WriteDataIntoSnapshotFileOnUIThread(
143 const std::string& storage_name, 155 const std::string& storage_name,
156 const bool read_only,
144 const SnapshotRequestInfo& request_info, 157 const SnapshotRequestInfo& request_info,
145 const base::File::Info& snapshot_file_info) { 158 const base::File::Info& snapshot_file_info) {
146 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 159 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
147 MTPDeviceTaskHelper* task_helper = 160 MTPDeviceTaskHelper* task_helper =
148 GetDeviceTaskHelperForStorage(storage_name); 161 GetDeviceTaskHelperForStorage(storage_name, read_only);
149 if (!task_helper) 162 if (!task_helper)
150 return; 163 return;
151 task_helper->WriteDataIntoSnapshotFile(request_info, snapshot_file_info); 164 task_helper->WriteDataIntoSnapshotFile(request_info, snapshot_file_info);
152 } 165 }
153 166
154 // Copies the contents of |device_file_path| to |snapshot_file_path|. 167 // Copies the contents of |device_file_path| to |snapshot_file_path|.
155 // 168 //
156 // Called on the UI thread to dispatch the request to the 169 // Called on the UI thread to dispatch the request to the
157 // MediaTransferProtocolManager. 170 // MediaTransferProtocolManager.
158 // 171 //
159 // |storage_name| specifies the name of the storage device. 172 // |storage_name| specifies the name of the storage device.
173 // |read_only| specifies the mode of the storage device.
160 // |request| is a struct containing details about the byte read request. 174 // |request| is a struct containing details about the byte read request.
161 void ReadBytesOnUIThread( 175 void ReadBytesOnUIThread(
162 const std::string& storage_name, 176 const std::string& storage_name,
177 const bool read_only,
163 const MTPDeviceAsyncDelegate::ReadBytesRequest& request) { 178 const MTPDeviceAsyncDelegate::ReadBytesRequest& request) {
164 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 179 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
165 MTPDeviceTaskHelper* task_helper = 180 MTPDeviceTaskHelper* task_helper =
166 GetDeviceTaskHelperForStorage(storage_name); 181 GetDeviceTaskHelperForStorage(storage_name, read_only);
167 if (!task_helper) 182 if (!task_helper)
168 return; 183 return;
169 task_helper->ReadBytes(request); 184 task_helper->ReadBytes(request);
170 } 185 }
171 186
187 // Copies the file |source_file_descriptor| to |file_name| in |parent_id|.
188 //
189 // |storage_name| specifies the name of the storage device.
190 // |read_only| specifies the mode of the storage device.
191 // |source_file_descriptor| file descriptor of source file.
192 // |parent_id| object id of a target directory.
193 // |file_name| file name of a target file.
194 // |success_callback| is called when the file is copied successfully.
195 // |error_callback| is called when it fails to copy file.
196 // Since this method does not close the file descriptor, callbacks are
197 // responsible for closing it.
198 void CopyFileFromLocalOnUIThread(
199 const std::string& storage_name,
200 const bool read_only,
201 const int source_file_descriptor,
202 const uint32 parent_id,
203 const std::string& file_name,
204 const MTPDeviceTaskHelper::CopyFileFromLocalSuccessCallback&
205 success_callback,
206 const MTPDeviceTaskHelper::ErrorCallback& error_callback) {
207 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
208 MTPDeviceTaskHelper* task_helper =
209 GetDeviceTaskHelperForStorage(storage_name, read_only);
210 if (!task_helper)
211 return;
212 task_helper->CopyFileFromLocal(storage_name, source_file_descriptor,
213 parent_id, file_name, success_callback,
214 error_callback);
215 }
216
172 // Closes the device storage specified by the |storage_name| and destroys the 217 // Closes the device storage specified by the |storage_name| and destroys the
173 // MTPDeviceTaskHelper object associated with the device storage. 218 // MTPDeviceTaskHelper object associated with the device storage.
174 // 219 //
175 // Called on the UI thread to dispatch the request to the 220 // Called on the UI thread to dispatch the request to the
176 // MediaTransferProtocolManager. 221 // MediaTransferProtocolManager.
177 void CloseStorageAndDestroyTaskHelperOnUIThread( 222 void CloseStorageAndDestroyTaskHelperOnUIThread(
178 const std::string& storage_name) { 223 const std::string& storage_name,
224 const bool read_only) {
179 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 225 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
180 MTPDeviceTaskHelper* task_helper = 226 MTPDeviceTaskHelper* task_helper =
181 GetDeviceTaskHelperForStorage(storage_name); 227 GetDeviceTaskHelperForStorage(storage_name, read_only);
182 if (!task_helper) 228 if (!task_helper)
183 return; 229 return;
184 task_helper->CloseStorage(); 230 task_helper->CloseStorage();
185 MTPDeviceTaskHelperMapService::GetInstance()->DestroyDeviceTaskHelper( 231 MTPDeviceTaskHelperMapService::GetInstance()->DestroyDeviceTaskHelper(
186 storage_name); 232 storage_name, read_only);
233 }
234
235 // Opens |file_path| with |flags|.
236 int OpenFileDescriptor(const char* file_path, const int flags) {
237 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE);
238
239 return open(file_path, flags);
240 }
241
242 // Closes |file_descriptor| on file thread.
243 void CloseFileDescriptor(const int file_descriptor) {
244 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE);
245
246 IGNORE_EINTR(close(file_descriptor));
187 } 247 }
188 248
189 } // namespace 249 } // namespace
190 250
191 MTPDeviceDelegateImplLinux::PendingTaskInfo::PendingTaskInfo( 251 MTPDeviceDelegateImplLinux::PendingTaskInfo::PendingTaskInfo(
192 const base::FilePath& path, 252 const base::FilePath& path,
193 content::BrowserThread::ID thread_id, 253 content::BrowserThread::ID thread_id,
194 const tracked_objects::Location& location, 254 const tracked_objects::Location& location,
195 const base::Closure& task) 255 const base::Closure& task)
196 : path(path), 256 : path(path),
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
303 it != children_.end(); ++it) { 363 it != children_.end(); ++it) {
304 if (it->second->file_id() == file_id) { 364 if (it->second->file_id() == file_id) {
305 children_.erase(it); 365 children_.erase(it);
306 return true; 366 return true;
307 } 367 }
308 } 368 }
309 return false; 369 return false;
310 } 370 }
311 371
312 MTPDeviceDelegateImplLinux::MTPDeviceDelegateImplLinux( 372 MTPDeviceDelegateImplLinux::MTPDeviceDelegateImplLinux(
313 const std::string& device_location) 373 const std::string& device_location,
374 const bool read_only)
314 : init_state_(UNINITIALIZED), 375 : init_state_(UNINITIALIZED),
315 task_in_progress_(false), 376 task_in_progress_(false),
316 device_path_(device_location), 377 device_path_(device_location),
378 read_only_(read_only),
317 root_node_(new MTPFileNode(mtpd::kRootFileId, 379 root_node_(new MTPFileNode(mtpd::kRootFileId,
318 "", // Root node has no name. 380 "", // Root node has no name.
319 NULL, // And no parent node. 381 NULL, // And no parent node.
320 &file_id_to_node_map_)), 382 &file_id_to_node_map_)),
321 weak_ptr_factory_(this) { 383 weak_ptr_factory_(this) {
322 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); 384 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
323 DCHECK(!device_path_.empty()); 385 DCHECK(!device_path_.empty());
324 base::RemoveChars(device_location, kRootPath, &storage_name_); 386 base::RemoveChars(device_location, kRootPath, &storage_name_);
325 DCHECK(!storage_name_.empty()); 387 DCHECK(!storage_name_.empty());
326 } 388 }
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
425 offset, 487 offset,
426 buf_len, 488 buf_len,
427 success_callback, 489 success_callback,
428 error_callback); 490 error_callback);
429 EnsureInitAndRunTask(PendingTaskInfo(device_file_path, 491 EnsureInitAndRunTask(PendingTaskInfo(device_file_path,
430 content::BrowserThread::IO, 492 content::BrowserThread::IO,
431 FROM_HERE, 493 FROM_HERE,
432 closure)); 494 closure));
433 } 495 }
434 496
497 bool MTPDeviceDelegateImplLinux::IsReadOnly() {
498 return read_only_;
499 }
500
501 void MTPDeviceDelegateImplLinux::CopyFileFromLocal(
502 const base::FilePath& source_file_path,
503 const base::FilePath& device_file_path,
504 const CopyFileFromLocalSuccessCallback& success_callback,
505 const ErrorCallback& error_callback) {
506 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
507 DCHECK(!source_file_path.empty());
508 DCHECK(!device_file_path.empty());
509
510 content::BrowserThread::PostTaskAndReplyWithResult(
511 content::BrowserThread::FILE,
512 FROM_HERE,
513 base::Bind(&OpenFileDescriptor,
514 source_file_path.value().c_str(),
515 O_RDONLY),
516 base::Bind(&MTPDeviceDelegateImplLinux::CopyFileFromLocalInternal,
517 weak_ptr_factory_.GetWeakPtr(),
518 device_file_path,
519 success_callback,
520 error_callback));
521 }
522
435 void MTPDeviceDelegateImplLinux::CancelPendingTasksAndDeleteDelegate() { 523 void MTPDeviceDelegateImplLinux::CancelPendingTasksAndDeleteDelegate() {
436 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); 524 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
437 // To cancel all the pending tasks, destroy the MTPDeviceTaskHelper object. 525 // To cancel all the pending tasks, destroy the MTPDeviceTaskHelper object.
438 content::BrowserThread::PostTask( 526 content::BrowserThread::PostTask(
439 content::BrowserThread::UI, 527 content::BrowserThread::UI,
440 FROM_HERE, 528 FROM_HERE,
441 base::Bind(&CloseStorageAndDestroyTaskHelperOnUIThread, storage_name_)); 529 base::Bind(&CloseStorageAndDestroyTaskHelperOnUIThread,
530 storage_name_,
531 read_only_));
442 delete this; 532 delete this;
443 } 533 }
444 534
445 void MTPDeviceDelegateImplLinux::GetFileInfoInternal( 535 void MTPDeviceDelegateImplLinux::GetFileInfoInternal(
446 const base::FilePath& file_path, 536 const base::FilePath& file_path,
447 const GetFileInfoSuccessCallback& success_callback, 537 const GetFileInfoSuccessCallback& success_callback,
448 const ErrorCallback& error_callback) { 538 const ErrorCallback& error_callback) {
449 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); 539 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
450 540
451 uint32 file_id; 541 uint32 file_id;
452 if (CachedPathToId(file_path, &file_id)) { 542 if (CachedPathToId(file_path, &file_id)) {
453 GetFileInfoSuccessCallback success_callback_wrapper = 543 GetFileInfoSuccessCallback success_callback_wrapper =
454 base::Bind(&MTPDeviceDelegateImplLinux::OnDidGetFileInfo, 544 base::Bind(&MTPDeviceDelegateImplLinux::OnDidGetFileInfo,
455 weak_ptr_factory_.GetWeakPtr(), 545 weak_ptr_factory_.GetWeakPtr(),
456 success_callback); 546 success_callback);
457 ErrorCallback error_callback_wrapper = 547 ErrorCallback error_callback_wrapper =
458 base::Bind(&MTPDeviceDelegateImplLinux::HandleDeviceFileError, 548 base::Bind(&MTPDeviceDelegateImplLinux::HandleDeviceFileError,
459 weak_ptr_factory_.GetWeakPtr(), 549 weak_ptr_factory_.GetWeakPtr(),
460 error_callback, 550 error_callback,
461 file_id); 551 file_id);
462 552
463 553
464 base::Closure closure = base::Bind(&GetFileInfoOnUIThread, 554 base::Closure closure = base::Bind(&GetFileInfoOnUIThread,
465 storage_name_, 555 storage_name_,
556 read_only_,
466 file_id, 557 file_id,
467 success_callback_wrapper, 558 success_callback_wrapper,
468 error_callback_wrapper); 559 error_callback_wrapper);
469 EnsureInitAndRunTask(PendingTaskInfo(base::FilePath(), 560 EnsureInitAndRunTask(PendingTaskInfo(base::FilePath(),
470 content::BrowserThread::UI, 561 content::BrowserThread::UI,
471 FROM_HERE, 562 FROM_HERE,
472 closure)); 563 closure));
473 } else { 564 } else {
474 error_callback.Run(base::File::FILE_ERROR_NOT_FOUND); 565 error_callback.Run(base::File::FILE_ERROR_NOT_FOUND);
475 } 566 }
(...skipping 14 matching lines...) Expand all
490 dir_id, 581 dir_id,
491 success_callback, 582 success_callback,
492 error_callback); 583 error_callback);
493 ErrorCallback error_callback_wrapper = 584 ErrorCallback error_callback_wrapper =
494 base::Bind(&MTPDeviceDelegateImplLinux::HandleDeviceFileError, 585 base::Bind(&MTPDeviceDelegateImplLinux::HandleDeviceFileError,
495 weak_ptr_factory_.GetWeakPtr(), 586 weak_ptr_factory_.GetWeakPtr(),
496 error_callback, 587 error_callback,
497 dir_id); 588 dir_id);
498 base::Closure closure = base::Bind(&GetFileInfoOnUIThread, 589 base::Closure closure = base::Bind(&GetFileInfoOnUIThread,
499 storage_name_, 590 storage_name_,
591 read_only_,
500 dir_id, 592 dir_id,
501 success_callback_wrapper, 593 success_callback_wrapper,
502 error_callback_wrapper); 594 error_callback_wrapper);
503 EnsureInitAndRunTask(PendingTaskInfo(base::FilePath(), 595 EnsureInitAndRunTask(PendingTaskInfo(base::FilePath(),
504 content::BrowserThread::UI, 596 content::BrowserThread::UI,
505 FROM_HERE, 597 FROM_HERE,
506 closure)); 598 closure));
507 } else { 599 } else {
508 error_callback.Run(base::File::FILE_ERROR_NOT_FOUND); 600 error_callback.Run(base::File::FILE_ERROR_NOT_FOUND);
509 } 601 }
(...skipping 19 matching lines...) Expand all
529 &MTPDeviceDelegateImplLinux::OnDidGetFileInfoToCreateSnapshotFile, 621 &MTPDeviceDelegateImplLinux::OnDidGetFileInfoToCreateSnapshotFile,
530 weak_ptr_factory_.GetWeakPtr(), 622 weak_ptr_factory_.GetWeakPtr(),
531 base::Passed(&request_info)); 623 base::Passed(&request_info));
532 ErrorCallback error_callback_wrapper = 624 ErrorCallback error_callback_wrapper =
533 base::Bind(&MTPDeviceDelegateImplLinux::HandleDeviceFileError, 625 base::Bind(&MTPDeviceDelegateImplLinux::HandleDeviceFileError,
534 weak_ptr_factory_.GetWeakPtr(), 626 weak_ptr_factory_.GetWeakPtr(),
535 error_callback, 627 error_callback,
536 file_id); 628 file_id);
537 base::Closure closure = base::Bind(&GetFileInfoOnUIThread, 629 base::Closure closure = base::Bind(&GetFileInfoOnUIThread,
538 storage_name_, 630 storage_name_,
631 read_only_,
539 file_id, 632 file_id,
540 success_callback_wrapper, 633 success_callback_wrapper,
541 error_callback_wrapper); 634 error_callback_wrapper);
542 EnsureInitAndRunTask(PendingTaskInfo(base::FilePath(), 635 EnsureInitAndRunTask(PendingTaskInfo(base::FilePath(),
543 content::BrowserThread::UI, 636 content::BrowserThread::UI,
544 FROM_HERE, 637 FROM_HERE,
545 closure)); 638 closure));
546 } else { 639 } else {
547 error_callback.Run(base::File::FILE_ERROR_NOT_FOUND); 640 error_callback.Run(base::File::FILE_ERROR_NOT_FOUND);
548 } 641 }
(...skipping 13 matching lines...) Expand all
562 file_id, buf, offset, buf_len, 655 file_id, buf, offset, buf_len,
563 base::Bind(&MTPDeviceDelegateImplLinux::OnDidReadBytes, 656 base::Bind(&MTPDeviceDelegateImplLinux::OnDidReadBytes,
564 weak_ptr_factory_.GetWeakPtr(), 657 weak_ptr_factory_.GetWeakPtr(),
565 success_callback), 658 success_callback),
566 base::Bind(&MTPDeviceDelegateImplLinux::HandleDeviceFileError, 659 base::Bind(&MTPDeviceDelegateImplLinux::HandleDeviceFileError,
567 weak_ptr_factory_.GetWeakPtr(), 660 weak_ptr_factory_.GetWeakPtr(),
568 error_callback, 661 error_callback,
569 file_id)); 662 file_id));
570 663
571 base::Closure closure = 664 base::Closure closure =
572 base::Bind(base::Bind(&ReadBytesOnUIThread, storage_name_, request)); 665 base::Bind(&ReadBytesOnUIThread, storage_name_, read_only_, request);
573 EnsureInitAndRunTask(PendingTaskInfo(base::FilePath(), 666 EnsureInitAndRunTask(PendingTaskInfo(base::FilePath(),
574 content::BrowserThread::UI, 667 content::BrowserThread::UI,
575 FROM_HERE, 668 FROM_HERE,
576 closure)); 669 closure));
577 } else { 670 } else {
578 error_callback.Run(base::File::FILE_ERROR_NOT_FOUND); 671 error_callback.Run(base::File::FILE_ERROR_NOT_FOUND);
579 } 672 }
580 PendingRequestDone(); 673 PendingRequestDone();
581 } 674 }
582 675
676 void MTPDeviceDelegateImplLinux::CopyFileFromLocalInternal(
677 const base::FilePath& device_file_path,
678 const CopyFileFromLocalSuccessCallback& success_callback,
679 const ErrorCallback& error_callback,
680 const int source_file_descriptor) {
681 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
682
683 if (source_file_descriptor < 0) {
684 error_callback.Run(base::File::FILE_ERROR_INVALID_OPERATION);
685 PendingRequestDone();
686 return;
687 }
688
689 uint32 parent_id;
690 if (CachedPathToId(device_file_path.DirName(), &parent_id)) {
691 CopyFileFromLocalSuccessCallback success_callback_wrapper =
692 base::Bind(&MTPDeviceDelegateImplLinux::OnDidCopyFileFromLocal,
693 weak_ptr_factory_.GetWeakPtr(), success_callback,
694 source_file_descriptor);
695
696 ErrorCallback error_callback_wrapper = base::Bind(
697 &MTPDeviceDelegateImplLinux::HandleCopyFileFromLocalError,
698 weak_ptr_factory_.GetWeakPtr(), error_callback, source_file_descriptor);
699
700 base::Closure closure = base::Bind(&CopyFileFromLocalOnUIThread,
701 storage_name_,
702 read_only_,
703 source_file_descriptor,
704 parent_id,
705 device_file_path.BaseName().value(),
706 success_callback_wrapper,
707 error_callback_wrapper);
708
709 EnsureInitAndRunTask(PendingTaskInfo(
710 base::FilePath(), content::BrowserThread::UI, FROM_HERE, closure));
711 } else {
712 HandleCopyFileFromLocalError(error_callback, source_file_descriptor,
713 base::File::FILE_ERROR_INVALID_OPERATION);
714 }
715
716 PendingRequestDone();
717 }
718
583 void MTPDeviceDelegateImplLinux::EnsureInitAndRunTask( 719 void MTPDeviceDelegateImplLinux::EnsureInitAndRunTask(
584 const PendingTaskInfo& task_info) { 720 const PendingTaskInfo& task_info) {
585 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); 721 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
586 if ((init_state_ == INITIALIZED) && !task_in_progress_) { 722 if ((init_state_ == INITIALIZED) && !task_in_progress_) {
587 RunTask(task_info); 723 RunTask(task_info);
588 return; 724 return;
589 } 725 }
590 726
591 // Only *Internal functions have empty paths. Since they are the continuation 727 // Only *Internal functions have empty paths. Since they are the continuation
592 // of the current running task, they get to cut in line. 728 // of the current running task, they get to cut in line.
593 if (task_info.path.empty()) 729 if (task_info.path.empty())
594 pending_tasks_.push_front(task_info); 730 pending_tasks_.push_front(task_info);
595 else 731 else
596 pending_tasks_.push_back(task_info); 732 pending_tasks_.push_back(task_info);
597 733
598 if (init_state_ == UNINITIALIZED) { 734 if (init_state_ == UNINITIALIZED) {
599 init_state_ = PENDING_INIT; 735 init_state_ = PENDING_INIT;
600 task_in_progress_ = true; 736 task_in_progress_ = true;
601 content::BrowserThread::PostTask( 737 content::BrowserThread::PostTask(
602 content::BrowserThread::UI, 738 content::BrowserThread::UI, FROM_HERE,
603 FROM_HERE, 739 base::Bind(&OpenStorageOnUIThread, storage_name_, read_only_,
604 base::Bind(&OpenStorageOnUIThread,
605 storage_name_,
606 base::Bind(&MTPDeviceDelegateImplLinux::OnInitCompleted, 740 base::Bind(&MTPDeviceDelegateImplLinux::OnInitCompleted,
607 weak_ptr_factory_.GetWeakPtr()))); 741 weak_ptr_factory_.GetWeakPtr())));
608 } 742 }
609 } 743 }
610 744
611 void MTPDeviceDelegateImplLinux::RunTask(const PendingTaskInfo& task_info) { 745 void MTPDeviceDelegateImplLinux::RunTask(const PendingTaskInfo& task_info) {
612 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); 746 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
613 DCHECK_EQ(INITIALIZED, init_state_); 747 DCHECK_EQ(INITIALIZED, init_state_);
614 DCHECK(!task_in_progress_); 748 DCHECK(!task_in_progress_);
615 task_in_progress_ = true; 749 task_in_progress_ = true;
(...skipping 26 matching lines...) Expand all
642 current_snapshot_request_info_->snapshot_file_path, 776 current_snapshot_request_info_->snapshot_file_path,
643 base::Bind( 777 base::Bind(
644 &MTPDeviceDelegateImplLinux::OnDidWriteDataIntoSnapshotFile, 778 &MTPDeviceDelegateImplLinux::OnDidWriteDataIntoSnapshotFile,
645 weak_ptr_factory_.GetWeakPtr()), 779 weak_ptr_factory_.GetWeakPtr()),
646 base::Bind( 780 base::Bind(
647 &MTPDeviceDelegateImplLinux::OnWriteDataIntoSnapshotFileError, 781 &MTPDeviceDelegateImplLinux::OnWriteDataIntoSnapshotFileError,
648 weak_ptr_factory_.GetWeakPtr())); 782 weak_ptr_factory_.GetWeakPtr()));
649 783
650 base::Closure task_closure = base::Bind(&WriteDataIntoSnapshotFileOnUIThread, 784 base::Closure task_closure = base::Bind(&WriteDataIntoSnapshotFileOnUIThread,
651 storage_name_, 785 storage_name_,
786 read_only_,
652 request_info, 787 request_info,
653 file_info); 788 file_info);
654 content::BrowserThread::PostTask(content::BrowserThread::UI, 789 content::BrowserThread::PostTask(content::BrowserThread::UI,
655 FROM_HERE, 790 FROM_HERE,
656 task_closure); 791 task_closure);
657 } 792 }
658 793
659 void MTPDeviceDelegateImplLinux::PendingRequestDone() { 794 void MTPDeviceDelegateImplLinux::PendingRequestDone() {
795 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
660 DCHECK(task_in_progress_); 796 DCHECK(task_in_progress_);
661 task_in_progress_ = false; 797 task_in_progress_ = false;
662 ProcessNextPendingRequest(); 798 ProcessNextPendingRequest();
663 } 799 }
664 800
665 void MTPDeviceDelegateImplLinux::ProcessNextPendingRequest() { 801 void MTPDeviceDelegateImplLinux::ProcessNextPendingRequest() {
666 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); 802 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
667 DCHECK(!task_in_progress_); 803 DCHECK(!task_in_progress_);
668 if (pending_tasks_.empty()) 804 if (pending_tasks_.empty())
669 return; 805 return;
(...skipping 26 matching lines...) Expand all
696 DCHECK(task_in_progress_); 832 DCHECK(task_in_progress_);
697 if (!file_info.is_directory) { 833 if (!file_info.is_directory) {
698 return HandleDeviceFileError(error_callback, 834 return HandleDeviceFileError(error_callback,
699 dir_id, 835 dir_id,
700 base::File::FILE_ERROR_NOT_A_DIRECTORY); 836 base::File::FILE_ERROR_NOT_A_DIRECTORY);
701 } 837 }
702 838
703 base::Closure task_closure = 839 base::Closure task_closure =
704 base::Bind(&ReadDirectoryOnUIThread, 840 base::Bind(&ReadDirectoryOnUIThread,
705 storage_name_, 841 storage_name_,
842 read_only_,
706 dir_id, 843 dir_id,
707 base::Bind(&MTPDeviceDelegateImplLinux::OnDidReadDirectory, 844 base::Bind(&MTPDeviceDelegateImplLinux::OnDidReadDirectory,
708 weak_ptr_factory_.GetWeakPtr(), 845 weak_ptr_factory_.GetWeakPtr(),
709 dir_id, 846 dir_id,
710 success_callback), 847 success_callback),
711 base::Bind(&MTPDeviceDelegateImplLinux::HandleDeviceFileError, 848 base::Bind(&MTPDeviceDelegateImplLinux::HandleDeviceFileError,
712 weak_ptr_factory_.GetWeakPtr(), 849 weak_ptr_factory_.GetWeakPtr(),
713 error_callback, 850 error_callback,
714 dir_id)); 851 dir_id));
715 content::BrowserThread::PostTask(content::BrowserThread::UI, 852 content::BrowserThread::PostTask(content::BrowserThread::UI,
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
847 984
848 void MTPDeviceDelegateImplLinux::OnFillFileCacheFailed( 985 void MTPDeviceDelegateImplLinux::OnFillFileCacheFailed(
849 base::File::Error /* error */) { 986 base::File::Error /* error */) {
850 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); 987 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
851 // When filling the cache fails for the task at the front of the queue, clear 988 // When filling the cache fails for the task at the front of the queue, clear
852 // the path of the task so it will not try to do any more caching. Instead, 989 // the path of the task so it will not try to do any more caching. Instead,
853 // the task will just run and fail the CachedPathToId() lookup. 990 // the task will just run and fail the CachedPathToId() lookup.
854 pending_tasks_.front().path.clear(); 991 pending_tasks_.front().path.clear();
855 } 992 }
856 993
994 void MTPDeviceDelegateImplLinux::OnDidCopyFileFromLocal(
995 const CopyFileFromLocalSuccessCallback& success_callback,
996 const int source_file_descriptor) {
997 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
998
999 const base::Closure closure = base::Bind(&CloseFileDescriptor,
1000 source_file_descriptor);
1001
1002 content::BrowserThread::PostTask(content::BrowserThread::FILE, FROM_HERE,
1003 closure);
1004
1005 success_callback.Run();
1006 PendingRequestDone();
1007 }
1008
1009 void MTPDeviceDelegateImplLinux::HandleCopyFileFromLocalError(
1010 const ErrorCallback& error_callback,
1011 const int source_file_descriptor,
1012 base::File::Error error) {
1013 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
1014
1015 const base::Closure closure = base::Bind(&CloseFileDescriptor,
1016 source_file_descriptor);
1017
1018 content::BrowserThread::PostTask(content::BrowserThread::FILE, FROM_HERE,
1019 closure);
1020
1021 error_callback.Run(error);
1022 PendingRequestDone();
1023 }
1024
857 void MTPDeviceDelegateImplLinux::HandleDeviceFileError( 1025 void MTPDeviceDelegateImplLinux::HandleDeviceFileError(
858 const ErrorCallback& error_callback, 1026 const ErrorCallback& error_callback,
859 uint32 file_id, 1027 uint32 file_id,
860 base::File::Error error) { 1028 base::File::Error error) {
861 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); 1029 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
862 1030
863 FileIdToMTPFileNodeMap::iterator it = file_id_to_node_map_.find(file_id); 1031 FileIdToMTPFileNodeMap::iterator it = file_id_to_node_map_.find(file_id);
864 if (it != file_id_to_node_map_.end()) { 1032 if (it != file_id_to_node_map_.end()) {
865 MTPFileNode* parent = it->second->parent(); 1033 MTPFileNode* parent = it->second->parent();
866 if (parent) { 1034 if (parent) {
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
936 current_node = current_node->GetChild(device_relpath_components[i]); 1104 current_node = current_node->GetChild(device_relpath_components[i]);
937 if (!current_node) 1105 if (!current_node)
938 return false; 1106 return false;
939 } 1107 }
940 *id = current_node->file_id(); 1108 *id = current_node->file_id();
941 return true; 1109 return true;
942 } 1110 }
943 1111
944 void CreateMTPDeviceAsyncDelegate( 1112 void CreateMTPDeviceAsyncDelegate(
945 const std::string& device_location, 1113 const std::string& device_location,
1114 const bool read_only,
946 const CreateMTPDeviceAsyncDelegateCallback& callback) { 1115 const CreateMTPDeviceAsyncDelegateCallback& callback) {
947 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); 1116 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
948 callback.Run(new MTPDeviceDelegateImplLinux(device_location)); 1117 callback.Run(new MTPDeviceDelegateImplLinux(device_location, read_only));
949 } 1118 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698