event_router.h revision 0529e5d033099cbfc42635f6f6183833b09dff6e
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/basictypes.h" 12#include "base/compiler_specific.h" 13#include "base/files/file_path_watcher.h" 14#include "base/memory/scoped_ptr.h" 15#include "chrome/browser/chromeos/drive/drive_integration_service.h" 16#include "chrome/browser/chromeos/drive/file_system_observer.h" 17#include "chrome/browser/chromeos/drive/job_list.h" 18#include "chrome/browser/chromeos/drive/sync_client.h" 19#include "chrome/browser/chromeos/file_manager/file_watcher.h" 20#include "chrome/browser/chromeos/file_manager/fileapi_util.h" 21#include "chrome/browser/chromeos/file_manager/volume_manager_observer.h" 22#include "chrome/browser/drive/drive_service_interface.h" 23#include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h" 24#include "chrome/common/extensions/api/file_browser_private.h" 25#include "chromeos/disks/disk_mount_manager.h" 26#include "chromeos/network/network_state_handler_observer.h" 27#include "content/public/browser/notification_observer.h" 28#include "content/public/browser/notification_registrar.h" 29#include "webkit/browser/fileapi/file_system_operation.h" 30 31class PrefChangeRegistrar; 32class Profile; 33 34using file_manager::util::EntryDefinition; 35 36namespace base { 37class ListValue; 38} 39 40namespace chromeos { 41class NetworkState; 42} 43 44namespace file_manager { 45 46// Monitors changes in disk mounts, network connection state and preferences 47// affecting File Manager. Dispatches appropriate File Browser events. 48class EventRouter 49 : public chromeos::NetworkStateHandlerObserver, 50 public drive::FileSystemObserver, 51 public drive::JobListObserver, 52 public drive::DriveServiceObserver, 53 public VolumeManagerObserver, 54 public content::NotificationObserver, 55 public chrome::MultiUserWindowManager::Observer { 56 public: 57 explicit EventRouter(Profile* profile); 58 virtual ~EventRouter(); 59 60 void Shutdown(); 61 62 // Starts observing file system change events. 63 void ObserveEvents(); 64 65 typedef base::Callback<void(bool success)> BoolCallback; 66 67 // Adds a file watch at |local_path|, associated with |virtual_path|, for 68 // an extension with |extension_id|. 69 // 70 // |callback| will be called with true on success, or false on failure. 71 // |callback| must not be null. 72 void AddFileWatch(const base::FilePath& local_path, 73 const base::FilePath& virtual_path, 74 const std::string& extension_id, 75 const BoolCallback& callback); 76 77 // Removes a file watch at |local_path| for an extension with |extension_id|. 78 void RemoveFileWatch(const base::FilePath& local_path, 79 const std::string& extension_id); 80 81 // Called when a copy task is completed. 82 void OnCopyCompleted( 83 int copy_id, const GURL& source_url, const GURL& destination_url, 84 base::File::Error error); 85 86 // Called when a copy task progress is updated. 87 void OnCopyProgress(int copy_id, 88 fileapi::FileSystemOperation::CopyProgressType type, 89 const GURL& source_url, 90 const GURL& destination_url, 91 int64 size); 92 93 // Register observer to the multi user window manager. 94 void RegisterMultiUserWindowManagerObserver(); 95 96 // chromeos::NetworkStateHandlerObserver overrides. 97 virtual void DefaultNetworkChanged( 98 const chromeos::NetworkState* network) OVERRIDE; 99 100 // drive::JobListObserver overrides. 101 virtual void OnJobAdded(const drive::JobInfo& job_info) OVERRIDE; 102 virtual void OnJobUpdated(const drive::JobInfo& job_info) OVERRIDE; 103 virtual void OnJobDone(const drive::JobInfo& job_info, 104 drive::FileError error) OVERRIDE; 105 106 // drive::DriveServiceObserver overrides. 107 virtual void OnRefreshTokenInvalid() OVERRIDE; 108 109 // drive::FileSystemObserver overrides. 110 virtual void OnDirectoryChanged(const base::FilePath& drive_path) OVERRIDE; 111 virtual void OnDriveSyncError(drive::file_system::DriveSyncErrorType type, 112 const base::FilePath& drive_path) OVERRIDE; 113 114 // VolumeManagerObserver overrides. 115 virtual void OnDiskAdded( 116 const chromeos::disks::DiskMountManager::Disk& disk, 117 bool mounting) OVERRIDE; 118 virtual void OnDiskRemoved( 119 const chromeos::disks::DiskMountManager::Disk& disk) OVERRIDE; 120 virtual void OnDeviceAdded(const std::string& device_path) OVERRIDE; 121 virtual void OnDeviceRemoved(const std::string& device_path, 122 bool hard_unplugged) OVERRIDE; 123 virtual void OnVolumeMounted(chromeos::MountError error_code, 124 const VolumeInfo& volume_info, 125 bool is_remounting) OVERRIDE; 126 virtual void OnVolumeUnmounted(chromeos::MountError error_code, 127 const VolumeInfo& volume_info) OVERRIDE; 128 virtual void OnFormatStarted( 129 const std::string& device_path, bool success) OVERRIDE; 130 virtual void OnFormatCompleted( 131 const std::string& device_path, bool success) OVERRIDE; 132 133 // content::NotificationObserver overrides. 134 virtual void Observe(int type, 135 const content::NotificationSource& source, 136 const content::NotificationDetails& details) OVERRIDE; 137 138 // chrome::MultiUserWindowManager::Observer overrides: 139 virtual void OnOwnerEntryChanged(aura::Window* window) OVERRIDE; 140 141 private: 142 typedef std::map<base::FilePath, FileWatcher*> WatcherMap; 143 144 // Called when prefs related to file manager change. 145 void OnFileManagerPrefsChanged(); 146 147 // Process file watch notifications. 148 void HandleFileWatchNotification(const base::FilePath& path, 149 bool got_error); 150 151 // Sends directory change event. 152 void DispatchDirectoryChangeEvent( 153 const base::FilePath& path, 154 bool error, 155 const std::vector<std::string>& extension_ids); 156 157 // Sends directory change event, after converting the file definition to entry 158 // definition. 159 void DispatchDirectoryChangeEventWithEntryDefinition( 160 bool watcher_error, 161 const EntryDefinition& entry_definition); 162 163 // If needed, opens a file manager window for the removable device mounted at 164 // |mount_path|. Disk.mount_path() is empty, since it is being filled out 165 // after calling notifying observers by DiskMountManager. 166 void ShowRemovableDeviceInFileManager(const base::FilePath& mount_path); 167 168 // Dispatches an onDeviceChanged event containing |type| and |path| to 169 // extensions. 170 void DispatchDeviceEvent( 171 extensions::api::file_browser_private::DeviceEventType type, 172 const std::string& path); 173 174 // Sends onFileTranferUpdated to extensions if needed. If |always| is true, 175 // it sends the event always. Otherwise, it sends the event if enough time has 176 // passed from the previous event so as not to make extension busy. 177 void SendDriveFileTransferEvent(bool always); 178 179 // Manages the list of currently active Drive file transfer jobs. 180 struct DriveJobInfoWithStatus { 181 DriveJobInfoWithStatus(); 182 DriveJobInfoWithStatus(const drive::JobInfo& info, 183 const std::string& status); 184 drive::JobInfo job_info; 185 std::string status; 186 }; 187 std::map<drive::JobID, DriveJobInfoWithStatus> drive_jobs_; 188 base::Time last_file_transfer_event_; 189 base::Time last_copy_progress_event_; 190 191 WatcherMap file_watchers_; 192 scoped_ptr<PrefChangeRegistrar> pref_change_registrar_; 193 Profile* profile_; 194 195 content::NotificationRegistrar notification_registrar_; 196 bool multi_user_window_manager_observer_registered_; 197 198 // Note: This should remain the last member so it'll be destroyed and 199 // invalidate the weak pointers before any other members are destroyed. 200 base::WeakPtrFactory<EventRouter> weak_factory_; 201 DISALLOW_COPY_AND_ASSIGN(EventRouter); 202}; 203 204} // namespace file_manager 205 206#endif // CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_MANAGER_EVENT_ROUTER_H_ 207