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