blob: e10e30b6f04c4414e3c8c30a09dea708ebef4e92 [file] [log] [blame]
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +01001// 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
Ben Murdoch2385ea32013-08-06 11:01:04 +01005#ifndef CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_MANAGER_EVENT_ROUTER_H_
6#define CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_MANAGER_EVENT_ROUTER_H_
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +01007
8#include <map>
9#include <string>
10
11#include "base/files/file_path_watcher.h"
12#include "base/memory/scoped_ptr.h"
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010013#include "chrome/browser/chromeos/drive/drive_integration_service.h"
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010014#include "chrome/browser/chromeos/drive/file_system_observer.h"
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010015#include "chrome/browser/chromeos/drive/job_list.h"
Ben Murdoch2385ea32013-08-06 11:01:04 +010016#include "chrome/browser/chromeos/extensions/file_manager/file_watcher.h"
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010017#include "chrome/browser/chromeos/net/connectivity_state_helper_observer.h"
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010018#include "chrome/browser/drive/drive_service_interface.h"
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010019#include "chromeos/disks/disk_mount_manager.h"
20
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010021class PrefChangeRegistrar;
22class Profile;
23
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +010024namespace file_manager {
25
Ben Murdoch2385ea32013-08-06 11:01:04 +010026class DesktopNotifications;
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +010027class MountedDiskMonitor;
28
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010029// Monitors changes in disk mounts, network connection state and preferences
30// affecting File Manager. Dispatches appropriate File Browser events.
Ben Murdoch2385ea32013-08-06 11:01:04 +010031class EventRouter
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010032 : public chromeos::disks::DiskMountManager::Observer,
33 public chromeos::ConnectivityStateHelperObserver,
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010034 public drive::DriveIntegrationServiceObserver,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010035 public drive::FileSystemObserver,
36 public drive::JobListObserver,
Ben Murdocheb525c52013-07-10 11:40:50 +010037 public drive::DriveServiceObserver {
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010038 public:
Ben Murdoch2385ea32013-08-06 11:01:04 +010039 explicit EventRouter(Profile* profile);
40 virtual ~EventRouter();
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010041
42 void Shutdown();
43
44 // Starts observing file system change events.
45 void ObserveFileSystemEvents();
46
47 typedef base::Callback<void(bool success)> BoolCallback;
48
49 // Adds a file watch at |local_path|, associated with |virtual_path|, for
50 // an extension with |extension_id|.
51 //
52 // |callback| will be called with true on success, or false on failure.
53 // |callback| must not be null.
54 void AddFileWatch(const base::FilePath& local_path,
55 const base::FilePath& virtual_path,
56 const std::string& extension_id,
57 const BoolCallback& callback);
58
59 // Removes a file watch at |local_path| for an extension with |extension_id|.
60 void RemoveFileWatch(const base::FilePath& local_path,
61 const std::string& extension_id);
62
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010063 // CrosDisksClient::Observer overrides.
64 virtual void OnDiskEvent(
65 chromeos::disks::DiskMountManager::DiskEvent event,
66 const chromeos::disks::DiskMountManager::Disk* disk) OVERRIDE;
67 virtual void OnDeviceEvent(
68 chromeos::disks::DiskMountManager::DeviceEvent event,
69 const std::string& device_path) OVERRIDE;
70 virtual void OnMountEvent(
71 chromeos::disks::DiskMountManager::MountEvent event,
72 chromeos::MountError error_code,
73 const chromeos::disks::DiskMountManager::MountPointInfo& mount_info)
74 OVERRIDE;
75 virtual void OnFormatEvent(
76 chromeos::disks::DiskMountManager::FormatEvent event,
77 chromeos::FormatError error_code,
78 const std::string& device_path) OVERRIDE;
79
80 // chromeos::ConnectivityStateHelperObserver override.
81 virtual void NetworkManagerChanged() OVERRIDE;
82 virtual void DefaultNetworkChanged() OVERRIDE;
83
84 // drive::JobListObserver overrides.
85 virtual void OnJobAdded(const drive::JobInfo& job_info) OVERRIDE;
86 virtual void OnJobUpdated(const drive::JobInfo& job_info) OVERRIDE;
87 virtual void OnJobDone(const drive::JobInfo& job_info,
88 drive::FileError error) OVERRIDE;
89
90 // drive::DriveServiceObserver overrides.
91 virtual void OnRefreshTokenInvalid() OVERRIDE;
92
93 // drive::FileSystemObserver overrides.
94 virtual void OnDirectoryChanged(
95 const base::FilePath& directory_path) OVERRIDE;
96
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010097 // drive::DriveIntegrationServiceObserver overrides.
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010098 virtual void OnFileSystemMounted() OVERRIDE;
99 virtual void OnFileSystemBeingUnmounted() OVERRIDE;
100
101 private:
Ben Murdoch2385ea32013-08-06 11:01:04 +0100102 typedef std::map<base::FilePath, FileWatcher*> WatcherMap;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100103
104 // USB mount event handlers.
105 void OnDiskAdded(const chromeos::disks::DiskMountManager::Disk* disk);
106 void OnDiskRemoved(const chromeos::disks::DiskMountManager::Disk* disk);
107 void OnDiskMounted(const chromeos::disks::DiskMountManager::Disk* disk);
108 void OnDiskUnmounted(const chromeos::disks::DiskMountManager::Disk* disk);
109 void OnDeviceAdded(const std::string& device_path);
110 void OnDeviceRemoved(const std::string& device_path);
111 void OnDeviceScanned(const std::string& device_path);
112 void OnFormatStarted(const std::string& device_path, bool success);
113 void OnFormatCompleted(const std::string& device_path, bool success);
114
115 // Called on change to kExternalStorageDisabled pref.
116 void OnExternalStorageDisabledChanged();
117
118 // Called when prefs related to file manager change.
119 void OnFileManagerPrefsChanged();
120
121 // Process file watch notifications.
122 void HandleFileWatchNotification(const base::FilePath& path,
123 bool got_error);
124
125 // Sends directory change event.
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100126 void DispatchDirectoryChangeEvent(
127 const base::FilePath& path,
128 bool error,
Ben Murdochbb1529c2013-08-08 10:24:53 +0100129 const std::vector<std::string>& extension_ids);
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100130
131 void DispatchMountEvent(
132 chromeos::disks::DiskMountManager::MountEvent event,
133 chromeos::MountError error_code,
134 const chromeos::disks::DiskMountManager::MountPointInfo& mount_info);
135
136 // If needed, opens a file manager window for the removable device mounted at
137 // |mount_path|. Disk.mount_path() is empty, since it is being filled out
138 // after calling notifying observers by DiskMountManager.
139 void ShowRemovableDeviceInFileManager(
140 const chromeos::disks::DiskMountManager::Disk& disk,
141 const base::FilePath& mount_path);
142
143 // Sends onFileTranferUpdated to extensions if needed. If |always| is true,
144 // it sends the event always. Otherwise, it sends the event if enough time has
145 // passed from the previous event so as not to make extension busy.
146 void SendDriveFileTransferEvent(bool always);
147
148 // Manages the list of currently active Drive file transfer jobs.
149 struct DriveJobInfoWithStatus {
150 DriveJobInfoWithStatus();
151 DriveJobInfoWithStatus(const drive::JobInfo& info,
152 const std::string& status);
153 drive::JobInfo job_info;
154 std::string status;
155 };
156 std::map<drive::JobID, DriveJobInfoWithStatus> drive_jobs_;
157 base::Time last_file_transfer_event_;
158
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100159 WatcherMap file_watchers_;
Ben Murdoch2385ea32013-08-06 11:01:04 +0100160 scoped_ptr<DesktopNotifications> notifications_;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100161 scoped_ptr<PrefChangeRegistrar> pref_change_registrar_;
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100162 scoped_ptr<MountedDiskMonitor> mounted_disk_monitor_;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100163 Profile* profile_;
164
165 // Note: This should remain the last member so it'll be destroyed and
166 // invalidate the weak pointers before any other members are destroyed.
Ben Murdoch2385ea32013-08-06 11:01:04 +0100167 base::WeakPtrFactory<EventRouter> weak_factory_;
168 DISALLOW_COPY_AND_ASSIGN(EventRouter);
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100169};
170
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +0100171} // namespace file_manager
172
Ben Murdoch2385ea32013-08-06 11:01:04 +0100173#endif // CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_MANAGER_EVENT_ROUTER_H_