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

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

Issue 7457001: Adding support for mount point different from removable devices to MountLibrary (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: add MountType type to extension_api.json 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 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
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 std::string MountTypeToString(MountType type) const OVERRIDE {
94 switch (type) {
95 case MOUNT_TYPE_DEVICE:
96 return "device";
97 case MOUNT_TYPE_ARCHIVE:
98 return "file";
99 case MOUNT_TYPE_NETWORK_STORAGE:
100 return "network";
101 case MOUNT_TYPE_INVALID:
102 return "invalid";
103 default:
104 NOTREACHED();
105 }
106 return "";
107 }
108
109 virtual MountType MountTypeFromString(const std::string& type_str) const
110 OVERRIDE {
111 if (type_str == "device") {
112 return MOUNT_TYPE_DEVICE;
113 } else if (type_str == "network") {
114 return MOUNT_TYPE_NETWORK_STORAGE;
115 } else if (type_str == "file") {
116 return MOUNT_TYPE_ARCHIVE;
117 } else {
118 return MOUNT_TYPE_INVALID;
119 }
120 }
121
122 virtual void MountPath(const char* source_path,
123 MountType type,
124 const MountPathOptions& options) OVERRIDE {
94 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 125 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
95 if (!CrosLibrary::Get()->EnsureLoaded()) { 126 if (!CrosLibrary::Get()->EnsureLoaded()) {
96 OnMountRemovableDevice(device_path, 127 OnMountCompleted(MOUNT_ERROR_LIBRARY_NOT_LOADED,
97 NULL, 128 source_path,
98 MOUNT_METHOD_ERROR_LOCAL, 129 type,
99 kLibraryNotLoaded); 130 NULL);
100 return; 131 return;
101 } 132 }
102 MountRemovableDevice(device_path, 133 MountSourcePath(source_path, type, options, &MountCompletedHandler, this);
103 &MountLibraryImpl::MountRemovableDeviceCallback,
104 this);
105 } 134 }
106 135
107 virtual void UnmountPath(const char* device_path) OVERRIDE { 136 virtual void UnmountPath(const char* path) OVERRIDE {
108 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 137 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
109 if (!CrosLibrary::Get()->EnsureLoaded()) { 138 if (!CrosLibrary::Get()->EnsureLoaded()) {
110 OnUnmountRemovableDevice(device_path, 139 OnUnmountPath(path,
111 MOUNT_METHOD_ERROR_LOCAL, 140 MOUNT_METHOD_ERROR_LOCAL,
112 kLibraryNotLoaded); 141 kLibraryNotLoaded);
113 return; 142 return;
114 } 143 }
115 UnmountRemovableDevice(device_path, 144 UnmountMountPoint(path, &MountLibraryImpl::UnmountMountPointCallback, this);
116 &MountLibraryImpl::UnmountRemovableDeviceCallback,
117 this);
118 } 145 }
119 146
120 virtual void UnmountDeviceRecursive(const char* device_path, 147 virtual void UnmountDeviceRecursive(const char* device_path,
121 UnmountDeviceRecursiveCallbackType callback, void* user_data) 148 UnmountDeviceRecursiveCallbackType callback, void* user_data)
122 OVERRIDE { 149 OVERRIDE {
123 bool success = true; 150 bool success = true;
124 const char* error_message = NULL; 151 const char* error_message = NULL;
125 std::vector<const char*> devices_to_unmount; 152 std::vector<const char*> devices_to_unmount;
126 153
127 if (!CrosLibrary::Get()->EnsureLoaded()) { 154 if (!CrosLibrary::Get()->EnsureLoaded()) {
(...skipping 16 matching lines...) Expand all
144 } 171 }
145 } 172 }
146 173
147 if (success) { 174 if (success) {
148 // We will send the same callback data object to all Unmount calls and use 175 // We will send the same callback data object to all Unmount calls and use
149 // it to syncronize callbacks. 176 // it to syncronize callbacks.
150 UnmountDeviceRecursiveCallbackData* 177 UnmountDeviceRecursiveCallbackData*
151 cb_data = new UnmountDeviceRecursiveCallbackData(this, user_data, 178 cb_data = new UnmountDeviceRecursiveCallbackData(this, user_data,
152 callback, devices_to_unmount.size()); 179 callback, devices_to_unmount.size());
153 for (std::vector<const char*>::iterator it = devices_to_unmount.begin(); 180 for (std::vector<const char*>::iterator it = devices_to_unmount.begin();
154 it != devices_to_unmount.end(); 181 it != devices_to_unmount.end();
155 ++it) { 182 ++it) {
156 UnmountRemovableDevice(*it, 183 UnmountMountPoint(
184 *it,
157 &MountLibraryImpl::UnmountDeviceRecursiveCallback, 185 &MountLibraryImpl::UnmountDeviceRecursiveCallback,
158 cb_data); 186 cb_data);
159 } 187 }
160 } else { 188 } else {
161 LOG(WARNING) << "Unmount recursive request failed for device " 189 LOG(WARNING) << "Unmount recursive request failed for device "
162 << device_path << ", with error: " << error_message; 190 << device_path << ", with error: " << error_message;
163 callback(user_data, false); 191 callback(user_data, false);
164 } 192 }
165 } 193 }
166 194
167 virtual void RequestMountInfoRefresh() OVERRIDE { 195 virtual void RequestMountInfoRefresh() OVERRIDE {
168 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 196 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
169 if (!CrosLibrary::Get()->EnsureLoaded()) { 197 if (!CrosLibrary::Get()->EnsureLoaded()) {
170 OnRequestMountInfo(NULL, 198 OnRequestMountInfo(NULL,
171 0, 199 0,
172 MOUNT_METHOD_ERROR_LOCAL, 200 MOUNT_METHOD_ERROR_LOCAL,
173 kLibraryNotLoaded); 201 kLibraryNotLoaded);
174 return; 202 return;
175 } 203 }
176 RequestMountInfo(&MountLibraryImpl::RequestMountInfoCallback, 204 RequestMountInfo(&MountLibraryImpl::RequestMountInfoCallback,
177 this); 205 this);
178 } 206 }
179 207
180 const DiskMap& disks() const OVERRIDE { return disks_; } 208 const DiskMap& disks() const OVERRIDE { return disks_; }
209 const MountPointMap& mount_points() const OVERRIDE { return mount_points_; }
181 210
182 private: 211 private:
183 // Callback for MountRemovableDevice method. 212 // Callback for MountComplete signal and MountSourcePath method.
184 static void MountRemovableDeviceCallback(void* object, 213 static void MountCompletedHandler(void* object,
185 const char* device_path, 214 MountError error_code,
186 const char* mount_path, 215 const char* source_path,
187 MountMethodErrorType error, 216 MountType type,
188 const char* error_message) { 217 const char* mount_path) {
189 DCHECK(object); 218 DCHECK(object);
190 MountLibraryImpl* self = static_cast<MountLibraryImpl*>(object); 219 MountLibraryImpl* self = static_cast<MountLibraryImpl*>(object);
191 self->OnMountRemovableDevice(device_path, 220 self->OnMountCompleted(static_cast<MountError>(error_code),
192 mount_path, 221 source_path,
193 error, 222 static_cast<MountType>(type),
194 error_message); 223 mount_path);
195 } 224 }
196 225
197 // Callback for UnmountRemovableDevice method. 226 // Callback for UnmountRemovableDevice method.
198 static void UnmountRemovableDeviceCallback(void* object, 227 static void UnmountMountPointCallback(void* object,
199 const char* device_path, 228 const char* device_path,
200 const char* mount_path, 229 MountMethodErrorType error,
201 MountMethodErrorType error, 230 const char* error_message) {
202 const char* error_message) {
203 DCHECK(object); 231 DCHECK(object);
204 MountLibraryImpl* self = static_cast<MountLibraryImpl*>(object); 232 MountLibraryImpl* self = static_cast<MountLibraryImpl*>(object);
205 self->OnUnmountRemovableDevice(device_path, 233 self->OnUnmountPath(device_path, error, error_message);
206 error,
207 error_message);
208 } 234 }
209 235
210 // Callback for UnmountDeviceRecursive. 236 // Callback for UnmountDeviceRecursive.
211 static void UnmountDeviceRecursiveCallback(void* object, 237 static void UnmountDeviceRecursiveCallback(void* object,
212 const char* device_path, 238 const char* device_path,
213 const char* mount_path,
214 MountMethodErrorType error, 239 MountMethodErrorType error,
215 const char* error_message) { 240 const char* error_message) {
216 DCHECK(object); 241 DCHECK(object);
217 UnmountDeviceRecursiveCallbackData* cb_data = 242 UnmountDeviceRecursiveCallbackData* cb_data =
218 static_cast<UnmountDeviceRecursiveCallbackData*>(object); 243 static_cast<UnmountDeviceRecursiveCallbackData*>(object);
219 244
220 // Do standard processing for Unmount event. 245 // Do standard processing for Unmount event.
221 cb_data->object->OnUnmountRemovableDevice(device_path, 246 cb_data->object->OnUnmountPath(device_path,
222 error, 247 error,
223 error_message); 248 error_message);
224 if (error == MOUNT_METHOD_ERROR_LOCAL) { 249 if (error == MOUNT_METHOD_ERROR_LOCAL) {
225 cb_data->success = false; 250 cb_data->success = false;
226 } else if (error == MOUNT_METHOD_ERROR_NONE) { 251 } else if (error == MOUNT_METHOD_ERROR_NONE) {
227 LOG(WARNING) << device_path << " unmounted."; 252 LOG(WARNING) << device_path << " unmounted.";
228 } 253 }
229 254
230 // This is safe as long as all callbacks are called on the same thread as 255 // This is safe as long as all callbacks are called on the same thread as
231 // UnmountDeviceRecursive. 256 // UnmountDeviceRecursive.
232 cb_data->pending_callbacks_count--; 257 cb_data->pending_callbacks_count--;
233 258
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
268 // This method will receive events that are caused by drive status changes. 293 // This method will receive events that are caused by drive status changes.
269 static void MonitorMountEventsHandler(void* object, 294 static void MonitorMountEventsHandler(void* object,
270 MountEventType evt, 295 MountEventType evt,
271 const char* device_path) { 296 const char* device_path) {
272 DCHECK(object); 297 DCHECK(object);
273 MountLibraryImpl* self = static_cast<MountLibraryImpl*>(object); 298 MountLibraryImpl* self = static_cast<MountLibraryImpl*>(object);
274 self->OnMountEvent(evt, device_path); 299 self->OnMountEvent(evt, device_path);
275 } 300 }
276 301
277 302
278 void OnMountRemovableDevice(const char* device_path, 303 void OnMountCompleted(MountError error_code,
279 const char* mount_path, 304 const char* source_path,
280 MountMethodErrorType error, 305 MountType type,
281 const char* error_message) { 306 const char* mount_path) {
282 DCHECK(device_path); 307 DCHECK(source_path);
283 308
284 if (error == MOUNT_METHOD_ERROR_NONE && device_path && mount_path) { 309 FireMountCompleted(error_code, source_path, type, mount_path);
285 std::string path(device_path); 310
311 if (error_code == MOUNT_ERROR_NONE &&
312 mount_points_.find(source_path) == mount_points_.end()) {
313 mount_points_.insert(MountPointMap::value_type(source_path,
314 MountPointInfo(source_path, mount_path, type)));
315 }
316
317 if (error_code == MOUNT_ERROR_NONE && type == MOUNT_TYPE_DEVICE &&
318 source_path && mount_path) {
319 std::string path(source_path);
286 DiskMap::iterator iter = disks_.find(path); 320 DiskMap::iterator iter = disks_.find(path);
287 if (iter == disks_.end()) { 321 if (iter == disks_.end()) {
288 // disk might have been removed by now? 322 // disk might have been removed by now?
289 return; 323 return;
290 } 324 }
291 Disk* disk = iter->second; 325 Disk* disk = iter->second;
292 DCHECK(disk); 326 DCHECK(disk);
293 disk->set_mount_path(mount_path); 327 disk->set_mount_path(mount_path);
294 FireDiskStatusUpdate(MOUNT_DISK_MOUNTED, disk); 328 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 } 329 }
300 } 330 }
301 331
302 void OnUnmountRemovableDevice(const char* device_path, 332 void OnUnmountPath(const char* source_path,
303 MountMethodErrorType error, 333 MountMethodErrorType error,
304 const char* error_message) { 334 const char* error_message) {
305 DCHECK(device_path); 335 DCHECK(source_path);
306 if (error == MOUNT_METHOD_ERROR_NONE && device_path) { 336
307 std::string path(device_path); 337 if (error == MOUNT_METHOD_ERROR_NONE && source_path) {
338 MountPointMap::iterator mount_points_it = mount_points_.find(source_path);
339 if (mount_points_it == mount_points_.end())
340 return;
341 // TODO(tbarzic): Add separate, PathUnmounted event to Observer.
342 FireMountCompleted(MOUNT_ERROR_PATH_UNMOUNTED,
343 mount_points_it->second.source_path.c_str(),
344 mount_points_it->second.mount_type,
345 mount_points_it->second.mount_path.c_str());
346 mount_points_.erase(mount_points_it);
347
348 std::string path(source_path);
308 DiskMap::iterator iter = disks_.find(path); 349 DiskMap::iterator iter = disks_.find(path);
309 if (iter == disks_.end()) { 350 if (iter == disks_.end()) {
310 // disk might have been removed by now? 351 // disk might have been removed by now?
311 return; 352 return;
312 } 353 }
313 Disk* disk = iter->second; 354 Disk* disk = iter->second;
314 DCHECK(disk); 355 DCHECK(disk);
315 disk->clear_mount_path(); 356 disk->clear_mount_path();
316 FireDiskStatusUpdate(MOUNT_DISK_UNMOUNTED, disk); 357 FireDiskStatusUpdate(MOUNT_DISK_UNMOUNTED, disk);
317 } else { 358 } else {
318 LOG(WARNING) << "Unmount request failed for device " 359 LOG(WARNING) << "Unmount request failed for device "
319 << device_path << ", with error: " 360 << source_path << ", with error: "
320 << (error_message ? error_message : "Unknown"); 361 << (error_message ? error_message : "Unknown");
321 } 362 }
322 } 363 }
323 364
324 void OnGetDiskProperties(const char* device_path, 365 void OnGetDiskProperties(const char* device_path,
325 const DiskInfo* disk1, 366 const DiskInfo* disk1,
326 MountMethodErrorType error, 367 MountMethodErrorType error,
327 const char* error_message) { 368 const char* error_message) {
328 DCHECK(device_path); 369 DCHECK(device_path);
329 if (error == MOUNT_METHOD_ERROR_NONE && device_path) { 370 if (error == MOUNT_METHOD_ERROR_NONE && device_path) {
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
469 break; 510 break;
470 } 511 }
471 case DEVICE_REMOVED: { 512 case DEVICE_REMOVED: {
472 type = MOUNT_DEVICE_REMOVED; 513 type = MOUNT_DEVICE_REMOVED;
473 break; 514 break;
474 } 515 }
475 case DEVICE_SCANNED: { 516 case DEVICE_SCANNED: {
476 type = MOUNT_DEVICE_SCANNED; 517 type = MOUNT_DEVICE_SCANNED;
477 break; 518 break;
478 } 519 }
520 default: {
521 return;
522 }
479 } 523 }
480 FireDeviceStatusUpdate(type, std::string(device_path)); 524 FireDeviceStatusUpdate(type, std::string(device_path));
481 } 525 }
482 526
483 void Init() { 527 void Init() {
484 // Getting the monitor status so that the daemon starts up. 528 // Getting the monitor status so that the daemon starts up.
485 mount_status_connection_ = MonitorMountEvents( 529 mount_status_connection_ = MonitorAllMountEvents(
486 &MonitorMountEventsHandler, this); 530 &MonitorMountEventsHandler, &MountCompletedHandler, this);
487 } 531 }
488 532
489 void FireDiskStatusUpdate(MountLibraryEventType evt, 533 void FireDiskStatusUpdate(MountLibraryEventType evt,
490 const Disk* disk) { 534 const Disk* disk) {
491 // Make sure we run on UI thread. 535 // Make sure we run on UI thread.
492 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 536 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
493 FOR_EACH_OBSERVER( 537 FOR_EACH_OBSERVER(
494 Observer, observers_, DiskChanged(evt, disk)); 538 Observer, observers_, DiskChanged(evt, disk));
495 } 539 }
496 540
497 void FireDeviceStatusUpdate(MountLibraryEventType evt, 541 void FireDeviceStatusUpdate(MountLibraryEventType evt,
498 const std::string& device_path) { 542 const std::string& device_path) {
499 // Make sure we run on UI thread. 543 // Make sure we run on UI thread.
500 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 544 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
501 FOR_EACH_OBSERVER( 545 FOR_EACH_OBSERVER(
502 Observer, observers_, DeviceChanged(evt, device_path)); 546 Observer, observers_, DeviceChanged(evt, device_path));
503 } 547 }
504 548
549 void FireMountCompleted(MountError error_code,
550 const char* source_path,
551 MountType mount_type,
552 const char* mount_path) {
553 // Make sure we run on UI thread.
554 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
555 FOR_EACH_OBSERVER(
556 Observer, observers_, MountCompleted(error_code,
557 source_path,
558 mount_type,
559 mount_path));
560 }
561
505 // Mount event change observers. 562 // Mount event change observers.
506 ObserverList<Observer> observers_; 563 ObserverList<Observer> observers_;
507 564
508 // A reference to the mount api, to allow callbacks when the mount 565 // A reference to the mount api, to allow callbacks when the mount
509 // status changes. 566 // status changes.
510 MountEventConnection mount_status_connection_; 567 MountEventConnection mount_status_connection_;
511 568
512 // The list of disks found. 569 // The list of disks found.
513 MountLibrary::DiskMap disks_; 570 MountLibrary::DiskMap disks_;
514 571
572 MountLibrary::MountPointMap mount_points_;
573
515 DISALLOW_COPY_AND_ASSIGN(MountLibraryImpl); 574 DISALLOW_COPY_AND_ASSIGN(MountLibraryImpl);
516 }; 575 };
517 576
518 class MountLibraryStubImpl : public MountLibrary { 577 class MountLibraryStubImpl : public MountLibrary {
519 public: 578 public:
520 MountLibraryStubImpl() {} 579 MountLibraryStubImpl() {}
521 virtual ~MountLibraryStubImpl() {} 580 virtual ~MountLibraryStubImpl() {}
522 581
523 // MountLibrary overrides. 582 // MountLibrary overrides.
524 virtual void AddObserver(Observer* observer) OVERRIDE {} 583 virtual void AddObserver(Observer* observer) OVERRIDE {}
525 virtual void RemoveObserver(Observer* observer) OVERRIDE {} 584 virtual void RemoveObserver(Observer* observer) OVERRIDE {}
526 virtual const DiskMap& disks() const OVERRIDE { return disks_; } 585 virtual const DiskMap& disks() const OVERRIDE { return disks_; }
586 virtual const MountPointMap& mount_points() const OVERRIDE {
587 return mount_points_;
588 }
589 virtual std::string MountTypeToString(MountType type) const OVERRIDE {
590 return "";
591 }
592 virtual MountType MountTypeFromString(const std::string& type_str) const
593 OVERRIDE {
594 return MOUNT_TYPE_INVALID;
595 }
527 virtual void RequestMountInfoRefresh() OVERRIDE {} 596 virtual void RequestMountInfoRefresh() OVERRIDE {}
528 virtual void MountPath(const char* device_path) OVERRIDE {} 597 virtual void MountPath(const char* source_path, MountType type,
529 virtual void UnmountPath(const char* device_path) OVERRIDE {} 598 const MountPathOptions& options) OVERRIDE {}
599 virtual void UnmountPath(const char* path) OVERRIDE {}
530 virtual void UnmountDeviceRecursive(const char* device_path, 600 virtual void UnmountDeviceRecursive(const char* device_path,
531 UnmountDeviceRecursiveCallbackType callback, void* user_data) 601 UnmountDeviceRecursiveCallbackType callback, void* user_data)
532 OVERRIDE {} 602 OVERRIDE {}
533 603
534 private: 604 private:
535 // The list of disks found. 605 // The list of disks found.
536 DiskMap disks_; 606 DiskMap disks_;
607 MountPointMap mount_points_;
537 608
538 DISALLOW_COPY_AND_ASSIGN(MountLibraryStubImpl); 609 DISALLOW_COPY_AND_ASSIGN(MountLibraryStubImpl);
539 }; 610 };
540 611
541 // static 612 // static
542 MountLibrary* MountLibrary::GetImpl(bool stub) { 613 MountLibrary* MountLibrary::GetImpl(bool stub) {
543 if (stub) 614 if (stub)
544 return new MountLibraryStubImpl(); 615 return new MountLibraryStubImpl();
545 else 616 else
546 return new MountLibraryImpl(); 617 return new MountLibraryImpl();
547 } 618 }
548 619
549 } // namespace chromeos 620 } // namespace chromeos
550 621
551 // Allows InvokeLater without adding refcounting. This class is a Singleton and 622 // Allows InvokeLater without adding refcounting. This class is a Singleton and
552 // won't be deleted until it's last InvokeLater is run. 623 // won't be deleted until it's last InvokeLater is run.
553 DISABLE_RUNNABLE_METHOD_REFCOUNT(chromeos::MountLibraryImpl); 624 DISABLE_RUNNABLE_METHOD_REFCOUNT(chromeos::MountLibraryImpl);
554 625
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698