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