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/common/extensions/api/file_browser_private.h"
23 #include "chromeos/disks/disk_mount_manager.h"
24 #include "chromeos/network/network_state_handler_observer.h"
25 #include "webkit/browser/fileapi/file_system_operation.h"
27 class PrefChangeRegistrar;
38 namespace file_manager {
40 // Monitors changes in disk mounts, network connection state and preferences
41 // affecting File Manager. Dispatches appropriate File Browser events.
43 : public chromeos::NetworkStateHandlerObserver,
44 public drive::FileSystemObserver,
45 public drive::JobListObserver,
46 public drive::DriveServiceObserver,
47 public VolumeManagerObserver {
49 explicit EventRouter(Profile* profile);
50 virtual ~EventRouter();
54 // Starts observing file system change events.
55 void ObserveFileSystemEvents();
57 typedef base::Callback<void(bool success)> BoolCallback;
59 // Adds a file watch at |local_path|, associated with |virtual_path|, for
60 // an extension with |extension_id|.
62 // |callback| will be called with true on success, or false on failure.
63 // |callback| must not be null.
64 void AddFileWatch(const base::FilePath& local_path,
65 const base::FilePath& virtual_path,
66 const std::string& extension_id,
67 const BoolCallback& callback);
69 // Removes a file watch at |local_path| for an extension with |extension_id|.
70 void RemoveFileWatch(const base::FilePath& local_path,
71 const std::string& extension_id);
73 // Called when a copy task is completed.
75 int copy_id, const GURL& source_url, const GURL& destination_url,
76 base::File::Error error);
78 // Called when a copy task progress is updated.
79 void OnCopyProgress(int copy_id,
80 fileapi::FileSystemOperation::CopyProgressType type,
81 const GURL& source_url,
82 const GURL& destination_url,
85 // chromeos::NetworkStateHandlerObserver overrides.
86 virtual void DefaultNetworkChanged(
87 const chromeos::NetworkState* network) OVERRIDE;
89 // drive::JobListObserver overrides.
90 virtual void OnJobAdded(const drive::JobInfo& job_info) OVERRIDE;
91 virtual void OnJobUpdated(const drive::JobInfo& job_info) OVERRIDE;
92 virtual void OnJobDone(const drive::JobInfo& job_info,
93 drive::FileError error) OVERRIDE;
95 // drive::DriveServiceObserver overrides.
96 virtual void OnRefreshTokenInvalid() OVERRIDE;
98 // drive::FileSystemObserver overrides.
99 virtual void OnDirectoryChanged(
100 const base::FilePath& directory_path) OVERRIDE;
101 virtual void OnDriveSyncError(
102 drive::file_system::DriveSyncErrorType type,
103 const base::FilePath& file_path) OVERRIDE;
105 // VolumeManagerObserver overrides.
106 virtual void OnDiskAdded(
107 const chromeos::disks::DiskMountManager::Disk& disk,
108 bool mounting) OVERRIDE;
109 virtual void OnDiskRemoved(
110 const chromeos::disks::DiskMountManager::Disk& disk) OVERRIDE;
111 virtual void OnDeviceAdded(const std::string& device_path) OVERRIDE;
112 virtual void OnDeviceRemoved(const std::string& device_path) OVERRIDE;
113 virtual void OnVolumeMounted(chromeos::MountError error_code,
114 const VolumeInfo& volume_info,
115 bool is_remounting) OVERRIDE;
116 virtual void OnVolumeUnmounted(chromeos::MountError error_code,
117 const VolumeInfo& volume_info) OVERRIDE;
118 virtual void OnFormatStarted(
119 const std::string& device_path, bool success) OVERRIDE;
120 virtual void OnFormatCompleted(
121 const std::string& device_path, bool success) OVERRIDE;
124 typedef std::map<base::FilePath, FileWatcher*> WatcherMap;
126 // Called when prefs related to file manager change.
127 void OnFileManagerPrefsChanged();
129 // Process file watch notifications.
130 void HandleFileWatchNotification(const base::FilePath& path,
133 // Sends directory change event.
134 void DispatchDirectoryChangeEvent(
135 const base::FilePath& path,
137 const std::vector<std::string>& extension_ids);
139 // If needed, opens a file manager window for the removable device mounted at
140 // |mount_path|. Disk.mount_path() is empty, since it is being filled out
141 // after calling notifying observers by DiskMountManager.
142 void ShowRemovableDeviceInFileManager(const base::FilePath& mount_path);
144 // Dispatches an onDeviceChanged event containing |type| and |path| to
146 void DispatchDeviceEvent(
147 extensions::api::file_browser_private::DeviceEventType type,
148 const std::string& path);
150 // Sends onFileTranferUpdated to extensions if needed. If |always| is true,
151 // it sends the event always. Otherwise, it sends the event if enough time has
152 // passed from the previous event so as not to make extension busy.
153 void SendDriveFileTransferEvent(bool always);
155 // Manages the list of currently active Drive file transfer jobs.
156 struct DriveJobInfoWithStatus {
157 DriveJobInfoWithStatus();
158 DriveJobInfoWithStatus(const drive::JobInfo& info,
159 const std::string& status);
160 drive::JobInfo job_info;
163 std::map<drive::JobID, DriveJobInfoWithStatus> drive_jobs_;
164 base::Time last_file_transfer_event_;
166 WatcherMap file_watchers_;
167 scoped_ptr<PrefChangeRegistrar> pref_change_registrar_;
170 // Note: This should remain the last member so it'll be destroyed and
171 // invalidate the weak pointers before any other members are destroyed.
172 base::WeakPtrFactory<EventRouter> weak_factory_;
173 DISALLOW_COPY_AND_ASSIGN(EventRouter);
176 } // namespace file_manager
178 #endif // CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_MANAGER_EVENT_ROUTER_H_