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

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: Address comments. 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);
187 } 233 }
188 234
189 } // namespace 235 } // namespace
190 236
191 MTPDeviceDelegateImplLinux::PendingTaskInfo::PendingTaskInfo( 237 MTPDeviceDelegateImplLinux::PendingTaskInfo::PendingTaskInfo(
192 const base::FilePath& path, 238 const base::FilePath& path,
193 content::BrowserThread::ID thread_id, 239 content::BrowserThread::ID thread_id,
194 const tracked_objects::Location& location, 240 const tracked_objects::Location& location,
195 const base::Closure& task) 241 const base::Closure& task)
196 : path(path), 242 : path(path),
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
303 it != children_.end(); ++it) { 349 it != children_.end(); ++it) {
304 if (it->second->file_id() == file_id) { 350 if (it->second->file_id() == file_id) {
305 children_.erase(it); 351 children_.erase(it);
306 return true; 352 return true;
307 } 353 }
308 } 354 }
309 return false; 355 return false;
310 } 356 }
311 357
312 MTPDeviceDelegateImplLinux::MTPDeviceDelegateImplLinux( 358 MTPDeviceDelegateImplLinux::MTPDeviceDelegateImplLinux(
313 const std::string& device_location) 359 const std::string& device_location,
360 const bool read_only)
314 : init_state_(UNINITIALIZED), 361 : init_state_(UNINITIALIZED),
315 task_in_progress_(false), 362 task_in_progress_(false),
316 device_path_(device_location), 363 device_path_(device_location),
364 read_only_(read_only),
317 root_node_(new MTPFileNode(mtpd::kRootFileId, 365 root_node_(new MTPFileNode(mtpd::kRootFileId,
318 "", // Root node has no name. 366 "", // Root node has no name.
319 NULL, // And no parent node. 367 NULL, // And no parent node.
320 &file_id_to_node_map_)), 368 &file_id_to_node_map_)),
321 weak_ptr_factory_(this) { 369 weak_ptr_factory_(this) {
322 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); 370 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
323 DCHECK(!device_path_.empty()); 371 DCHECK(!device_path_.empty());
324 base::RemoveChars(device_location, kRootPath, &storage_name_); 372 base::RemoveChars(device_location, kRootPath, &storage_name_);
325 DCHECK(!storage_name_.empty()); 373 DCHECK(!storage_name_.empty());
326 } 374 }
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
425 offset, 473 offset,
426 buf_len, 474 buf_len,
427 success_callback, 475 success_callback,
428 error_callback); 476 error_callback);
429 EnsureInitAndRunTask(PendingTaskInfo(device_file_path, 477 EnsureInitAndRunTask(PendingTaskInfo(device_file_path,
430 content::BrowserThread::IO, 478 content::BrowserThread::IO,
431 FROM_HERE, 479 FROM_HERE,
432 closure)); 480 closure));
433 } 481 }
434 482
483 bool MTPDeviceDelegateImplLinux::IsReadOnly() {
484 return read_only_;
485 }
486
487 void MTPDeviceDelegateImplLinux::CopyFileFromLocal(
488 const base::FilePath& source_file_path,
489 const base::FilePath& device_file_path,
490 const CopyFileFromLocalSuccessCallback& success_callback,
491 const ErrorCallback& error_callback) {
492 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
493 DCHECK(!source_file_path.empty());
494 DCHECK(!device_file_path.empty());
495 base::Closure closure =
496 base::Bind(&MTPDeviceDelegateImplLinux::CopyFileFromLocalInternal,
497 weak_ptr_factory_.GetWeakPtr(), source_file_path,
498 device_file_path, success_callback, error_callback);
499 EnsureInitAndRunTask(PendingTaskInfo(device_file_path.DirName(),
500 content::BrowserThread::FILE, FROM_HERE,
501 closure));
502 }
503
435 void MTPDeviceDelegateImplLinux::CancelPendingTasksAndDeleteDelegate() { 504 void MTPDeviceDelegateImplLinux::CancelPendingTasksAndDeleteDelegate() {
436 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); 505 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
437 // To cancel all the pending tasks, destroy the MTPDeviceTaskHelper object. 506 // To cancel all the pending tasks, destroy the MTPDeviceTaskHelper object.
438 content::BrowserThread::PostTask( 507 content::BrowserThread::PostTask(
439 content::BrowserThread::UI, 508 content::BrowserThread::UI,
440 FROM_HERE, 509 FROM_HERE,
441 base::Bind(&CloseStorageAndDestroyTaskHelperOnUIThread, storage_name_)); 510 base::Bind(&CloseStorageAndDestroyTaskHelperOnUIThread,
511 storage_name_,
512 read_only_));
442 delete this; 513 delete this;
443 } 514 }
444 515
445 void MTPDeviceDelegateImplLinux::GetFileInfoInternal( 516 void MTPDeviceDelegateImplLinux::GetFileInfoInternal(
446 const base::FilePath& file_path, 517 const base::FilePath& file_path,
447 const GetFileInfoSuccessCallback& success_callback, 518 const GetFileInfoSuccessCallback& success_callback,
448 const ErrorCallback& error_callback) { 519 const ErrorCallback& error_callback) {
449 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); 520 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
450 521
451 uint32 file_id; 522 uint32 file_id;
452 if (CachedPathToId(file_path, &file_id)) { 523 if (CachedPathToId(file_path, &file_id)) {
453 GetFileInfoSuccessCallback success_callback_wrapper = 524 GetFileInfoSuccessCallback success_callback_wrapper =
454 base::Bind(&MTPDeviceDelegateImplLinux::OnDidGetFileInfo, 525 base::Bind(&MTPDeviceDelegateImplLinux::OnDidGetFileInfo,
455 weak_ptr_factory_.GetWeakPtr(), 526 weak_ptr_factory_.GetWeakPtr(),
456 success_callback); 527 success_callback);
457 ErrorCallback error_callback_wrapper = 528 ErrorCallback error_callback_wrapper =
458 base::Bind(&MTPDeviceDelegateImplLinux::HandleDeviceFileError, 529 base::Bind(&MTPDeviceDelegateImplLinux::HandleDeviceFileError,
459 weak_ptr_factory_.GetWeakPtr(), 530 weak_ptr_factory_.GetWeakPtr(),
460 error_callback, 531 error_callback,
461 file_id); 532 file_id);
462 533
463 534
464 base::Closure closure = base::Bind(&GetFileInfoOnUIThread, 535 base::Closure closure = base::Bind(&GetFileInfoOnUIThread,
465 storage_name_, 536 storage_name_,
537 read_only_,
466 file_id, 538 file_id,
467 success_callback_wrapper, 539 success_callback_wrapper,
468 error_callback_wrapper); 540 error_callback_wrapper);
469 EnsureInitAndRunTask(PendingTaskInfo(base::FilePath(), 541 EnsureInitAndRunTask(PendingTaskInfo(base::FilePath(),
470 content::BrowserThread::UI, 542 content::BrowserThread::UI,
471 FROM_HERE, 543 FROM_HERE,
472 closure)); 544 closure));
473 } else { 545 } else {
474 error_callback.Run(base::File::FILE_ERROR_NOT_FOUND); 546 error_callback.Run(base::File::FILE_ERROR_NOT_FOUND);
475 } 547 }
(...skipping 14 matching lines...) Expand all
490 dir_id, 562 dir_id,
491 success_callback, 563 success_callback,
492 error_callback); 564 error_callback);
493 ErrorCallback error_callback_wrapper = 565 ErrorCallback error_callback_wrapper =
494 base::Bind(&MTPDeviceDelegateImplLinux::HandleDeviceFileError, 566 base::Bind(&MTPDeviceDelegateImplLinux::HandleDeviceFileError,
495 weak_ptr_factory_.GetWeakPtr(), 567 weak_ptr_factory_.GetWeakPtr(),
496 error_callback, 568 error_callback,
497 dir_id); 569 dir_id);
498 base::Closure closure = base::Bind(&GetFileInfoOnUIThread, 570 base::Closure closure = base::Bind(&GetFileInfoOnUIThread,
499 storage_name_, 571 storage_name_,
572 read_only_,
500 dir_id, 573 dir_id,
501 success_callback_wrapper, 574 success_callback_wrapper,
502 error_callback_wrapper); 575 error_callback_wrapper);
503 EnsureInitAndRunTask(PendingTaskInfo(base::FilePath(), 576 EnsureInitAndRunTask(PendingTaskInfo(base::FilePath(),
504 content::BrowserThread::UI, 577 content::BrowserThread::UI,
505 FROM_HERE, 578 FROM_HERE,
506 closure)); 579 closure));
507 } else { 580 } else {
508 error_callback.Run(base::File::FILE_ERROR_NOT_FOUND); 581 error_callback.Run(base::File::FILE_ERROR_NOT_FOUND);
509 } 582 }
(...skipping 19 matching lines...) Expand all
529 &MTPDeviceDelegateImplLinux::OnDidGetFileInfoToCreateSnapshotFile, 602 &MTPDeviceDelegateImplLinux::OnDidGetFileInfoToCreateSnapshotFile,
530 weak_ptr_factory_.GetWeakPtr(), 603 weak_ptr_factory_.GetWeakPtr(),
531 base::Passed(&request_info)); 604 base::Passed(&request_info));
532 ErrorCallback error_callback_wrapper = 605 ErrorCallback error_callback_wrapper =
533 base::Bind(&MTPDeviceDelegateImplLinux::HandleDeviceFileError, 606 base::Bind(&MTPDeviceDelegateImplLinux::HandleDeviceFileError,
534 weak_ptr_factory_.GetWeakPtr(), 607 weak_ptr_factory_.GetWeakPtr(),
535 error_callback, 608 error_callback,
536 file_id); 609 file_id);
537 base::Closure closure = base::Bind(&GetFileInfoOnUIThread, 610 base::Closure closure = base::Bind(&GetFileInfoOnUIThread,
538 storage_name_, 611 storage_name_,
612 read_only_,
539 file_id, 613 file_id,
540 success_callback_wrapper, 614 success_callback_wrapper,
541 error_callback_wrapper); 615 error_callback_wrapper);
542 EnsureInitAndRunTask(PendingTaskInfo(base::FilePath(), 616 EnsureInitAndRunTask(PendingTaskInfo(base::FilePath(),
543 content::BrowserThread::UI, 617 content::BrowserThread::UI,
544 FROM_HERE, 618 FROM_HERE,
545 closure)); 619 closure));
546 } else { 620 } else {
547 error_callback.Run(base::File::FILE_ERROR_NOT_FOUND); 621 error_callback.Run(base::File::FILE_ERROR_NOT_FOUND);
548 } 622 }
(...skipping 13 matching lines...) Expand all
562 file_id, buf, offset, buf_len, 636 file_id, buf, offset, buf_len,
563 base::Bind(&MTPDeviceDelegateImplLinux::OnDidReadBytes, 637 base::Bind(&MTPDeviceDelegateImplLinux::OnDidReadBytes,
564 weak_ptr_factory_.GetWeakPtr(), 638 weak_ptr_factory_.GetWeakPtr(),
565 success_callback), 639 success_callback),
566 base::Bind(&MTPDeviceDelegateImplLinux::HandleDeviceFileError, 640 base::Bind(&MTPDeviceDelegateImplLinux::HandleDeviceFileError,
567 weak_ptr_factory_.GetWeakPtr(), 641 weak_ptr_factory_.GetWeakPtr(),
568 error_callback, 642 error_callback,
569 file_id)); 643 file_id));
570 644
571 base::Closure closure = 645 base::Closure closure =
572 base::Bind(base::Bind(&ReadBytesOnUIThread, storage_name_, request)); 646 base::Bind(&ReadBytesOnUIThread, storage_name_, read_only_, request);
573 EnsureInitAndRunTask(PendingTaskInfo(base::FilePath(), 647 EnsureInitAndRunTask(PendingTaskInfo(base::FilePath(),
574 content::BrowserThread::UI, 648 content::BrowserThread::UI,
575 FROM_HERE, 649 FROM_HERE,
576 closure)); 650 closure));
577 } else { 651 } else {
578 error_callback.Run(base::File::FILE_ERROR_NOT_FOUND); 652 error_callback.Run(base::File::FILE_ERROR_NOT_FOUND);
579 } 653 }
580 PendingRequestDone(); 654 PendingRequestDone();
581 } 655 }
582 656
657 void MTPDeviceDelegateImplLinux::CopyFileFromLocalInternal(
658 const base::FilePath& source_file_path,
659 const base::FilePath& device_file_path,
660 const CopyFileFromLocalSuccessCallback& success_callback,
661 const ErrorCallback& error_callback) {
662 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE);
663
664 const int source_file_descriptor =
665 open(source_file_path.value().c_str(), O_RDONLY);
666
667 if (source_file_descriptor != -1) {
Lei Zhang 2015/03/03 10:03:18 nit: just write this as >= 0. Although open() shou
yawano 2015/03/03 10:33:14 Done.
668 const base::Closure closure = base::Bind(
669 &MTPDeviceDelegateImplLinux::OnDidOpenFileDescriptorToCopyFileFromLocal,
670 weak_ptr_factory_.GetWeakPtr(), source_file_descriptor,
671 device_file_path, success_callback, error_callback);
672 EnsureInitAndRunTask(PendingTaskInfo(
673 base::FilePath(), content::BrowserThread::IO, FROM_HERE, closure));
674 } else {
675 const base::Closure closure =
676 base::Bind(error_callback, base::File::FILE_ERROR_INVALID_OPERATION);
677 content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE,
678 closure);
679 }
680
681 PendingRequestDone();
Lei Zhang 2015/03/03 10:03:18 This is being called on the wrong thread. Should b
yawano 2015/03/03 10:33:14 Done.
682 }
683
684 void MTPDeviceDelegateImplLinux::OnDidOpenFileDescriptorToCopyFileFromLocal(
685 const int source_file_descriptor,
686 const base::FilePath& device_file_path,
687 const CopyFileFromLocalSuccessCallback& success_callback,
688 const ErrorCallback& error_callback) {
689 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
690
691 uint32 parent_id;
692 if (CachedPathToId(device_file_path.DirName(), &parent_id)) {
693 CopyFileFromLocalSuccessCallback success_callback_wrapper =
694 base::Bind(&MTPDeviceDelegateImplLinux::OnDidCopyFileFromLocal,
695 weak_ptr_factory_.GetWeakPtr(), success_callback,
696 source_file_descriptor);
697
698 ErrorCallback error_callback_wrapper = base::Bind(
699 &MTPDeviceDelegateImplLinux::HandleCopyFileFromLocalError,
700 weak_ptr_factory_.GetWeakPtr(), error_callback, source_file_descriptor);
701
702 base::Closure closure = base::Bind(&CopyFileFromLocalOnUIThread,
703 storage_name_,
704 read_only_,
705 source_file_descriptor,
706 parent_id,
707 device_file_path.BaseName().value(),
708 success_callback_wrapper,
709 error_callback_wrapper);
710
711 EnsureInitAndRunTask(PendingTaskInfo(
712 base::FilePath(), content::BrowserThread::UI, FROM_HERE, closure));
713 } else {
714 HandleCopyFileFromLocalError(error_callback, source_file_descriptor,
715 base::File::FILE_ERROR_INVALID_OPERATION);
716 }
717
718 PendingRequestDone();
719 }
720
583 void MTPDeviceDelegateImplLinux::EnsureInitAndRunTask( 721 void MTPDeviceDelegateImplLinux::EnsureInitAndRunTask(
584 const PendingTaskInfo& task_info) { 722 const PendingTaskInfo& task_info) {
585 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); 723 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
586 if ((init_state_ == INITIALIZED) && !task_in_progress_) { 724 if ((init_state_ == INITIALIZED) && !task_in_progress_) {
587 RunTask(task_info); 725 RunTask(task_info);
588 return; 726 return;
589 } 727 }
590 728
591 // Only *Internal functions have empty paths. Since they are the continuation 729 // Only *Internal functions have empty paths. Since they are the continuation
592 // of the current running task, they get to cut in line. 730 // of the current running task, they get to cut in line.
593 if (task_info.path.empty()) 731 if (task_info.path.empty())
594 pending_tasks_.push_front(task_info); 732 pending_tasks_.push_front(task_info);
595 else 733 else
596 pending_tasks_.push_back(task_info); 734 pending_tasks_.push_back(task_info);
597 735
598 if (init_state_ == UNINITIALIZED) { 736 if (init_state_ == UNINITIALIZED) {
599 init_state_ = PENDING_INIT; 737 init_state_ = PENDING_INIT;
600 task_in_progress_ = true; 738 task_in_progress_ = true;
601 content::BrowserThread::PostTask( 739 content::BrowserThread::PostTask(
602 content::BrowserThread::UI, 740 content::BrowserThread::UI, FROM_HERE,
603 FROM_HERE, 741 base::Bind(&OpenStorageOnUIThread, storage_name_, read_only_,
604 base::Bind(&OpenStorageOnUIThread,
605 storage_name_,
606 base::Bind(&MTPDeviceDelegateImplLinux::OnInitCompleted, 742 base::Bind(&MTPDeviceDelegateImplLinux::OnInitCompleted,
607 weak_ptr_factory_.GetWeakPtr()))); 743 weak_ptr_factory_.GetWeakPtr())));
608 } 744 }
609 } 745 }
610 746
611 void MTPDeviceDelegateImplLinux::RunTask(const PendingTaskInfo& task_info) { 747 void MTPDeviceDelegateImplLinux::RunTask(const PendingTaskInfo& task_info) {
612 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); 748 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
613 DCHECK_EQ(INITIALIZED, init_state_); 749 DCHECK_EQ(INITIALIZED, init_state_);
614 DCHECK(!task_in_progress_); 750 DCHECK(!task_in_progress_);
615 task_in_progress_ = true; 751 task_in_progress_ = true;
(...skipping 26 matching lines...) Expand all
642 current_snapshot_request_info_->snapshot_file_path, 778 current_snapshot_request_info_->snapshot_file_path,
643 base::Bind( 779 base::Bind(
644 &MTPDeviceDelegateImplLinux::OnDidWriteDataIntoSnapshotFile, 780 &MTPDeviceDelegateImplLinux::OnDidWriteDataIntoSnapshotFile,
645 weak_ptr_factory_.GetWeakPtr()), 781 weak_ptr_factory_.GetWeakPtr()),
646 base::Bind( 782 base::Bind(
647 &MTPDeviceDelegateImplLinux::OnWriteDataIntoSnapshotFileError, 783 &MTPDeviceDelegateImplLinux::OnWriteDataIntoSnapshotFileError,
648 weak_ptr_factory_.GetWeakPtr())); 784 weak_ptr_factory_.GetWeakPtr()));
649 785
650 base::Closure task_closure = base::Bind(&WriteDataIntoSnapshotFileOnUIThread, 786 base::Closure task_closure = base::Bind(&WriteDataIntoSnapshotFileOnUIThread,
651 storage_name_, 787 storage_name_,
788 read_only_,
652 request_info, 789 request_info,
653 file_info); 790 file_info);
654 content::BrowserThread::PostTask(content::BrowserThread::UI, 791 content::BrowserThread::PostTask(content::BrowserThread::UI,
655 FROM_HERE, 792 FROM_HERE,
656 task_closure); 793 task_closure);
657 } 794 }
658 795
659 void MTPDeviceDelegateImplLinux::PendingRequestDone() { 796 void MTPDeviceDelegateImplLinux::PendingRequestDone() {
660 DCHECK(task_in_progress_); 797 DCHECK(task_in_progress_);
661 task_in_progress_ = false; 798 task_in_progress_ = false;
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
696 DCHECK(task_in_progress_); 833 DCHECK(task_in_progress_);
697 if (!file_info.is_directory) { 834 if (!file_info.is_directory) {
698 return HandleDeviceFileError(error_callback, 835 return HandleDeviceFileError(error_callback,
699 dir_id, 836 dir_id,
700 base::File::FILE_ERROR_NOT_A_DIRECTORY); 837 base::File::FILE_ERROR_NOT_A_DIRECTORY);
701 } 838 }
702 839
703 base::Closure task_closure = 840 base::Closure task_closure =
704 base::Bind(&ReadDirectoryOnUIThread, 841 base::Bind(&ReadDirectoryOnUIThread,
705 storage_name_, 842 storage_name_,
843 read_only_,
706 dir_id, 844 dir_id,
707 base::Bind(&MTPDeviceDelegateImplLinux::OnDidReadDirectory, 845 base::Bind(&MTPDeviceDelegateImplLinux::OnDidReadDirectory,
708 weak_ptr_factory_.GetWeakPtr(), 846 weak_ptr_factory_.GetWeakPtr(),
709 dir_id, 847 dir_id,
710 success_callback), 848 success_callback),
711 base::Bind(&MTPDeviceDelegateImplLinux::HandleDeviceFileError, 849 base::Bind(&MTPDeviceDelegateImplLinux::HandleDeviceFileError,
712 weak_ptr_factory_.GetWeakPtr(), 850 weak_ptr_factory_.GetWeakPtr(),
713 error_callback, 851 error_callback,
714 dir_id)); 852 dir_id));
715 content::BrowserThread::PostTask(content::BrowserThread::UI, 853 content::BrowserThread::PostTask(content::BrowserThread::UI,
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
847 985
848 void MTPDeviceDelegateImplLinux::OnFillFileCacheFailed( 986 void MTPDeviceDelegateImplLinux::OnFillFileCacheFailed(
849 base::File::Error /* error */) { 987 base::File::Error /* error */) {
850 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); 988 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
851 // When filling the cache fails for the task at the front of the queue, clear 989 // 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, 990 // 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. 991 // the task will just run and fail the CachedPathToId() lookup.
854 pending_tasks_.front().path.clear(); 992 pending_tasks_.front().path.clear();
855 } 993 }
856 994
995 void MTPDeviceDelegateImplLinux::OnDidCopyFileFromLocal(
996 const CopyFileFromLocalSuccessCallback& success_callback,
997 const int source_file_descriptor) {
998 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
999
1000 const base::Closure closure =
1001 base::Bind(&MTPDeviceDelegateImplLinux::CloseFileDescriptor,
1002 weak_ptr_factory_.GetWeakPtr(), source_file_descriptor);
1003 content::BrowserThread::PostTask(content::BrowserThread::FILE, FROM_HERE,
1004 closure);
1005
1006 success_callback.Run();
1007 PendingRequestDone();
1008 }
1009
1010 void MTPDeviceDelegateImplLinux::HandleCopyFileFromLocalError(
1011 const ErrorCallback& error_callback,
1012 const int source_file_descriptor,
1013 base::File::Error error) {
1014 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
1015
1016 const base::Closure closure =
1017 base::Bind(&MTPDeviceDelegateImplLinux::CloseFileDescriptor,
1018 weak_ptr_factory_.GetWeakPtr(), source_file_descriptor);
1019 content::BrowserThread::PostTask(content::BrowserThread::FILE, FROM_HERE,
1020 closure);
1021
1022 error_callback.Run(error);
1023 PendingRequestDone();
1024 }
1025
1026 void MTPDeviceDelegateImplLinux::CloseFileDescriptor(
Lei Zhang 2015/03/03 10:03:17 nit: This can just be a function in the anonymous
yawano 2015/03/03 10:33:14 Done.
1027 const int file_descriptor) {
1028 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE);
1029
1030 IGNORE_EINTR(close(file_descriptor));
1031 }
1032
857 void MTPDeviceDelegateImplLinux::HandleDeviceFileError( 1033 void MTPDeviceDelegateImplLinux::HandleDeviceFileError(
858 const ErrorCallback& error_callback, 1034 const ErrorCallback& error_callback,
859 uint32 file_id, 1035 uint32 file_id,
860 base::File::Error error) { 1036 base::File::Error error) {
861 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); 1037 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
862 1038
863 FileIdToMTPFileNodeMap::iterator it = file_id_to_node_map_.find(file_id); 1039 FileIdToMTPFileNodeMap::iterator it = file_id_to_node_map_.find(file_id);
864 if (it != file_id_to_node_map_.end()) { 1040 if (it != file_id_to_node_map_.end()) {
865 MTPFileNode* parent = it->second->parent(); 1041 MTPFileNode* parent = it->second->parent();
866 if (parent) { 1042 if (parent) {
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
936 current_node = current_node->GetChild(device_relpath_components[i]); 1112 current_node = current_node->GetChild(device_relpath_components[i]);
937 if (!current_node) 1113 if (!current_node)
938 return false; 1114 return false;
939 } 1115 }
940 *id = current_node->file_id(); 1116 *id = current_node->file_id();
941 return true; 1117 return true;
942 } 1118 }
943 1119
944 void CreateMTPDeviceAsyncDelegate( 1120 void CreateMTPDeviceAsyncDelegate(
945 const std::string& device_location, 1121 const std::string& device_location,
1122 const bool read_only,
946 const CreateMTPDeviceAsyncDelegateCallback& callback) { 1123 const CreateMTPDeviceAsyncDelegateCallback& callback) {
947 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); 1124 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
948 callback.Run(new MTPDeviceDelegateImplLinux(device_location)); 1125 callback.Run(new MTPDeviceDelegateImplLinux(device_location, read_only));
949 } 1126 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698