event_router.h revision 03b57e008b61dfcb1fbad3aea950ae0e001748b0
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
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/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/browser/drive/drive_service_interface.h"
24#include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h"
25#include "chrome/common/extensions/api/file_browser_private.h"
26#include "chromeos/disks/disk_mount_manager.h"
27#include "chromeos/network/network_state_handler_observer.h"
28#include "content/public/browser/notification_observer.h"
29#include "content/public/browser/notification_registrar.h"
30#include "webkit/browser/fileapi/file_system_operation.h"
31
32class PrefChangeRegistrar;
33class Profile;
34
35using file_manager::util::EntryDefinition;
36
37namespace base {
38class ListValue;
39}
40
41namespace chromeos {
42class NetworkState;
43}
44
45namespace drive {
46class FileChange;
47}
48
49namespace file_manager {
50
51// Monitors changes in disk mounts, network connection state and preferences
52// affecting File Manager. Dispatches appropriate File Browser events.
53class EventRouter
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 chrome::MultiUserWindowManager::Observer {
61 public:
62  explicit EventRouter(Profile* profile);
63  virtual ~EventRouter();
64
65  void Shutdown();
66
67  // Starts observing file system change events.
68  void ObserveEvents();
69
70  typedef base::Callback<void(bool success)> BoolCallback;
71
72  // Adds a file watch at |local_path|, associated with |virtual_path|, for
73  // an extension with |extension_id|.
74  //
75  // |callback| will be called with true on success, or false on failure.
76  // |callback| must not be null.
77  void AddFileWatch(const base::FilePath& local_path,
78                    const base::FilePath& virtual_path,
79                    const std::string& extension_id,
80                    const BoolCallback& callback);
81
82  // Removes a file watch at |local_path| for an extension with |extension_id|.
83  void RemoveFileWatch(const base::FilePath& local_path,
84                       const std::string& extension_id);
85
86  // Called when a copy task is completed.
87  void OnCopyCompleted(
88      int copy_id, const GURL& source_url, const GURL& destination_url,
89      base::File::Error error);
90
91  // Called when a copy task progress is updated.
92  void OnCopyProgress(int copy_id,
93                      storage::FileSystemOperation::CopyProgressType type,
94                      const GURL& source_url,
95                      const GURL& destination_url,
96                      int64 size);
97
98  // Register observer to the multi user window manager.
99  void RegisterMultiUserWindowManagerObserver();
100
101  // chromeos::NetworkStateHandlerObserver overrides.
102  virtual void DefaultNetworkChanged(
103      const chromeos::NetworkState* network) OVERRIDE;
104
105  // drive::JobListObserver overrides.
106  virtual void OnJobAdded(const drive::JobInfo& job_info) OVERRIDE;
107  virtual void OnJobUpdated(const drive::JobInfo& job_info) OVERRIDE;
108  virtual void OnJobDone(const drive::JobInfo& job_info,
109                         drive::FileError error) OVERRIDE;
110
111  // drive::DriveServiceObserver overrides.
112  virtual void OnRefreshTokenInvalid() OVERRIDE;
113
114  // drive::FileSystemObserver overrides.
115  virtual void OnDirectoryChanged(const base::FilePath& drive_path) OVERRIDE;
116  virtual void OnFileChanged(const drive::FileChange& changed_files) OVERRIDE;
117  virtual void OnDriveSyncError(drive::file_system::DriveSyncErrorType type,
118                                const base::FilePath& drive_path) OVERRIDE;
119
120  // VolumeManagerObserver overrides.
121  virtual void OnDiskAdded(
122      const chromeos::disks::DiskMountManager::Disk& disk,
123      bool mounting) OVERRIDE;
124  virtual void OnDiskRemoved(
125      const chromeos::disks::DiskMountManager::Disk& disk) OVERRIDE;
126  virtual void OnDeviceAdded(const std::string& device_path) OVERRIDE;
127  virtual void OnDeviceRemoved(const std::string& device_path) OVERRIDE;
128  virtual void OnVolumeMounted(chromeos::MountError error_code,
129                               const VolumeInfo& volume_info,
130                               bool is_remounting) OVERRIDE;
131  virtual void OnVolumeUnmounted(chromeos::MountError error_code,
132                                 const VolumeInfo& volume_info) OVERRIDE;
133  virtual void OnHardUnplugged(const std::string& device_path) OVERRIDE;
134  virtual void OnFormatStarted(
135      const std::string& device_path, bool success) OVERRIDE;
136  virtual void OnFormatCompleted(
137      const std::string& device_path, bool success) OVERRIDE;
138
139  // content::NotificationObserver overrides.
140  virtual void Observe(int type,
141                       const content::NotificationSource& source,
142                       const content::NotificationDetails& details) OVERRIDE;
143
144  // chrome::MultiUserWindowManager::Observer overrides:
145  virtual void OnOwnerEntryChanged(aura::Window* window) OVERRIDE;
146
147 private:
148  typedef std::map<base::FilePath, FileWatcher*> WatcherMap;
149
150  // Called when prefs related to file manager change.
151  void OnFileManagerPrefsChanged();
152
153  // Process file watch notifications.
154  void HandleFileWatchNotification(const drive::FileChange* list,
155                                   const base::FilePath& path,
156                                   bool got_error);
157
158  // Sends directory change event.
159  void DispatchDirectoryChangeEvent(
160      const base::FilePath& path,
161      const drive::FileChange* list,
162      bool got_error,
163      const std::vector<std::string>& extension_ids);
164
165  // Sends directory change event, after converting the file definition to entry
166  // definition.
167  void DispatchDirectoryChangeEventWithEntryDefinition(
168      const linked_ptr<drive::FileChange> list,
169      const std::string* extension_id,
170      bool watcher_error,
171      const EntryDefinition& entry_definition);
172
173  // Dispatches the mount completed event.
174  void DispatchMountCompletedEvent(
175      extensions::api::file_browser_private::MountCompletedEventType event_type,
176      chromeos::MountError error,
177      const VolumeInfo& volume_info,
178      bool is_remounting);
179
180  // If needed, opens a file manager window for the removable device mounted at
181  // |mount_path|. Disk.mount_path() is empty, since it is being filled out
182  // after calling notifying observers by DiskMountManager.
183  void ShowRemovableDeviceInFileManager(VolumeType type,
184                                        const base::FilePath& mount_path);
185
186  // Dispatches an onDeviceChanged event containing |type| and |path| to
187  // extensions.
188  void DispatchDeviceEvent(
189      extensions::api::file_browser_private::DeviceEventType type,
190      const std::string& path);
191
192  // Sends onFileTranferUpdated to extensions if needed. If |always| is true,
193  // it sends the event always. Otherwise, it sends the event if enough time has
194  // passed from the previous event so as not to make extension busy.
195  void SendDriveFileTransferEvent(bool always);
196
197  // Manages the list of currently active Drive file transfer jobs.
198  struct DriveJobInfoWithStatus {
199    DriveJobInfoWithStatus();
200    DriveJobInfoWithStatus(const drive::JobInfo& info,
201                           const std::string& status);
202    drive::JobInfo job_info;
203    std::string status;
204  };
205  std::map<drive::JobID, DriveJobInfoWithStatus> drive_jobs_;
206  base::Time last_file_transfer_event_;
207  base::Time last_copy_progress_event_;
208
209  WatcherMap file_watchers_;
210  scoped_ptr<PrefChangeRegistrar> pref_change_registrar_;
211  Profile* profile_;
212
213  content::NotificationRegistrar notification_registrar_;
214  bool multi_user_window_manager_observer_registered_;
215
216  // Note: This should remain the last member so it'll be destroyed and
217  // invalidate the weak pointers before any other members are destroyed.
218  base::WeakPtrFactory<EventRouter> weak_factory_;
219  DISALLOW_COPY_AND_ASSIGN(EventRouter);
220};
221
222}  // namespace file_manager
223
224#endif  // CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_MANAGER_EVENT_ROUTER_H_
225