| OLD | NEW |
| 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_manager/event_router.h" | 5 #include "chrome/browser/chromeos/extensions/file_manager/event_router.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/prefs/pref_change_registrar.h" | 10 #include "base/prefs/pref_change_registrar.h" |
| 11 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
| 12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
| 13 #include "base/threading/sequenced_worker_pool.h" | 13 #include "base/threading/sequenced_worker_pool.h" |
| 14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 #include "chrome/browser/app_mode/app_mode_utils.h" | 15 #include "chrome/browser/app_mode/app_mode_utils.h" |
| 16 #include "chrome/browser/chrome_notification_types.h" | 16 #include "chrome/browser/chrome_notification_types.h" |
| 17 #include "chrome/browser/chromeos/drive/drive_integration_service.h" | 17 #include "chrome/browser/chromeos/drive/drive_integration_service.h" |
| 18 #include "chrome/browser/chromeos/drive/file_change.h" |
| 18 #include "chrome/browser/chromeos/drive/file_system_interface.h" | 19 #include "chrome/browser/chromeos/drive/file_system_interface.h" |
| 19 #include "chrome/browser/chromeos/drive/file_system_util.h" | 20 #include "chrome/browser/chromeos/drive/file_system_util.h" |
| 20 #include "chrome/browser/chromeos/extensions/file_manager/private_api_util.h" | 21 #include "chrome/browser/chromeos/extensions/file_manager/private_api_util.h" |
| 21 #include "chrome/browser/chromeos/file_manager/app_id.h" | 22 #include "chrome/browser/chromeos/file_manager/app_id.h" |
| 22 #include "chrome/browser/chromeos/file_manager/fileapi_util.h" | 23 #include "chrome/browser/chromeos/file_manager/fileapi_util.h" |
| 23 #include "chrome/browser/chromeos/file_manager/open_util.h" | 24 #include "chrome/browser/chromeos/file_manager/open_util.h" |
| 24 #include "chrome/browser/chromeos/file_manager/volume_manager.h" | 25 #include "chrome/browser/chromeos/file_manager/volume_manager.h" |
| 25 #include "chrome/browser/chromeos/login/lock/screen_locker.h" | 26 #include "chrome/browser/chromeos/login/lock/screen_locker.h" |
| 26 #include "chrome/browser/chromeos/login/ui/login_display_host_impl.h" | 27 #include "chrome/browser/chromeos/login/ui/login_display_host_impl.h" |
| 27 #include "chrome/browser/drive/drive_service_interface.h" | 28 #include "chrome/browser/drive/drive_service_interface.h" |
| 28 #include "chrome/browser/extensions/extension_service.h" | 29 #include "chrome/browser/extensions/extension_service.h" |
| 30 #include "chrome/browser/extensions/extension_util.h" |
| 29 #include "chrome/browser/profiles/profile.h" | 31 #include "chrome/browser/profiles/profile.h" |
| 30 #include "chrome/browser/profiles/profile_manager.h" | 32 #include "chrome/browser/profiles/profile_manager.h" |
| 31 #include "chrome/common/pref_names.h" | 33 #include "chrome/common/pref_names.h" |
| 32 #include "chromeos/login/login_state.h" | 34 #include "chromeos/login/login_state.h" |
| 33 #include "chromeos/network/network_handler.h" | 35 #include "chromeos/network/network_handler.h" |
| 34 #include "chromeos/network/network_state_handler.h" | 36 #include "chromeos/network/network_state_handler.h" |
| 35 #include "content/public/browser/browser_thread.h" | 37 #include "content/public/browser/browser_thread.h" |
| 36 #include "content/public/browser/notification_source.h" | 38 #include "content/public/browser/notification_source.h" |
| 37 #include "content/public/browser/render_process_host.h" | 39 #include "content/public/browser/render_process_host.h" |
| 40 #include "content/public/browser/storage_partition.h" |
| 38 #include "extensions/browser/event_router.h" | 41 #include "extensions/browser/event_router.h" |
| 39 #include "extensions/browser/extension_host.h" | 42 #include "extensions/browser/extension_host.h" |
| 40 #include "extensions/browser/extension_prefs.h" | 43 #include "extensions/browser/extension_prefs.h" |
| 41 #include "extensions/browser/extension_system.h" | 44 #include "extensions/browser/extension_system.h" |
| 42 #include "webkit/common/fileapi/file_system_types.h" | 45 #include "webkit/common/fileapi/file_system_types.h" |
| 43 #include "webkit/common/fileapi/file_system_util.h" | 46 #include "webkit/common/fileapi/file_system_util.h" |
| 44 | 47 |
| 45 using chromeos::disks::DiskMountManager; | 48 using chromeos::disks::DiskMountManager; |
| 46 using chromeos::NetworkHandler; | 49 using chromeos::NetworkHandler; |
| 47 using content::BrowserThread; | 50 using content::BrowserThread; |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 261 return file_browser_private::COPY_PROGRESS_STATUS_TYPE_BEGIN_COPY_ENTRY; | 264 return file_browser_private::COPY_PROGRESS_STATUS_TYPE_BEGIN_COPY_ENTRY; |
| 262 case fileapi::FileSystemOperation::END_COPY_ENTRY: | 265 case fileapi::FileSystemOperation::END_COPY_ENTRY: |
| 263 return file_browser_private::COPY_PROGRESS_STATUS_TYPE_END_COPY_ENTRY; | 266 return file_browser_private::COPY_PROGRESS_STATUS_TYPE_END_COPY_ENTRY; |
| 264 case fileapi::FileSystemOperation::PROGRESS: | 267 case fileapi::FileSystemOperation::PROGRESS: |
| 265 return file_browser_private::COPY_PROGRESS_STATUS_TYPE_PROGRESS; | 268 return file_browser_private::COPY_PROGRESS_STATUS_TYPE_PROGRESS; |
| 266 } | 269 } |
| 267 NOTREACHED(); | 270 NOTREACHED(); |
| 268 return file_browser_private::COPY_PROGRESS_STATUS_TYPE_NONE; | 271 return file_browser_private::COPY_PROGRESS_STATUS_TYPE_NONE; |
| 269 } | 272 } |
| 270 | 273 |
| 274 file_browser_private::ChangeType ConvertChangeTypeFromDriveToApi( |
| 275 drive::FileChange::ChangeType type) { |
| 276 switch (type) { |
| 277 case drive::FileChange::ADD_OR_UPDATE: |
| 278 return file_browser_private::CHANGE_TYPE_ADD_OR_UPDATE; |
| 279 case drive::FileChange::DELETE: |
| 280 return file_browser_private::CHANGE_TYPE_DELETE; |
| 281 } |
| 282 NOTREACHED(); |
| 283 return file_browser_private::CHANGE_TYPE_ADD_OR_UPDATE; |
| 284 } |
| 285 |
| 271 std::string FileErrorToErrorName(base::File::Error error_code) { | 286 std::string FileErrorToErrorName(base::File::Error error_code) { |
| 272 namespace js = extensions::api::file_browser_private; | 287 namespace js = extensions::api::file_browser_private; |
| 273 switch (error_code) { | 288 switch (error_code) { |
| 274 case base::File::FILE_ERROR_NOT_FOUND: | 289 case base::File::FILE_ERROR_NOT_FOUND: |
| 275 return "NotFoundError"; | 290 return "NotFoundError"; |
| 276 case base::File::FILE_ERROR_INVALID_OPERATION: | 291 case base::File::FILE_ERROR_INVALID_OPERATION: |
| 277 case base::File::FILE_ERROR_EXISTS: | 292 case base::File::FILE_ERROR_EXISTS: |
| 278 case base::File::FILE_ERROR_NOT_EMPTY: | 293 case base::File::FILE_ERROR_NOT_EMPTY: |
| 279 return "InvalidModificationError"; | 294 return "InvalidModificationError"; |
| 280 case base::File::FILE_ERROR_NOT_A_DIRECTORY: | 295 case base::File::FILE_ERROR_NOT_A_DIRECTORY: |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 464 | 479 |
| 465 if (is_on_drive) { | 480 if (is_on_drive) { |
| 466 // For Drive, file watching is done via OnDirectoryChanged(). | 481 // For Drive, file watching is done via OnDirectoryChanged(). |
| 467 base::MessageLoopProxy::current()->PostTask(FROM_HERE, | 482 base::MessageLoopProxy::current()->PostTask(FROM_HERE, |
| 468 base::Bind(callback, true)); | 483 base::Bind(callback, true)); |
| 469 } else { | 484 } else { |
| 470 // For local files, start watching using FileWatcher. | 485 // For local files, start watching using FileWatcher. |
| 471 watcher->WatchLocalFile( | 486 watcher->WatchLocalFile( |
| 472 watch_path, | 487 watch_path, |
| 473 base::Bind(&EventRouter::HandleFileWatchNotification, | 488 base::Bind(&EventRouter::HandleFileWatchNotification, |
| 474 weak_factory_.GetWeakPtr()), | 489 weak_factory_.GetWeakPtr(), |
| 490 static_cast<drive::FileChange*>(NULL)), |
| 475 callback); | 491 callback); |
| 476 } | 492 } |
| 477 | 493 |
| 478 file_watchers_[watch_path] = watcher.release(); | 494 file_watchers_[watch_path] = watcher.release(); |
| 479 } else { | 495 } else { |
| 480 iter->second->AddExtension(extension_id); | 496 iter->second->AddExtension(extension_id); |
| 481 base::MessageLoopProxy::current()->PostTask(FROM_HERE, | 497 base::MessageLoopProxy::current()->PostTask(FROM_HERE, |
| 482 base::Bind(callback, true)); | 498 base::Bind(callback, true)); |
| 483 } | 499 } |
| 484 } | 500 } |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 645 iter->second.job_info, | 661 iter->second.job_info, |
| 646 status.get()); | 662 status.get()); |
| 647 status_list.push_back(status); | 663 status_list.push_back(status); |
| 648 } | 664 } |
| 649 BroadcastEvent( | 665 BroadcastEvent( |
| 650 profile_, | 666 profile_, |
| 651 file_browser_private::OnFileTransfersUpdated::kEventName, | 667 file_browser_private::OnFileTransfersUpdated::kEventName, |
| 652 file_browser_private::OnFileTransfersUpdated::Create(status_list)); | 668 file_browser_private::OnFileTransfersUpdated::Create(status_list)); |
| 653 } | 669 } |
| 654 | 670 |
| 655 void EventRouter::OnDirectoryChanged(const base::FilePath& drive_path) { | 671 void EventRouter::OnDirectoryChanged(const base::FilePath& drive_path, |
| 656 HandleFileWatchNotification(drive_path, false); | 672 const drive::FileChange* list) { |
| 673 HandleFileWatchNotification(list, drive_path, false); |
| 657 } | 674 } |
| 658 | 675 |
| 659 void EventRouter::OnDriveSyncError(drive::file_system::DriveSyncErrorType type, | 676 void EventRouter::OnDriveSyncError(drive::file_system::DriveSyncErrorType type, |
| 660 const base::FilePath& drive_path) { | 677 const base::FilePath& drive_path) { |
| 661 file_browser_private::DriveSyncErrorEvent event; | 678 file_browser_private::DriveSyncErrorEvent event; |
| 662 switch (type) { | 679 switch (type) { |
| 663 case drive::file_system::DRIVE_SYNC_ERROR_DELETE_WITHOUT_PERMISSION: | 680 case drive::file_system::DRIVE_SYNC_ERROR_DELETE_WITHOUT_PERMISSION: |
| 664 event.type = | 681 event.type = |
| 665 file_browser_private::DRIVE_SYNC_ERROR_TYPE_DELETE_WITHOUT_PERMISSION; | 682 file_browser_private::DRIVE_SYNC_ERROR_TYPE_DELETE_WITHOUT_PERMISSION; |
| 666 break; | 683 break; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 684 void EventRouter::OnRefreshTokenInvalid() { | 701 void EventRouter::OnRefreshTokenInvalid() { |
| 685 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 702 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 686 | 703 |
| 687 // Raise a DriveConnectionStatusChanged event to notify the status offline. | 704 // Raise a DriveConnectionStatusChanged event to notify the status offline. |
| 688 BroadcastEvent( | 705 BroadcastEvent( |
| 689 profile_, | 706 profile_, |
| 690 file_browser_private::OnDriveConnectionStatusChanged::kEventName, | 707 file_browser_private::OnDriveConnectionStatusChanged::kEventName, |
| 691 file_browser_private::OnDriveConnectionStatusChanged::Create()); | 708 file_browser_private::OnDriveConnectionStatusChanged::Create()); |
| 692 } | 709 } |
| 693 | 710 |
| 694 void EventRouter::HandleFileWatchNotification(const base::FilePath& local_path, | 711 void EventRouter::HandleFileWatchNotification(const drive::FileChange* list, |
| 712 const base::FilePath& local_path, |
| 695 bool got_error) { | 713 bool got_error) { |
| 696 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 714 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 697 | 715 |
| 698 WatcherMap::const_iterator iter = file_watchers_.find(local_path); | 716 WatcherMap::const_iterator iter = file_watchers_.find(local_path); |
| 699 if (iter == file_watchers_.end()) { | 717 if (iter == file_watchers_.end()) { |
| 700 return; | 718 return; |
| 701 } | 719 } |
| 702 DispatchDirectoryChangeEvent(iter->second->virtual_path(), got_error, | 720 DispatchDirectoryChangeEvent(iter->second->virtual_path(), |
| 721 list, |
| 722 got_error, |
| 703 iter->second->GetExtensionIds()); | 723 iter->second->GetExtensionIds()); |
| 704 } | 724 } |
| 705 | 725 |
| 706 void EventRouter::DispatchDirectoryChangeEvent( | 726 void EventRouter::DispatchDirectoryChangeEvent( |
| 707 const base::FilePath& virtual_path, | 727 const base::FilePath& virtual_path, |
| 728 const drive::FileChange* list, |
| 708 bool got_error, | 729 bool got_error, |
| 709 const std::vector<std::string>& extension_ids) { | 730 const std::vector<std::string>& extension_ids) { |
| 710 if (!profile_) { | 731 if (!profile_) { |
| 711 NOTREACHED(); | 732 NOTREACHED(); |
| 712 return; | 733 return; |
| 713 } | 734 } |
| 735 linked_ptr<drive::FileChange> changes; |
| 736 if (list) |
| 737 changes.reset(new drive::FileChange(*list)); // Copy |
| 714 | 738 |
| 715 for (size_t i = 0; i < extension_ids.size(); ++i) { | 739 for (size_t i = 0; i < extension_ids.size(); ++i) { |
| 716 const std::string& extension_id = extension_ids[i]; | 740 std::string* extension_id = new std::string(extension_ids[i]); |
| 717 | 741 |
| 718 FileDefinition file_definition; | 742 FileDefinition file_definition; |
| 719 file_definition.virtual_path = virtual_path; | 743 file_definition.virtual_path = virtual_path; |
| 720 file_definition.is_directory = true; | 744 file_definition.is_directory = true; |
| 721 | 745 |
| 722 file_manager::util::ConvertFileDefinitionToEntryDefinition( | 746 file_manager::util::ConvertFileDefinitionToEntryDefinition( |
| 723 profile_, | 747 profile_, |
| 724 extension_id, | 748 *extension_id, |
| 725 file_definition, | 749 file_definition, |
| 726 base::Bind( | 750 base::Bind( |
| 727 &EventRouter::DispatchDirectoryChangeEventWithEntryDefinition, | 751 &EventRouter::DispatchDirectoryChangeEventWithEntryDefinition, |
| 728 weak_factory_.GetWeakPtr(), | 752 weak_factory_.GetWeakPtr(), |
| 753 changes, |
| 754 base::Owned(extension_id), |
| 729 got_error)); | 755 got_error)); |
| 730 } | 756 } |
| 731 } | 757 } |
| 732 | 758 |
| 733 void EventRouter::DispatchDirectoryChangeEventWithEntryDefinition( | 759 void EventRouter::DispatchDirectoryChangeEventWithEntryDefinition( |
| 760 const linked_ptr<drive::FileChange> list, |
| 761 const std::string* extension_id, |
| 734 bool watcher_error, | 762 bool watcher_error, |
| 735 const EntryDefinition& entry_definition) { | 763 const EntryDefinition& entry_definition) { |
| 764 typedef std::map<base::FilePath, drive::FileChange::ChangeList> ChangeListMap; |
| 765 |
| 736 if (entry_definition.error != base::File::FILE_OK || | 766 if (entry_definition.error != base::File::FILE_OK || |
| 737 !entry_definition.is_directory) { | 767 !entry_definition.is_directory) { |
| 738 DVLOG(1) << "Unable to dispatch event because resolving the directory " | 768 DVLOG(1) << "Unable to dispatch event because resolving the directory " |
| 739 << "entry definition failed."; | 769 << "entry definition failed."; |
| 740 return; | 770 return; |
| 741 } | 771 } |
| 742 | 772 |
| 743 file_browser_private::FileWatchEvent event; | 773 file_browser_private::FileWatchEvent event; |
| 744 event.event_type = watcher_error | 774 event.event_type = watcher_error |
| 745 ? file_browser_private::FILE_WATCH_EVENT_TYPE_ERROR | 775 ? file_browser_private::FILE_WATCH_EVENT_TYPE_ERROR |
| 746 : file_browser_private::FILE_WATCH_EVENT_TYPE_CHANGED; | 776 : file_browser_private::FILE_WATCH_EVENT_TYPE_CHANGED; |
| 747 | 777 |
| 778 // Detailed information is available. |
| 779 if (list.get()) { |
| 780 event.changed_files.reset( |
| 781 new std::vector<linked_ptr<file_browser_private::FileChange> >); |
| 782 |
| 783 if (list->map().empty()) |
| 784 return; |
| 785 |
| 786 for (drive::FileChange::Map::const_iterator it = list->map().begin(); |
| 787 it != list->map().end(); |
| 788 it++) { |
| 789 linked_ptr<file_browser_private::FileChange> change_list( |
| 790 new file_browser_private::FileChange); |
| 791 |
| 792 GURL url = util::ConvertDrivePathToFileSystemUrl( |
| 793 profile_, it->first, *extension_id); |
| 794 change_list->url = url.spec(); |
| 795 |
| 796 for (drive::FileChange::ChangeList::List::const_iterator change = |
| 797 it->second.list().begin(); |
| 798 change != it->second.list().end(); |
| 799 change++) { |
| 800 change_list->changes.push_back( |
| 801 ConvertChangeTypeFromDriveToApi(change->change())); |
| 802 } |
| 803 |
| 804 event.changed_files->push_back(change_list); |
| 805 } |
| 806 } |
| 807 |
| 748 event.entry.additional_properties.SetString( | 808 event.entry.additional_properties.SetString( |
| 749 "fileSystemName", entry_definition.file_system_name); | 809 "fileSystemName", entry_definition.file_system_name); |
| 750 event.entry.additional_properties.SetString( | 810 event.entry.additional_properties.SetString( |
| 751 "fileSystemRoot", entry_definition.file_system_root_url); | 811 "fileSystemRoot", entry_definition.file_system_root_url); |
| 752 event.entry.additional_properties.SetString( | 812 event.entry.additional_properties.SetString( |
| 753 "fileFullPath", "/" + entry_definition.full_path.value()); | 813 "fileFullPath", "/" + entry_definition.full_path.value()); |
| 754 event.entry.additional_properties.SetBoolean("fileIsDirectory", | 814 event.entry.additional_properties.SetBoolean("fileIsDirectory", |
| 755 entry_definition.is_directory); | 815 entry_definition.is_directory); |
| 756 | 816 |
| 757 BroadcastEvent(profile_, | 817 BroadcastEvent(profile_, |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 792 } | 852 } |
| 793 } | 853 } |
| 794 | 854 |
| 795 util::OpenRemovableDrive(profile_, mount_path); | 855 util::OpenRemovableDrive(profile_, mount_path); |
| 796 } | 856 } |
| 797 | 857 |
| 798 void EventRouter::DispatchDeviceEvent( | 858 void EventRouter::DispatchDeviceEvent( |
| 799 file_browser_private::DeviceEventType type, | 859 file_browser_private::DeviceEventType type, |
| 800 const std::string& device_path) { | 860 const std::string& device_path) { |
| 801 file_browser_private::DeviceEvent event; | 861 file_browser_private::DeviceEvent event; |
| 862 |
| 802 event.type = type; | 863 event.type = type; |
| 803 event.device_path = device_path; | 864 event.device_path = device_path; |
| 804 BroadcastEvent(profile_, | 865 BroadcastEvent(profile_, |
| 805 file_browser_private::OnDeviceChanged::kEventName, | 866 file_browser_private::OnDeviceChanged::kEventName, |
| 806 file_browser_private::OnDeviceChanged::Create(event)); | 867 file_browser_private::OnDeviceChanged::Create(event)); |
| 807 } | 868 } |
| 808 | 869 |
| 809 void EventRouter::OnDiskAdded( | 870 void EventRouter::OnDiskAdded( |
| 810 const DiskMountManager::Disk& disk, bool mounting) { | 871 const DiskMountManager::Disk& disk, bool mounting) { |
| 811 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 872 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 942 } | 1003 } |
| 943 } | 1004 } |
| 944 | 1005 |
| 945 void EventRouter::OnOwnerEntryChanged(aura::Window* window) { | 1006 void EventRouter::OnOwnerEntryChanged(aura::Window* window) { |
| 946 BroadcastEvent(profile_, | 1007 BroadcastEvent(profile_, |
| 947 file_browser_private::OnDesktopChanged::kEventName, | 1008 file_browser_private::OnDesktopChanged::kEventName, |
| 948 file_browser_private::OnDesktopChanged::Create()); | 1009 file_browser_private::OnDesktopChanged::Create()); |
| 949 } | 1010 } |
| 950 | 1011 |
| 951 } // namespace file_manager | 1012 } // namespace file_manager |
| OLD | NEW |