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