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_
12 #include "base/basictypes.h"
13 #include "base/compiler_specific.h"
14 #include "base/files/file_path_watcher.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "chrome/browser/chromeos/drive/drive_integration_service.h"
17 #include "chrome/browser/chromeos/drive/file_system_observer.h"
18 #include "chrome/browser/chromeos/drive/job_list.h"
19 #include "chrome/browser/chromeos/drive/sync_client.h"
20 #include "chrome/browser/chromeos/file_manager/file_watcher.h"
21 #include "chrome/browser/chromeos/file_manager/fileapi_util.h"
22 #include "chrome/browser/chromeos/file_manager/volume_manager.h"
23 #include "chrome/browser/chromeos/file_manager/volume_manager_observer.h"
24 #include "chrome/browser/drive/drive_service_interface.h"
25 #include "chrome/common/extensions/api/file_manager_private.h"
26 #include "chromeos/disks/disk_mount_manager.h"
27 #include "chromeos/network/network_state_handler_observer.h"
28 #include "components/keyed_service/core/keyed_service.h"
29 #include "storage/browser/fileapi/file_system_operation.h"
31 class PrefChangeRegistrar
;
34 using file_manager::util::EntryDefinition
;
48 namespace file_manager
{
49 class DeviceEventRouter
;
51 // Monitors changes in disk mounts, network connection state and preferences
52 // affecting File Manager. Dispatches appropriate File Browser events.
53 class EventRouter
: public KeyedService
,
54 public chromeos::NetworkStateHandlerObserver
,
55 public drive::FileSystemObserver
,
56 public drive::JobListObserver
,
57 public drive::DriveServiceObserver
,
58 public VolumeManagerObserver
,
59 public content::NotificationObserver
{
61 typedef base::Callback
<void(const base::FilePath
& virtual_path
,
62 const drive::FileChange
* list
,
64 const std::vector
<std::string
>& extension_ids
)>
65 DispatchDirectoryChangeEventImplCallback
;
67 explicit EventRouter(Profile
* profile
);
68 ~EventRouter() override
;
70 // KeyedService overrides.
71 void Shutdown() override
;
73 typedef base::Callback
<void(bool success
)> BoolCallback
;
75 // Adds a file watch at |local_path|, associated with |virtual_path|, for
76 // an extension with |extension_id|.
78 // |callback| will be called with true on success, or false on failure.
79 // |callback| must not be null.
81 // Obsolete. Used as fallback for files which backends do not implement the
82 // storage::WatcherManager interface.
83 void AddFileWatch(const base::FilePath
& local_path
,
84 const base::FilePath
& virtual_path
,
85 const std::string
& extension_id
,
86 const BoolCallback
& callback
);
88 // Removes a file watch at |local_path| for an extension with |extension_id|.
90 // Obsolete. Used as fallback for files which backends do not implement the
91 // storage::WatcherManager interface.
92 void RemoveFileWatch(const base::FilePath
& local_path
,
93 const std::string
& extension_id
);
95 // Called when a copy task is completed.
97 int copy_id
, const GURL
& source_url
, const GURL
& destination_url
,
98 base::File::Error error
);
100 // Called when a copy task progress is updated.
101 void OnCopyProgress(int copy_id
,
102 storage::FileSystemOperation::CopyProgressType type
,
103 const GURL
& source_url
,
104 const GURL
& destination_url
,
107 // Called when a notification from a watcher manager arrives.
108 void OnWatcherManagerNotification(
109 const storage::FileSystemURL
& file_system_url
,
110 const std::string
& extension_id
,
111 storage::WatcherManager::ChangeType change_type
);
113 // chromeos::NetworkStateHandlerObserver overrides.
114 void DefaultNetworkChanged(const chromeos::NetworkState
* network
) override
;
116 // drive::JobListObserver overrides.
117 void OnJobAdded(const drive::JobInfo
& job_info
) override
;
118 void OnJobUpdated(const drive::JobInfo
& job_info
) override
;
119 void OnJobDone(const drive::JobInfo
& job_info
,
120 drive::FileError error
) override
;
122 // drive::DriveServiceObserver overrides.
123 void OnRefreshTokenInvalid() override
;
124 void OnReadyToSendRequests() override
;
126 // drive::FileSystemObserver overrides.
127 void OnDirectoryChanged(const base::FilePath
& drive_path
) override
;
128 void OnFileChanged(const drive::FileChange
& changed_files
) override
;
129 void OnDriveSyncError(drive::file_system::DriveSyncErrorType type
,
130 const base::FilePath
& drive_path
) override
;
132 // VolumeManagerObserver overrides.
133 void OnDiskAdded(const chromeos::disks::DiskMountManager::Disk
& disk
,
134 bool mounting
) override
;
136 const chromeos::disks::DiskMountManager::Disk
& disk
) override
;
137 void OnDeviceAdded(const std::string
& device_path
) override
;
138 void OnDeviceRemoved(const std::string
& device_path
) override
;
139 void OnVolumeMounted(chromeos::MountError error_code
,
140 const VolumeInfo
& volume_info
) override
;
141 void OnVolumeUnmounted(chromeos::MountError error_code
,
142 const VolumeInfo
& volume_info
) override
;
143 void OnFormatStarted(const std::string
& device_path
, bool success
) override
;
144 void OnFormatCompleted(const std::string
& device_path
, bool success
) override
;
146 // content::NotificationObserver overrides.
147 void Observe(int type
,
148 const content::NotificationSource
& source
,
149 const content::NotificationDetails
& details
) override
;
151 // Set custom dispatch directory change event implementation for testing.
152 void SetDispatchDirectoryChangeEventImplForTesting(
153 const DispatchDirectoryChangeEventImplCallback
& callback
);
155 // Returns a weak pointer for the event router.
156 base::WeakPtr
<EventRouter
> GetWeakPtr();
159 typedef std::map
<base::FilePath
, FileWatcher
*> WatcherMap
;
161 // Starts observing file system change events.
162 void ObserveEvents();
164 // Called when prefs related to file manager change.
165 void OnFileManagerPrefsChanged();
167 // Process file watch notifications.
168 void HandleFileWatchNotification(const drive::FileChange
* list
,
169 const base::FilePath
& path
,
172 // Sends directory change event.
173 void DispatchDirectoryChangeEvent(
174 const base::FilePath
& path
,
175 const drive::FileChange
* list
,
177 const std::vector
<std::string
>& extension_ids
);
179 // Default implementation of DispatchDirectoryChangeEvent.
180 void DispatchDirectoryChangeEventImpl(
181 const base::FilePath
& path
,
182 const drive::FileChange
* list
,
184 const std::vector
<std::string
>& extension_ids
);
186 // Sends directory change event, after converting the file definition to entry
188 void DispatchDirectoryChangeEventWithEntryDefinition(
189 const linked_ptr
<drive::FileChange
> list
,
190 const std::string
* extension_id
,
192 const EntryDefinition
& entry_definition
);
194 // Dispatches the mount completed event.
195 void DispatchMountCompletedEvent(
196 extensions::api::file_manager_private::MountCompletedEventType event_type
,
197 chromeos::MountError error
,
198 const VolumeInfo
& volume_info
);
200 // If needed, opens a file manager window for the removable device mounted at
201 // |mount_path|. Disk.mount_path() is empty, since it is being filled out
202 // after calling notifying observers by DiskMountManager.
203 void ShowRemovableDeviceInFileManager(VolumeType type
,
204 const base::FilePath
& mount_path
);
206 // Manages the list of currently active Drive file transfer jobs.
207 struct DriveJobInfoWithStatus
{
208 DriveJobInfoWithStatus();
209 DriveJobInfoWithStatus(const drive::JobInfo
& info
,
210 const std::string
& status
);
211 drive::JobInfo job_info
;
215 // Sends onFileTransferUpdate event right now if |immediate| is set. Otherwise
216 // it refrains from sending for a short while, and after that it sends the
217 // most recently scheduled event once.
218 // The delay is for waiting subsequent 'added' events to come after the first
219 // one when multiple tasks are added. This way, we can avoid frequent UI
220 // update caused by differences between singular and plural cases.
221 void ScheduleDriveFileTransferEvent(const drive::JobInfo
& job_info
,
222 const std::string
& status
,
225 // Sends the most recently scheduled onFileTransferUpdated event to
227 // This is used for implementing ScheduledDriveFileTransferEvent().
228 void SendDriveFileTransferEvent();
230 std::map
<drive::JobID
, DriveJobInfoWithStatus
> drive_jobs_
;
231 scoped_ptr
<DriveJobInfoWithStatus
> drive_job_info_for_scheduled_event_
;
232 base::Time last_copy_progress_event_
;
233 base::Time next_send_file_transfer_event_
;
235 WatcherMap file_watchers_
;
236 scoped_ptr
<PrefChangeRegistrar
> pref_change_registrar_
;
239 content::NotificationRegistrar notification_registrar_
;
241 scoped_ptr
<DeviceEventRouter
> device_event_router_
;
243 DispatchDirectoryChangeEventImplCallback
244 dispatch_directory_change_event_impl_
;
246 // Note: This should remain the last member so it'll be destroyed and
247 // invalidate the weak pointers before any other members are destroyed.
248 base::WeakPtrFactory
<EventRouter
> weak_factory_
;
250 DISALLOW_COPY_AND_ASSIGN(EventRouter
);
253 } // namespace file_manager
255 #endif // CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_MANAGER_EVENT_ROUTER_H_