Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(340)

Side by Side Diff: chrome/browser/chromeos/extensions/file_browser_event_router.cc

Issue 9720020: [COPY] - Implemented API for tracking ongoing file transfers from file manager. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/chromeos/extensions/file_browser_event_router.h" 5 #include "chrome/browser/chromeos/extensions/file_browser_event_router.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/json/json_writer.h" 8 #include "base/json/json_writer.h"
9 #include "base/message_loop.h" 9 #include "base/message_loop.h"
10 #include "base/stl_util.h" 10 #include "base/stl_util.h"
11 #include "base/values.h" 11 #include "base/values.h"
12 #include "chrome/browser/chromeos/extensions/file_browser_notifications.h" 12 #include "chrome/browser/chromeos/extensions/file_browser_notifications.h"
13 #include "chrome/browser/chromeos/extensions/file_manager_util.h" 13 #include "chrome/browser/chromeos/extensions/file_manager_util.h"
14 #include "chrome/browser/chromeos/login/user_manager.h" 14 #include "chrome/browser/chromeos/login/user_manager.h"
15 #include "chrome/browser/extensions/extension_event_names.h" 15 #include "chrome/browser/extensions/extension_event_names.h"
16 #include "chrome/browser/extensions/extension_event_router.h" 16 #include "chrome/browser/extensions/extension_event_router.h"
17 #include "chrome/browser/extensions/extension_service.h" 17 #include "chrome/browser/extensions/extension_service.h"
18 #include "chrome/browser/profiles/profile.h" 18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/browser/profiles/profile_dependency_manager.h"
20 #include "chrome/common/chrome_notification_types.h"
21 #include "content/public/browser/notification_source.h"
19 #include "grit/generated_resources.h" 22 #include "grit/generated_resources.h"
20 #include "webkit/fileapi/file_system_types.h" 23 #include "webkit/fileapi/file_system_types.h"
21 #include "webkit/fileapi/file_system_util.h" 24 #include "webkit/fileapi/file_system_util.h"
22 25
23 using chromeos::disks::DiskMountManager; 26 using chromeos::disks::DiskMountManager;
24 using chromeos::disks::DiskMountManagerEventType; 27 using chromeos::disks::DiskMountManagerEventType;
25 using content::BrowserThread; 28 using content::BrowserThread;
29 using gdata::GDataFileSystem;
30 using gdata::GDataFileSystemFactory;
26 31
27 namespace { 32 namespace {
28 const char kDiskAddedEventType[] = "added"; 33 const char kDiskAddedEventType[] = "added";
29 const char kDiskRemovedEventType[] = "removed"; 34 const char kDiskRemovedEventType[] = "removed";
30 35
31 const char kPathChanged[] = "changed"; 36 const char kPathChanged[] = "changed";
32 const char kPathWatchError[] = "error"; 37 const char kPathWatchError[] = "error";
33 38
34 DictionaryValue* DiskToDictionaryValue( 39 DictionaryValue* DiskToDictionaryValue(
35 const DiskMountManager::Disk* disk) { 40 const DiskMountManager::Disk* disk) {
(...skipping 30 matching lines...) Expand all
66 case chromeos::MOUNT_ERROR_NETWORK_ERROR: 71 case chromeos::MOUNT_ERROR_NETWORK_ERROR:
67 return "error_libcros_missing"; 72 return "error_libcros_missing";
68 case chromeos::MOUNT_ERROR_PATH_UNMOUNTED: 73 case chromeos::MOUNT_ERROR_PATH_UNMOUNTED:
69 return "error_path_unmounted"; 74 return "error_path_unmounted";
70 default: 75 default:
71 NOTREACHED(); 76 NOTREACHED();
72 } 77 }
73 return ""; 78 return "";
74 } 79 }
75 80
76 ExtensionFileBrowserEventRouter::ExtensionFileBrowserEventRouter( 81 FileBrowserEventRouter::FileBrowserEventRouter(
77 Profile* profile) 82 Profile* profile)
78 : delegate_(new ExtensionFileBrowserEventRouter::FileWatcherDelegate(this)), 83 : delegate_(new FileBrowserEventRouter::FileWatcherDelegate(this)),
79 notifications_(new FileBrowserNotifications(profile)), 84 notifications_(new FileBrowserNotifications(profile)),
80 profile_(profile) { 85 profile_(profile) {
81 } 86 }
82 87
83 ExtensionFileBrowserEventRouter::~ExtensionFileBrowserEventRouter() { 88 FileBrowserEventRouter::~FileBrowserEventRouter() {
89 }
90
91 void FileBrowserEventRouter::Shutdown() {
84 DCHECK(file_watchers_.empty()); 92 DCHECK(file_watchers_.empty());
85 STLDeleteValues(&file_watchers_); 93 STLDeleteValues(&file_watchers_);
86
87 if (!profile_) { 94 if (!profile_) {
88 NOTREACHED(); 95 NOTREACHED();
89 return; 96 return;
90 } 97 }
98 DiskMountManager::GetInstance()->RemoveObserver(this);
99
100 GDataFileSystem* file_system =
101 GDataFileSystemFactory::GetForProfile(profile_);
102 file_system->RemoveOperationObserver(this);
103
91 profile_ = NULL; 104 profile_ = NULL;
92 DiskMountManager::GetInstance()->RemoveObserver(this);
93 } 105 }
94 106
95 void ExtensionFileBrowserEventRouter::ObserveFileSystemEvents() { 107 void FileBrowserEventRouter::ObserveFileSystemEvents() {
96 if (!profile_) { 108 if (!profile_) {
97 NOTREACHED(); 109 NOTREACHED();
98 return; 110 return;
99 } 111 }
100 if (chromeos::UserManager::Get()->IsUserLoggedIn()) { 112 if (!chromeos::UserManager::Get()->IsUserLoggedIn())
101 DiskMountManager* disk_mount_manager = DiskMountManager::GetInstance(); 113 return;
102 disk_mount_manager->RemoveObserver(this); 114
103 disk_mount_manager->AddObserver(this); 115 DiskMountManager* disk_mount_manager = DiskMountManager::GetInstance();
104 disk_mount_manager->RequestMountInfoRefresh(); 116 disk_mount_manager->RemoveObserver(this);
117 disk_mount_manager->AddObserver(this);
118 disk_mount_manager->RequestMountInfoRefresh();
119
120 GDataFileSystem* file_system =
121 GDataFileSystemFactory::GetForProfile(profile_);
122 if (!file_system) {
123 NOTREACHED();
124 return;
105 } 125 }
126 file_system->AddOperationObserver(this);
106 } 127 }
107 128
108 // File watch setup routines. 129 // File watch setup routines.
109 bool ExtensionFileBrowserEventRouter::AddFileWatch( 130 bool FileBrowserEventRouter::AddFileWatch(
110 const FilePath& local_path, 131 const FilePath& local_path,
111 const FilePath& virtual_path, 132 const FilePath& virtual_path,
112 const std::string& extension_id) { 133 const std::string& extension_id) {
113 base::AutoLock lock(lock_); 134 base::AutoLock lock(lock_);
114 WatcherMap::iterator iter = file_watchers_.find(local_path); 135 WatcherMap::iterator iter = file_watchers_.find(local_path);
115 if (iter == file_watchers_.end()) { 136 if (iter == file_watchers_.end()) {
116 scoped_ptr<FileWatcherExtensions> 137 scoped_ptr<FileWatcherExtensions>
117 watch(new FileWatcherExtensions(virtual_path, extension_id)); 138 watch(new FileWatcherExtensions(virtual_path, extension_id));
118 139
119 if (watch->Watch(local_path, delegate_.get())) 140 if (watch->Watch(local_path, delegate_.get()))
120 file_watchers_[local_path] = watch.release(); 141 file_watchers_[local_path] = watch.release();
121 else 142 else
122 return false; 143 return false;
123 } else { 144 } else {
124 iter->second->AddExtension(extension_id); 145 iter->second->AddExtension(extension_id);
125 } 146 }
126 return true; 147 return true;
127 } 148 }
128 149
129 void ExtensionFileBrowserEventRouter::RemoveFileWatch( 150 void FileBrowserEventRouter::RemoveFileWatch(
130 const FilePath& local_path, 151 const FilePath& local_path,
131 const std::string& extension_id) { 152 const std::string& extension_id) {
132 base::AutoLock lock(lock_); 153 base::AutoLock lock(lock_);
133 WatcherMap::iterator iter = file_watchers_.find(local_path); 154 WatcherMap::iterator iter = file_watchers_.find(local_path);
134 if (iter == file_watchers_.end()) 155 if (iter == file_watchers_.end())
135 return; 156 return;
136 // Remove the renderer process for this watch. 157 // Remove the renderer process for this watch.
137 iter->second->RemoveExtension(extension_id); 158 iter->second->RemoveExtension(extension_id);
138 if (iter->second->GetRefCount() == 0) { 159 if (iter->second->GetRefCount() == 0) {
139 delete iter->second; 160 delete iter->second;
140 file_watchers_.erase(iter); 161 file_watchers_.erase(iter);
141 } 162 }
142 } 163 }
143 164
144 void ExtensionFileBrowserEventRouter::DiskChanged( 165 void FileBrowserEventRouter::DiskChanged(
145 DiskMountManagerEventType event, 166 DiskMountManagerEventType event,
146 const DiskMountManager::Disk* disk) { 167 const DiskMountManager::Disk* disk) {
147 // Disregard hidden devices. 168 // Disregard hidden devices.
148 if (disk->is_hidden()) 169 if (disk->is_hidden())
149 return; 170 return;
150 if (event == chromeos::disks::MOUNT_DISK_ADDED) { 171 if (event == chromeos::disks::MOUNT_DISK_ADDED) {
151 OnDiskAdded(disk); 172 OnDiskAdded(disk);
152 } else if (event == chromeos::disks::MOUNT_DISK_REMOVED) { 173 } else if (event == chromeos::disks::MOUNT_DISK_REMOVED) {
153 OnDiskRemoved(disk); 174 OnDiskRemoved(disk);
154 } 175 }
155 } 176 }
156 177
157 void ExtensionFileBrowserEventRouter::DeviceChanged( 178 void FileBrowserEventRouter::DeviceChanged(
158 DiskMountManagerEventType event, 179 DiskMountManagerEventType event,
159 const std::string& device_path) { 180 const std::string& device_path) {
160 if (event == chromeos::disks::MOUNT_DEVICE_ADDED) { 181 if (event == chromeos::disks::MOUNT_DEVICE_ADDED) {
161 OnDeviceAdded(device_path); 182 OnDeviceAdded(device_path);
162 } else if (event == chromeos::disks::MOUNT_DEVICE_REMOVED) { 183 } else if (event == chromeos::disks::MOUNT_DEVICE_REMOVED) {
163 OnDeviceRemoved(device_path); 184 OnDeviceRemoved(device_path);
164 } else if (event == chromeos::disks::MOUNT_DEVICE_SCANNED) { 185 } else if (event == chromeos::disks::MOUNT_DEVICE_SCANNED) {
165 OnDeviceScanned(device_path); 186 OnDeviceScanned(device_path);
166 } else if (event == chromeos::disks::MOUNT_FORMATTING_STARTED) { 187 } else if (event == chromeos::disks::MOUNT_FORMATTING_STARTED) {
167 // TODO(tbarzic): get rid of '!'. 188 // TODO(tbarzic): get rid of '!'.
168 if (device_path[0] == '!') { 189 if (device_path[0] == '!') {
169 OnFormattingStarted(device_path.substr(1), false); 190 OnFormattingStarted(device_path.substr(1), false);
170 } else { 191 } else {
171 OnFormattingStarted(device_path, true); 192 OnFormattingStarted(device_path, true);
172 } 193 }
173 } else if (event == chromeos::disks::MOUNT_FORMATTING_FINISHED) { 194 } else if (event == chromeos::disks::MOUNT_FORMATTING_FINISHED) {
174 if (device_path[0] == '!') { 195 if (device_path[0] == '!') {
175 OnFormattingFinished(device_path.substr(1), false); 196 OnFormattingFinished(device_path.substr(1), false);
176 } else { 197 } else {
177 OnFormattingFinished(device_path, true); 198 OnFormattingFinished(device_path, true);
178 } 199 }
179 } 200 }
180 } 201 }
181 202
182 void ExtensionFileBrowserEventRouter::MountCompleted( 203 void FileBrowserEventRouter::MountCompleted(
183 DiskMountManager::MountEvent event_type, 204 DiskMountManager::MountEvent event_type,
184 chromeos::MountError error_code, 205 chromeos::MountError error_code,
185 const DiskMountManager::MountPointInfo& mount_info) { 206 const DiskMountManager::MountPointInfo& mount_info) {
186 DispatchMountCompletedEvent(event_type, error_code, mount_info); 207 DispatchMountCompletedEvent(event_type, error_code, mount_info);
187 208
188 if (mount_info.mount_type == chromeos::MOUNT_TYPE_DEVICE && 209 if (mount_info.mount_type == chromeos::MOUNT_TYPE_DEVICE &&
189 event_type == DiskMountManager::MOUNTING) { 210 event_type == DiskMountManager::MOUNTING) {
190 DiskMountManager* disk_mount_manager = DiskMountManager::GetInstance(); 211 DiskMountManager* disk_mount_manager = DiskMountManager::GetInstance();
191 DiskMountManager::DiskMap::const_iterator disk_it = 212 DiskMountManager::DiskMap::const_iterator disk_it =
192 disk_mount_manager->disks().find(mount_info.source_path); 213 disk_mount_manager->disks().find(mount_info.source_path);
193 if (disk_it == disk_mount_manager->disks().end()) { 214 if (disk_it == disk_mount_manager->disks().end()) {
194 return; 215 return;
195 } 216 }
196 DiskMountManager::Disk* disk = disk_it->second; 217 DiskMountManager::Disk* disk = disk_it->second;
197 218
198 notifications_->ManageNotificationsOnMountCompleted( 219 notifications_->ManageNotificationsOnMountCompleted(
199 disk->system_path_prefix(), disk->drive_label(), disk->is_parent(), 220 disk->system_path_prefix(), disk->drive_label(), disk->is_parent(),
200 error_code == chromeos::MOUNT_ERROR_NONE, 221 error_code == chromeos::MOUNT_ERROR_NONE,
201 error_code == chromeos::MOUNT_ERROR_UNSUPORTED_FILESYSTEM); 222 error_code == chromeos::MOUNT_ERROR_UNSUPORTED_FILESYSTEM);
202 } 223 }
203 } 224 }
204 225
205 void ExtensionFileBrowserEventRouter::HandleFileWatchNotification( 226 void FileBrowserEventRouter::OnProgressUpdate(
227 const std::vector<gdata::GDataOperationRegistry::ProgressStatus>& list) {
228 scoped_ptr<ListValue> event_list(
229 file_manager_util::ProgressStatusVectorToListValue(
230 profile_,
231 file_manager_util::GetFileBrowserExtensionUrl().GetOrigin(),
232 list));
233
234 std::string args_json;
235 base::JSONWriter::Write(event_list.get(),
236 &args_json);
237
238 profile_->GetExtensionEventRouter()->DispatchEventToExtension(
239 std::string(kFileBrowserDomain),
240 extension_event_names::kOnFileTransfersUpdated, args_json,
241 NULL, GURL());
242 }
243
244
245 void FileBrowserEventRouter::HandleFileWatchNotification(
206 const FilePath& local_path, bool got_error) { 246 const FilePath& local_path, bool got_error) {
207 base::AutoLock lock(lock_); 247 base::AutoLock lock(lock_);
208 WatcherMap::const_iterator iter = file_watchers_.find(local_path); 248 WatcherMap::const_iterator iter = file_watchers_.find(local_path);
209 if (iter == file_watchers_.end()) { 249 if (iter == file_watchers_.end()) {
210 NOTREACHED(); 250 NOTREACHED();
211 return; 251 return;
212 } 252 }
213 DispatchFolderChangeEvent(iter->second->GetVirtualPath(), got_error, 253 DispatchFolderChangeEvent(iter->second->GetVirtualPath(), got_error,
214 iter->second->GetExtensions()); 254 iter->second->GetExtensions());
215 } 255 }
216 256
217 void ExtensionFileBrowserEventRouter::DispatchFolderChangeEvent( 257 void FileBrowserEventRouter::DispatchFolderChangeEvent(
218 const FilePath& virtual_path, bool got_error, 258 const FilePath& virtual_path, bool got_error,
219 const ExtensionFileBrowserEventRouter::ExtensionUsageRegistry& extensions) { 259 const FileBrowserEventRouter::ExtensionUsageRegistry& extensions) {
220 if (!profile_) { 260 if (!profile_) {
221 NOTREACHED(); 261 NOTREACHED();
222 return; 262 return;
223 } 263 }
224 264
225 for (ExtensionUsageRegistry::const_iterator iter = extensions.begin(); 265 for (ExtensionUsageRegistry::const_iterator iter = extensions.begin();
226 iter != extensions.end(); ++iter) { 266 iter != extensions.end(); ++iter) {
227 GURL target_origin_url(Extension::GetBaseURLFromExtensionId( 267 GURL target_origin_url(Extension::GetBaseURLFromExtensionId(
228 iter->first)); 268 iter->first));
229 GURL base_url = fileapi::GetFileSystemRootURI(target_origin_url, 269 GURL base_url = fileapi::GetFileSystemRootURI(target_origin_url,
230 fileapi::kFileSystemTypeExternal); 270 fileapi::kFileSystemTypeExternal);
231 GURL target_file_url = GURL(base_url.spec() + virtual_path.value()); 271 GURL target_file_url = GURL(base_url.spec() + virtual_path.value());
232 ListValue args; 272 ListValue args;
233 DictionaryValue* watch_info = new DictionaryValue(); 273 DictionaryValue* watch_info = new DictionaryValue();
234 args.Append(watch_info); 274 args.Append(watch_info);
235 watch_info->SetString("fileUrl", target_file_url.spec()); 275 watch_info->SetString("fileUrl", target_file_url.spec());
236 watch_info->SetString("eventType", 276 watch_info->SetString("eventType",
237 got_error ? kPathWatchError : kPathChanged); 277 got_error ? kPathWatchError : kPathChanged);
238 278
239 std::string args_json; 279 std::string args_json;
240 base::JSONWriter::Write(&args, &args_json); 280 base::JSONWriter::Write(&args, &args_json);
241 281
242 profile_->GetExtensionEventRouter()->DispatchEventToExtension( 282 profile_->GetExtensionEventRouter()->DispatchEventToExtension(
243 iter->first, extension_event_names::kOnFileChanged, args_json, 283 iter->first, extension_event_names::kOnFileChanged, args_json,
244 NULL, GURL()); 284 NULL, GURL());
245 } 285 }
246 } 286 }
247 287
248 void ExtensionFileBrowserEventRouter::DispatchDiskEvent( 288 void FileBrowserEventRouter::DispatchDiskEvent(
249 const DiskMountManager::Disk* disk, bool added) { 289 const DiskMountManager::Disk* disk, bool added) {
250 if (!profile_) { 290 if (!profile_) {
251 NOTREACHED(); 291 NOTREACHED();
252 return; 292 return;
253 } 293 }
254 294
255 ListValue args; 295 ListValue args;
256 DictionaryValue* mount_info = new DictionaryValue(); 296 DictionaryValue* mount_info = new DictionaryValue();
257 args.Append(mount_info); 297 args.Append(mount_info);
258 mount_info->SetString("eventType", 298 mount_info->SetString("eventType",
259 added ? kDiskAddedEventType : kDiskRemovedEventType); 299 added ? kDiskAddedEventType : kDiskRemovedEventType);
260 DictionaryValue* disk_info = DiskToDictionaryValue(disk); 300 DictionaryValue* disk_info = DiskToDictionaryValue(disk);
261 mount_info->Set("volumeInfo", disk_info); 301 mount_info->Set("volumeInfo", disk_info);
262 302
263 std::string args_json; 303 std::string args_json;
264 base::JSONWriter::Write(&args, &args_json); 304 base::JSONWriter::Write(&args, &args_json);
265 profile_->GetExtensionEventRouter()->DispatchEventToRenderers( 305 profile_->GetExtensionEventRouter()->DispatchEventToRenderers(
266 extension_event_names::kOnFileBrowserDiskChanged, args_json, NULL, 306 extension_event_names::kOnFileBrowserDiskChanged, args_json, NULL,
267 GURL()); 307 GURL());
268 } 308 }
269 309
270 void ExtensionFileBrowserEventRouter::DispatchMountCompletedEvent( 310 void FileBrowserEventRouter::DispatchMountCompletedEvent(
271 DiskMountManager::MountEvent event, 311 DiskMountManager::MountEvent event,
272 chromeos::MountError error_code, 312 chromeos::MountError error_code,
273 const DiskMountManager::MountPointInfo& mount_info) { 313 const DiskMountManager::MountPointInfo& mount_info) {
274 if (!profile_ || mount_info.mount_type == chromeos::MOUNT_TYPE_INVALID) { 314 if (!profile_ || mount_info.mount_type == chromeos::MOUNT_TYPE_INVALID) {
275 NOTREACHED(); 315 NOTREACHED();
276 return; 316 return;
277 } 317 }
278 318
279 ListValue args; 319 ListValue args;
280 DictionaryValue* mount_info_value = new DictionaryValue(); 320 DictionaryValue* mount_info_value = new DictionaryValue();
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
324 GURL()); 364 GURL());
325 365
326 if (relative_mount_path_set && 366 if (relative_mount_path_set &&
327 mount_info.mount_type == chromeos::MOUNT_TYPE_DEVICE && 367 mount_info.mount_type == chromeos::MOUNT_TYPE_DEVICE &&
328 !mount_info.mount_condition && 368 !mount_info.mount_condition &&
329 event == DiskMountManager::MOUNTING) { 369 event == DiskMountManager::MOUNTING) {
330 file_manager_util::ViewRemovableDrive(FilePath(mount_info.mount_path)); 370 file_manager_util::ViewRemovableDrive(FilePath(mount_info.mount_path));
331 } 371 }
332 } 372 }
333 373
334 void ExtensionFileBrowserEventRouter::OnDiskAdded( 374 void FileBrowserEventRouter::OnDiskAdded(
335 const DiskMountManager::Disk* disk) { 375 const DiskMountManager::Disk* disk) {
336 VLOG(1) << "Disk added: " << disk->device_path(); 376 VLOG(1) << "Disk added: " << disk->device_path();
337 if (disk->device_path().empty()) { 377 if (disk->device_path().empty()) {
338 VLOG(1) << "Empty system path for " << disk->device_path(); 378 VLOG(1) << "Empty system path for " << disk->device_path();
339 return; 379 return;
340 } 380 }
341 381
342 // If disk is not mounted yet, give it a try. 382 // If disk is not mounted yet, give it a try.
343 if (disk->mount_path().empty()) { 383 if (disk->mount_path().empty()) {
344 // Initiate disk mount operation. 384 // Initiate disk mount operation.
345 DiskMountManager::GetInstance()->MountPath( 385 DiskMountManager::GetInstance()->MountPath(
346 disk->device_path(), chromeos::MOUNT_TYPE_DEVICE); 386 disk->device_path(), chromeos::MOUNT_TYPE_DEVICE);
347 } 387 }
348 DispatchDiskEvent(disk, true); 388 DispatchDiskEvent(disk, true);
349 } 389 }
350 390
351 void ExtensionFileBrowserEventRouter::OnDiskRemoved( 391 void FileBrowserEventRouter::OnDiskRemoved(
352 const DiskMountManager::Disk* disk) { 392 const DiskMountManager::Disk* disk) {
353 VLOG(1) << "Disk removed: " << disk->device_path(); 393 VLOG(1) << "Disk removed: " << disk->device_path();
354 394
355 if (!disk->mount_path().empty()) { 395 if (!disk->mount_path().empty()) {
356 DiskMountManager::GetInstance()->UnmountPath(disk->mount_path()); 396 DiskMountManager::GetInstance()->UnmountPath(disk->mount_path());
357 } 397 }
358 DispatchDiskEvent(disk, false); 398 DispatchDiskEvent(disk, false);
359 } 399 }
360 400
361 void ExtensionFileBrowserEventRouter::OnDeviceAdded( 401 void FileBrowserEventRouter::OnDeviceAdded(
362 const std::string& device_path) { 402 const std::string& device_path) {
363 VLOG(1) << "Device added : " << device_path; 403 VLOG(1) << "Device added : " << device_path;
364 404
365 notifications_->RegisterDevice(device_path); 405 notifications_->RegisterDevice(device_path);
366 notifications_->ShowNotificationDelayed(FileBrowserNotifications::DEVICE, 406 notifications_->ShowNotificationDelayed(FileBrowserNotifications::DEVICE,
367 device_path, 407 device_path,
368 4000); 408 4000);
369 } 409 }
370 410
371 void ExtensionFileBrowserEventRouter::OnDeviceRemoved( 411 void FileBrowserEventRouter::OnDeviceRemoved(
372 const std::string& device_path) { 412 const std::string& device_path) {
373 VLOG(1) << "Device removed : " << device_path; 413 VLOG(1) << "Device removed : " << device_path;
374 notifications_->HideNotification(FileBrowserNotifications::DEVICE, 414 notifications_->HideNotification(FileBrowserNotifications::DEVICE,
375 device_path); 415 device_path);
376 notifications_->HideNotification(FileBrowserNotifications::DEVICE_FAIL, 416 notifications_->HideNotification(FileBrowserNotifications::DEVICE_FAIL,
377 device_path); 417 device_path);
378 notifications_->UnregisterDevice(device_path); 418 notifications_->UnregisterDevice(device_path);
379 } 419 }
380 420
381 void ExtensionFileBrowserEventRouter::OnDeviceScanned( 421 void FileBrowserEventRouter::OnDeviceScanned(
382 const std::string& device_path) { 422 const std::string& device_path) {
383 VLOG(1) << "Device scanned : " << device_path; 423 VLOG(1) << "Device scanned : " << device_path;
384 } 424 }
385 425
386 void ExtensionFileBrowserEventRouter::OnFormattingStarted( 426 void FileBrowserEventRouter::OnFormattingStarted(
387 const std::string& device_path, bool success) { 427 const std::string& device_path, bool success) {
388 if (success) { 428 if (success) {
389 notifications_->ShowNotification(FileBrowserNotifications::FORMAT_START, 429 notifications_->ShowNotification(FileBrowserNotifications::FORMAT_START,
390 device_path); 430 device_path);
391 } else { 431 } else {
392 notifications_->ShowNotification( 432 notifications_->ShowNotification(
393 FileBrowserNotifications::FORMAT_START_FAIL, device_path); 433 FileBrowserNotifications::FORMAT_START_FAIL, device_path);
394 } 434 }
395 } 435 }
396 436
397 void ExtensionFileBrowserEventRouter::OnFormattingFinished( 437 void FileBrowserEventRouter::OnFormattingFinished(
398 const std::string& device_path, bool success) { 438 const std::string& device_path, bool success) {
399 if (success) { 439 if (success) {
400 notifications_->HideNotification(FileBrowserNotifications::FORMAT_START, 440 notifications_->HideNotification(FileBrowserNotifications::FORMAT_START,
401 device_path); 441 device_path);
402 notifications_->ShowNotification(FileBrowserNotifications::FORMAT_SUCCESS, 442 notifications_->ShowNotification(FileBrowserNotifications::FORMAT_SUCCESS,
403 device_path); 443 device_path);
404 // Hide it after a couple of seconds. 444 // Hide it after a couple of seconds.
405 notifications_->HideNotificationDelayed( 445 notifications_->HideNotificationDelayed(
406 FileBrowserNotifications::FORMAT_SUCCESS, device_path, 4000); 446 FileBrowserNotifications::FORMAT_SUCCESS, device_path, 4000);
407 447
408 DiskMountManager::GetInstance()->MountPath(device_path, 448 DiskMountManager::GetInstance()->MountPath(device_path,
409 chromeos::MOUNT_TYPE_DEVICE); 449 chromeos::MOUNT_TYPE_DEVICE);
410 } else { 450 } else {
411 notifications_->HideNotification(FileBrowserNotifications::FORMAT_START, 451 notifications_->HideNotification(FileBrowserNotifications::FORMAT_START,
412 device_path); 452 device_path);
413 notifications_->ShowNotification(FileBrowserNotifications::FORMAT_FAIL, 453 notifications_->ShowNotification(FileBrowserNotifications::FORMAT_FAIL,
414 device_path); 454 device_path);
415 } 455 }
416 } 456 }
417 457
418 // ExtensionFileBrowserEventRouter::WatcherDelegate methods. 458 // FileBrowserEventRouter::WatcherDelegate methods.
419 ExtensionFileBrowserEventRouter::FileWatcherDelegate::FileWatcherDelegate( 459 FileBrowserEventRouter::FileWatcherDelegate::FileWatcherDelegate(
420 ExtensionFileBrowserEventRouter* router) : router_(router) { 460 FileBrowserEventRouter* router) : router_(router) {
421 } 461 }
422 462
423 void ExtensionFileBrowserEventRouter::FileWatcherDelegate::OnFilePathChanged( 463 void FileBrowserEventRouter::FileWatcherDelegate::OnFilePathChanged(
424 const FilePath& local_path) { 464 const FilePath& local_path) {
425 BrowserThread::PostTask( 465 BrowserThread::PostTask(
426 BrowserThread::UI, FROM_HERE, 466 BrowserThread::UI, FROM_HERE,
427 base::Bind(&FileWatcherDelegate::HandleFileWatchOnUIThread, 467 base::Bind(&FileWatcherDelegate::HandleFileWatchOnUIThread,
428 this, local_path, false)); 468 this, local_path, false));
429 } 469 }
430 470
431 void ExtensionFileBrowserEventRouter::FileWatcherDelegate::OnFilePathError( 471 void FileBrowserEventRouter::FileWatcherDelegate::OnFilePathError(
432 const FilePath& local_path) { 472 const FilePath& local_path) {
433 BrowserThread::PostTask( 473 BrowserThread::PostTask(
434 BrowserThread::UI, FROM_HERE, 474 BrowserThread::UI, FROM_HERE,
435 base::Bind(&FileWatcherDelegate::HandleFileWatchOnUIThread, 475 base::Bind(&FileWatcherDelegate::HandleFileWatchOnUIThread,
436 this, local_path, true)); 476 this, local_path, true));
437 } 477 }
438 478
439 void 479 void
440 ExtensionFileBrowserEventRouter::FileWatcherDelegate::HandleFileWatchOnUIThread( 480 FileBrowserEventRouter::FileWatcherDelegate::HandleFileWatchOnUIThread(
441 const FilePath& local_path, bool got_error) { 481 const FilePath& local_path, bool got_error) {
442 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 482 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
443 router_->HandleFileWatchNotification(local_path, got_error); 483 router_->HandleFileWatchNotification(local_path, got_error);
444 } 484 }
445 485
446 486
447 ExtensionFileBrowserEventRouter::FileWatcherExtensions::FileWatcherExtensions( 487 FileBrowserEventRouter::FileWatcherExtensions::FileWatcherExtensions(
448 const FilePath& path, const std::string& extension_id) 488 const FilePath& path, const std::string& extension_id)
449 : ref_count(0) { 489 : ref_count(0) {
450 file_watcher.reset(new base::files::FilePathWatcher()); 490 file_watcher.reset(new base::files::FilePathWatcher());
451 virtual_path = path; 491 virtual_path = path;
452 AddExtension(extension_id); 492 AddExtension(extension_id);
453 } 493 }
454 494
455 void ExtensionFileBrowserEventRouter::FileWatcherExtensions::AddExtension( 495 void FileBrowserEventRouter::FileWatcherExtensions::AddExtension(
456 const std::string& extension_id) { 496 const std::string& extension_id) {
457 ExtensionUsageRegistry::iterator it = extensions.find(extension_id); 497 ExtensionUsageRegistry::iterator it = extensions.find(extension_id);
458 if (it != extensions.end()) { 498 if (it != extensions.end()) {
459 it->second++; 499 it->second++;
460 } else { 500 } else {
461 extensions.insert(ExtensionUsageRegistry::value_type(extension_id, 1)); 501 extensions.insert(ExtensionUsageRegistry::value_type(extension_id, 1));
462 } 502 }
463 503
464 ref_count++; 504 ref_count++;
465 } 505 }
466 506
467 void ExtensionFileBrowserEventRouter::FileWatcherExtensions::RemoveExtension( 507 void FileBrowserEventRouter::FileWatcherExtensions::RemoveExtension(
468 const std::string& extension_id) { 508 const std::string& extension_id) {
469 ExtensionUsageRegistry::iterator it = extensions.find(extension_id); 509 ExtensionUsageRegistry::iterator it = extensions.find(extension_id);
470 510
471 if (it != extensions.end()) { 511 if (it != extensions.end()) {
472 // If entry found - decrease it's count and remove if necessary 512 // If entry found - decrease it's count and remove if necessary
473 if (0 == it->second--) { 513 if (0 == it->second--) {
474 extensions.erase(it); 514 extensions.erase(it);
475 } 515 }
476 516
477 ref_count--; 517 ref_count--;
478 } else { 518 } else {
479 // Might be reference counting problem - e.g. if some component of 519 // Might be reference counting problem - e.g. if some component of
480 // extension subscribes/unsubscribes correctly, but other component 520 // extension subscribes/unsubscribes correctly, but other component
481 // only unsubscribes, developer of first one might receive this message 521 // only unsubscribes, developer of first one might receive this message
482 LOG(FATAL) << " Extension [" << extension_id 522 LOG(FATAL) << " Extension [" << extension_id
483 << "] tries to unsubscribe from folder [" << local_path.value() 523 << "] tries to unsubscribe from folder [" << local_path.value()
484 << "] it isn't subscribed"; 524 << "] it isn't subscribed";
485 } 525 }
486 } 526 }
487 527
488 const ExtensionFileBrowserEventRouter::ExtensionUsageRegistry& 528 const FileBrowserEventRouter::ExtensionUsageRegistry&
489 ExtensionFileBrowserEventRouter::FileWatcherExtensions::GetExtensions() const { 529 FileBrowserEventRouter::FileWatcherExtensions::GetExtensions() const {
490 return extensions; 530 return extensions;
491 } 531 }
492 532
493 unsigned int 533 unsigned int
494 ExtensionFileBrowserEventRouter::FileWatcherExtensions::GetRefCount() const { 534 FileBrowserEventRouter::FileWatcherExtensions::GetRefCount() const {
495 return ref_count; 535 return ref_count;
496 } 536 }
497 537
498 const FilePath& 538 const FilePath&
499 ExtensionFileBrowserEventRouter::FileWatcherExtensions::GetVirtualPath() const { 539 FileBrowserEventRouter::FileWatcherExtensions::GetVirtualPath() const {
500 return virtual_path; 540 return virtual_path;
501 } 541 }
502 542
503 bool ExtensionFileBrowserEventRouter::FileWatcherExtensions::Watch 543 bool FileBrowserEventRouter::FileWatcherExtensions::Watch
504 (const FilePath& path, FileWatcherDelegate* delegate) { 544 (const FilePath& path, FileWatcherDelegate* delegate) {
505 return file_watcher->Watch(path, delegate); 545 return file_watcher->Watch(path, delegate);
506 } 546 }
547
548 // static
549 FileBrowserEventRouter*
550 FileBrowserEventRouterFactory::GetForProfile(Profile* profile) {
551 return static_cast<FileBrowserEventRouter*>(
552 GetInstance()->GetServiceForProfile(profile, true));
553 }
554
555 // static
556 FileBrowserEventRouterFactory*
557 FileBrowserEventRouterFactory::GetInstance() {
558 return Singleton<FileBrowserEventRouterFactory>::get();
559 }
560
561 FileBrowserEventRouterFactory::FileBrowserEventRouterFactory()
562 : ProfileKeyedServiceFactory("FileBrowserEventRouter",
563 ProfileDependencyManager::GetInstance()) {
564 DependsOn(GDataFileSystemFactory::GetInstance());
565 }
566
567 FileBrowserEventRouterFactory::~FileBrowserEventRouterFactory() {
568 }
569
570 ProfileKeyedService* FileBrowserEventRouterFactory::BuildServiceInstanceFor(
571 Profile* profile) const {
572 return new FileBrowserEventRouter(profile);
573 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698