OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "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 Loading... | |
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 return open(file_path, flags); | |
yawano
2015/03/04 02:29:07
Since open() takes variable number of arguments, w
Lei Zhang
2015/03/04 02:36:59
Can you add DCHECK_CURRENTLY_ON(content::BrowserTh
yawano
2015/03/04 04:11:27
Done.
| |
238 } | |
239 | |
240 // Closes |file_descriptor| on file thread. | |
241 void CloseFileDescriptor(const int file_descriptor) { | |
242 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE); | |
243 | |
244 IGNORE_EINTR(close(file_descriptor)); | |
187 } | 245 } |
188 | 246 |
189 } // namespace | 247 } // namespace |
190 | 248 |
191 MTPDeviceDelegateImplLinux::PendingTaskInfo::PendingTaskInfo( | 249 MTPDeviceDelegateImplLinux::PendingTaskInfo::PendingTaskInfo( |
192 const base::FilePath& path, | 250 const base::FilePath& path, |
193 content::BrowserThread::ID thread_id, | 251 content::BrowserThread::ID thread_id, |
194 const tracked_objects::Location& location, | 252 const tracked_objects::Location& location, |
195 const base::Closure& task) | 253 const base::Closure& task) |
196 : path(path), | 254 : path(path), |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
303 it != children_.end(); ++it) { | 361 it != children_.end(); ++it) { |
304 if (it->second->file_id() == file_id) { | 362 if (it->second->file_id() == file_id) { |
305 children_.erase(it); | 363 children_.erase(it); |
306 return true; | 364 return true; |
307 } | 365 } |
308 } | 366 } |
309 return false; | 367 return false; |
310 } | 368 } |
311 | 369 |
312 MTPDeviceDelegateImplLinux::MTPDeviceDelegateImplLinux( | 370 MTPDeviceDelegateImplLinux::MTPDeviceDelegateImplLinux( |
313 const std::string& device_location) | 371 const std::string& device_location, |
372 const bool read_only) | |
314 : init_state_(UNINITIALIZED), | 373 : init_state_(UNINITIALIZED), |
315 task_in_progress_(false), | 374 task_in_progress_(false), |
316 device_path_(device_location), | 375 device_path_(device_location), |
376 read_only_(read_only), | |
317 root_node_(new MTPFileNode(mtpd::kRootFileId, | 377 root_node_(new MTPFileNode(mtpd::kRootFileId, |
318 "", // Root node has no name. | 378 "", // Root node has no name. |
319 NULL, // And no parent node. | 379 NULL, // And no parent node. |
320 &file_id_to_node_map_)), | 380 &file_id_to_node_map_)), |
321 weak_ptr_factory_(this) { | 381 weak_ptr_factory_(this) { |
322 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 382 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
323 DCHECK(!device_path_.empty()); | 383 DCHECK(!device_path_.empty()); |
324 base::RemoveChars(device_location, kRootPath, &storage_name_); | 384 base::RemoveChars(device_location, kRootPath, &storage_name_); |
325 DCHECK(!storage_name_.empty()); | 385 DCHECK(!storage_name_.empty()); |
326 } | 386 } |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
425 offset, | 485 offset, |
426 buf_len, | 486 buf_len, |
427 success_callback, | 487 success_callback, |
428 error_callback); | 488 error_callback); |
429 EnsureInitAndRunTask(PendingTaskInfo(device_file_path, | 489 EnsureInitAndRunTask(PendingTaskInfo(device_file_path, |
430 content::BrowserThread::IO, | 490 content::BrowserThread::IO, |
431 FROM_HERE, | 491 FROM_HERE, |
432 closure)); | 492 closure)); |
433 } | 493 } |
434 | 494 |
495 bool MTPDeviceDelegateImplLinux::IsReadOnly() { | |
496 return read_only_; | |
497 } | |
498 | |
499 void MTPDeviceDelegateImplLinux::CopyFileFromLocal( | |
500 const base::FilePath& source_file_path, | |
501 const base::FilePath& device_file_path, | |
502 const CopyFileFromLocalSuccessCallback& success_callback, | |
503 const ErrorCallback& error_callback) { | |
504 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
505 DCHECK(!source_file_path.empty()); | |
506 DCHECK(!device_file_path.empty()); | |
507 | |
508 content::BrowserThread::PostTaskAndReplyWithResult( | |
509 content::BrowserThread::FILE, | |
510 FROM_HERE, | |
511 base::Bind(&OpenFileDescriptor, | |
512 source_file_path.value().c_str(), | |
513 O_RDONLY), | |
514 base::Bind(&MTPDeviceDelegateImplLinux::CopyFileFromLocalInternal, | |
515 weak_ptr_factory_.GetWeakPtr(), | |
516 device_file_path, | |
517 success_callback, | |
518 error_callback)); | |
519 } | |
520 | |
435 void MTPDeviceDelegateImplLinux::CancelPendingTasksAndDeleteDelegate() { | 521 void MTPDeviceDelegateImplLinux::CancelPendingTasksAndDeleteDelegate() { |
436 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 522 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
437 // To cancel all the pending tasks, destroy the MTPDeviceTaskHelper object. | 523 // To cancel all the pending tasks, destroy the MTPDeviceTaskHelper object. |
438 content::BrowserThread::PostTask( | 524 content::BrowserThread::PostTask( |
439 content::BrowserThread::UI, | 525 content::BrowserThread::UI, |
440 FROM_HERE, | 526 FROM_HERE, |
441 base::Bind(&CloseStorageAndDestroyTaskHelperOnUIThread, storage_name_)); | 527 base::Bind(&CloseStorageAndDestroyTaskHelperOnUIThread, |
528 storage_name_, | |
529 read_only_)); | |
442 delete this; | 530 delete this; |
443 } | 531 } |
444 | 532 |
445 void MTPDeviceDelegateImplLinux::GetFileInfoInternal( | 533 void MTPDeviceDelegateImplLinux::GetFileInfoInternal( |
446 const base::FilePath& file_path, | 534 const base::FilePath& file_path, |
447 const GetFileInfoSuccessCallback& success_callback, | 535 const GetFileInfoSuccessCallback& success_callback, |
448 const ErrorCallback& error_callback) { | 536 const ErrorCallback& error_callback) { |
449 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 537 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
450 | 538 |
451 uint32 file_id; | 539 uint32 file_id; |
452 if (CachedPathToId(file_path, &file_id)) { | 540 if (CachedPathToId(file_path, &file_id)) { |
453 GetFileInfoSuccessCallback success_callback_wrapper = | 541 GetFileInfoSuccessCallback success_callback_wrapper = |
454 base::Bind(&MTPDeviceDelegateImplLinux::OnDidGetFileInfo, | 542 base::Bind(&MTPDeviceDelegateImplLinux::OnDidGetFileInfo, |
455 weak_ptr_factory_.GetWeakPtr(), | 543 weak_ptr_factory_.GetWeakPtr(), |
456 success_callback); | 544 success_callback); |
457 ErrorCallback error_callback_wrapper = | 545 ErrorCallback error_callback_wrapper = |
458 base::Bind(&MTPDeviceDelegateImplLinux::HandleDeviceFileError, | 546 base::Bind(&MTPDeviceDelegateImplLinux::HandleDeviceFileError, |
459 weak_ptr_factory_.GetWeakPtr(), | 547 weak_ptr_factory_.GetWeakPtr(), |
460 error_callback, | 548 error_callback, |
461 file_id); | 549 file_id); |
462 | 550 |
463 | 551 |
464 base::Closure closure = base::Bind(&GetFileInfoOnUIThread, | 552 base::Closure closure = base::Bind(&GetFileInfoOnUIThread, |
465 storage_name_, | 553 storage_name_, |
554 read_only_, | |
466 file_id, | 555 file_id, |
467 success_callback_wrapper, | 556 success_callback_wrapper, |
468 error_callback_wrapper); | 557 error_callback_wrapper); |
469 EnsureInitAndRunTask(PendingTaskInfo(base::FilePath(), | 558 EnsureInitAndRunTask(PendingTaskInfo(base::FilePath(), |
470 content::BrowserThread::UI, | 559 content::BrowserThread::UI, |
471 FROM_HERE, | 560 FROM_HERE, |
472 closure)); | 561 closure)); |
473 } else { | 562 } else { |
474 error_callback.Run(base::File::FILE_ERROR_NOT_FOUND); | 563 error_callback.Run(base::File::FILE_ERROR_NOT_FOUND); |
475 } | 564 } |
(...skipping 14 matching lines...) Expand all Loading... | |
490 dir_id, | 579 dir_id, |
491 success_callback, | 580 success_callback, |
492 error_callback); | 581 error_callback); |
493 ErrorCallback error_callback_wrapper = | 582 ErrorCallback error_callback_wrapper = |
494 base::Bind(&MTPDeviceDelegateImplLinux::HandleDeviceFileError, | 583 base::Bind(&MTPDeviceDelegateImplLinux::HandleDeviceFileError, |
495 weak_ptr_factory_.GetWeakPtr(), | 584 weak_ptr_factory_.GetWeakPtr(), |
496 error_callback, | 585 error_callback, |
497 dir_id); | 586 dir_id); |
498 base::Closure closure = base::Bind(&GetFileInfoOnUIThread, | 587 base::Closure closure = base::Bind(&GetFileInfoOnUIThread, |
499 storage_name_, | 588 storage_name_, |
589 read_only_, | |
500 dir_id, | 590 dir_id, |
501 success_callback_wrapper, | 591 success_callback_wrapper, |
502 error_callback_wrapper); | 592 error_callback_wrapper); |
503 EnsureInitAndRunTask(PendingTaskInfo(base::FilePath(), | 593 EnsureInitAndRunTask(PendingTaskInfo(base::FilePath(), |
504 content::BrowserThread::UI, | 594 content::BrowserThread::UI, |
505 FROM_HERE, | 595 FROM_HERE, |
506 closure)); | 596 closure)); |
507 } else { | 597 } else { |
508 error_callback.Run(base::File::FILE_ERROR_NOT_FOUND); | 598 error_callback.Run(base::File::FILE_ERROR_NOT_FOUND); |
509 } | 599 } |
(...skipping 19 matching lines...) Expand all Loading... | |
529 &MTPDeviceDelegateImplLinux::OnDidGetFileInfoToCreateSnapshotFile, | 619 &MTPDeviceDelegateImplLinux::OnDidGetFileInfoToCreateSnapshotFile, |
530 weak_ptr_factory_.GetWeakPtr(), | 620 weak_ptr_factory_.GetWeakPtr(), |
531 base::Passed(&request_info)); | 621 base::Passed(&request_info)); |
532 ErrorCallback error_callback_wrapper = | 622 ErrorCallback error_callback_wrapper = |
533 base::Bind(&MTPDeviceDelegateImplLinux::HandleDeviceFileError, | 623 base::Bind(&MTPDeviceDelegateImplLinux::HandleDeviceFileError, |
534 weak_ptr_factory_.GetWeakPtr(), | 624 weak_ptr_factory_.GetWeakPtr(), |
535 error_callback, | 625 error_callback, |
536 file_id); | 626 file_id); |
537 base::Closure closure = base::Bind(&GetFileInfoOnUIThread, | 627 base::Closure closure = base::Bind(&GetFileInfoOnUIThread, |
538 storage_name_, | 628 storage_name_, |
629 read_only_, | |
539 file_id, | 630 file_id, |
540 success_callback_wrapper, | 631 success_callback_wrapper, |
541 error_callback_wrapper); | 632 error_callback_wrapper); |
542 EnsureInitAndRunTask(PendingTaskInfo(base::FilePath(), | 633 EnsureInitAndRunTask(PendingTaskInfo(base::FilePath(), |
543 content::BrowserThread::UI, | 634 content::BrowserThread::UI, |
544 FROM_HERE, | 635 FROM_HERE, |
545 closure)); | 636 closure)); |
546 } else { | 637 } else { |
547 error_callback.Run(base::File::FILE_ERROR_NOT_FOUND); | 638 error_callback.Run(base::File::FILE_ERROR_NOT_FOUND); |
548 } | 639 } |
(...skipping 13 matching lines...) Expand all Loading... | |
562 file_id, buf, offset, buf_len, | 653 file_id, buf, offset, buf_len, |
563 base::Bind(&MTPDeviceDelegateImplLinux::OnDidReadBytes, | 654 base::Bind(&MTPDeviceDelegateImplLinux::OnDidReadBytes, |
564 weak_ptr_factory_.GetWeakPtr(), | 655 weak_ptr_factory_.GetWeakPtr(), |
565 success_callback), | 656 success_callback), |
566 base::Bind(&MTPDeviceDelegateImplLinux::HandleDeviceFileError, | 657 base::Bind(&MTPDeviceDelegateImplLinux::HandleDeviceFileError, |
567 weak_ptr_factory_.GetWeakPtr(), | 658 weak_ptr_factory_.GetWeakPtr(), |
568 error_callback, | 659 error_callback, |
569 file_id)); | 660 file_id)); |
570 | 661 |
571 base::Closure closure = | 662 base::Closure closure = |
572 base::Bind(base::Bind(&ReadBytesOnUIThread, storage_name_, request)); | 663 base::Bind(&ReadBytesOnUIThread, storage_name_, read_only_, request); |
573 EnsureInitAndRunTask(PendingTaskInfo(base::FilePath(), | 664 EnsureInitAndRunTask(PendingTaskInfo(base::FilePath(), |
574 content::BrowserThread::UI, | 665 content::BrowserThread::UI, |
575 FROM_HERE, | 666 FROM_HERE, |
576 closure)); | 667 closure)); |
577 } else { | 668 } else { |
578 error_callback.Run(base::File::FILE_ERROR_NOT_FOUND); | 669 error_callback.Run(base::File::FILE_ERROR_NOT_FOUND); |
579 } | 670 } |
580 PendingRequestDone(); | 671 PendingRequestDone(); |
581 } | 672 } |
582 | 673 |
674 void MTPDeviceDelegateImplLinux::CopyFileFromLocalInternal( | |
675 const base::FilePath& device_file_path, | |
676 const CopyFileFromLocalSuccessCallback& success_callback, | |
677 const ErrorCallback& error_callback, | |
678 const int source_file_descriptor) { | |
679 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
680 | |
681 if (source_file_descriptor < 0) { | |
682 error_callback.Run(base::File::FILE_ERROR_INVALID_OPERATION); | |
683 PendingRequestDone(); | |
684 return; | |
685 } | |
686 | |
687 uint32 parent_id; | |
688 if (CachedPathToId(device_file_path.DirName(), &parent_id)) { | |
689 CopyFileFromLocalSuccessCallback success_callback_wrapper = | |
690 base::Bind(&MTPDeviceDelegateImplLinux::OnDidCopyFileFromLocal, | |
691 weak_ptr_factory_.GetWeakPtr(), success_callback, | |
692 source_file_descriptor); | |
693 | |
694 ErrorCallback error_callback_wrapper = base::Bind( | |
695 &MTPDeviceDelegateImplLinux::HandleCopyFileFromLocalError, | |
696 weak_ptr_factory_.GetWeakPtr(), error_callback, source_file_descriptor); | |
697 | |
698 base::Closure closure = base::Bind(&CopyFileFromLocalOnUIThread, | |
699 storage_name_, | |
700 read_only_, | |
701 source_file_descriptor, | |
702 parent_id, | |
703 device_file_path.BaseName().value(), | |
704 success_callback_wrapper, | |
705 error_callback_wrapper); | |
706 | |
707 EnsureInitAndRunTask(PendingTaskInfo( | |
708 base::FilePath(), content::BrowserThread::UI, FROM_HERE, closure)); | |
709 } else { | |
710 HandleCopyFileFromLocalError(error_callback, source_file_descriptor, | |
711 base::File::FILE_ERROR_INVALID_OPERATION); | |
712 } | |
713 | |
714 PendingRequestDone(); | |
715 } | |
716 | |
583 void MTPDeviceDelegateImplLinux::EnsureInitAndRunTask( | 717 void MTPDeviceDelegateImplLinux::EnsureInitAndRunTask( |
584 const PendingTaskInfo& task_info) { | 718 const PendingTaskInfo& task_info) { |
585 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 719 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
586 if ((init_state_ == INITIALIZED) && !task_in_progress_) { | 720 if ((init_state_ == INITIALIZED) && !task_in_progress_) { |
587 RunTask(task_info); | 721 RunTask(task_info); |
588 return; | 722 return; |
589 } | 723 } |
590 | 724 |
591 // Only *Internal functions have empty paths. Since they are the continuation | 725 // Only *Internal functions have empty paths. Since they are the continuation |
592 // of the current running task, they get to cut in line. | 726 // of the current running task, they get to cut in line. |
593 if (task_info.path.empty()) | 727 if (task_info.path.empty()) |
594 pending_tasks_.push_front(task_info); | 728 pending_tasks_.push_front(task_info); |
595 else | 729 else |
596 pending_tasks_.push_back(task_info); | 730 pending_tasks_.push_back(task_info); |
597 | 731 |
598 if (init_state_ == UNINITIALIZED) { | 732 if (init_state_ == UNINITIALIZED) { |
599 init_state_ = PENDING_INIT; | 733 init_state_ = PENDING_INIT; |
600 task_in_progress_ = true; | 734 task_in_progress_ = true; |
601 content::BrowserThread::PostTask( | 735 content::BrowserThread::PostTask( |
602 content::BrowserThread::UI, | 736 content::BrowserThread::UI, FROM_HERE, |
603 FROM_HERE, | 737 base::Bind(&OpenStorageOnUIThread, storage_name_, read_only_, |
604 base::Bind(&OpenStorageOnUIThread, | |
605 storage_name_, | |
606 base::Bind(&MTPDeviceDelegateImplLinux::OnInitCompleted, | 738 base::Bind(&MTPDeviceDelegateImplLinux::OnInitCompleted, |
607 weak_ptr_factory_.GetWeakPtr()))); | 739 weak_ptr_factory_.GetWeakPtr()))); |
608 } | 740 } |
609 } | 741 } |
610 | 742 |
611 void MTPDeviceDelegateImplLinux::RunTask(const PendingTaskInfo& task_info) { | 743 void MTPDeviceDelegateImplLinux::RunTask(const PendingTaskInfo& task_info) { |
612 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 744 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
613 DCHECK_EQ(INITIALIZED, init_state_); | 745 DCHECK_EQ(INITIALIZED, init_state_); |
614 DCHECK(!task_in_progress_); | 746 DCHECK(!task_in_progress_); |
615 task_in_progress_ = true; | 747 task_in_progress_ = true; |
(...skipping 26 matching lines...) Expand all Loading... | |
642 current_snapshot_request_info_->snapshot_file_path, | 774 current_snapshot_request_info_->snapshot_file_path, |
643 base::Bind( | 775 base::Bind( |
644 &MTPDeviceDelegateImplLinux::OnDidWriteDataIntoSnapshotFile, | 776 &MTPDeviceDelegateImplLinux::OnDidWriteDataIntoSnapshotFile, |
645 weak_ptr_factory_.GetWeakPtr()), | 777 weak_ptr_factory_.GetWeakPtr()), |
646 base::Bind( | 778 base::Bind( |
647 &MTPDeviceDelegateImplLinux::OnWriteDataIntoSnapshotFileError, | 779 &MTPDeviceDelegateImplLinux::OnWriteDataIntoSnapshotFileError, |
648 weak_ptr_factory_.GetWeakPtr())); | 780 weak_ptr_factory_.GetWeakPtr())); |
649 | 781 |
650 base::Closure task_closure = base::Bind(&WriteDataIntoSnapshotFileOnUIThread, | 782 base::Closure task_closure = base::Bind(&WriteDataIntoSnapshotFileOnUIThread, |
651 storage_name_, | 783 storage_name_, |
784 read_only_, | |
652 request_info, | 785 request_info, |
653 file_info); | 786 file_info); |
654 content::BrowserThread::PostTask(content::BrowserThread::UI, | 787 content::BrowserThread::PostTask(content::BrowserThread::UI, |
655 FROM_HERE, | 788 FROM_HERE, |
656 task_closure); | 789 task_closure); |
657 } | 790 } |
658 | 791 |
659 void MTPDeviceDelegateImplLinux::PendingRequestDone() { | 792 void MTPDeviceDelegateImplLinux::PendingRequestDone() { |
793 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
660 DCHECK(task_in_progress_); | 794 DCHECK(task_in_progress_); |
661 task_in_progress_ = false; | 795 task_in_progress_ = false; |
662 ProcessNextPendingRequest(); | 796 ProcessNextPendingRequest(); |
663 } | 797 } |
664 | 798 |
665 void MTPDeviceDelegateImplLinux::ProcessNextPendingRequest() { | 799 void MTPDeviceDelegateImplLinux::ProcessNextPendingRequest() { |
666 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 800 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
667 DCHECK(!task_in_progress_); | 801 DCHECK(!task_in_progress_); |
668 if (pending_tasks_.empty()) | 802 if (pending_tasks_.empty()) |
669 return; | 803 return; |
(...skipping 26 matching lines...) Expand all Loading... | |
696 DCHECK(task_in_progress_); | 830 DCHECK(task_in_progress_); |
697 if (!file_info.is_directory) { | 831 if (!file_info.is_directory) { |
698 return HandleDeviceFileError(error_callback, | 832 return HandleDeviceFileError(error_callback, |
699 dir_id, | 833 dir_id, |
700 base::File::FILE_ERROR_NOT_A_DIRECTORY); | 834 base::File::FILE_ERROR_NOT_A_DIRECTORY); |
701 } | 835 } |
702 | 836 |
703 base::Closure task_closure = | 837 base::Closure task_closure = |
704 base::Bind(&ReadDirectoryOnUIThread, | 838 base::Bind(&ReadDirectoryOnUIThread, |
705 storage_name_, | 839 storage_name_, |
840 read_only_, | |
706 dir_id, | 841 dir_id, |
707 base::Bind(&MTPDeviceDelegateImplLinux::OnDidReadDirectory, | 842 base::Bind(&MTPDeviceDelegateImplLinux::OnDidReadDirectory, |
708 weak_ptr_factory_.GetWeakPtr(), | 843 weak_ptr_factory_.GetWeakPtr(), |
709 dir_id, | 844 dir_id, |
710 success_callback), | 845 success_callback), |
711 base::Bind(&MTPDeviceDelegateImplLinux::HandleDeviceFileError, | 846 base::Bind(&MTPDeviceDelegateImplLinux::HandleDeviceFileError, |
712 weak_ptr_factory_.GetWeakPtr(), | 847 weak_ptr_factory_.GetWeakPtr(), |
713 error_callback, | 848 error_callback, |
714 dir_id)); | 849 dir_id)); |
715 content::BrowserThread::PostTask(content::BrowserThread::UI, | 850 content::BrowserThread::PostTask(content::BrowserThread::UI, |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
847 | 982 |
848 void MTPDeviceDelegateImplLinux::OnFillFileCacheFailed( | 983 void MTPDeviceDelegateImplLinux::OnFillFileCacheFailed( |
849 base::File::Error /* error */) { | 984 base::File::Error /* error */) { |
850 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 985 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
851 // When filling the cache fails for the task at the front of the queue, clear | 986 // 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, | 987 // 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. | 988 // the task will just run and fail the CachedPathToId() lookup. |
854 pending_tasks_.front().path.clear(); | 989 pending_tasks_.front().path.clear(); |
855 } | 990 } |
856 | 991 |
992 void MTPDeviceDelegateImplLinux::OnDidCopyFileFromLocal( | |
993 const CopyFileFromLocalSuccessCallback& success_callback, | |
994 const int source_file_descriptor) { | |
995 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
996 | |
997 const base::Closure closure = base::Bind(&CloseFileDescriptor, | |
998 source_file_descriptor); | |
999 | |
1000 content::BrowserThread::PostTask(content::BrowserThread::FILE, FROM_HERE, | |
1001 closure); | |
1002 | |
1003 success_callback.Run(); | |
1004 PendingRequestDone(); | |
1005 } | |
1006 | |
1007 void MTPDeviceDelegateImplLinux::HandleCopyFileFromLocalError( | |
1008 const ErrorCallback& error_callback, | |
1009 const int source_file_descriptor, | |
1010 base::File::Error error) { | |
1011 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
1012 | |
1013 const base::Closure closure = base::Bind(&CloseFileDescriptor, | |
1014 source_file_descriptor); | |
1015 | |
1016 content::BrowserThread::PostTask(content::BrowserThread::FILE, FROM_HERE, | |
1017 closure); | |
1018 | |
1019 error_callback.Run(error); | |
1020 PendingRequestDone(); | |
1021 } | |
1022 | |
857 void MTPDeviceDelegateImplLinux::HandleDeviceFileError( | 1023 void MTPDeviceDelegateImplLinux::HandleDeviceFileError( |
858 const ErrorCallback& error_callback, | 1024 const ErrorCallback& error_callback, |
859 uint32 file_id, | 1025 uint32 file_id, |
860 base::File::Error error) { | 1026 base::File::Error error) { |
861 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 1027 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
862 | 1028 |
863 FileIdToMTPFileNodeMap::iterator it = file_id_to_node_map_.find(file_id); | 1029 FileIdToMTPFileNodeMap::iterator it = file_id_to_node_map_.find(file_id); |
864 if (it != file_id_to_node_map_.end()) { | 1030 if (it != file_id_to_node_map_.end()) { |
865 MTPFileNode* parent = it->second->parent(); | 1031 MTPFileNode* parent = it->second->parent(); |
866 if (parent) { | 1032 if (parent) { |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
936 current_node = current_node->GetChild(device_relpath_components[i]); | 1102 current_node = current_node->GetChild(device_relpath_components[i]); |
937 if (!current_node) | 1103 if (!current_node) |
938 return false; | 1104 return false; |
939 } | 1105 } |
940 *id = current_node->file_id(); | 1106 *id = current_node->file_id(); |
941 return true; | 1107 return true; |
942 } | 1108 } |
943 | 1109 |
944 void CreateMTPDeviceAsyncDelegate( | 1110 void CreateMTPDeviceAsyncDelegate( |
945 const std::string& device_location, | 1111 const std::string& device_location, |
1112 const bool read_only, | |
946 const CreateMTPDeviceAsyncDelegateCallback& callback) { | 1113 const CreateMTPDeviceAsyncDelegateCallback& callback) { |
947 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 1114 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
948 callback.Run(new MTPDeviceDelegateImplLinux(device_location)); | 1115 callback.Run(new MTPDeviceDelegateImplLinux(device_location, read_only)); |
949 } | 1116 } |
OLD | NEW |