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