| 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* MountErrorToString(chromeos::MountError error) { |
| 59 switch (error) { |
| 60 case chromeos::MOUNT_ERROR_NONE: |
| 61 return "success"; |
| 62 case chromeos::MOUNT_ERROR_UNKNOWN: |
| 63 return "unknown"; |
| 64 case chromeos::MOUNT_ERROR_INTERNAL: |
| 65 return "internal"; |
| 66 case chromeos::MOUNT_ERROR_UNKNOWN_FILESYSTEM: |
| 67 return "unknown_filesystem"; |
| 68 case chromeos::MOUNT_ERROR_UNSUPORTED_FILESYSTEM: |
| 69 return "unsuported_filesystem"; |
| 70 case chromeos::MOUNT_ERROR_INVALID_ARCHIVE: |
| 71 return "invalid_archive"; |
| 72 case chromeos::MOUNT_ERROR_LIBRARY_NOT_LOADED: |
| 73 return "libcros_missing"; |
| 74 case chromeos::MOUNT_ERROR_PATH_UNMOUNTED: |
| 75 return "path_unmounted"; |
| 76 default: |
| 77 NOTREACHED(); |
| 78 } |
| 79 return ""; |
| 80 } |
| 81 |
| 58 ExtensionFileBrowserEventRouter::ExtensionFileBrowserEventRouter() | 82 ExtensionFileBrowserEventRouter::ExtensionFileBrowserEventRouter() |
| 59 : delegate_(new ExtensionFileBrowserEventRouter::FileWatcherDelegate()), | 83 : delegate_(new ExtensionFileBrowserEventRouter::FileWatcherDelegate()), |
| 60 profile_(NULL) { | 84 profile_(NULL) { |
| 61 } | 85 } |
| 62 | 86 |
| 63 ExtensionFileBrowserEventRouter::~ExtensionFileBrowserEventRouter() { | 87 ExtensionFileBrowserEventRouter::~ExtensionFileBrowserEventRouter() { |
| 64 DCHECK(file_watchers_.empty()); | 88 DCHECK(file_watchers_.empty()); |
| 65 STLDeleteValues(&file_watchers_); | 89 STLDeleteValues(&file_watchers_); |
| 66 | 90 |
| 67 if (!profile_) | 91 if (!profile_) |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 const std::string& device_path) { | 185 const std::string& device_path) { |
| 162 if (event == chromeos::MOUNT_DEVICE_ADDED) { | 186 if (event == chromeos::MOUNT_DEVICE_ADDED) { |
| 163 OnDeviceAdded(device_path); | 187 OnDeviceAdded(device_path); |
| 164 } else if (event == chromeos::MOUNT_DEVICE_REMOVED) { | 188 } else if (event == chromeos::MOUNT_DEVICE_REMOVED) { |
| 165 OnDeviceRemoved(device_path); | 189 OnDeviceRemoved(device_path); |
| 166 } else if (event == chromeos::MOUNT_DEVICE_SCANNED) { | 190 } else if (event == chromeos::MOUNT_DEVICE_SCANNED) { |
| 167 OnDeviceScanned(device_path); | 191 OnDeviceScanned(device_path); |
| 168 } | 192 } |
| 169 } | 193 } |
| 170 | 194 |
| 195 void ExtensionFileBrowserEventRouter::MountCompleted( |
| 196 chromeos::MountError error_code, |
| 197 const std::string& source_path, |
| 198 chromeos::MountType type, |
| 199 const std::string& mount_path) { |
| 200 DispatchMountCompletedEvent(error_code, source_path, type, mount_path); |
| 201 } |
| 202 |
| 171 void ExtensionFileBrowserEventRouter::HandleFileWatchNotification( | 203 void ExtensionFileBrowserEventRouter::HandleFileWatchNotification( |
| 172 const FilePath& local_path, bool got_error) { | 204 const FilePath& local_path, bool got_error) { |
| 173 base::AutoLock lock(lock_); | 205 base::AutoLock lock(lock_); |
| 174 WatcherMap::const_iterator iter = file_watchers_.find(local_path); | 206 WatcherMap::const_iterator iter = file_watchers_.find(local_path); |
| 175 if (iter == file_watchers_.end()) { | 207 if (iter == file_watchers_.end()) { |
| 176 NOTREACHED(); | 208 NOTREACHED(); |
| 177 return; | 209 return; |
| 178 } | 210 } |
| 179 DispatchFolderChangeEvent(iter->second->virtual_path, got_error, | 211 DispatchFolderChangeEvent(iter->second->virtual_path, got_error, |
| 180 iter->second->extensions); | 212 iter->second->extensions); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 DictionaryValue* disk_info = DiskToDictionaryValue(disk); | 258 DictionaryValue* disk_info = DiskToDictionaryValue(disk); |
| 227 mount_info->Set("volumeInfo", disk_info); | 259 mount_info->Set("volumeInfo", disk_info); |
| 228 | 260 |
| 229 std::string args_json; | 261 std::string args_json; |
| 230 base::JSONWriter::Write(&args, false /* pretty_print */, &args_json); | 262 base::JSONWriter::Write(&args, false /* pretty_print */, &args_json); |
| 231 profile_->GetExtensionEventRouter()->DispatchEventToRenderers( | 263 profile_->GetExtensionEventRouter()->DispatchEventToRenderers( |
| 232 extension_event_names::kOnFileBrowserDiskChanged, args_json, NULL, | 264 extension_event_names::kOnFileBrowserDiskChanged, args_json, NULL, |
| 233 GURL()); | 265 GURL()); |
| 234 } | 266 } |
| 235 | 267 |
| 268 void ExtensionFileBrowserEventRouter::DispatchMountCompletedEvent( |
| 269 chromeos::MountError error_code, const std::string& source_path, |
| 270 chromeos::MountType type, const std::string& mount_path ) { |
| 271 if (!profile_ || type == chromeos::MOUNT_TYPE_INVALID) { |
| 272 NOTREACHED(); |
| 273 return; |
| 274 } |
| 275 |
| 276 ListValue args; |
| 277 DictionaryValue* mount_info = new DictionaryValue(); |
| 278 args.Append(mount_info); |
| 279 mount_info->SetString("sourcePath", source_path); |
| 280 mount_info->SetString("eventType", MountErrorToString(error_code)); |
| 281 chromeos::MountLibrary* lib = chromeos::CrosLibrary::Get()->GetMountLibrary(); |
| 282 mount_info->SetString("mountType", lib->MountTypeToString(type)); |
| 283 mount_info->SetString("mountPath", mount_path); |
| 284 |
| 285 std::string args_json; |
| 286 base::JSONWriter::Write(&args, false /* pretty_print */, &args_json); |
| 287 profile_->GetExtensionEventRouter()->DispatchEventToRenderers( |
| 288 extension_event_names::kOnFileBrowserMountCompleted, args_json, NULL, |
| 289 GURL()); |
| 290 } |
| 291 |
| 236 void ExtensionFileBrowserEventRouter::OnDiskAdded( | 292 void ExtensionFileBrowserEventRouter::OnDiskAdded( |
| 237 const chromeos::MountLibrary::Disk* disk) { | 293 const chromeos::MountLibrary::Disk* disk) { |
| 238 VLOG(1) << "Disk added: " << disk->device_path(); | 294 VLOG(1) << "Disk added: " << disk->device_path(); |
| 239 if (disk->device_path().empty()) { | 295 if (disk->device_path().empty()) { |
| 240 VLOG(1) << "Empty system path for " << disk->device_path(); | 296 VLOG(1) << "Empty system path for " << disk->device_path(); |
| 241 return; | 297 return; |
| 242 } | 298 } |
| 243 if (disk->is_parent()) { | 299 if (disk->is_parent()) { |
| 244 if (!disk->has_media()) { | 300 if (!disk->has_media()) { |
| 245 HideDeviceNotification(disk->system_path()); | 301 HideDeviceNotification(disk->system_path()); |
| 246 return; | 302 return; |
| 247 } | 303 } |
| 248 } | 304 } |
| 249 | 305 |
| 250 // If disk is not mounted yet, give it a try. | 306 // If disk is not mounted yet, give it a try. |
| 251 if (disk->mount_path().empty()) { | 307 if (disk->mount_path().empty()) { |
| 252 // Initiate disk mount operation. | 308 // Initiate disk mount operation. |
| 253 chromeos::MountLibrary* lib = | 309 chromeos::MountLibrary* lib = |
| 254 chromeos::CrosLibrary::Get()->GetMountLibrary(); | 310 chromeos::CrosLibrary::Get()->GetMountLibrary(); |
| 255 lib->MountPath(disk->device_path().c_str()); | 311 lib->MountPath(disk->device_path().c_str(), |
| 312 chromeos::MOUNT_TYPE_DEVICE, |
| 313 chromeos::MountPathOptions()); // Unused. |
| 256 } | 314 } |
| 257 } | 315 } |
| 258 | 316 |
| 259 void ExtensionFileBrowserEventRouter::OnDiskRemoved( | 317 void ExtensionFileBrowserEventRouter::OnDiskRemoved( |
| 260 const chromeos::MountLibrary::Disk* disk) { | 318 const chromeos::MountLibrary::Disk* disk) { |
| 261 VLOG(1) << "Disk removed: " << disk->device_path(); | 319 VLOG(1) << "Disk removed: " << disk->device_path(); |
| 262 HideDeviceNotification(disk->system_path()); | 320 HideDeviceNotification(disk->system_path()); |
| 263 MountPointMap::iterator iter = mounted_devices_.find(disk->device_path()); | 321 MountPointMap::iterator iter = mounted_devices_.find(disk->device_path()); |
| 264 if (iter == mounted_devices_.end()) | 322 if (iter == mounted_devices_.end()) |
| 265 return; | 323 return; |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 383 local_path, | 441 local_path, |
| 384 true)); // got_error | 442 true)); // got_error |
| 385 } | 443 } |
| 386 | 444 |
| 387 void | 445 void |
| 388 ExtensionFileBrowserEventRouter::FileWatcherDelegate::HandleFileWatchOnUIThread( | 446 ExtensionFileBrowserEventRouter::FileWatcherDelegate::HandleFileWatchOnUIThread( |
| 389 const FilePath& local_path, bool got_error) { | 447 const FilePath& local_path, bool got_error) { |
| 390 ExtensionFileBrowserEventRouter::GetInstance()->HandleFileWatchNotification( | 448 ExtensionFileBrowserEventRouter::GetInstance()->HandleFileWatchNotification( |
| 391 local_path, got_error); | 449 local_path, got_error); |
| 392 } | 450 } |
| OLD | NEW |