Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / extensions / file_manager / event_router.h
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.
4
5 #ifndef CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_MANAGER_EVENT_ROUTER_H_
6 #define CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_MANAGER_EVENT_ROUTER_H_
7
8 #include <map>
9 #include <string>
10 #include <vector>
11
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"
30
31 class PrefChangeRegistrar;
32 class Profile;
33
34 using file_manager::util::EntryDefinition;
35
36 namespace base {
37 class ListValue;
38 }
39
40 namespace chromeos {
41 class NetworkState;
42 }
43
44 namespace drive {
45 class FileChange;
46 }
47
48 namespace file_manager {
49 class DeviceEventRouter;
50
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 {
60  public:
61   typedef base::Callback<void(const base::FilePath& virtual_path,
62                               const drive::FileChange* list,
63                               bool got_error,
64                               const std::vector<std::string>& extension_ids)>
65       DispatchDirectoryChangeEventImplCallback;
66
67   explicit EventRouter(Profile* profile);
68   virtual ~EventRouter();
69
70   // KeyedService overrides.
71   virtual void Shutdown() override;
72
73   typedef base::Callback<void(bool success)> BoolCallback;
74
75   // Adds a file watch at |local_path|, associated with |virtual_path|, for
76   // an extension with |extension_id|.
77   //
78   // |callback| will be called with true on success, or false on failure.
79   // |callback| must not be null.
80   //
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);
87
88   // Removes a file watch at |local_path| for an extension with |extension_id|.
89   //
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);
94
95   // Called when a copy task is completed.
96   void OnCopyCompleted(
97       int copy_id, const GURL& source_url, const GURL& destination_url,
98       base::File::Error error);
99
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,
105                       int64 size);
106
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);
112
113   // chromeos::NetworkStateHandlerObserver overrides.
114   virtual void DefaultNetworkChanged(
115       const chromeos::NetworkState* network) override;
116
117   // drive::JobListObserver overrides.
118   virtual void OnJobAdded(const drive::JobInfo& job_info) override;
119   virtual void OnJobUpdated(const drive::JobInfo& job_info) override;
120   virtual void OnJobDone(const drive::JobInfo& job_info,
121                          drive::FileError error) override;
122
123   // drive::DriveServiceObserver overrides.
124   virtual void OnRefreshTokenInvalid() override;
125
126   // drive::FileSystemObserver overrides.
127   virtual void OnDirectoryChanged(const base::FilePath& drive_path) override;
128   virtual void OnFileChanged(const drive::FileChange& changed_files) override;
129   virtual void OnDriveSyncError(drive::file_system::DriveSyncErrorType type,
130                                 const base::FilePath& drive_path) override;
131
132   // VolumeManagerObserver overrides.
133   virtual void OnDiskAdded(
134       const chromeos::disks::DiskMountManager::Disk& disk,
135       bool mounting) override;
136   virtual void OnDiskRemoved(
137       const chromeos::disks::DiskMountManager::Disk& disk) override;
138   virtual void OnDeviceAdded(const std::string& device_path) override;
139   virtual void OnDeviceRemoved(const std::string& device_path) override;
140   virtual void OnVolumeMounted(chromeos::MountError error_code,
141                                const VolumeInfo& volume_info) override;
142   virtual void OnVolumeUnmounted(chromeos::MountError error_code,
143                                  const VolumeInfo& volume_info) override;
144   virtual void OnFormatStarted(
145       const std::string& device_path, bool success) override;
146   virtual void OnFormatCompleted(
147       const std::string& device_path, bool success) override;
148
149   // content::NotificationObserver overrides.
150   virtual void Observe(int type,
151                        const content::NotificationSource& source,
152                        const content::NotificationDetails& details) override;
153
154   // Set custom dispatch directory change event implementation for testing.
155   void SetDispatchDirectoryChangeEventImplForTesting(
156       const DispatchDirectoryChangeEventImplCallback& callback);
157
158   // Returns a weak pointer for the event router.
159   base::WeakPtr<EventRouter> GetWeakPtr();
160
161  private:
162   typedef std::map<base::FilePath, FileWatcher*> WatcherMap;
163
164   // Starts observing file system change events.
165   void ObserveEvents();
166
167   // Called when prefs related to file manager change.
168   void OnFileManagerPrefsChanged();
169
170   // Process file watch notifications.
171   void HandleFileWatchNotification(const drive::FileChange* list,
172                                    const base::FilePath& path,
173                                    bool got_error);
174
175   // Sends directory change event.
176   void DispatchDirectoryChangeEvent(
177       const base::FilePath& path,
178       const drive::FileChange* list,
179       bool got_error,
180       const std::vector<std::string>& extension_ids);
181
182   // Default implementation of DispatchDirectoryChangeEvent.
183   void DispatchDirectoryChangeEventImpl(
184       const base::FilePath& path,
185       const drive::FileChange* list,
186       bool got_error,
187       const std::vector<std::string>& extension_ids);
188
189   // Sends directory change event, after converting the file definition to entry
190   // definition.
191   void DispatchDirectoryChangeEventWithEntryDefinition(
192       const linked_ptr<drive::FileChange> list,
193       const std::string* extension_id,
194       bool watcher_error,
195       const EntryDefinition& entry_definition);
196
197   // Dispatches the mount completed event.
198   void DispatchMountCompletedEvent(
199       extensions::api::file_manager_private::MountCompletedEventType event_type,
200       chromeos::MountError error,
201       const VolumeInfo& volume_info);
202
203   // If needed, opens a file manager window for the removable device mounted at
204   // |mount_path|. Disk.mount_path() is empty, since it is being filled out
205   // after calling notifying observers by DiskMountManager.
206   void ShowRemovableDeviceInFileManager(VolumeType type,
207                                         const base::FilePath& mount_path);
208
209   // Manages the list of currently active Drive file transfer jobs.
210   struct DriveJobInfoWithStatus {
211     DriveJobInfoWithStatus();
212     DriveJobInfoWithStatus(const drive::JobInfo& info,
213                            const std::string& status);
214     drive::JobInfo job_info;
215     std::string status;
216   };
217
218   // Sends onFileTransferUpdate event right now if |immediate| is set. Otherwise
219   // it refrains from sending for a short while, and after that it sends the
220   // most recently scheduled event once.
221   // The delay is for waiting subsequent 'added' events to come after the first
222   // one when multiple tasks are added. This way, we can avoid frequent UI
223   // update caused by differences between singular and plural cases.
224   void ScheduleDriveFileTransferEvent(const drive::JobInfo& job_info,
225                                       const std::string& status,
226                                       bool immediate);
227
228   // Sends the most recently scheduled onFileTransferUpdated event to
229   // extensions.
230   // This is used for implementing ScheduledDriveFileTransferEvent().
231   void SendDriveFileTransferEvent();
232
233   std::map<drive::JobID, DriveJobInfoWithStatus> drive_jobs_;
234   scoped_ptr<DriveJobInfoWithStatus> drive_job_info_for_scheduled_event_;
235   base::Time last_copy_progress_event_;
236   base::Time next_send_file_transfer_event_;
237
238   WatcherMap file_watchers_;
239   scoped_ptr<PrefChangeRegistrar> pref_change_registrar_;
240   Profile* profile_;
241
242   content::NotificationRegistrar notification_registrar_;
243
244   scoped_ptr<DeviceEventRouter> device_event_router_;
245
246   DispatchDirectoryChangeEventImplCallback
247       dispatch_directory_change_event_impl_;
248
249   // Note: This should remain the last member so it'll be destroyed and
250   // invalidate the weak pointers before any other members are destroyed.
251   base::WeakPtrFactory<EventRouter> weak_factory_;
252
253   DISALLOW_COPY_AND_ASSIGN(EventRouter);
254 };
255
256 }  // namespace file_manager
257
258 #endif  // CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_MANAGER_EVENT_ROUTER_H_