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/volume_manager_observer.h"
21 #include "chrome/browser/drive/drive_service_interface.h"
22 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h"
23 #include "chrome/common/extensions/api/file_browser_private.h"
24 #include "chromeos/disks/disk_mount_manager.h"
25 #include "chromeos/network/network_state_handler_observer.h"
26 #include "content/public/browser/notification_observer.h"
27 #include "content/public/browser/notification_registrar.h"
28 #include "webkit/browser/fileapi/file_system_operation.h"
30 class PrefChangeRegistrar;
41 namespace file_manager {
43 // Monitors changes in disk mounts, network connection state and preferences
44 // affecting File Manager. Dispatches appropriate File Browser events.
46 : public chromeos::NetworkStateHandlerObserver,
47 public drive::FileSystemObserver,
48 public drive::JobListObserver,
49 public drive::DriveServiceObserver,
50 public VolumeManagerObserver,
51 public content::NotificationObserver,
52 public chrome::MultiUserWindowManager::Observer {
54 explicit EventRouter(Profile* profile);
55 virtual ~EventRouter();
59 // Starts observing file system change events.
62 typedef base::Callback<void(bool success)> BoolCallback;
64 // Adds a file watch at |local_path|, associated with |virtual_path|, for
65 // an extension with |extension_id|.
67 // |callback| will be called with true on success, or false on failure.
68 // |callback| must not be null.
69 void AddFileWatch(const base::FilePath& local_path,
70 const base::FilePath& virtual_path,
71 const std::string& extension_id,
72 const BoolCallback& callback);
74 // Removes a file watch at |local_path| for an extension with |extension_id|.
75 void RemoveFileWatch(const base::FilePath& local_path,
76 const std::string& extension_id);
78 // Called when a copy task is completed.
80 int copy_id, const GURL& source_url, const GURL& destination_url,
81 base::File::Error error);
83 // Called when a copy task progress is updated.
84 void OnCopyProgress(int copy_id,
85 fileapi::FileSystemOperation::CopyProgressType type,
86 const GURL& source_url,
87 const GURL& destination_url,
90 // Register observer to the multi user window manager.
91 void RegisterMultiUserWindowManagerObserver();
93 // chromeos::NetworkStateHandlerObserver overrides.
94 virtual void DefaultNetworkChanged(
95 const chromeos::NetworkState* network) OVERRIDE;
97 // drive::JobListObserver overrides.
98 virtual void OnJobAdded(const drive::JobInfo& job_info) OVERRIDE;
99 virtual void OnJobUpdated(const drive::JobInfo& job_info) OVERRIDE;
100 virtual void OnJobDone(const drive::JobInfo& job_info,
101 drive::FileError error) OVERRIDE;
103 // drive::DriveServiceObserver overrides.
104 virtual void OnRefreshTokenInvalid() OVERRIDE;
106 // drive::FileSystemObserver overrides.
107 virtual void OnDirectoryChanged(const base::FilePath& drive_path) OVERRIDE;
108 virtual void OnDriveSyncError(drive::file_system::DriveSyncErrorType type,
109 const base::FilePath& drive_path) OVERRIDE;
111 // VolumeManagerObserver overrides.
112 virtual void OnDiskAdded(
113 const chromeos::disks::DiskMountManager::Disk& disk,
114 bool mounting) OVERRIDE;
115 virtual void OnDiskRemoved(
116 const chromeos::disks::DiskMountManager::Disk& disk) OVERRIDE;
117 virtual void OnDeviceAdded(const std::string& device_path) OVERRIDE;
118 virtual void OnDeviceRemoved(const std::string& device_path) OVERRIDE;
119 virtual void OnVolumeMounted(chromeos::MountError error_code,
120 const VolumeInfo& volume_info,
121 bool is_remounting) OVERRIDE;
122 virtual void OnVolumeUnmounted(chromeos::MountError error_code,
123 const VolumeInfo& volume_info) OVERRIDE;
124 virtual void OnFormatStarted(
125 const std::string& device_path, bool success) OVERRIDE;
126 virtual void OnFormatCompleted(
127 const std::string& device_path, bool success) OVERRIDE;
129 // content::NotificationObserver overrides.
130 virtual void Observe(int type,
131 const content::NotificationSource& source,
132 const content::NotificationDetails& details) OVERRIDE;
134 // chrome::MultiUserWindowManager::Observer overrides:
135 virtual void OnOwnerEntryChanged(aura::Window* window) OVERRIDE;
138 typedef std::map<base::FilePath, FileWatcher*> WatcherMap;
140 // Called when prefs related to file manager change.
141 void OnFileManagerPrefsChanged();
143 // Process file watch notifications.
144 void HandleFileWatchNotification(const base::FilePath& path,
147 // Sends directory change event.
148 void DispatchDirectoryChangeEvent(
149 const base::FilePath& path,
151 const std::vector<std::string>& extension_ids);
153 // If needed, opens a file manager window for the removable device mounted at
154 // |mount_path|. Disk.mount_path() is empty, since it is being filled out
155 // after calling notifying observers by DiskMountManager.
156 void ShowRemovableDeviceInFileManager(const base::FilePath& mount_path);
158 // Dispatches an onDeviceChanged event containing |type| and |path| to
160 void DispatchDeviceEvent(
161 extensions::api::file_browser_private::DeviceEventType type,
162 const std::string& path);
164 // Sends onFileTranferUpdated to extensions if needed. If |always| is true,
165 // it sends the event always. Otherwise, it sends the event if enough time has
166 // passed from the previous event so as not to make extension busy.
167 void SendDriveFileTransferEvent(bool always);
169 // Manages the list of currently active Drive file transfer jobs.
170 struct DriveJobInfoWithStatus {
171 DriveJobInfoWithStatus();
172 DriveJobInfoWithStatus(const drive::JobInfo& info,
173 const std::string& status);
174 drive::JobInfo job_info;
177 std::map<drive::JobID, DriveJobInfoWithStatus> drive_jobs_;
178 base::Time last_file_transfer_event_;
180 WatcherMap file_watchers_;
181 scoped_ptr<PrefChangeRegistrar> pref_change_registrar_;
184 content::NotificationRegistrar notification_registrar_;
185 bool multi_user_window_manager_observer_registered_;
187 // Note: This should remain the last member so it'll be destroyed and
188 // invalidate the weak pointers before any other members are destroyed.
189 base::WeakPtrFactory<EventRouter> weak_factory_;
190 DISALLOW_COPY_AND_ASSIGN(EventRouter);
193 } // namespace file_manager
195 #endif // CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_MANAGER_EVENT_ROUTER_H_