1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_BROWSER_EVENT_ROUTER_H_
#define CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_BROWSER_EVENT_ROUTER_H_
#pragma once
#include <map>
#include <set>
#include <string>
#include "base/files/file_path_watcher.h"
#include "base/memory/linked_ptr.h"
#include "base/memory/scoped_ptr.h"
#include "base/string16.h"
#include "base/synchronization/lock.h"
#include "chrome/browser/chromeos/cros/network_library.h"
#include "chrome/browser/chromeos/disks/disk_mount_manager.h"
#include "chrome/browser/chromeos/gdata/gdata_file_system.h"
#include "chrome/browser/chromeos/gdata/gdata_operation_registry.h"
#include "chrome/browser/profiles/refcounted_profile_keyed_service.h"
#include "chrome/browser/profiles/refcounted_profile_keyed_service_factory.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_source.h"
class FileBrowserNotifications;
class PrefChangeRegistrar;
class Profile;
// Monitors changes in disk mounts, network connection state and preferences
// affecting File Manager. Dispatches appropriate File Browser events.
class FileBrowserEventRouter
: public RefcountedProfileKeyedService,
public chromeos::disks::DiskMountManager::Observer,
public chromeos::NetworkLibrary::NetworkManagerObserver,
public content::NotificationObserver,
public gdata::GDataOperationRegistry::Observer,
public gdata::GDataFileSystem::Observer {
public:
// RefcountedProfileKeyedService overrides.
virtual void ShutdownOnUIThread() OVERRIDE;
// Starts observing file system change events. Currently only
// CrosDisksClient events are being observed.
void ObserveFileSystemEvents();
// File watch setup routines.
bool AddFileWatch(const FilePath& file_path,
const FilePath& virtual_path,
const std::string& extension_id);
void RemoveFileWatch(const FilePath& file_path,
const std::string& extension_id);
// CrosDisksClient::Observer overrides.
virtual void DiskChanged(chromeos::disks::DiskMountManagerEventType event,
const chromeos::disks::DiskMountManager::Disk* disk)
OVERRIDE;
virtual void DeviceChanged(chromeos::disks::DiskMountManagerEventType event,
const std::string& device_path) OVERRIDE;
virtual void MountCompleted(
chromeos::disks::DiskMountManager::MountEvent event_type,
chromeos::MountError error_code,
const chromeos::disks::DiskMountManager::MountPointInfo& mount_info)
OVERRIDE;
// chromeos::NetworkLibrary::NetworkManagerObserver override.
virtual void OnNetworkManagerChanged(
chromeos::NetworkLibrary* network_library) OVERRIDE;
// Overridden from content::NotificationObserver:
virtual void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) OVERRIDE;
// GDataOperationRegistry::Observer overrides.
virtual void OnProgressUpdate(
const std::vector<gdata::GDataOperationRegistry::ProgressStatus>& list)
OVERRIDE;
virtual void OnAuthenticationFailed() OVERRIDE;
// gdata::GDataFileSystem::Observer overrides.
virtual void OnDirectoryChanged(const FilePath& directory_path) OVERRIDE;
virtual void OnDocumentFeedFetched(int num_accumulated_entries) OVERRIDE;
private:
friend class FileBrowserEventRouterFactory;
// Helper class for passing through file watch notification events.
class FileWatcherDelegate : public base::files::FilePathWatcher::Delegate {
public:
explicit FileWatcherDelegate(FileBrowserEventRouter* router);
private:
// base::files::FilePathWatcher::Delegate overrides.
virtual void OnFilePathChanged(const FilePath& path) OVERRIDE;
virtual void OnFilePathError(const FilePath& path) OVERRIDE;
void HandleFileWatchOnUIThread(const FilePath& local_path, bool got_error);
FileBrowserEventRouter* router_;
};
typedef std::map<std::string, int> ExtensionUsageRegistry;
class FileWatcherExtensions {
public:
FileWatcherExtensions(const FilePath& path,
const std::string& extension_id,
bool is_remote_file_system);
~FileWatcherExtensions() {}
void AddExtension(const std::string& extension_id);
void RemoveExtension(const std::string& extension_id);
const ExtensionUsageRegistry& GetExtensions() const;
unsigned int GetRefCount() const;
const FilePath& GetVirtualPath() const;
bool Watch(const FilePath& path, FileWatcherDelegate* delegate);
private:
linked_ptr<base::files::FilePathWatcher> file_watcher_;
FilePath local_path_;
FilePath virtual_path_;
ExtensionUsageRegistry extensions_;
unsigned int ref_count_;
bool is_remote_file_system_;
};
typedef std::map<FilePath, FileWatcherExtensions*> WatcherMap;
explicit FileBrowserEventRouter(Profile* profile);
virtual ~FileBrowserEventRouter();
// USB mount event handlers.
void OnDiskAdded(const chromeos::disks::DiskMountManager::Disk* disk);
void OnDiskRemoved(const chromeos::disks::DiskMountManager::Disk* disk);
void OnDiskMounted(const chromeos::disks::DiskMountManager::Disk* disk);
void OnDiskUnmounted(const chromeos::disks::DiskMountManager::Disk* disk);
void OnDeviceAdded(const std::string& device_path);
void OnDeviceRemoved(const std::string& device_path);
void OnDeviceScanned(const std::string& device_path);
void OnFormattingStarted(const std::string& device_path, bool success);
void OnFormattingFinished(const std::string& device_path, bool success);
// Process file watch notifications.
void HandleFileWatchNotification(const FilePath& path,
bool got_error);
// Sends folder change event.
void DispatchFolderChangeEvent(const FilePath& path, bool error,
const ExtensionUsageRegistry& extensions);
// Sends filesystem changed extension message to all renderers.
void DispatchDiskEvent(const chromeos::disks::DiskMountManager::Disk* disk,
bool added);
void DispatchMountCompletedEvent(
chromeos::disks::DiskMountManager::MountEvent event,
chromeos::MountError error_code,
const chromeos::disks::DiskMountManager::MountPointInfo& mount_info);
void RemoveBrowserFromVector(const std::string& path);
// Used to create a window of a standard size, and add it to a list
// of tracked browser windows in case that device goes away.
void OpenFileBrowse(const std::string& url,
const std::string& device_path,
bool small);
// Returns the GDataFileSystem for the current profile.
gdata::GDataFileSystem* GetRemoteFileSystem() const;
// Handles requests to start and stop periodic updates on remote file system.
// When |start| is set to true, this function starts periodic updates only if
// it is not yet started; when |start| is set to false, this function stops
// periodic updates only if the number of outstanding update requests reaches
// zero.
void HandleRemoteUpdateRequestOnUIThread(bool start);
scoped_refptr<FileWatcherDelegate> delegate_;
WatcherMap file_watchers_;
scoped_ptr<FileBrowserNotifications> notifications_;
scoped_ptr<PrefChangeRegistrar> pref_change_registrar_;
Profile* profile_;
base::Lock lock_;
bool current_gdata_operation_failed_;
int last_active_gdata_operation_count_;
// Number of active update requests on the remote file system.
int num_remote_update_requests_;
DISALLOW_COPY_AND_ASSIGN(FileBrowserEventRouter);
};
// Singleton that owns all FileBrowserEventRouter and associates
// them with Profiles.
class FileBrowserEventRouterFactory
: public RefcountedProfileKeyedServiceFactory {
public:
// Returns the FileBrowserEventRouter for |profile|, creating it if
// it is not yet created.
static scoped_refptr<FileBrowserEventRouter> GetForProfile(Profile* profile);
// Returns the FileBrowserEventRouterFactory instance.
static FileBrowserEventRouterFactory* GetInstance();
protected:
// ProfileKeyedBasedFactory overrides:
virtual bool ServiceHasOwnInstanceInIncognito() OVERRIDE;
private:
friend struct DefaultSingletonTraits<FileBrowserEventRouterFactory>;
FileBrowserEventRouterFactory();
virtual ~FileBrowserEventRouterFactory();
// ProfileKeyedServiceFactory:
virtual scoped_refptr<RefcountedProfileKeyedService> BuildServiceInstanceFor(
Profile* profile) const OVERRIDE;
};
#endif // CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_BROWSER_EVENT_ROUTER_H_
|