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