OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/chromeos/cros/mount_library.h" | 5 #include "chrome/browser/chromeos/cros/mount_library.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 | 8 |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
83 | 83 |
84 // MountLibrary overrides. | 84 // MountLibrary overrides. |
85 virtual void AddObserver(Observer* observer) OVERRIDE { | 85 virtual void AddObserver(Observer* observer) OVERRIDE { |
86 observers_.AddObserver(observer); | 86 observers_.AddObserver(observer); |
87 } | 87 } |
88 | 88 |
89 virtual void RemoveObserver(Observer* observer) OVERRIDE { | 89 virtual void RemoveObserver(Observer* observer) OVERRIDE { |
90 observers_.RemoveObserver(observer); | 90 observers_.RemoveObserver(observer); |
91 } | 91 } |
92 | 92 |
93 virtual void MountPath(const char* device_path) OVERRIDE { | 93 virtual void MountPath(const char* source_path, |
94 MountType type, | |
95 const MountPathOptions& options) OVERRIDE { | |
94 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 96 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
95 if (!CrosLibrary::Get()->EnsureLoaded()) { | 97 if (!CrosLibrary::Get()->EnsureLoaded()) { |
96 OnMountRemovableDevice(device_path, | 98 OnMountCompleted(MOUNT_ERROR_LIBRARY_NOT_LOADED, |
97 NULL, | 99 source_path, |
98 MOUNT_METHOD_ERROR_LOCAL, | 100 type, |
99 kLibraryNotLoaded); | 101 NULL); |
100 return; | 102 return; |
101 } | 103 } |
102 MountRemovableDevice(device_path, | 104 MountSourcePath(source_path, type, options, &MountCompletedHandler, this); |
103 &MountLibraryImpl::MountRemovableDeviceCallback, | |
104 this); | |
105 } | 105 } |
106 | 106 |
107 virtual void UnmountPath(const char* device_path) OVERRIDE { | 107 virtual void UnmountPath(const char* path) OVERRIDE { |
108 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 108 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
109 if (!CrosLibrary::Get()->EnsureLoaded()) { | 109 if (!CrosLibrary::Get()->EnsureLoaded()) { |
110 OnUnmountRemovableDevice(device_path, | 110 OnUnmountPath(path, |
111 MOUNT_METHOD_ERROR_LOCAL, | 111 MOUNT_METHOD_ERROR_LOCAL, |
112 kLibraryNotLoaded); | 112 kLibraryNotLoaded); |
113 return; | 113 return; |
114 } | 114 } |
115 UnmountRemovableDevice(device_path, | 115 UnmountMountPoint(path, &MountLibraryImpl::UnmountMountPointCallback, this); |
116 &MountLibraryImpl::UnmountRemovableDeviceCallback, | |
117 this); | |
118 } | 116 } |
119 | 117 |
120 virtual void UnmountDeviceRecursive(const char* device_path, | 118 virtual void UnmountDeviceRecursive(const char* device_path, |
121 UnmountDeviceRecursiveCallbackType callback, void* user_data) | 119 UnmountDeviceRecursiveCallbackType callback, void* user_data) |
122 OVERRIDE { | 120 OVERRIDE { |
123 bool success = true; | 121 bool success = true; |
124 const char* error_message = NULL; | 122 const char* error_message = NULL; |
125 std::vector<const char*> devices_to_unmount; | 123 std::vector<const char*> devices_to_unmount; |
126 | 124 |
127 if (!CrosLibrary::Get()->EnsureLoaded()) { | 125 if (!CrosLibrary::Get()->EnsureLoaded()) { |
(...skipping 16 matching lines...) Expand all Loading... | |
144 } | 142 } |
145 } | 143 } |
146 | 144 |
147 if (success) { | 145 if (success) { |
148 // We will send the same callback data object to all Unmount calls and use | 146 // We will send the same callback data object to all Unmount calls and use |
149 // it to syncronize callbacks. | 147 // it to syncronize callbacks. |
150 UnmountDeviceRecursiveCallbackData* | 148 UnmountDeviceRecursiveCallbackData* |
151 cb_data = new UnmountDeviceRecursiveCallbackData(this, user_data, | 149 cb_data = new UnmountDeviceRecursiveCallbackData(this, user_data, |
152 callback, devices_to_unmount.size()); | 150 callback, devices_to_unmount.size()); |
153 for (std::vector<const char*>::iterator it = devices_to_unmount.begin(); | 151 for (std::vector<const char*>::iterator it = devices_to_unmount.begin(); |
154 it != devices_to_unmount.end(); | 152 it != devices_to_unmount.end(); |
155 ++it) { | 153 ++it) { |
156 UnmountRemovableDevice(*it, | 154 UnmountMountPoint( |
155 *it, | |
157 &MountLibraryImpl::UnmountDeviceRecursiveCallback, | 156 &MountLibraryImpl::UnmountDeviceRecursiveCallback, |
158 cb_data); | 157 cb_data); |
159 } | 158 } |
160 } else { | 159 } else { |
161 LOG(WARNING) << "Unmount recursive request failed for device " | 160 LOG(WARNING) << "Unmount recursive request failed for device " |
162 << device_path << ", with error: " << error_message; | 161 << device_path << ", with error: " << error_message; |
163 callback(user_data, false); | 162 callback(user_data, false); |
164 } | 163 } |
165 } | 164 } |
166 | 165 |
167 virtual void RequestMountInfoRefresh() OVERRIDE { | 166 virtual void RequestMountInfoRefresh() OVERRIDE { |
168 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 167 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
169 if (!CrosLibrary::Get()->EnsureLoaded()) { | 168 if (!CrosLibrary::Get()->EnsureLoaded()) { |
170 OnRequestMountInfo(NULL, | 169 OnRequestMountInfo(NULL, |
171 0, | 170 0, |
172 MOUNT_METHOD_ERROR_LOCAL, | 171 MOUNT_METHOD_ERROR_LOCAL, |
173 kLibraryNotLoaded); | 172 kLibraryNotLoaded); |
174 return; | 173 return; |
175 } | 174 } |
176 RequestMountInfo(&MountLibraryImpl::RequestMountInfoCallback, | 175 RequestMountInfo(&MountLibraryImpl::RequestMountInfoCallback, |
177 this); | 176 this); |
178 } | 177 } |
179 | 178 |
180 const DiskMap& disks() const OVERRIDE { return disks_; } | 179 const DiskMap& disks() const OVERRIDE { return disks_; } |
180 const MountPointMap& mount_points() const OVERRIDE { return mount_points_; } | |
181 | 181 |
182 private: | 182 private: |
183 // Callback for MountRemovableDevice method. | 183 // Callback for MountComplete signal and MountSourcePath method. |
184 static void MountRemovableDeviceCallback(void* object, | 184 static void MountCompletedHandler(void* object, |
185 const char* device_path, | 185 MountError error_code, |
186 const char* mount_path, | 186 const char* source_path, |
187 MountMethodErrorType error, | 187 MountType type, |
188 const char* error_message) { | 188 const char* mount_path) { |
189 LOG(WARNING) <<"MountCompleted" <<error_code<<source_path<<mount_path; | |
189 DCHECK(object); | 190 DCHECK(object); |
190 MountLibraryImpl* self = static_cast<MountLibraryImpl*>(object); | 191 MountLibraryImpl* self = static_cast<MountLibraryImpl*>(object); |
191 self->OnMountRemovableDevice(device_path, | 192 self->OnMountCompleted(static_cast<MountError>(error_code), |
192 mount_path, | 193 source_path, |
193 error, | 194 static_cast<MountType>(type), |
194 error_message); | 195 mount_path); |
195 } | 196 } |
196 | 197 |
197 // Callback for UnmountRemovableDevice method. | 198 // Callback for UnmountRemovableDevice method. |
198 static void UnmountRemovableDeviceCallback(void* object, | 199 static void UnmountMountPointCallback(void* object, |
199 const char* device_path, | 200 const char* device_path, |
200 const char* mount_path, | 201 MountMethodErrorType error, |
201 MountMethodErrorType error, | 202 const char* error_message) { |
202 const char* error_message) { | |
203 DCHECK(object); | 203 DCHECK(object); |
204 MountLibraryImpl* self = static_cast<MountLibraryImpl*>(object); | 204 MountLibraryImpl* self = static_cast<MountLibraryImpl*>(object); |
205 self->OnUnmountRemovableDevice(device_path, | 205 self->OnUnmountPath(device_path, error, error_message); |
206 error, | |
207 error_message); | |
208 } | 206 } |
209 | 207 |
210 // Callback for UnmountDeviceRecursive. | 208 // Callback for UnmountDeviceRecursive. |
211 static void UnmountDeviceRecursiveCallback(void* object, | 209 static void UnmountDeviceRecursiveCallback(void* object, |
212 const char* device_path, | 210 const char* device_path, |
213 const char* mount_path, | |
214 MountMethodErrorType error, | 211 MountMethodErrorType error, |
215 const char* error_message) { | 212 const char* error_message) { |
216 DCHECK(object); | 213 DCHECK(object); |
217 UnmountDeviceRecursiveCallbackData* cb_data = | 214 UnmountDeviceRecursiveCallbackData* cb_data = |
218 static_cast<UnmountDeviceRecursiveCallbackData*>(object); | 215 static_cast<UnmountDeviceRecursiveCallbackData*>(object); |
219 | 216 |
220 // Do standard processing for Unmount event. | 217 // Do standard processing for Unmount event. |
221 cb_data->object->OnUnmountRemovableDevice(device_path, | 218 cb_data->object->OnUnmountPath(device_path, |
222 error, | 219 error, |
223 error_message); | 220 error_message); |
224 if (error == MOUNT_METHOD_ERROR_LOCAL) { | 221 if (error == MOUNT_METHOD_ERROR_LOCAL) { |
225 cb_data->success = false; | 222 cb_data->success = false; |
226 } else if (error == MOUNT_METHOD_ERROR_NONE) { | 223 } else if (error == MOUNT_METHOD_ERROR_NONE) { |
227 LOG(WARNING) << device_path << " unmounted."; | 224 LOG(WARNING) << device_path << " unmounted."; |
228 } | 225 } |
229 | 226 |
230 // This is safe as long as all callbacks are called on the same thread as | 227 // This is safe as long as all callbacks are called on the same thread as |
231 // UnmountDeviceRecursive. | 228 // UnmountDeviceRecursive. |
232 cb_data->pending_callbacks_count--; | 229 cb_data->pending_callbacks_count--; |
233 | 230 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
268 // This method will receive events that are caused by drive status changes. | 265 // This method will receive events that are caused by drive status changes. |
269 static void MonitorMountEventsHandler(void* object, | 266 static void MonitorMountEventsHandler(void* object, |
270 MountEventType evt, | 267 MountEventType evt, |
271 const char* device_path) { | 268 const char* device_path) { |
272 DCHECK(object); | 269 DCHECK(object); |
273 MountLibraryImpl* self = static_cast<MountLibraryImpl*>(object); | 270 MountLibraryImpl* self = static_cast<MountLibraryImpl*>(object); |
274 self->OnMountEvent(evt, device_path); | 271 self->OnMountEvent(evt, device_path); |
275 } | 272 } |
276 | 273 |
277 | 274 |
278 void OnMountRemovableDevice(const char* device_path, | 275 void OnMountCompleted(MountError error_code, |
279 const char* mount_path, | 276 const char* source_path, |
280 MountMethodErrorType error, | 277 MountType type, |
281 const char* error_message) { | 278 const char* mount_path) { |
282 DCHECK(device_path); | 279 DCHECK(source_path); |
283 | 280 |
284 if (error == MOUNT_METHOD_ERROR_NONE && device_path && mount_path) { | 281 FireMountCompleted(error_code, source_path, type, mount_path); |
285 std::string path(device_path); | 282 |
283 if (error_code == MOUNT_ERROR_NONE && | |
284 mount_points_.find(mount_path) == mount_points_.end()) { | |
285 mount_points_.insert(std::pair<std::string, MountPointInfo>(mount_path, | |
286 MountPointInfo(source_path, type))); | |
287 } | |
288 | |
289 if (error_code == MOUNT_ERROR_NONE && type == MOUNT_TYPE_DEVICE && | |
290 source_path && mount_path) { | |
291 std::string path(source_path); | |
286 DiskMap::iterator iter = disks_.find(path); | 292 DiskMap::iterator iter = disks_.find(path); |
287 if (iter == disks_.end()) { | 293 if (iter == disks_.end()) { |
288 // disk might have been removed by now? | 294 // disk might have been removed by now? |
289 return; | 295 return; |
290 } | 296 } |
291 Disk* disk = iter->second; | 297 Disk* disk = iter->second; |
292 DCHECK(disk); | 298 DCHECK(disk); |
293 disk->set_mount_path(mount_path); | 299 disk->set_mount_path(mount_path); |
294 FireDiskStatusUpdate(MOUNT_DISK_MOUNTED, disk); | 300 FireDiskStatusUpdate(MOUNT_DISK_MOUNTED, disk); |
295 } else { | |
296 LOG(WARNING) << "Mount request failed for device " | |
297 << device_path << ", with error: " | |
298 << (error_message ? error_message : "Unknown"); | |
299 } | 301 } |
300 } | 302 } |
301 | 303 |
302 void OnUnmountRemovableDevice(const char* device_path, | 304 void RemoveFromMountPoints(const char* source_path) { |
303 MountMethodErrorType error, | 305 for (MountPointMap::iterator it = mount_points_.begin(); |
304 const char* error_message) { | 306 it != mount_points_.end(); |
305 DCHECK(device_path); | 307 ++it) { |
306 if (error == MOUNT_METHOD_ERROR_NONE && device_path) { | 308 if (strcmp(it->second.first.c_str(), source_path) == 0) { |
307 std::string path(device_path); | 309 mount_points_.erase(it); |
310 return; | |
311 } | |
312 } | |
313 } | |
314 | |
315 void OnUnmountPath(const char* source_path, | |
316 MountMethodErrorType error, | |
317 const char* error_message) { | |
318 DCHECK(source_path); | |
319 | |
320 RemoveFromMountPoints(source_path); | |
321 | |
322 if (error == MOUNT_METHOD_ERROR_NONE && source_path) { | |
323 std::string path(source_path); | |
308 DiskMap::iterator iter = disks_.find(path); | 324 DiskMap::iterator iter = disks_.find(path); |
309 if (iter == disks_.end()) { | 325 if (iter == disks_.end()) { |
310 // disk might have been removed by now? | 326 // disk might have been removed by now? |
311 return; | 327 return; |
312 } | 328 } |
313 Disk* disk = iter->second; | 329 Disk* disk = iter->second; |
314 DCHECK(disk); | 330 DCHECK(disk); |
315 disk->clear_mount_path(); | 331 disk->clear_mount_path(); |
316 FireDiskStatusUpdate(MOUNT_DISK_UNMOUNTED, disk); | 332 FireDiskStatusUpdate(MOUNT_DISK_UNMOUNTED, disk); |
317 } else { | 333 } else { |
318 LOG(WARNING) << "Unmount request failed for device " | 334 LOG(WARNING) << "Unmount request failed for device " |
319 << device_path << ", with error: " | 335 << source_path << ", with error: " |
320 << (error_message ? error_message : "Unknown"); | 336 << (error_message ? error_message : "Unknown"); |
321 } | 337 } |
322 } | 338 } |
323 | 339 |
324 void OnGetDiskProperties(const char* device_path, | 340 void OnGetDiskProperties(const char* device_path, |
325 const DiskInfo* disk1, | 341 const DiskInfo* disk1, |
326 MountMethodErrorType error, | 342 MountMethodErrorType error, |
327 const char* error_message) { | 343 const char* error_message) { |
328 DCHECK(device_path); | 344 DCHECK(device_path); |
329 if (error == MOUNT_METHOD_ERROR_NONE && device_path) { | 345 if (error == MOUNT_METHOD_ERROR_NONE && device_path) { |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
469 break; | 485 break; |
470 } | 486 } |
471 case DEVICE_REMOVED: { | 487 case DEVICE_REMOVED: { |
472 type = MOUNT_DEVICE_REMOVED; | 488 type = MOUNT_DEVICE_REMOVED; |
473 break; | 489 break; |
474 } | 490 } |
475 case DEVICE_SCANNED: { | 491 case DEVICE_SCANNED: { |
476 type = MOUNT_DEVICE_SCANNED; | 492 type = MOUNT_DEVICE_SCANNED; |
477 break; | 493 break; |
478 } | 494 } |
495 default: { | |
tbarzic
2011/07/22 06:21:51
Will remove { }
| |
496 return; | |
497 } | |
479 } | 498 } |
480 FireDeviceStatusUpdate(type, std::string(device_path)); | 499 FireDeviceStatusUpdate(type, std::string(device_path)); |
481 } | 500 } |
482 | 501 |
483 void Init() { | 502 void Init() { |
484 // Getting the monitor status so that the daemon starts up. | 503 // Getting the monitor status so that the daemon starts up. |
485 mount_status_connection_ = MonitorMountEvents( | 504 mount_status_connection_ = MonitorAllMountEvents( |
486 &MonitorMountEventsHandler, this); | 505 &MonitorMountEventsHandler, &MountCompletedHandler, this); |
487 } | 506 } |
488 | 507 |
489 void FireDiskStatusUpdate(MountLibraryEventType evt, | 508 void FireDiskStatusUpdate(MountLibraryEventType evt, |
490 const Disk* disk) { | 509 const Disk* disk) { |
491 // Make sure we run on UI thread. | 510 // Make sure we run on UI thread. |
492 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 511 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
493 FOR_EACH_OBSERVER( | 512 FOR_EACH_OBSERVER( |
494 Observer, observers_, DiskChanged(evt, disk)); | 513 Observer, observers_, DiskChanged(evt, disk)); |
495 } | 514 } |
496 | 515 |
497 void FireDeviceStatusUpdate(MountLibraryEventType evt, | 516 void FireDeviceStatusUpdate(MountLibraryEventType evt, |
498 const std::string& device_path) { | 517 const std::string& device_path) { |
499 // Make sure we run on UI thread. | 518 // Make sure we run on UI thread. |
500 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 519 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
501 FOR_EACH_OBSERVER( | 520 FOR_EACH_OBSERVER( |
502 Observer, observers_, DeviceChanged(evt, device_path)); | 521 Observer, observers_, DeviceChanged(evt, device_path)); |
503 } | 522 } |
504 | 523 |
524 void FireMountCompleted(MountError error_code, | |
525 const char* source_path, | |
526 MountType mount_type, | |
527 const char* mount_path) { | |
528 // Make sure we run on UI thread. | |
529 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
530 FOR_EACH_OBSERVER( | |
531 Observer, observers_, MountCompleted(error_code, | |
532 source_path, | |
533 mount_type, | |
534 mount_path)); | |
535 } | |
536 | |
505 // Mount event change observers. | 537 // Mount event change observers. |
506 ObserverList<Observer> observers_; | 538 ObserverList<Observer> observers_; |
507 | 539 |
508 // A reference to the mount api, to allow callbacks when the mount | 540 // A reference to the mount api, to allow callbacks when the mount |
509 // status changes. | 541 // status changes. |
510 MountEventConnection mount_status_connection_; | 542 MountEventConnection mount_status_connection_; |
511 | 543 |
512 // The list of disks found. | 544 // The list of disks found. |
513 MountLibrary::DiskMap disks_; | 545 MountLibrary::DiskMap disks_; |
514 | 546 |
547 MountLibrary::MountPointMap mount_points_; | |
548 | |
515 DISALLOW_COPY_AND_ASSIGN(MountLibraryImpl); | 549 DISALLOW_COPY_AND_ASSIGN(MountLibraryImpl); |
516 }; | 550 }; |
517 | 551 |
518 class MountLibraryStubImpl : public MountLibrary { | 552 class MountLibraryStubImpl : public MountLibrary { |
519 public: | 553 public: |
520 MountLibraryStubImpl() {} | 554 MountLibraryStubImpl() {} |
521 virtual ~MountLibraryStubImpl() {} | 555 virtual ~MountLibraryStubImpl() {} |
522 | 556 |
523 // MountLibrary overrides. | 557 // MountLibrary overrides. |
524 virtual void AddObserver(Observer* observer) OVERRIDE {} | 558 virtual void AddObserver(Observer* observer) OVERRIDE {} |
525 virtual void RemoveObserver(Observer* observer) OVERRIDE {} | 559 virtual void RemoveObserver(Observer* observer) OVERRIDE {} |
526 virtual const DiskMap& disks() const OVERRIDE { return disks_; } | 560 virtual const DiskMap& disks() const OVERRIDE { return disks_; } |
561 virtual const MountPointMap& mount_points() const OVERRIDE { | |
562 return mount_points_; | |
563 } | |
527 virtual void RequestMountInfoRefresh() OVERRIDE {} | 564 virtual void RequestMountInfoRefresh() OVERRIDE {} |
528 virtual void MountPath(const char* device_path) OVERRIDE {} | 565 virtual void MountPath(const char* source_path, MountType type, |
529 virtual void UnmountPath(const char* device_path) OVERRIDE {} | 566 const MountPathOptions& options) OVERRIDE {} |
567 virtual void UnmountPath(const char* path) OVERRIDE {} | |
530 virtual void UnmountDeviceRecursive(const char* device_path, | 568 virtual void UnmountDeviceRecursive(const char* device_path, |
531 UnmountDeviceRecursiveCallbackType callback, void* user_data) | 569 UnmountDeviceRecursiveCallbackType callback, void* user_data) |
532 OVERRIDE {} | 570 OVERRIDE {} |
533 | 571 |
534 private: | 572 private: |
535 // The list of disks found. | 573 // The list of disks found. |
536 DiskMap disks_; | 574 DiskMap disks_; |
575 MountPointMap mount_points_; | |
537 | 576 |
538 DISALLOW_COPY_AND_ASSIGN(MountLibraryStubImpl); | 577 DISALLOW_COPY_AND_ASSIGN(MountLibraryStubImpl); |
539 }; | 578 }; |
540 | 579 |
541 // static | 580 // static |
542 MountLibrary* MountLibrary::GetImpl(bool stub) { | 581 MountLibrary* MountLibrary::GetImpl(bool stub) { |
543 if (stub) | 582 if (stub) |
544 return new MountLibraryStubImpl(); | 583 return new MountLibraryStubImpl(); |
545 else | 584 else |
546 return new MountLibraryImpl(); | 585 return new MountLibraryImpl(); |
547 } | 586 } |
548 | 587 |
549 } // namespace chromeos | 588 } // namespace chromeos |
550 | 589 |
551 // Allows InvokeLater without adding refcounting. This class is a Singleton and | 590 // Allows InvokeLater without adding refcounting. This class is a Singleton and |
552 // won't be deleted until it's last InvokeLater is run. | 591 // won't be deleted until it's last InvokeLater is run. |
553 DISABLE_RUNNABLE_METHOD_REFCOUNT(chromeos::MountLibraryImpl); | 592 DISABLE_RUNNABLE_METHOD_REFCOUNT(chromeos::MountLibraryImpl); |
554 | 593 |
OLD | NEW |