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

Side by Side Diff: chrome/browser/chromeos/extensions/file_browser_event_router.cc

Issue 11365142: Small refactoring in DiskMountManager (event reporting; format method). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years, 1 month 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) 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/chromeos/extensions/file_browser_event_router.h" 5 #include "chrome/browser/chromeos/extensions/file_browser_event_router.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/json/json_writer.h" 8 #include "base/json/json_writer.h"
9 #include "base/message_loop.h" 9 #include "base/message_loop.h"
10 #include "base/prefs/public/pref_change_registrar.h" 10 #include "base/prefs/public/pref_change_registrar.h"
(...skipping 18 matching lines...) Expand all
29 #include "chrome/browser/profiles/profile_dependency_manager.h" 29 #include "chrome/browser/profiles/profile_dependency_manager.h"
30 #include "chrome/common/chrome_notification_types.h" 30 #include "chrome/common/chrome_notification_types.h"
31 #include "chrome/common/pref_names.h" 31 #include "chrome/common/pref_names.h"
32 #include "content/public/browser/notification_source.h" 32 #include "content/public/browser/notification_source.h"
33 #include "grit/generated_resources.h" 33 #include "grit/generated_resources.h"
34 #include "ui/base/l10n/l10n_util.h" 34 #include "ui/base/l10n/l10n_util.h"
35 #include "webkit/fileapi/file_system_types.h" 35 #include "webkit/fileapi/file_system_types.h"
36 #include "webkit/fileapi/file_system_util.h" 36 #include "webkit/fileapi/file_system_util.h"
37 37
38 using chromeos::disks::DiskMountManager; 38 using chromeos::disks::DiskMountManager;
39 using chromeos::disks::DiskMountManagerEventType;
40 using content::BrowserThread; 39 using content::BrowserThread;
41 using drive::DriveSystemService; 40 using drive::DriveSystemService;
42 using drive::DriveSystemServiceFactory; 41 using drive::DriveSystemServiceFactory;
43 42
44 namespace { 43 namespace {
45 const char kDiskAddedEventType[] = "added"; 44 const char kDiskAddedEventType[] = "added";
46 const char kDiskRemovedEventType[] = "removed"; 45 const char kDiskRemovedEventType[] = "removed";
47 46
48 const char kPathChanged[] = "changed"; 47 const char kPathChanged[] = "changed";
49 const char kPathWatchError[] = "error"; 48 const char kPathWatchError[] = "error";
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after
258 257
259 // Pass back the gdata mount point path as source path. 258 // Pass back the gdata mount point path as source path.
260 const std::string& gdata_path = drive::util::GetDriveMountPointPathAsString(); 259 const std::string& gdata_path = drive::util::GetDriveMountPointPathAsString();
261 DiskMountManager::MountPointInfo mount_info( 260 DiskMountManager::MountPointInfo mount_info(
262 gdata_path, 261 gdata_path,
263 gdata_path, 262 gdata_path,
264 chromeos::MOUNT_TYPE_GDATA, 263 chromeos::MOUNT_TYPE_GDATA,
265 chromeos::disks::MOUNT_CONDITION_NONE); 264 chromeos::disks::MOUNT_CONDITION_NONE);
266 265
267 // Raise mount event. 266 // Raise mount event.
268 MountCompleted(DiskMountManager::MOUNTING, error_code, mount_info); 267 OnMountEvent(DiskMountManager::MOUNTING, error_code, mount_info);
269 268
270 if (!callback.is_null()) 269 if (!callback.is_null())
271 callback.Run(); 270 callback.Run();
272 } 271 }
273 272
274 void FileBrowserEventRouter::HandleRemoteUpdateRequestOnUIThread(bool start) { 273 void FileBrowserEventRouter::HandleRemoteUpdateRequestOnUIThread(bool start) {
275 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 274 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
276 275
277 drive::DriveFileSystemInterface* file_system = GetRemoteFileSystem(); 276 drive::DriveFileSystemInterface* file_system = GetRemoteFileSystem();
278 // |file_system| is NULL if Drive is disabled. 277 // |file_system| is NULL if Drive is disabled.
279 if (!file_system) 278 if (!file_system)
280 return; 279 return;
281 280
282 if (start) { 281 if (start) {
283 file_system->CheckForUpdates(); 282 file_system->CheckForUpdates();
284 if (num_remote_update_requests_ == 0) 283 if (num_remote_update_requests_ == 0)
285 file_system->StartPolling(); 284 file_system->StartPolling();
286 ++num_remote_update_requests_; 285 ++num_remote_update_requests_;
287 } else { 286 } else {
288 DCHECK_LE(1, num_remote_update_requests_); 287 DCHECK_LE(1, num_remote_update_requests_);
289 --num_remote_update_requests_; 288 --num_remote_update_requests_;
290 if (num_remote_update_requests_ == 0) 289 if (num_remote_update_requests_ == 0)
291 file_system->StopPolling(); 290 file_system->StopPolling();
292 } 291 }
293 } 292 }
294 293
295 void FileBrowserEventRouter::DiskChanged( 294 void FileBrowserEventRouter::OnDiskEvent(
296 DiskMountManagerEventType event, 295 DiskMountManager::DiskEvent event,
297 const DiskMountManager::Disk* disk) { 296 const DiskMountManager::Disk* disk) {
298 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 297 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
299 298
300 // Disregard hidden devices. 299 // Disregard hidden devices.
301 if (disk->is_hidden()) 300 if (disk->is_hidden())
302 return; 301 return;
303 if (event == chromeos::disks::MOUNT_DISK_ADDED) { 302 if (event == DiskMountManager::DISK_ADDED) {
304 OnDiskAdded(disk); 303 OnDiskAdded(disk);
305 } else if (event == chromeos::disks::MOUNT_DISK_REMOVED) { 304 } else if (event == DiskMountManager::DISK_REMOVED) {
306 OnDiskRemoved(disk); 305 OnDiskRemoved(disk);
307 } 306 }
308 } 307 }
309 308
310 void FileBrowserEventRouter::DeviceChanged( 309 void FileBrowserEventRouter::OnDeviceEvent(
311 DiskMountManagerEventType event, 310 DiskMountManager::DeviceEvent event,
312 const std::string& device_path) { 311 const std::string& device_path) {
313 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 312 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
314 313
315 if (event == chromeos::disks::MOUNT_DEVICE_ADDED) { 314 if (event == DiskMountManager::DEVICE_ADDED) {
316 OnDeviceAdded(device_path); 315 OnDeviceAdded(device_path);
317 } else if (event == chromeos::disks::MOUNT_DEVICE_REMOVED) { 316 } else if (event == DiskMountManager::DEVICE_REMOVED) {
318 OnDeviceRemoved(device_path); 317 OnDeviceRemoved(device_path);
319 } else if (event == chromeos::disks::MOUNT_DEVICE_SCANNED) { 318 } else if (event == DiskMountManager::DEVICE_SCANNED) {
320 OnDeviceScanned(device_path); 319 OnDeviceScanned(device_path);
321 } else if (event == chromeos::disks::MOUNT_FORMATTING_STARTED) {
322 // TODO(tbarzic): get rid of '!'.
323 if (device_path[0] == '!') {
324 OnFormattingStarted(device_path.substr(1), false);
325 } else {
326 OnFormattingStarted(device_path, true);
327 }
328 } else if (event == chromeos::disks::MOUNT_FORMATTING_FINISHED) {
329 if (device_path[0] == '!') {
330 OnFormattingFinished(device_path.substr(1), false);
331 } else {
332 OnFormattingFinished(device_path, true);
333 }
334 } 320 }
335 } 321 }
336 322
337 void FileBrowserEventRouter::MountCompleted( 323 void FileBrowserEventRouter::OnMountEvent(
338 DiskMountManager::MountEvent event_type, 324 DiskMountManager::MountEvent event,
339 chromeos::MountError error_code, 325 chromeos::MountError error_code,
340 const DiskMountManager::MountPointInfo& mount_info) { 326 const DiskMountManager::MountPointInfo& mount_info) {
341 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 327 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
342 328
343 DispatchMountCompletedEvent(event_type, error_code, mount_info); 329 DispatchMountEvent(event, error_code, mount_info);
344 330
345 if (mount_info.mount_type == chromeos::MOUNT_TYPE_DEVICE && 331 if (mount_info.mount_type == chromeos::MOUNT_TYPE_DEVICE &&
346 event_type == DiskMountManager::MOUNTING) { 332 event == DiskMountManager::MOUNTING) {
347 DiskMountManager* disk_mount_manager = DiskMountManager::GetInstance(); 333 DiskMountManager* disk_mount_manager = DiskMountManager::GetInstance();
348 const DiskMountManager::Disk* disk = 334 const DiskMountManager::Disk* disk =
349 disk_mount_manager->FindDiskBySourcePath(mount_info.source_path); 335 disk_mount_manager->FindDiskBySourcePath(mount_info.source_path);
350 if (!disk) 336 if (!disk)
351 return; 337 return;
352 338
353 notifications_->ManageNotificationsOnMountCompleted( 339 notifications_->ManageNotificationsOnMountCompleted(
354 disk->system_path_prefix(), disk->drive_label(), disk->is_parent(), 340 disk->system_path_prefix(), disk->drive_label(), disk->is_parent(),
355 error_code == chromeos::MOUNT_ERROR_NONE, 341 error_code == chromeos::MOUNT_ERROR_NONE,
356 error_code == chromeos::MOUNT_ERROR_UNSUPPORTED_FILESYSTEM); 342 error_code == chromeos::MOUNT_ERROR_UNSUPPORTED_FILESYSTEM);
357 } else if (mount_info.mount_type == chromeos::MOUNT_TYPE_ARCHIVE) { 343 } else if (mount_info.mount_type == chromeos::MOUNT_TYPE_ARCHIVE) {
358 // Clear the "mounted" state for archive files in gdata cache 344 // Clear the "mounted" state for archive files in gdata cache
359 // when mounting failed or unmounting succeeded. 345 // when mounting failed or unmounting succeeded.
360 if ((event_type == DiskMountManager::MOUNTING) != 346 if ((event == DiskMountManager::MOUNTING) !=
361 (error_code == chromeos::MOUNT_ERROR_NONE)) { 347 (error_code == chromeos::MOUNT_ERROR_NONE)) {
362 FilePath source_path(mount_info.source_path); 348 FilePath source_path(mount_info.source_path);
363 DriveSystemService* system_service = 349 DriveSystemService* system_service =
364 DriveSystemServiceFactory::GetForProfile(profile_); 350 DriveSystemServiceFactory::GetForProfile(profile_);
365 drive::DriveCache* cache = 351 drive::DriveCache* cache =
366 system_service ? system_service->cache() : NULL; 352 system_service ? system_service->cache() : NULL;
367 if (cache) { 353 if (cache) {
368 cache->SetMountedState( 354 cache->SetMountedState(
369 source_path, false, drive::GetFileFromCacheCallback()); 355 source_path, false, drive::GetFileFromCacheCallback());
370 } 356 }
371 } 357 }
372 } 358 }
373 } 359 }
374 360
361 void FileBrowserEventRouter::OnFormatEvent(
362 DiskMountManager::FormatEvent event,
363 chromeos::FormatError error_code,
364 const std::string& device_path) {
365 if (event == DiskMountManager::FORMAT_STARTED) {
366 OnFormatStarted(device_path, error_code == chromeos::FORMAT_ERROR_NONE);
367 } else if (event == DiskMountManager::FORMAT_COMPLETED) {
368 OnFormatCompleted(device_path, error_code == chromeos::FORMAT_ERROR_NONE);
369 }
370 }
371
375 void FileBrowserEventRouter::OnNetworkManagerChanged( 372 void FileBrowserEventRouter::OnNetworkManagerChanged(
376 chromeos::NetworkLibrary* network_library) { 373 chromeos::NetworkLibrary* network_library) {
377 if (!profile_ || 374 if (!profile_ ||
378 !extensions::ExtensionSystem::Get(profile_)->event_router()) { 375 !extensions::ExtensionSystem::Get(profile_)->event_router()) {
379 NOTREACHED(); 376 NOTREACHED();
380 return; 377 return;
381 } 378 }
382 extensions::ExtensionSystem::Get(profile_)->event_router()-> 379 extensions::ExtensionSystem::Get(profile_)->event_router()->
383 DispatchEventToRenderers( 380 DispatchEventToRenderers(
384 extensions::event_names::kOnFileBrowserNetworkConnectionChanged, 381 extensions::event_names::kOnFileBrowserNetworkConnectionChanged,
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
461 458
462 void FileBrowserEventRouter::OnFileSystemMounted() { 459 void FileBrowserEventRouter::OnFileSystemMounted() {
463 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 460 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
464 461
465 MountDrive(base::Bind(&base::DoNothing)); // Callback does nothing. 462 MountDrive(base::Bind(&base::DoNothing)); // Callback does nothing.
466 } 463 }
467 464
468 void FileBrowserEventRouter::OnFileSystemBeingUnmounted() { 465 void FileBrowserEventRouter::OnFileSystemBeingUnmounted() {
469 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 466 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
470 467
471 // Raise a MountCompleted event to notify the File Manager. 468 // Raise a mount event to notify the File Manager.
472 const std::string& gdata_path = drive::util::GetDriveMountPointPathAsString(); 469 const std::string& gdata_path = drive::util::GetDriveMountPointPathAsString();
473 DiskMountManager::MountPointInfo mount_info( 470 DiskMountManager::MountPointInfo mount_info(
474 gdata_path, 471 gdata_path,
475 gdata_path, 472 gdata_path,
476 chromeos::MOUNT_TYPE_GDATA, 473 chromeos::MOUNT_TYPE_GDATA,
477 chromeos::disks::MOUNT_CONDITION_NONE); 474 chromeos::disks::MOUNT_CONDITION_NONE);
478 MountCompleted(DiskMountManager::UNMOUNTING, chromeos::MOUNT_ERROR_NONE, 475 OnMountEvent(DiskMountManager::UNMOUNTING, chromeos::MOUNT_ERROR_NONE,
479 mount_info); 476 mount_info);
480 } 477 }
481 478
482 void FileBrowserEventRouter::OnAuthenticationFailed( 479 void FileBrowserEventRouter::OnAuthenticationFailed(
483 google_apis::GDataErrorCode error) { 480 google_apis::GDataErrorCode error) {
484 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 481 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
485 482
486 if (error == google_apis::GDATA_NO_CONNECTION) 483 if (error == google_apis::GDATA_NO_CONNECTION)
487 return; 484 return;
488 485
489 // Raise a MountCompleted event to notify the File Manager. 486 // Raise a mount event to notify the File Manager.
490 const std::string& gdata_path = drive::util::GetDriveMountPointPathAsString(); 487 const std::string& gdata_path = drive::util::GetDriveMountPointPathAsString();
491 DiskMountManager::MountPointInfo mount_info( 488 DiskMountManager::MountPointInfo mount_info(
492 gdata_path, 489 gdata_path,
493 gdata_path, 490 gdata_path,
494 chromeos::MOUNT_TYPE_GDATA, 491 chromeos::MOUNT_TYPE_GDATA,
495 chromeos::disks::MOUNT_CONDITION_NONE); 492 chromeos::disks::MOUNT_CONDITION_NONE);
496 MountCompleted(DiskMountManager::UNMOUNTING, chromeos::MOUNT_ERROR_NONE, 493 OnMountEvent(DiskMountManager::UNMOUNTING, chromeos::MOUNT_ERROR_NONE,
497 mount_info); 494 mount_info);
498 } 495 }
499 496
500 void FileBrowserEventRouter::HandleFileWatchNotification( 497 void FileBrowserEventRouter::HandleFileWatchNotification(
501 const FilePath& local_path, bool got_error) { 498 const FilePath& local_path, bool got_error) {
502 base::AutoLock lock(lock_); 499 base::AutoLock lock(lock_);
503 WatcherMap::const_iterator iter = file_watchers_.find(local_path); 500 WatcherMap::const_iterator iter = file_watchers_.find(local_path);
504 if (iter == file_watchers_.end()) { 501 if (iter == file_watchers_.end()) {
505 return; 502 return;
506 } 503 }
507 DispatchDirectoryChangeEvent(iter->second->GetVirtualPath(), got_error, 504 DispatchDirectoryChangeEvent(iter->second->GetVirtualPath(), got_error,
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
560 added ? kDiskAddedEventType : kDiskRemovedEventType); 557 added ? kDiskAddedEventType : kDiskRemovedEventType);
561 DictionaryValue* disk_info = DiskToDictionaryValue(disk); 558 DictionaryValue* disk_info = DiskToDictionaryValue(disk);
562 mount_info->Set("volumeInfo", disk_info); 559 mount_info->Set("volumeInfo", disk_info);
563 560
564 extensions::ExtensionSystem::Get(profile_)->event_router()-> 561 extensions::ExtensionSystem::Get(profile_)->event_router()->
565 DispatchEventToRenderers( 562 DispatchEventToRenderers(
566 extensions::event_names::kOnFileBrowserDiskChanged, args.Pass(), NULL, 563 extensions::event_names::kOnFileBrowserDiskChanged, args.Pass(), NULL,
567 GURL()); 564 GURL());
568 } 565 }
569 566
570 void FileBrowserEventRouter::DispatchMountCompletedEvent( 567 void FileBrowserEventRouter::DispatchMountEvent(
571 DiskMountManager::MountEvent event, 568 DiskMountManager::MountEvent event,
572 chromeos::MountError error_code, 569 chromeos::MountError error_code,
573 const DiskMountManager::MountPointInfo& mount_info) { 570 const DiskMountManager::MountPointInfo& mount_info) {
574 // profile_ is NULL if ShutdownOnUIThread() is called earlier. This can 571 // profile_ is NULL if ShutdownOnUIThread() is called earlier. This can
575 // happen at shutdown. 572 // happen at shutdown.
576 if (!profile_) 573 if (!profile_)
577 return; 574 return;
578 575
579 if (mount_info.mount_type == chromeos::MOUNT_TYPE_INVALID) { 576 if (mount_info.mount_type == chromeos::MOUNT_TYPE_INVALID) {
580 NOTREACHED(); 577 NOTREACHED();
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
713 device_path); 710 device_path);
714 notifications_->UnregisterDevice(device_path); 711 notifications_->UnregisterDevice(device_path);
715 } 712 }
716 713
717 void FileBrowserEventRouter::OnDeviceScanned( 714 void FileBrowserEventRouter::OnDeviceScanned(
718 const std::string& device_path) { 715 const std::string& device_path) {
719 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 716 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
720 VLOG(1) << "Device scanned : " << device_path; 717 VLOG(1) << "Device scanned : " << device_path;
721 } 718 }
722 719
723 void FileBrowserEventRouter::OnFormattingStarted( 720 void FileBrowserEventRouter::OnFormatStarted(
724 const std::string& device_path, bool success) { 721 const std::string& device_path, bool success) {
725 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 722 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
726 723
727 if (success) { 724 if (success) {
728 notifications_->ShowNotification(FileBrowserNotifications::FORMAT_START, 725 notifications_->ShowNotification(FileBrowserNotifications::FORMAT_START,
729 device_path); 726 device_path);
730 } else { 727 } else {
731 notifications_->ShowNotification( 728 notifications_->ShowNotification(
732 FileBrowserNotifications::FORMAT_START_FAIL, device_path); 729 FileBrowserNotifications::FORMAT_START_FAIL, device_path);
733 } 730 }
734 } 731 }
735 732
736 void FileBrowserEventRouter::OnFormattingFinished( 733 void FileBrowserEventRouter::OnFormatCompleted(
737 const std::string& device_path, bool success) { 734 const std::string& device_path, bool success) {
738 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 735 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
739 736
740 if (success) { 737 if (success) {
741 notifications_->HideNotification(FileBrowserNotifications::FORMAT_START, 738 notifications_->HideNotification(FileBrowserNotifications::FORMAT_START,
742 device_path); 739 device_path);
743 notifications_->ShowNotification(FileBrowserNotifications::FORMAT_SUCCESS, 740 notifications_->ShowNotification(FileBrowserNotifications::FORMAT_SUCCESS,
744 device_path); 741 device_path);
745 // Hide it after a couple of seconds. 742 // Hide it after a couple of seconds.
746 notifications_->HideNotificationDelayed( 743 notifications_->HideNotificationDelayed(
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
894 return scoped_refptr<RefcountedProfileKeyedService>( 891 return scoped_refptr<RefcountedProfileKeyedService>(
895 new FileBrowserEventRouter(profile)); 892 new FileBrowserEventRouter(profile));
896 } 893 }
897 894
898 bool FileBrowserEventRouterFactory::ServiceHasOwnInstanceInIncognito() const { 895 bool FileBrowserEventRouterFactory::ServiceHasOwnInstanceInIncognito() const {
899 // Explicitly and always allow this router in guest login mode. see 896 // Explicitly and always allow this router in guest login mode. see
900 // chrome/browser/profiles/profile_keyed_base_factory.h comment 897 // chrome/browser/profiles/profile_keyed_base_factory.h comment
901 // for the details. 898 // for the details.
902 return true; 899 return true;
903 } 900 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698