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

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

Powered by Google App Engine
This is Rietveld 408576698