| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/json/json_writer.h" | 7 #include "base/json/json_writer.h" |
| 8 #include "base/memory/singleton.h" | 8 #include "base/memory/singleton.h" |
| 9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 DictionaryValue* result = new DictionaryValue(); | 48 DictionaryValue* result = new DictionaryValue(); |
| 49 result->SetString("mountPath", disk->mount_path()); | 49 result->SetString("mountPath", disk->mount_path()); |
| 50 result->SetString("devicePath", disk->device_path()); | 50 result->SetString("devicePath", disk->device_path()); |
| 51 result->SetString("label", disk->device_label()); | 51 result->SetString("label", disk->device_label()); |
| 52 result->SetString("deviceType", DeviceTypeToString(disk->device_type())); | 52 result->SetString("deviceType", DeviceTypeToString(disk->device_type())); |
| 53 result->SetInteger("totalSizeKB", disk->total_size() / 1024); | 53 result->SetInteger("totalSizeKB", disk->total_size() / 1024); |
| 54 result->SetBoolean("readOnly", disk->is_read_only()); | 54 result->SetBoolean("readOnly", disk->is_read_only()); |
| 55 return result; | 55 return result; |
| 56 } | 56 } |
| 57 | 57 |
| 58 const char* MountTypeToString(chromeos::MountType type) { |
| 59 switch (type) { |
| 60 case chromeos::MOUNT_TYPE_DEVICE: |
| 61 return "device"; |
| 62 case chromeos::MOUNT_TYPE_ARCHIVE: |
| 63 return "file"; |
| 64 case chromeos::MOUNT_TYPE_NETWORK_STORAGE: |
| 65 return "network"; |
| 66 case chromeos::MOUNT_TYPE_INVALID: |
| 67 return "invalid"; |
| 68 default: |
| 69 NOTREACHED(); |
| 70 } |
| 71 return ""; |
| 72 } |
| 73 |
| 74 const char* MountErrorToString(chromeos::MountError error) { |
| 75 switch (error) { |
| 76 case chromeos::MOUNT_ERROR_NONE: |
| 77 return "success"; |
| 78 case chromeos::MOUNT_ERROR_UNKNOWN: |
| 79 return "unknown"; |
| 80 case chromeos::MOUNT_ERROR_INTERNAL: |
| 81 return "internal"; |
| 82 case chromeos::MOUNT_ERROR_UNKNOWN_FILESYSTEM: |
| 83 return "unknown_filesystem"; |
| 84 case chromeos::MOUNT_ERROR_UNSUPORTED_FILESYSTEM: |
| 85 return "unsuported_filesystem"; |
| 86 case chromeos::MOUNT_ERROR_INVALID_ARCHIVE: |
| 87 return "invalid_archive"; |
| 88 case chromeos::MOUNT_ERROR_LIBRARY_NOT_LOADED: |
| 89 return "libcros_missing"; |
| 90 default: |
| 91 NOTREACHED(); |
| 92 } |
| 93 return ""; |
| 94 } |
| 95 |
| 58 ExtensionFileBrowserEventRouter::ExtensionFileBrowserEventRouter() | 96 ExtensionFileBrowserEventRouter::ExtensionFileBrowserEventRouter() |
| 59 : delegate_(new ExtensionFileBrowserEventRouter::FileWatcherDelegate()), | 97 : delegate_(new ExtensionFileBrowserEventRouter::FileWatcherDelegate()), |
| 60 profile_(NULL) { | 98 profile_(NULL) { |
| 61 } | 99 } |
| 62 | 100 |
| 63 ExtensionFileBrowserEventRouter::~ExtensionFileBrowserEventRouter() { | 101 ExtensionFileBrowserEventRouter::~ExtensionFileBrowserEventRouter() { |
| 64 DCHECK(file_watchers_.empty()); | 102 DCHECK(file_watchers_.empty()); |
| 65 STLDeleteValues(&file_watchers_); | 103 STLDeleteValues(&file_watchers_); |
| 66 | 104 |
| 67 if (!profile_) | 105 if (!profile_) |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 const std::string& device_path) { | 199 const std::string& device_path) { |
| 162 if (event == chromeos::MOUNT_DEVICE_ADDED) { | 200 if (event == chromeos::MOUNT_DEVICE_ADDED) { |
| 163 OnDeviceAdded(device_path); | 201 OnDeviceAdded(device_path); |
| 164 } else if (event == chromeos::MOUNT_DEVICE_REMOVED) { | 202 } else if (event == chromeos::MOUNT_DEVICE_REMOVED) { |
| 165 OnDeviceRemoved(device_path); | 203 OnDeviceRemoved(device_path); |
| 166 } else if (event == chromeos::MOUNT_DEVICE_SCANNED) { | 204 } else if (event == chromeos::MOUNT_DEVICE_SCANNED) { |
| 167 OnDeviceScanned(device_path); | 205 OnDeviceScanned(device_path); |
| 168 } | 206 } |
| 169 } | 207 } |
| 170 | 208 |
| 209 void ExtensionFileBrowserEventRouter::MountCompleted( |
| 210 chromeos::MountError error_code, |
| 211 const std::string& source_path, |
| 212 chromeos::MountType type, |
| 213 const std::string& mount_path) { |
| 214 DispatchMountCompletedEvent(error_code, source_path, type, mount_path); |
| 215 } |
| 216 |
| 171 void ExtensionFileBrowserEventRouter::HandleFileWatchNotification( | 217 void ExtensionFileBrowserEventRouter::HandleFileWatchNotification( |
| 172 const FilePath& local_path, bool got_error) { | 218 const FilePath& local_path, bool got_error) { |
| 173 base::AutoLock lock(lock_); | 219 base::AutoLock lock(lock_); |
| 174 WatcherMap::const_iterator iter = file_watchers_.find(local_path); | 220 WatcherMap::const_iterator iter = file_watchers_.find(local_path); |
| 175 if (iter == file_watchers_.end()) { | 221 if (iter == file_watchers_.end()) { |
| 176 NOTREACHED(); | 222 NOTREACHED(); |
| 177 return; | 223 return; |
| 178 } | 224 } |
| 179 DispatchFolderChangeEvent(iter->second->virtual_path, got_error, | 225 DispatchFolderChangeEvent(iter->second->virtual_path, got_error, |
| 180 iter->second->extensions); | 226 iter->second->extensions); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 DictionaryValue* disk_info = DiskToDictionaryValue(disk); | 272 DictionaryValue* disk_info = DiskToDictionaryValue(disk); |
| 227 mount_info->Set("volumeInfo", disk_info); | 273 mount_info->Set("volumeInfo", disk_info); |
| 228 | 274 |
| 229 std::string args_json; | 275 std::string args_json; |
| 230 base::JSONWriter::Write(&args, false /* pretty_print */, &args_json); | 276 base::JSONWriter::Write(&args, false /* pretty_print */, &args_json); |
| 231 profile_->GetExtensionEventRouter()->DispatchEventToRenderers( | 277 profile_->GetExtensionEventRouter()->DispatchEventToRenderers( |
| 232 extension_event_names::kOnFileBrowserDiskChanged, args_json, NULL, | 278 extension_event_names::kOnFileBrowserDiskChanged, args_json, NULL, |
| 233 GURL()); | 279 GURL()); |
| 234 } | 280 } |
| 235 | 281 |
| 282 void ExtensionFileBrowserEventRouter::DispatchMountCompletedEvent( |
| 283 chromeos::MountError error_code, const std::string& source_path, |
| 284 chromeos::MountType type, const std::string& mount_path ) { |
| 285 if (!profile_ || type == chromeos::MOUNT_TYPE_INVALID) { |
| 286 NOTREACHED(); |
| 287 return; |
| 288 } |
| 289 |
| 290 ListValue args; |
| 291 DictionaryValue* mount_info = new DictionaryValue(); |
| 292 args.Append(mount_info); |
| 293 mount_info->SetString("sourcePath", source_path); |
| 294 mount_info->SetString("eventType", MountErrorToString(error_code)); |
| 295 mount_info->SetString("mountType", MountTypeToString(type)); |
| 296 mount_info->SetString("mountPath", mount_path); |
| 297 LOG(WARNING) << "MountCompleted" << error_code << source_path; |
| 298 std::string args_json; |
| 299 base::JSONWriter::Write(&args, false /* pretty_print */, &args_json); |
| 300 profile_->GetExtensionEventRouter()->DispatchEventToRenderers( |
| 301 extension_event_names::kOnFileBrowserMountCompleted, args_json, NULL, |
| 302 GURL()); |
| 303 } |
| 304 |
| 236 void ExtensionFileBrowserEventRouter::OnDiskAdded( | 305 void ExtensionFileBrowserEventRouter::OnDiskAdded( |
| 237 const chromeos::MountLibrary::Disk* disk) { | 306 const chromeos::MountLibrary::Disk* disk) { |
| 238 VLOG(1) << "Disk added: " << disk->device_path(); | 307 VLOG(1) << "Disk added: " << disk->device_path(); |
| 239 if (disk->device_path().empty()) { | 308 if (disk->device_path().empty()) { |
| 240 VLOG(1) << "Empty system path for " << disk->device_path(); | 309 VLOG(1) << "Empty system path for " << disk->device_path(); |
| 241 return; | 310 return; |
| 242 } | 311 } |
| 243 if (disk->is_parent()) { | 312 if (disk->is_parent()) { |
| 244 if (!disk->has_media()) { | 313 if (!disk->has_media()) { |
| 245 HideDeviceNotification(disk->system_path()); | 314 HideDeviceNotification(disk->system_path()); |
| 246 return; | 315 return; |
| 247 } | 316 } |
| 248 } | 317 } |
| 249 | 318 |
| 250 // If disk is not mounted yet, give it a try. | 319 // If disk is not mounted yet, give it a try. |
| 251 if (disk->mount_path().empty()) { | 320 if (disk->mount_path().empty()) { |
| 252 // Initiate disk mount operation. | 321 // Initiate disk mount operation. |
| 253 chromeos::MountLibrary* lib = | 322 chromeos::MountLibrary* lib = |
| 254 chromeos::CrosLibrary::Get()->GetMountLibrary(); | 323 chromeos::CrosLibrary::Get()->GetMountLibrary(); |
| 255 lib->MountPath(disk->device_path().c_str()); | 324 lib->MountPath(disk->device_path().c_str(), |
| 325 chromeos::MOUNT_TYPE_DEVICE, |
| 326 chromeos::MountPathOptions()); // Unused. |
| 256 } | 327 } |
| 257 } | 328 } |
| 258 | 329 |
| 259 void ExtensionFileBrowserEventRouter::OnDiskRemoved( | 330 void ExtensionFileBrowserEventRouter::OnDiskRemoved( |
| 260 const chromeos::MountLibrary::Disk* disk) { | 331 const chromeos::MountLibrary::Disk* disk) { |
| 261 VLOG(1) << "Disk removed: " << disk->device_path(); | 332 VLOG(1) << "Disk removed: " << disk->device_path(); |
| 262 HideDeviceNotification(disk->system_path()); | 333 HideDeviceNotification(disk->system_path()); |
| 263 MountPointMap::iterator iter = mounted_devices_.find(disk->device_path()); | 334 MountPointMap::iterator iter = mounted_devices_.find(disk->device_path()); |
| 264 if (iter == mounted_devices_.end()) | 335 if (iter == mounted_devices_.end()) |
| 265 return; | 336 return; |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 383 local_path, | 454 local_path, |
| 384 true)); // got_error | 455 true)); // got_error |
| 385 } | 456 } |
| 386 | 457 |
| 387 void | 458 void |
| 388 ExtensionFileBrowserEventRouter::FileWatcherDelegate::HandleFileWatchOnUIThread( | 459 ExtensionFileBrowserEventRouter::FileWatcherDelegate::HandleFileWatchOnUIThread( |
| 389 const FilePath& local_path, bool got_error) { | 460 const FilePath& local_path, bool got_error) { |
| 390 ExtensionFileBrowserEventRouter::GetInstance()->HandleFileWatchNotification( | 461 ExtensionFileBrowserEventRouter::GetInstance()->HandleFileWatchNotification( |
| 391 local_path, got_error); | 462 local_path, got_error); |
| 392 } | 463 } |
| OLD | NEW |