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 3// found in the LICENSE file. 4 5#ifndef CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_MANAGER_EVENT_ROUTER_H_ 6#define CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_MANAGER_EVENT_ROUTER_H_ 7 8#include <map> 9#include <string> 10 11#include "base/files/file_path_watcher.h" 12#include "base/memory/scoped_ptr.h" 13#include "chrome/browser/chromeos/drive/drive_integration_service.h" 14#include "chrome/browser/chromeos/drive/file_system_observer.h" 15#include "chrome/browser/chromeos/drive/job_list.h" 16#include "chrome/browser/chromeos/extensions/file_manager/file_watcher.h" 17#include "chrome/browser/drive/drive_service_interface.h" 18#include "chromeos/disks/disk_mount_manager.h" 19#include "chromeos/network/network_state_handler_observer.h" 20 21class PrefChangeRegistrar; 22class Profile; 23 24namespace chromeos { 25class NetworkState; 26} 27 28namespace file_manager { 29 30class DesktopNotifications; 31class MountedDiskMonitor; 32 33// Monitors changes in disk mounts, network connection state and preferences 34// affecting File Manager. Dispatches appropriate File Browser events. 35class EventRouter 36 : public chromeos::disks::DiskMountManager::Observer, 37 public chromeos::NetworkStateHandlerObserver, 38 public drive::DriveIntegrationServiceObserver, 39 public drive::FileSystemObserver, 40 public drive::JobListObserver, 41 public drive::DriveServiceObserver { 42 public: 43 explicit EventRouter(Profile* profile); 44 virtual ~EventRouter(); 45 46 void Shutdown(); 47 48 // Starts observing file system change events. 49 void ObserveFileSystemEvents(); 50 51 typedef base::Callback<void(bool success)> BoolCallback; 52 53 // Adds a file watch at |local_path|, associated with |virtual_path|, for 54 // an extension with |extension_id|. 55 // 56 // |callback| will be called with true on success, or false on failure. 57 // |callback| must not be null. 58 void AddFileWatch(const base::FilePath& local_path, 59 const base::FilePath& virtual_path, 60 const std::string& extension_id, 61 const BoolCallback& callback); 62 63 // Removes a file watch at |local_path| for an extension with |extension_id|. 64 void RemoveFileWatch(const base::FilePath& local_path, 65 const std::string& extension_id); 66 67 // CrosDisksClient::Observer overrides. 68 virtual void OnDiskEvent( 69 chromeos::disks::DiskMountManager::DiskEvent event, 70 const chromeos::disks::DiskMountManager::Disk* disk) OVERRIDE; 71 virtual void OnDeviceEvent( 72 chromeos::disks::DiskMountManager::DeviceEvent event, 73 const std::string& device_path) OVERRIDE; 74 virtual void OnMountEvent( 75 chromeos::disks::DiskMountManager::MountEvent event, 76 chromeos::MountError error_code, 77 const chromeos::disks::DiskMountManager::MountPointInfo& mount_info) 78 OVERRIDE; 79 virtual void OnFormatEvent( 80 chromeos::disks::DiskMountManager::FormatEvent event, 81 chromeos::FormatError error_code, 82 const std::string& device_path) OVERRIDE; 83 84 // chromeos::NetworkStateHandlerObserver overrides. 85 virtual void NetworkManagerChanged() OVERRIDE; 86 virtual void DefaultNetworkChanged( 87 const chromeos::NetworkState* network) OVERRIDE; 88 89 // drive::JobListObserver overrides. 90 virtual void OnJobAdded(const drive::JobInfo& job_info) OVERRIDE; 91 virtual void OnJobUpdated(const drive::JobInfo& job_info) OVERRIDE; 92 virtual void OnJobDone(const drive::JobInfo& job_info, 93 drive::FileError error) OVERRIDE; 94 95 // drive::DriveServiceObserver overrides. 96 virtual void OnRefreshTokenInvalid() OVERRIDE; 97 98 // drive::FileSystemObserver overrides. 99 virtual void OnDirectoryChanged( 100 const base::FilePath& directory_path) OVERRIDE; 101 102 // drive::DriveIntegrationServiceObserver overrides. 103 virtual void OnFileSystemMounted() OVERRIDE; 104 virtual void OnFileSystemBeingUnmounted() OVERRIDE; 105 106 private: 107 typedef std::map<base::FilePath, FileWatcher*> WatcherMap; 108 109 // USB mount event handlers. 110 void OnDiskAdded(const chromeos::disks::DiskMountManager::Disk* disk); 111 void OnDiskRemoved(const chromeos::disks::DiskMountManager::Disk* disk); 112 void OnDiskMounted(const chromeos::disks::DiskMountManager::Disk* disk); 113 void OnDiskUnmounted(const chromeos::disks::DiskMountManager::Disk* disk); 114 void OnDeviceAdded(const std::string& device_path); 115 void OnDeviceRemoved(const std::string& device_path); 116 void OnDeviceScanned(const std::string& device_path); 117 void OnFormatStarted(const std::string& device_path, bool success); 118 void OnFormatCompleted(const std::string& device_path, bool success); 119 120 // Called on change to kExternalStorageDisabled pref. 121 void OnExternalStorageDisabledChanged(); 122 123 // Called when prefs related to file manager change. 124 void OnFileManagerPrefsChanged(); 125 126 // Process file watch notifications. 127 void HandleFileWatchNotification(const base::FilePath& path, 128 bool got_error); 129 130 // Sends directory change event. 131 void DispatchDirectoryChangeEvent( 132 const base::FilePath& path, 133 bool error, 134 const std::vector<std::string>& extension_ids); 135 136 void DispatchMountEvent( 137 chromeos::disks::DiskMountManager::MountEvent event, 138 chromeos::MountError error_code, 139 const chromeos::disks::DiskMountManager::MountPointInfo& mount_info); 140 141 // If needed, opens a file manager window for the removable device mounted at 142 // |mount_path|. Disk.mount_path() is empty, since it is being filled out 143 // after calling notifying observers by DiskMountManager. 144 void ShowRemovableDeviceInFileManager( 145 const chromeos::disks::DiskMountManager::Disk& disk, 146 const base::FilePath& mount_path); 147 148 // Sends onFileTranferUpdated to extensions if needed. If |always| is true, 149 // it sends the event always. Otherwise, it sends the event if enough time has 150 // passed from the previous event so as not to make extension busy. 151 void SendDriveFileTransferEvent(bool always); 152 153 // Manages the list of currently active Drive file transfer jobs. 154 struct DriveJobInfoWithStatus { 155 DriveJobInfoWithStatus(); 156 DriveJobInfoWithStatus(const drive::JobInfo& info, 157 const std::string& status); 158 drive::JobInfo job_info; 159 std::string status; 160 }; 161 std::map<drive::JobID, DriveJobInfoWithStatus> drive_jobs_; 162 base::Time last_file_transfer_event_; 163 164 WatcherMap file_watchers_; 165 scoped_ptr<DesktopNotifications> notifications_; 166 scoped_ptr<PrefChangeRegistrar> pref_change_registrar_; 167 scoped_ptr<MountedDiskMonitor> mounted_disk_monitor_; 168 Profile* profile_; 169 170 // Note: This should remain the last member so it'll be destroyed and 171 // invalidate the weak pointers before any other members are destroyed. 172 base::WeakPtrFactory<EventRouter> weak_factory_; 173 DISALLOW_COPY_AND_ASSIGN(EventRouter); 174}; 175 176} // namespace file_manager 177 178#endif // CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_MANAGER_EVENT_ROUTER_H_ 179