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/extensions/file_manager/device_event_router.h"
18 #include "chrome/browser/chromeos/extensions/file_manager/job_event_router.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/common/extensions/api/file_manager_private.h"
24 #include "chromeos/disks/disk_mount_manager.h"
25 #include "chromeos/network/network_state_handler_observer.h"
26 #include "components/drive/file_system_observer.h"
27 #include "components/drive/service/drive_service_interface.h"
28 #include "components/drive/sync_client.h"
29 #include "components/keyed_service/core/keyed_service.h"
30 #include "storage/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.
53 class EventRouter
: public KeyedService
,
54 public chromeos::NetworkStateHandlerObserver
,
55 public drive::FileSystemObserver
,
56 public drive::DriveServiceObserver
,
57 public VolumeManagerObserver
{
59 typedef base::Callback
<void(const base::FilePath
& virtual_path
,
60 const drive::FileChange
* list
,
62 const std::vector
<std::string
>& extension_ids
)>
63 DispatchDirectoryChangeEventImplCallback
;
65 explicit EventRouter(Profile
* profile
);
66 ~EventRouter() override
;
68 // KeyedService overrides.
69 void Shutdown() override
;
71 typedef base::Callback
<void(bool success
)> BoolCallback
;
73 // Adds a file watch at |local_path|, associated with |virtual_path|, for
74 // an extension with |extension_id|.
76 // |callback| will be called with true on success, or false on failure.
77 // |callback| must not be null.
79 // Obsolete. Used as fallback for files which backends do not implement the
80 // storage::WatcherManager interface.
81 void AddFileWatch(const base::FilePath
& local_path
,
82 const base::FilePath
& virtual_path
,
83 const std::string
& extension_id
,
84 const BoolCallback
& callback
);
86 // Removes a file watch at |local_path| for an extension with |extension_id|.
88 // Obsolete. Used as fallback for files which backends do not implement the
89 // storage::WatcherManager interface.
90 void RemoveFileWatch(const base::FilePath
& local_path
,
91 const std::string
& extension_id
);
93 // Called when a copy task is completed.
95 int copy_id
, const GURL
& source_url
, const GURL
& destination_url
,
96 base::File::Error error
);
98 // Called when a copy task progress is updated.
99 void OnCopyProgress(int copy_id
,
100 storage::FileSystemOperation::CopyProgressType type
,
101 const GURL
& source_url
,
102 const GURL
& destination_url
,
105 // Called when a notification from a watcher manager arrives.
106 void OnWatcherManagerNotification(
107 const storage::FileSystemURL
& file_system_url
,
108 const std::string
& extension_id
,
109 storage::WatcherManager::ChangeType change_type
);
111 // chromeos::NetworkStateHandlerObserver overrides.
112 void DefaultNetworkChanged(const chromeos::NetworkState
* network
) override
;
114 // drive::DriveServiceObserver overrides.
115 void OnRefreshTokenInvalid() override
;
116 void OnReadyToSendRequests() override
;
118 // drive::FileSystemObserver overrides.
119 void OnDirectoryChanged(const base::FilePath
& drive_path
) override
;
120 void OnFileChanged(const drive::FileChange
& changed_files
) override
;
121 void OnDriveSyncError(drive::file_system::DriveSyncErrorType type
,
122 const base::FilePath
& drive_path
) override
;
124 // VolumeManagerObserver overrides.
125 void OnDiskAdded(const chromeos::disks::DiskMountManager::Disk
& disk
,
126 bool mounting
) override
;
128 const chromeos::disks::DiskMountManager::Disk
& disk
) override
;
129 void OnDeviceAdded(const std::string
& device_path
) override
;
130 void OnDeviceRemoved(const std::string
& device_path
) override
;
131 void OnVolumeMounted(chromeos::MountError error_code
,
132 const Volume
& volume
) override
;
133 void OnVolumeUnmounted(chromeos::MountError error_code
,
134 const Volume
& volume
) override
;
135 void OnFormatStarted(const std::string
& device_path
, bool success
) override
;
136 void OnFormatCompleted(const std::string
& device_path
, bool success
) override
;
138 // Set custom dispatch directory change event implementation for testing.
139 void SetDispatchDirectoryChangeEventImplForTesting(
140 const DispatchDirectoryChangeEventImplCallback
& callback
);
142 // Returns a weak pointer for the event router.
143 base::WeakPtr
<EventRouter
> GetWeakPtr();
146 typedef std::map
<base::FilePath
, FileWatcher
*> WatcherMap
;
148 // Starts observing file system change events.
149 void ObserveEvents();
151 // Called when prefs related to file manager change.
152 void OnFileManagerPrefsChanged();
154 // Process file watch notifications.
155 void HandleFileWatchNotification(const drive::FileChange
* list
,
156 const base::FilePath
& path
,
159 // Sends directory change event.
160 void DispatchDirectoryChangeEvent(
161 const base::FilePath
& path
,
162 const drive::FileChange
* list
,
164 const std::vector
<std::string
>& extension_ids
);
166 // Default implementation of DispatchDirectoryChangeEvent.
167 void DispatchDirectoryChangeEventImpl(
168 const base::FilePath
& path
,
169 const drive::FileChange
* list
,
171 const std::vector
<std::string
>& extension_ids
);
173 // Sends directory change event, after converting the file definition to entry
175 void DispatchDirectoryChangeEventWithEntryDefinition(
176 const linked_ptr
<drive::FileChange
> list
,
177 const std::string
* extension_id
,
179 const EntryDefinition
& entry_definition
);
181 // Dispatches the mount completed event.
182 void DispatchMountCompletedEvent(
183 extensions::api::file_manager_private::MountCompletedEventType event_type
,
184 chromeos::MountError error
,
185 const Volume
& volume
);
187 // If needed, opens a file manager window for the removable device mounted at
188 // |mount_path|. Disk.mount_path() is empty, since it is being filled out
189 // after calling notifying observers by DiskMountManager.
190 void ShowRemovableDeviceInFileManager(VolumeType type
,
191 const base::FilePath
& mount_path
);
193 // Sends onFileTransferUpdate event right now if |immediate| is set. Otherwise
194 // it refrains from sending for a short while, and after that it sends the
195 // most recently scheduled event once.
196 // The delay is for waiting subsequent 'added' events to come after the first
197 // one when multiple tasks are added. This way, we can avoid frequent UI
198 // update caused by differences between singular and plural cases.
199 void ScheduleDriveFileTransferEvent(const drive::JobInfo
& job_info
,
200 const std::string
& status
,
203 // Sends the most recently scheduled onFileTransferUpdated event to
205 // This is used for implementing ScheduledDriveFileTransferEvent().
206 void SendDriveFileTransferEvent();
208 base::Time last_copy_progress_event_
;
210 WatcherMap file_watchers_
;
211 scoped_ptr
<PrefChangeRegistrar
> pref_change_registrar_
;
214 scoped_ptr
<DeviceEventRouter
> device_event_router_
;
215 scoped_ptr
<JobEventRouter
> job_event_router_
;
217 DispatchDirectoryChangeEventImplCallback
218 dispatch_directory_change_event_impl_
;
220 // Note: This should remain the last member so it'll be destroyed and
221 // invalidate the weak pointers before any other members are destroyed.
222 base::WeakPtrFactory
<EventRouter
> weak_factory_
;
224 DISALLOW_COPY_AND_ASSIGN(EventRouter
);
227 } // namespace file_manager
229 #endif // CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_MANAGER_EVENT_ROUTER_H_