| OLD | NEW |
| (Empty) |
| 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_BROWSER_EVENT_ROUTER_H_ | |
| 6 #define CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_BROWSER_EVENT_ROUTER_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <set> | |
| 10 #include <string> | |
| 11 | |
| 12 #include "base/files/file_path_watcher.h" | |
| 13 #include "base/memory/linked_ptr.h" | |
| 14 #include "base/memory/scoped_ptr.h" | |
| 15 #include "base/string16.h" | |
| 16 #include "base/synchronization/lock.h" | |
| 17 #include "chrome/browser/chromeos/drive/drive_file_system_observer.h" | |
| 18 #include "chrome/browser/chromeos/drive/drive_resource_metadata.h" | |
| 19 #include "chrome/browser/chromeos/net/connectivity_state_helper_observer.h" | |
| 20 #include "chrome/browser/chromeos/system_key_event_listener.h" | |
| 21 #include "chrome/browser/google_apis/drive_service_interface.h" | |
| 22 #include "chrome/browser/google_apis/operation_registry.h" | |
| 23 #include "chromeos/disks/disk_mount_manager.h" | |
| 24 | |
| 25 class FileBrowserNotifications; | |
| 26 class PrefChangeRegistrar; | |
| 27 class Profile; | |
| 28 | |
| 29 namespace drive { | |
| 30 class DriveEntryProto; | |
| 31 class DriveFileSystemInterface; | |
| 32 } | |
| 33 | |
| 34 // Monitors changes in disk mounts, network connection state and preferences | |
| 35 // affecting File Manager. Dispatches appropriate File Browser events. | |
| 36 class FileBrowserEventRouter | |
| 37 : public chromeos::disks::DiskMountManager::Observer, | |
| 38 public chromeos::ConnectivityStateHelperObserver, | |
| 39 public chromeos::SystemKeyEventListener::ModifiersObserver, | |
| 40 public drive::DriveFileSystemObserver, | |
| 41 public google_apis::DriveServiceObserver { | |
| 42 public: | |
| 43 // Interface that should keep track of the system state in regards to system | |
| 44 // suspend and resume events. | |
| 45 // When the |IsResuming()| returns true, it should be able to check if a | |
| 46 // removable device was present before the was system suspended. | |
| 47 class SuspendStateDelegate { | |
| 48 public: | |
| 49 virtual ~SuspendStateDelegate() {} | |
| 50 | |
| 51 // Returns true if the system has recently woken up. | |
| 52 virtual bool SystemIsResuming() const = 0; | |
| 53 // If system is resuming, returns true if the disk was present before the | |
| 54 // system suspend. Should return false if the system is not resuming. | |
| 55 virtual bool DiskWasPresentBeforeSuspend( | |
| 56 const chromeos::disks::DiskMountManager::Disk& disk) const = 0; | |
| 57 }; | |
| 58 | |
| 59 explicit FileBrowserEventRouter(Profile* profile); | |
| 60 virtual ~FileBrowserEventRouter(); | |
| 61 | |
| 62 void Shutdown(); | |
| 63 | |
| 64 // Starts observing file system change events. | |
| 65 void ObserveFileSystemEvents(); | |
| 66 | |
| 67 typedef base::Callback<void(bool success)> BoolCallback; | |
| 68 | |
| 69 // Adds a file watch at |local_path|, associated with |virtual_path|, for | |
| 70 // an extension with |extension_id|. | |
| 71 // | |
| 72 // |callback| will be called with true on success, or false on failure. | |
| 73 // |callback| must not be null. | |
| 74 void AddFileWatch(const base::FilePath& local_path, | |
| 75 const base::FilePath& virtual_path, | |
| 76 const std::string& extension_id, | |
| 77 const BoolCallback& callback); | |
| 78 | |
| 79 // Removes a file watch at |local_path| for an extension with |extension_id|. | |
| 80 void RemoveFileWatch(const base::FilePath& local_path, | |
| 81 const std::string& extension_id); | |
| 82 | |
| 83 // Mounts Drive on File browser. |callback| will be called after raising a | |
| 84 // mount request event to file manager on JS-side. | |
| 85 void MountDrive(const base::Closure& callback); | |
| 86 | |
| 87 // CrosDisksClient::Observer overrides. | |
| 88 virtual void OnDiskEvent( | |
| 89 chromeos::disks::DiskMountManager::DiskEvent event, | |
| 90 const chromeos::disks::DiskMountManager::Disk* disk) OVERRIDE; | |
| 91 virtual void OnDeviceEvent( | |
| 92 chromeos::disks::DiskMountManager::DeviceEvent event, | |
| 93 const std::string& device_path) OVERRIDE; | |
| 94 virtual void OnMountEvent( | |
| 95 chromeos::disks::DiskMountManager::MountEvent event, | |
| 96 chromeos::MountError error_code, | |
| 97 const chromeos::disks::DiskMountManager::MountPointInfo& mount_info) | |
| 98 OVERRIDE; | |
| 99 virtual void OnFormatEvent( | |
| 100 chromeos::disks::DiskMountManager::FormatEvent event, | |
| 101 chromeos::FormatError error_code, | |
| 102 const std::string& device_path) OVERRIDE; | |
| 103 | |
| 104 // chromeos::ConnectivityStateHelperObserver override. | |
| 105 virtual void NetworkManagerChanged() OVERRIDE; | |
| 106 | |
| 107 // drive::DriveServiceObserver overrides. | |
| 108 virtual void OnProgressUpdate( | |
| 109 const google_apis::OperationProgressStatusList& list) OVERRIDE; | |
| 110 virtual void OnRefreshTokenInvalid() OVERRIDE; | |
| 111 | |
| 112 // drive::DriveFileSystemInterface::Observer overrides. | |
| 113 virtual void OnDirectoryChanged( | |
| 114 const base::FilePath& directory_path) OVERRIDE; | |
| 115 virtual void OnFileSystemMounted() OVERRIDE; | |
| 116 virtual void OnFileSystemBeingUnmounted() OVERRIDE; | |
| 117 | |
| 118 // chromeos::SystemKeyEventListener::ModifiersObserver overrides. | |
| 119 virtual void OnModifiersChange(int pressed_modifiers) OVERRIDE; | |
| 120 | |
| 121 private: | |
| 122 typedef std::map<std::string, int> ExtensionUsageRegistry; | |
| 123 | |
| 124 // This class is used to remember what extensions are watching |virtual_path|. | |
| 125 class FileWatcherExtensions { | |
| 126 public: | |
| 127 FileWatcherExtensions(const base::FilePath& virtual_path, | |
| 128 const std::string& extension_id, | |
| 129 bool is_remote_file_system); | |
| 130 | |
| 131 ~FileWatcherExtensions(); | |
| 132 | |
| 133 void AddExtension(const std::string& extension_id); | |
| 134 | |
| 135 void RemoveExtension(const std::string& extension_id); | |
| 136 | |
| 137 const ExtensionUsageRegistry& GetExtensions() const; | |
| 138 | |
| 139 unsigned int GetRefCount() const; | |
| 140 | |
| 141 const base::FilePath& GetVirtualPath() const; | |
| 142 | |
| 143 // Starts a file watch at |local_path|. |file_watcher_callback| will be | |
| 144 // called when changes are notified. | |
| 145 // | |
| 146 // |callback| will be called with true, if the file watch is started | |
| 147 // successfully, or false if failed. |callback| must not be null. | |
| 148 void Watch(const base::FilePath& local_path, | |
| 149 const base::FilePathWatcher::Callback& file_watcher_callback, | |
| 150 const BoolCallback& callback); | |
| 151 | |
| 152 private: | |
| 153 // Called when a FilePathWatcher is created and started. | |
| 154 // |file_path_watcher| is NULL, if the watcher wasn't started successfully. | |
| 155 void OnWatcherStarted(const BoolCallback& callback, | |
| 156 base::FilePathWatcher* file_path_watcher); | |
| 157 | |
| 158 base::FilePathWatcher* file_watcher_; | |
| 159 base::FilePath local_path_; | |
| 160 base::FilePath virtual_path_; | |
| 161 ExtensionUsageRegistry extensions_; | |
| 162 unsigned int ref_count_; | |
| 163 bool is_remote_file_system_; | |
| 164 | |
| 165 // Note: This should remain the last member so it'll be destroyed and | |
| 166 // invalidate the weak pointers before any other members are destroyed. | |
| 167 base::WeakPtrFactory<FileWatcherExtensions> weak_ptr_factory_; | |
| 168 }; | |
| 169 | |
| 170 typedef std::map<base::FilePath, FileWatcherExtensions*> WatcherMap; | |
| 171 | |
| 172 // USB mount event handlers. | |
| 173 void OnDiskAdded(const chromeos::disks::DiskMountManager::Disk* disk); | |
| 174 void OnDiskRemoved(const chromeos::disks::DiskMountManager::Disk* disk); | |
| 175 void OnDiskMounted(const chromeos::disks::DiskMountManager::Disk* disk); | |
| 176 void OnDiskUnmounted(const chromeos::disks::DiskMountManager::Disk* disk); | |
| 177 void OnDeviceAdded(const std::string& device_path); | |
| 178 void OnDeviceRemoved(const std::string& device_path); | |
| 179 void OnDeviceScanned(const std::string& device_path); | |
| 180 void OnFormatStarted(const std::string& device_path, bool success); | |
| 181 void OnFormatCompleted(const std::string& device_path, bool success); | |
| 182 | |
| 183 // Called on change to kExternalStorageDisabled pref. | |
| 184 void OnExternalStorageDisabledChanged(); | |
| 185 | |
| 186 // Called when prefs related to file browser change. | |
| 187 void OnFileBrowserPrefsChanged(); | |
| 188 | |
| 189 // Process file watch notifications. | |
| 190 void HandleFileWatchNotification(const base::FilePath& path, | |
| 191 bool got_error); | |
| 192 | |
| 193 // Sends directory change event. | |
| 194 void DispatchDirectoryChangeEvent(const base::FilePath& path, bool error, | |
| 195 const ExtensionUsageRegistry& extensions); | |
| 196 | |
| 197 void DispatchMountEvent( | |
| 198 chromeos::disks::DiskMountManager::MountEvent event, | |
| 199 chromeos::MountError error_code, | |
| 200 const chromeos::disks::DiskMountManager::MountPointInfo& mount_info); | |
| 201 | |
| 202 // If needed, opens a file manager window for the removable device mounted at | |
| 203 // |mount_path|. Disk.mount_path() is empty, since it is being filled out | |
| 204 // after calling notifying observers by DiskMountManager. | |
| 205 void ShowRemovableDeviceInFileManager( | |
| 206 const chromeos::disks::DiskMountManager::Disk& disk, | |
| 207 const base::FilePath& mount_path); | |
| 208 | |
| 209 // Returns the DriveFileSystem for the current profile. | |
| 210 drive::DriveFileSystemInterface* GetRemoteFileSystem() const; | |
| 211 | |
| 212 // Handles requests to start and stop periodic updates on remote file system. | |
| 213 // When |start| is set to true, this function starts periodic updates only if | |
| 214 // it is not yet started; when |start| is set to false, this function stops | |
| 215 // periodic updates only if the number of outstanding update requests reaches | |
| 216 // zero. | |
| 217 void HandleRemoteUpdateRequestOnUIThread(bool start); | |
| 218 | |
| 219 base::WeakPtrFactory<FileBrowserEventRouter> weak_factory_; | |
| 220 base::FilePathWatcher::Callback file_watcher_callback_; | |
| 221 WatcherMap file_watchers_; | |
| 222 scoped_ptr<FileBrowserNotifications> notifications_; | |
| 223 scoped_ptr<PrefChangeRegistrar> pref_change_registrar_; | |
| 224 scoped_ptr<SuspendStateDelegate> suspend_state_delegate_; | |
| 225 Profile* profile_; | |
| 226 | |
| 227 // Number of active update requests on the remote file system. | |
| 228 int num_remote_update_requests_; | |
| 229 | |
| 230 // Event router behavior depends on shift modifier status. This is designed | |
| 231 // for power users. | |
| 232 bool shift_pressed_; | |
| 233 | |
| 234 DISALLOW_COPY_AND_ASSIGN(FileBrowserEventRouter); | |
| 235 }; | |
| 236 | |
| 237 #endif // CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_BROWSER_EVENT_ROUTER_H_ | |
| OLD | NEW |