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.
5 #ifndef CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_MANAGER_EVENT_ROUTER_H_
6 #define CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_MANAGER_EVENT_ROUTER_H_
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.h"
22 #include "chrome/browser/chromeos/file_manager/volume_manager_observer.h"
23 #include "chrome/browser/drive/drive_service_interface.h"
24 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h"
25 #include "chrome/common/extensions/api/file_browser_private.h"
26 #include "chromeos/disks/disk_mount_manager.h"
27 #include "chromeos/network/network_state_handler_observer.h"
28 #include "content/public/browser/notification_observer.h"
29 #include "content/public/browser/notification_registrar.h"
30 #include "webkit/browser/fileapi/file_system_operation.h"
32 class PrefChangeRegistrar;
35 using file_manager::util::EntryDefinition;
49 namespace file_manager {
51 // Monitors changes in disk mounts, network connection state and preferences
52 // affecting File Manager. Dispatches appropriate File Browser events.
54 : public chromeos::NetworkStateHandlerObserver,
55 public drive::FileSystemObserver,
56 public drive::JobListObserver,
57 public drive::DriveServiceObserver,
58 public VolumeManagerObserver,
59 public content::NotificationObserver,
60 public chrome::MultiUserWindowManager::Observer {
62 explicit EventRouter(Profile* profile);
63 virtual ~EventRouter();
67 // Starts observing file system change events.
70 typedef base::Callback<void(bool success)> BoolCallback;
72 // Adds a file watch at |local_path|, associated with |virtual_path|, for
73 // an extension with |extension_id|.
75 // |callback| will be called with true on success, or false on failure.
76 // |callback| must not be null.
77 void AddFileWatch(const base::FilePath& local_path,
78 const base::FilePath& virtual_path,
79 const std::string& extension_id,
80 const BoolCallback& callback);
82 // Removes a file watch at |local_path| for an extension with |extension_id|.
83 void RemoveFileWatch(const base::FilePath& local_path,
84 const std::string& extension_id);
86 // Called when a copy task is completed.
88 int copy_id, const GURL& source_url, const GURL& destination_url,
89 base::File::Error error);
91 // Called when a copy task progress is updated.
92 void OnCopyProgress(int copy_id,
93 fileapi::FileSystemOperation::CopyProgressType type,
94 const GURL& source_url,
95 const GURL& destination_url,
98 // Register observer to the multi user window manager.
99 void RegisterMultiUserWindowManagerObserver();
101 // chromeos::NetworkStateHandlerObserver overrides.
102 virtual void DefaultNetworkChanged(
103 const chromeos::NetworkState* network) OVERRIDE;
105 // drive::JobListObserver overrides.
106 virtual void OnJobAdded(const drive::JobInfo& job_info) OVERRIDE;
107 virtual void OnJobUpdated(const drive::JobInfo& job_info) OVERRIDE;
108 virtual void OnJobDone(const drive::JobInfo& job_info,
109 drive::FileError error) OVERRIDE;
111 // drive::DriveServiceObserver overrides.
112 virtual void OnRefreshTokenInvalid() OVERRIDE;
114 // drive::FileSystemObserver overrides.
115 virtual void OnDirectoryChanged(const base::FilePath& drive_path) OVERRIDE;
116 virtual void OnFileChanged(const drive::FileChange& changed_files) OVERRIDE;
117 virtual void OnDriveSyncError(drive::file_system::DriveSyncErrorType type,
118 const base::FilePath& drive_path) OVERRIDE;
120 // VolumeManagerObserver overrides.
121 virtual void OnDiskAdded(
122 const chromeos::disks::DiskMountManager::Disk& disk,
123 bool mounting) OVERRIDE;
124 virtual void OnDiskRemoved(
125 const chromeos::disks::DiskMountManager::Disk& disk) OVERRIDE;
126 virtual void OnDeviceAdded(const std::string& device_path) OVERRIDE;
127 virtual void OnDeviceRemoved(const std::string& device_path) OVERRIDE;
128 virtual void OnVolumeMounted(chromeos::MountError error_code,
129 const VolumeInfo& volume_info,
130 bool is_remounting) OVERRIDE;
131 virtual void OnVolumeUnmounted(chromeos::MountError error_code,
132 const VolumeInfo& volume_info) OVERRIDE;
133 virtual void OnHardUnplugged(const std::string& device_path) OVERRIDE;
134 virtual void OnFormatStarted(
135 const std::string& device_path, bool success) OVERRIDE;
136 virtual void OnFormatCompleted(
137 const std::string& device_path, bool success) OVERRIDE;
139 // content::NotificationObserver overrides.
140 virtual void Observe(int type,
141 const content::NotificationSource& source,
142 const content::NotificationDetails& details) OVERRIDE;
144 // chrome::MultiUserWindowManager::Observer overrides:
145 virtual void OnOwnerEntryChanged(aura::Window* window) OVERRIDE;
148 typedef std::map<base::FilePath, FileWatcher*> WatcherMap;
150 // Called when prefs related to file manager change.
151 void OnFileManagerPrefsChanged();
153 // Process file watch notifications.
154 void HandleFileWatchNotification(const drive::FileChange* list,
155 const base::FilePath& path,
158 // Sends directory change event.
159 void DispatchDirectoryChangeEvent(
160 const base::FilePath& path,
161 const drive::FileChange* list,
163 const std::vector<std::string>& extension_ids);
165 // Sends directory change event, after converting the file definition to entry
167 void DispatchDirectoryChangeEventWithEntryDefinition(
168 const linked_ptr<drive::FileChange> list,
169 const std::string* extension_id,
171 const EntryDefinition& entry_definition);
173 // Dispatches the mount completed event.
174 void DispatchMountCompletedEvent(
175 extensions::api::file_browser_private::MountCompletedEventType event_type,
176 chromeos::MountError error,
177 const VolumeInfo& volume_info,
180 // If needed, opens a file manager window for the removable device mounted at
181 // |mount_path|. Disk.mount_path() is empty, since it is being filled out
182 // after calling notifying observers by DiskMountManager.
183 void ShowRemovableDeviceInFileManager(VolumeType type,
184 const base::FilePath& mount_path);
186 // Dispatches an onDeviceChanged event containing |type| and |path| to
188 void DispatchDeviceEvent(
189 extensions::api::file_browser_private::DeviceEventType type,
190 const std::string& path);
192 // Sends onFileTranferUpdated to extensions if needed. If |always| is true,
193 // it sends the event always. Otherwise, it sends the event if enough time has
194 // passed from the previous event so as not to make extension busy.
195 void SendDriveFileTransferEvent(bool always);
197 // Manages the list of currently active Drive file transfer jobs.
198 struct DriveJobInfoWithStatus {
199 DriveJobInfoWithStatus();
200 DriveJobInfoWithStatus(const drive::JobInfo& info,
201 const std::string& status);
202 drive::JobInfo job_info;
205 std::map<drive::JobID, DriveJobInfoWithStatus> drive_jobs_;
206 base::Time last_file_transfer_event_;
207 base::Time last_copy_progress_event_;
209 WatcherMap file_watchers_;
210 scoped_ptr<PrefChangeRegistrar> pref_change_registrar_;
213 content::NotificationRegistrar notification_registrar_;
214 bool multi_user_window_manager_observer_registered_;
216 // Note: This should remain the last member so it'll be destroyed and
217 // invalidate the weak pointers before any other members are destroyed.
218 base::WeakPtrFactory<EventRouter> weak_factory_;
219 DISALLOW_COPY_AND_ASSIGN(EventRouter);
222 } // namespace file_manager
224 #endif // CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_MANAGER_EVENT_ROUTER_H_