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

Side by Side Diff: chrome/browser/chromeos/cros/mount_library.cc

Issue 7471024: Formatting feature initial commit for ChromeOS Tree (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Created 9 years, 5 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 | Annotate | Revision Log
OLDNEW
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
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* device_path) OVERRIDE {
122 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
achuithb 2011/07/21 00:58:21 DCHECK?
sidor 2011/07/21 18:24:15 Done.
123 if (!CrosLibrary::Get()->EnsureLoaded()) {
124 OnFormatDevice(device_path,
125 false,
126 MOUNT_METHOD_ERROR_LOCAL,
127 kLibraryNotLoaded);
128 return;
129 }
130 FormatDevice(device_path,
131 "vfat", // currently format in vfat by default
132 &MountLibraryImpl::FormatDeviceCallback,
133 this);
134 }
135
136 virtual void FormatMountedDevice(const char* device_mount_path) OVERRIDE {
137 DCHECK(device_mount_path);
138 std::string device_path = "";
139 std::string file_path = "";
achuithb 2011/07/21 00:58:21 Don't need = "", since the empty string is the def
sidor 2011/07/21 18:24:15 Done.
140 for (MountLibrary::DiskMap::iterator it = disks_.begin();
141 it != disks_.end(); ++it) {
142 if (it->second->mount_path().compare(device_mount_path) == 0) {
143 device_path = it->second->device_path();
144 file_path = it->second->file_path();
145 }
146 }
147 if (device_path.compare("") == 0) {
achuithb 2011/07/21 00:58:21 Use device_path.empty()
sidor 2011/07/21 18:24:15 Done.
148 OnFormatDevice(device_path.c_str(),
149 false,
150 MOUNT_METHOD_ERROR_LOCAL,
151 "Device with this mount path not found.");
152 return;
153 }
154 PathMap::iterator it = to_be_formated_.find(device_path);
155 if (it != to_be_formated_.end()) {
achuithb 2011/07/21 00:58:21 You can just do: if (to_be_formated_.find(device_p
sidor 2011/07/21 18:24:15 Done.
156 OnFormatDevice(device_path.c_str(),
157 false,
158 MOUNT_METHOD_ERROR_LOCAL,
159 "Formatting is already pending.");
160 return;
161 }
162 // Formatting process continues, after unmounting
achuithb 2011/07/21 00:58:21 Period at the end of comments please.
sidor 2011/07/21 18:24:15 Done.
163 to_be_formated_[device_path] = file_path;
164 UnmountPath(device_path.c_str());
165 }
166
120 virtual void UnmountDeviceRecursive(const char* device_path, 167 virtual void UnmountDeviceRecursive(const char* device_path,
121 UnmountDeviceRecursiveCallbackType callback, void* user_data) 168 UnmountDeviceRecursiveCallbackType callback, void* user_data)
122 OVERRIDE { 169 OVERRIDE {
123 bool success = true; 170 bool success = true;
124 const char* error_message = NULL; 171 const char* error_message = NULL;
125 std::vector<const char*> devices_to_unmount; 172 std::vector<const char*> devices_to_unmount;
126 173
127 if (!CrosLibrary::Get()->EnsureLoaded()) { 174 if (!CrosLibrary::Get()->EnsureLoaded()) {
128 success = false; 175 success = false;
129 error_message = kLibraryNotLoaded; 176 error_message = kLibraryNotLoaded;
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
200 const char* mount_path, 247 const char* mount_path,
201 MountMethodErrorType error, 248 MountMethodErrorType error,
202 const char* error_message) { 249 const char* error_message) {
203 DCHECK(object); 250 DCHECK(object);
204 MountLibraryImpl* self = static_cast<MountLibraryImpl*>(object); 251 MountLibraryImpl* self = static_cast<MountLibraryImpl*>(object);
205 self->OnUnmountRemovableDevice(device_path, 252 self->OnUnmountRemovableDevice(device_path,
206 error, 253 error,
207 error_message); 254 error_message);
208 } 255 }
209 256
257 // Callback for FormatRemovableDevice method.
258 static void FormatDeviceCallback(void* object,
259 const char* device_path,
260 bool success,
261 MountMethodErrorType error,
262 const char* error_message) {
263 DCHECK(object);
264 MountLibraryImpl* self = static_cast<MountLibraryImpl*>(object);
265 self->OnFormatDevice(device_path, success, error, error_message);
266 }
267
210 // Callback for UnmountDeviceRecursive. 268 // Callback for UnmountDeviceRecursive.
211 static void UnmountDeviceRecursiveCallback(void* object, 269 static void UnmountDeviceRecursiveCallback(void* object,
212 const char* device_path, 270 const char* device_path,
213 const char* mount_path, 271 const char* mount_path,
214 MountMethodErrorType error, 272 MountMethodErrorType error,
215 const char* error_message) { 273 const char* error_message) {
216 DCHECK(object); 274 DCHECK(object);
217 UnmountDeviceRecursiveCallbackData* cb_data = 275 UnmountDeviceRecursiveCallbackData* cb_data =
218 static_cast<UnmountDeviceRecursiveCallbackData*>(object); 276 static_cast<UnmountDeviceRecursiveCallbackData*>(object);
219 277
220 // Do standard processing for Unmount event. 278 // Do standard processing for Unmount event.
221 cb_data->object->OnUnmountRemovableDevice(device_path, 279 cb_data->object->OnUnmountRemovableDevice(device_path,
222 error, 280 error,
223 error_message); 281 error_message);
224 if (error == MOUNT_METHOD_ERROR_LOCAL) { 282 if (error == MOUNT_METHOD_ERROR_LOCAL) {
225 cb_data->success = false; 283 cb_data->success = false;
226 } else if (error == MOUNT_METHOD_ERROR_NONE) { 284 } else if (error == MOUNT_METHOD_ERROR_NONE) {
227 LOG(WARNING) << device_path << " unmounted."; 285 LOG(WARNING) << device_path << " unmounted.";
228 } 286 }
229 287
230 // This is safe as long as all callbacks are called on the same thread as 288 // This is safe as long as all callbacks are called on the same thread as
231 // UnmountDeviceRecursive. 289 // UnmountDeviceRecursive.
232 cb_data->pending_callbacks_count--; 290 cb_data->pending_callbacks_count--;
233 291
234 if (cb_data->pending_callbacks_count == 0) { 292 if (cb_data->pending_callbacks_count == 0) {
235 cb_data->callback(cb_data->user_data, cb_data->success); 293 cb_data->callback(cb_data->user_data, cb_data->success);
236 delete cb_data; 294 delete cb_data;
237 } 295 }
238 } 296 }
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
307 std::string path(device_path); 365 std::string path(device_path);
308 DiskMap::iterator iter = disks_.find(path); 366 DiskMap::iterator iter = disks_.find(path);
309 if (iter == disks_.end()) { 367 if (iter == disks_.end()) {
310 // disk might have been removed by now? 368 // disk might have been removed by now?
311 return; 369 return;
312 } 370 }
313 Disk* disk = iter->second; 371 Disk* disk = iter->second;
314 DCHECK(disk); 372 DCHECK(disk);
315 disk->clear_mount_path(); 373 disk->clear_mount_path();
316 FireDiskStatusUpdate(MOUNT_DISK_UNMOUNTED, disk); 374 FireDiskStatusUpdate(MOUNT_DISK_UNMOUNTED, disk);
375 // Check if there is a formatting scheduled
376 PathMap::iterator it = to_be_formated_.find(device_path);
377 if (it != to_be_formated_.end()) {
378 const std::string* file_path = &(it->second);
achuithb 2011/07/21 00:58:21 I don't think this is safe. You should do const st
sidor 2011/07/21 18:24:15 That's really weird. It intuitively it shouldn't w
379 to_be_formated_.erase(it);
380 FormatUnmountedDevice(file_path->c_str());
381 }
317 } else { 382 } else {
318 LOG(WARNING) << "Unmount request failed for device " 383 LOG(WARNING) << "Unmount request failed for device "
319 << device_path << ", with error: " 384 << device_path << ", with error: "
320 << (error_message ? error_message : "Unknown"); 385 << (error_message ? error_message : "Unknown");
321 } 386 }
322 } 387 }
323 388
389 void OnFormatDevice(const char* device_path,
390 bool success,
391 MountMethodErrorType error,
392 const char* error_message) {
393 DCHECK(device_path);
394
395 if (error == MOUNT_METHOD_ERROR_NONE && device_path && success) {
396 FireDeviceStatusUpdate(MOUNT_FORMATTING_STARTED, device_path);
397 } else {
398 FireDeviceStatusUpdate(MOUNT_FORMATTING_STARTED,
399 std::string("!").append(device_path));
400 LOG(WARNING) << "Format request failed for device "
401 << device_path << ", with error: "
402 << (error_message ? error_message : "Unknown");
403 }
404 }
405
406
324 void OnGetDiskProperties(const char* device_path, 407 void OnGetDiskProperties(const char* device_path,
325 const DiskInfo* disk1, 408 const DiskInfo* disk1,
326 MountMethodErrorType error, 409 MountMethodErrorType error,
327 const char* error_message) { 410 const char* error_message) {
328 DCHECK(device_path); 411 DCHECK(device_path);
329 if (error == MOUNT_METHOD_ERROR_NONE && device_path) { 412 if (error == MOUNT_METHOD_ERROR_NONE && device_path) {
330 // TODO(zelidrag): Find a better way to filter these out before we 413 // TODO(zelidrag): Find a better way to filter these out before we
331 // fetch the properties: 414 // fetch the properties:
332 // Ignore disks coming from the device we booted the system from. 415 // Ignore disks coming from the device we booted the system from.
333 416
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
469 break; 552 break;
470 } 553 }
471 case DEVICE_REMOVED: { 554 case DEVICE_REMOVED: {
472 type = MOUNT_DEVICE_REMOVED; 555 type = MOUNT_DEVICE_REMOVED;
473 break; 556 break;
474 } 557 }
475 case DEVICE_SCANNED: { 558 case DEVICE_SCANNED: {
476 type = MOUNT_DEVICE_SCANNED; 559 type = MOUNT_DEVICE_SCANNED;
477 break; 560 break;
478 } 561 }
562 case FORMATTING_FINISHED: {
563 type = MOUNT_FORMATTING_FINISHED;
564 break;
565 }
479 } 566 }
480 FireDeviceStatusUpdate(type, std::string(device_path)); 567 FireDeviceStatusUpdate(type, std::string(device_path));
481 } 568 }
482 569
483 void Init() { 570 void Init() {
484 // Getting the monitor status so that the daemon starts up. 571 // Getting the monitor status so that the daemon starts up.
485 mount_status_connection_ = MonitorMountEvents( 572 mount_status_connection_ = MonitorMountEvents(
486 &MonitorMountEventsHandler, this); 573 &MonitorMountEventsHandler, this);
487 } 574 }
488 575
(...skipping 16 matching lines...) Expand all
505 // Mount event change observers. 592 // Mount event change observers.
506 ObserverList<Observer> observers_; 593 ObserverList<Observer> observers_;
507 594
508 // A reference to the mount api, to allow callbacks when the mount 595 // A reference to the mount api, to allow callbacks when the mount
509 // status changes. 596 // status changes.
510 MountEventConnection mount_status_connection_; 597 MountEventConnection mount_status_connection_;
511 598
512 // The list of disks found. 599 // The list of disks found.
513 MountLibrary::DiskMap disks_; 600 MountLibrary::DiskMap disks_;
514 601
602 // Set of devices that are supposed to be formated, but are currently waiting
603 // to be unmounted
achuithb 2011/07/21 00:58:21 Period at the end of comments please.
sidor 2011/07/21 18:24:15 Done.
604 PathMap to_be_formated_;
achuithb 2011/07/21 00:58:21 Maybe format_pending_?
sidor 2011/07/21 18:24:15 That doesn't sound correct. This is map holding th
achuithb 2011/07/21 22:02:32 Isn't that what format_pending_ means? Format hasn
605
515 DISALLOW_COPY_AND_ASSIGN(MountLibraryImpl); 606 DISALLOW_COPY_AND_ASSIGN(MountLibraryImpl);
516 }; 607 };
517 608
518 class MountLibraryStubImpl : public MountLibrary { 609 class MountLibraryStubImpl : public MountLibrary {
519 public: 610 public:
520 MountLibraryStubImpl() {} 611 MountLibraryStubImpl() {}
521 virtual ~MountLibraryStubImpl() {} 612 virtual ~MountLibraryStubImpl() {}
522 613
523 // MountLibrary overrides. 614 // MountLibrary overrides.
524 virtual void AddObserver(Observer* observer) OVERRIDE {} 615 virtual void AddObserver(Observer* observer) OVERRIDE {}
525 virtual void RemoveObserver(Observer* observer) OVERRIDE {} 616 virtual void RemoveObserver(Observer* observer) OVERRIDE {}
526 virtual const DiskMap& disks() const OVERRIDE { return disks_; } 617 virtual const DiskMap& disks() const OVERRIDE { return disks_; }
527 virtual void RequestMountInfoRefresh() OVERRIDE {} 618 virtual void RequestMountInfoRefresh() OVERRIDE {}
528 virtual void MountPath(const char* device_path) OVERRIDE {} 619 virtual void MountPath(const char* device_path) OVERRIDE {}
529 virtual void UnmountPath(const char* device_path) OVERRIDE {} 620 virtual void UnmountPath(const char* device_path) OVERRIDE {}
621 virtual void FormatUnmountedDevice(const char* device_path) OVERRIDE {}
622 virtual void FormatMountedDevice(const char* device_mount_path) OVERRIDE {}
530 virtual void UnmountDeviceRecursive(const char* device_path, 623 virtual void UnmountDeviceRecursive(const char* device_path,
531 UnmountDeviceRecursiveCallbackType callback, void* user_data) 624 UnmountDeviceRecursiveCallbackType callback, void* user_data)
532 OVERRIDE {} 625 OVERRIDE {}
533 626
534 private: 627 private:
535 // The list of disks found. 628 // The list of disks found.
536 DiskMap disks_; 629 DiskMap disks_;
537 630
538 DISALLOW_COPY_AND_ASSIGN(MountLibraryStubImpl); 631 DISALLOW_COPY_AND_ASSIGN(MountLibraryStubImpl);
539 }; 632 };
540 633
541 // static 634 // static
542 MountLibrary* MountLibrary::GetImpl(bool stub) { 635 MountLibrary* MountLibrary::GetImpl(bool stub) {
543 if (stub) 636 if (stub)
544 return new MountLibraryStubImpl(); 637 return new MountLibraryStubImpl();
545 else 638 else
546 return new MountLibraryImpl(); 639 return new MountLibraryImpl();
547 } 640 }
548 641
549 } // namespace chromeos 642 } // namespace chromeos
550 643
551 // Allows InvokeLater without adding refcounting. This class is a Singleton and 644 // Allows InvokeLater without adding refcounting. This class is a Singleton and
552 // won't be deleted until it's last InvokeLater is run. 645 // won't be deleted until it's last InvokeLater is run.
553 DISABLE_RUNNABLE_METHOD_REFCOUNT(chromeos::MountLibraryImpl); 646 DISABLE_RUNNABLE_METHOD_REFCOUNT(chromeos::MountLibraryImpl);
554 647
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698