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 #include <vector> | |
8 | 9 |
9 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
10 #include "base/string_util.h" | 11 #include "base/string_util.h" |
11 #include "chrome/browser/chromeos/cros/cros_library.h" | 12 #include "chrome/browser/chromeos/cros/cros_library.h" |
12 #include "content/browser/browser_thread.h" | 13 #include "content/browser/browser_thread.h" |
13 | 14 |
14 const char* kLibraryNotLoaded = "Cros Library not loaded"; | 15 const char* kLibraryNotLoaded = "Cros Library not loaded"; |
15 const char* kDeviceNotFound = "Device could not be found"; | 16 const char* kDeviceNotFound = "Device could not be found"; |
16 | 17 |
17 namespace chromeos { | 18 namespace chromeos { |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
110 OnUnmountRemovableDevice(device_path, | 111 OnUnmountRemovableDevice(device_path, |
111 MOUNT_METHOD_ERROR_LOCAL, | 112 MOUNT_METHOD_ERROR_LOCAL, |
112 kLibraryNotLoaded); | 113 kLibraryNotLoaded); |
113 return; | 114 return; |
114 } | 115 } |
115 UnmountRemovableDevice(device_path, | 116 UnmountRemovableDevice(device_path, |
116 &MountLibraryImpl::UnmountRemovableDeviceCallback, | 117 &MountLibraryImpl::UnmountRemovableDeviceCallback, |
117 this); | 118 this); |
118 } | 119 } |
119 | 120 |
121 virtual void FormatUnmountedDevice(const char* file_path) OVERRIDE { | |
122 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
123 if (!CrosLibrary::Get()->EnsureLoaded()) { | |
124 OnFormatDevice(file_path, | |
125 false, | |
126 MOUNT_METHOD_ERROR_LOCAL, | |
127 kLibraryNotLoaded); | |
128 return; | |
129 } | |
130 for (MountLibrary::DiskMap::iterator it = disks_.begin(); | |
131 it != disks_.end(); ++it) { | |
132 if (it->second->file_path().compare(file_path) == 0 && | |
133 !it->second->mount_path().empty()) { | |
134 OnFormatDevice(file_path, | |
135 false, | |
136 MOUNT_METHOD_ERROR_LOCAL, | |
137 "Device is still mounted."); | |
138 } | |
139 } | |
140 FormatDevice(file_path, | |
141 "vfat", // currently format in vfat by default | |
142 &MountLibraryImpl::FormatDeviceCallback, | |
143 this); | |
144 } | |
145 | |
146 virtual void FormatMountedDevice(const char* mount_path) OVERRIDE { | |
147 DCHECK(mount_path); | |
148 std::string device_path, file_path; | |
149 for (MountLibrary::DiskMap::iterator it = disks_.begin(); | |
150 it != disks_.end(); ++it) { | |
151 if (it->second->mount_path().compare(mount_path) == 0) { | |
152 device_path = it->second->device_path(); | |
153 file_path = it->second->file_path(); | |
tbarzic
2011/07/26 22:49:50
are you missing break here?
sidor
2011/07/28 00:20:51
Done.
| |
154 } | |
155 } | |
156 if (device_path.empty()) { | |
157 OnFormatDevice(device_path.c_str(), | |
158 false, | |
159 MOUNT_METHOD_ERROR_LOCAL, | |
160 "Device with this mount path not found."); | |
161 return; | |
162 } | |
163 if (formatting_pending_.find(device_path) != formatting_pending_.end()) { | |
164 OnFormatDevice(device_path.c_str(), | |
165 false, | |
166 MOUNT_METHOD_ERROR_LOCAL, | |
167 "Formatting is already pending."); | |
168 return; | |
169 } | |
170 // Formatting process continues, after unmounting. | |
171 formatting_pending_[device_path] = file_path; | |
172 UnmountPath(device_path.c_str()); | |
173 } | |
174 | |
120 virtual void UnmountDeviceRecursive(const char* device_path, | 175 virtual void UnmountDeviceRecursive(const char* device_path, |
121 UnmountDeviceRecursiveCallbackType callback, void* user_data) | 176 UnmountDeviceRecursiveCallbackType callback, void* user_data) |
122 OVERRIDE { | 177 OVERRIDE { |
123 bool success = true; | 178 bool success = true; |
124 const char* error_message = NULL; | 179 const char* error_message = NULL; |
125 std::vector<const char*> devices_to_unmount; | 180 std::vector<const char*> devices_to_unmount; |
126 | 181 |
127 if (!CrosLibrary::Get()->EnsureLoaded()) { | 182 if (!CrosLibrary::Get()->EnsureLoaded()) { |
128 success = false; | 183 success = false; |
129 error_message = kLibraryNotLoaded; | 184 error_message = kLibraryNotLoaded; |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
200 const char* mount_path, | 255 const char* mount_path, |
201 MountMethodErrorType error, | 256 MountMethodErrorType error, |
202 const char* error_message) { | 257 const char* error_message) { |
203 DCHECK(object); | 258 DCHECK(object); |
204 MountLibraryImpl* self = static_cast<MountLibraryImpl*>(object); | 259 MountLibraryImpl* self = static_cast<MountLibraryImpl*>(object); |
205 self->OnUnmountRemovableDevice(device_path, | 260 self->OnUnmountRemovableDevice(device_path, |
206 error, | 261 error, |
207 error_message); | 262 error_message); |
208 } | 263 } |
209 | 264 |
265 // Callback for FormatRemovableDevice method. | |
266 static void FormatDeviceCallback(void* object, | |
267 const char* device_path, | |
268 bool success, | |
269 MountMethodErrorType error, | |
270 const char* error_message) { | |
271 DCHECK(object); | |
272 MountLibraryImpl* self = static_cast<MountLibraryImpl*>(object); | |
273 self->OnFormatDevice(device_path, success, error, error_message); | |
274 } | |
275 | |
210 // Callback for UnmountDeviceRecursive. | 276 // Callback for UnmountDeviceRecursive. |
211 static void UnmountDeviceRecursiveCallback(void* object, | 277 static void UnmountDeviceRecursiveCallback(void* object, |
212 const char* device_path, | 278 const char* device_path, |
213 const char* mount_path, | 279 const char* mount_path, |
214 MountMethodErrorType error, | 280 MountMethodErrorType error, |
215 const char* error_message) { | 281 const char* error_message) { |
216 DCHECK(object); | 282 DCHECK(object); |
217 UnmountDeviceRecursiveCallbackData* cb_data = | 283 UnmountDeviceRecursiveCallbackData* cb_data = |
218 static_cast<UnmountDeviceRecursiveCallbackData*>(object); | 284 static_cast<UnmountDeviceRecursiveCallbackData*>(object); |
219 | 285 |
220 // Do standard processing for Unmount event. | 286 // Do standard processing for Unmount event. |
221 cb_data->object->OnUnmountRemovableDevice(device_path, | 287 cb_data->object->OnUnmountRemovableDevice(device_path, |
222 error, | 288 error, |
223 error_message); | 289 error_message); |
224 if (error == MOUNT_METHOD_ERROR_LOCAL) { | 290 if (error == MOUNT_METHOD_ERROR_LOCAL) { |
225 cb_data->success = false; | 291 cb_data->success = false; |
226 } else if (error == MOUNT_METHOD_ERROR_NONE) { | 292 } else if (error == MOUNT_METHOD_ERROR_NONE) { |
227 LOG(WARNING) << device_path << " unmounted."; | 293 LOG(WARNING) << device_path << " unmounted."; |
228 } | 294 } |
229 | 295 |
230 // This is safe as long as all callbacks are called on the same thread as | 296 // This is safe as long as all callbacks are called on the same thread as |
231 // UnmountDeviceRecursive. | 297 // UnmountDeviceRecursive. |
232 cb_data->pending_callbacks_count--; | 298 cb_data->pending_callbacks_count--; |
233 | 299 |
234 if (cb_data->pending_callbacks_count == 0) { | 300 if (cb_data->pending_callbacks_count == 0) { |
235 cb_data->callback(cb_data->user_data, cb_data->success); | 301 cb_data->callback(cb_data->user_data, cb_data->success); |
236 delete cb_data; | 302 delete cb_data; |
237 } | 303 } |
238 } | 304 } |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
307 std::string path(device_path); | 373 std::string path(device_path); |
308 DiskMap::iterator iter = disks_.find(path); | 374 DiskMap::iterator iter = disks_.find(path); |
309 if (iter == disks_.end()) { | 375 if (iter == disks_.end()) { |
310 // disk might have been removed by now? | 376 // disk might have been removed by now? |
311 return; | 377 return; |
312 } | 378 } |
313 Disk* disk = iter->second; | 379 Disk* disk = iter->second; |
314 DCHECK(disk); | 380 DCHECK(disk); |
315 disk->clear_mount_path(); | 381 disk->clear_mount_path(); |
316 FireDiskStatusUpdate(MOUNT_DISK_UNMOUNTED, disk); | 382 FireDiskStatusUpdate(MOUNT_DISK_UNMOUNTED, disk); |
383 // Check if there is a formatting scheduled | |
384 PathMap::iterator it = formatting_pending_.find(device_path); | |
385 if (it != formatting_pending_.end()) { | |
386 const std::string file_path = it->second; | |
387 formatting_pending_.erase(it); | |
388 FormatUnmountedDevice(file_path.c_str()); | |
389 } | |
317 } else { | 390 } else { |
318 LOG(WARNING) << "Unmount request failed for device " | 391 LOG(WARNING) << "Unmount request failed for device " |
319 << device_path << ", with error: " | 392 << device_path << ", with error: " |
320 << (error_message ? error_message : "Unknown"); | 393 << (error_message ? error_message : "Unknown"); |
321 } | 394 } |
322 } | 395 } |
323 | 396 |
397 void OnFormatDevice(const char* device_path, | |
398 bool success, | |
399 MountMethodErrorType error, | |
400 const char* error_message) { | |
401 DCHECK(device_path); | |
402 | |
403 if (error == MOUNT_METHOD_ERROR_NONE && device_path && success) { | |
404 FireDeviceStatusUpdate(MOUNT_FORMATTING_STARTED, device_path); | |
405 } else { | |
406 FireDeviceStatusUpdate(MOUNT_FORMATTING_STARTED, | |
407 std::string("!") + device_path); | |
408 LOG(WARNING) << "Format request failed for device " | |
409 << device_path << ", with error: " | |
410 << (error_message ? error_message : "Unknown"); | |
411 } | |
412 } | |
413 | |
414 | |
324 void OnGetDiskProperties(const char* device_path, | 415 void OnGetDiskProperties(const char* device_path, |
325 const DiskInfo* disk1, | 416 const DiskInfo* disk1, |
326 MountMethodErrorType error, | 417 MountMethodErrorType error, |
327 const char* error_message) { | 418 const char* error_message) { |
328 DCHECK(device_path); | 419 DCHECK(device_path); |
329 if (error == MOUNT_METHOD_ERROR_NONE && device_path) { | 420 if (error == MOUNT_METHOD_ERROR_NONE && device_path) { |
330 // TODO(zelidrag): Find a better way to filter these out before we | 421 // TODO(zelidrag): Find a better way to filter these out before we |
331 // fetch the properties: | 422 // fetch the properties: |
332 // Ignore disks coming from the device we booted the system from. | 423 // Ignore disks coming from the device we booted the system from. |
333 | 424 |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
469 break; | 560 break; |
470 } | 561 } |
471 case DEVICE_REMOVED: { | 562 case DEVICE_REMOVED: { |
472 type = MOUNT_DEVICE_REMOVED; | 563 type = MOUNT_DEVICE_REMOVED; |
473 break; | 564 break; |
474 } | 565 } |
475 case DEVICE_SCANNED: { | 566 case DEVICE_SCANNED: { |
476 type = MOUNT_DEVICE_SCANNED; | 567 type = MOUNT_DEVICE_SCANNED; |
477 break; | 568 break; |
478 } | 569 } |
570 case FORMATTING_FINISHED: { | |
571 type = MOUNT_FORMATTING_FINISHED; | |
572 break; | |
573 } | |
479 } | 574 } |
480 FireDeviceStatusUpdate(type, std::string(device_path)); | 575 FireDeviceStatusUpdate(type, std::string(device_path)); |
481 } | 576 } |
482 | 577 |
483 void Init() { | 578 void Init() { |
484 // Getting the monitor status so that the daemon starts up. | 579 // Getting the monitor status so that the daemon starts up. |
485 mount_status_connection_ = MonitorMountEvents( | 580 mount_status_connection_ = MonitorMountEvents( |
486 &MonitorMountEventsHandler, this); | 581 &MonitorMountEventsHandler, this); |
487 } | 582 } |
488 | 583 |
(...skipping 16 matching lines...) Expand all Loading... | |
505 // Mount event change observers. | 600 // Mount event change observers. |
506 ObserverList<Observer> observers_; | 601 ObserverList<Observer> observers_; |
507 | 602 |
508 // A reference to the mount api, to allow callbacks when the mount | 603 // A reference to the mount api, to allow callbacks when the mount |
509 // status changes. | 604 // status changes. |
510 MountEventConnection mount_status_connection_; | 605 MountEventConnection mount_status_connection_; |
511 | 606 |
512 // The list of disks found. | 607 // The list of disks found. |
513 MountLibrary::DiskMap disks_; | 608 MountLibrary::DiskMap disks_; |
514 | 609 |
610 // Set of devices that are supposed to be formated, but are currently waiting | |
611 // to be unmounted. When device is in this map, the formatting process HAVEN'T | |
612 // started yet. | |
613 PathMap formatting_pending_; | |
614 | |
515 DISALLOW_COPY_AND_ASSIGN(MountLibraryImpl); | 615 DISALLOW_COPY_AND_ASSIGN(MountLibraryImpl); |
516 }; | 616 }; |
517 | 617 |
518 class MountLibraryStubImpl : public MountLibrary { | 618 class MountLibraryStubImpl : public MountLibrary { |
519 public: | 619 public: |
520 MountLibraryStubImpl() {} | 620 MountLibraryStubImpl() {} |
521 virtual ~MountLibraryStubImpl() {} | 621 virtual ~MountLibraryStubImpl() {} |
522 | 622 |
523 // MountLibrary overrides. | 623 // MountLibrary overrides. |
524 virtual void AddObserver(Observer* observer) OVERRIDE {} | 624 virtual void AddObserver(Observer* observer) OVERRIDE {} |
525 virtual void RemoveObserver(Observer* observer) OVERRIDE {} | 625 virtual void RemoveObserver(Observer* observer) OVERRIDE {} |
526 virtual const DiskMap& disks() const OVERRIDE { return disks_; } | 626 virtual const DiskMap& disks() const OVERRIDE { return disks_; } |
527 virtual void RequestMountInfoRefresh() OVERRIDE {} | 627 virtual void RequestMountInfoRefresh() OVERRIDE {} |
528 virtual void MountPath(const char* device_path) OVERRIDE {} | 628 virtual void MountPath(const char* device_path) OVERRIDE {} |
529 virtual void UnmountPath(const char* device_path) OVERRIDE {} | 629 virtual void UnmountPath(const char* device_path) OVERRIDE {} |
630 virtual void FormatUnmountedDevice(const char* device_path) OVERRIDE {} | |
631 virtual void FormatMountedDevice(const char* mount_path) OVERRIDE {} | |
530 virtual void UnmountDeviceRecursive(const char* device_path, | 632 virtual void UnmountDeviceRecursive(const char* device_path, |
531 UnmountDeviceRecursiveCallbackType callback, void* user_data) | 633 UnmountDeviceRecursiveCallbackType callback, void* user_data) |
532 OVERRIDE {} | 634 OVERRIDE {} |
533 | 635 |
534 private: | 636 private: |
535 // The list of disks found. | 637 // The list of disks found. |
536 DiskMap disks_; | 638 DiskMap disks_; |
537 | 639 |
538 DISALLOW_COPY_AND_ASSIGN(MountLibraryStubImpl); | 640 DISALLOW_COPY_AND_ASSIGN(MountLibraryStubImpl); |
539 }; | 641 }; |
540 | 642 |
541 // static | 643 // static |
542 MountLibrary* MountLibrary::GetImpl(bool stub) { | 644 MountLibrary* MountLibrary::GetImpl(bool stub) { |
543 if (stub) | 645 if (stub) |
544 return new MountLibraryStubImpl(); | 646 return new MountLibraryStubImpl(); |
545 else | 647 else |
546 return new MountLibraryImpl(); | 648 return new MountLibraryImpl(); |
547 } | 649 } |
548 | 650 |
549 } // namespace chromeos | 651 } // namespace chromeos |
550 | 652 |
551 // Allows InvokeLater without adding refcounting. This class is a Singleton and | 653 // Allows InvokeLater without adding refcounting. This class is a Singleton and |
552 // won't be deleted until it's last InvokeLater is run. | 654 // won't be deleted until it's last InvokeLater is run. |
553 DISABLE_RUNNABLE_METHOD_REFCOUNT(chromeos::MountLibraryImpl); | 655 DISABLE_RUNNABLE_METHOD_REFCOUNT(chromeos::MountLibraryImpl); |
554 | 656 |
OLD | NEW |