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/bind.h" | 7 #include "base/bind.h" |
8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
9 #include "base/memory/singleton.h" | 9 #include "base/memory/singleton.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
| 12 #include "base/utf_string_conversions.h" |
12 #include "base/values.h" | 13 #include "base/values.h" |
13 #include "chrome/browser/chromeos/cros/cros_library.h" | 14 #include "chrome/browser/chromeos/cros/cros_library.h" |
14 #include "chrome/browser/chromeos/login/user_manager.h" | 15 #include "chrome/browser/chromeos/login/user_manager.h" |
15 #include "chrome/browser/chromeos/notifications/system_notification.h" | 16 #include "chrome/browser/chromeos/notifications/system_notification.h" |
16 #include "chrome/browser/extensions/extension_event_names.h" | 17 #include "chrome/browser/extensions/extension_event_names.h" |
17 #include "chrome/browser/extensions/extension_event_router.h" | 18 #include "chrome/browser/extensions/extension_event_router.h" |
18 #include "chrome/browser/extensions/extension_service.h" | 19 #include "chrome/browser/extensions/extension_service.h" |
19 #include "chrome/browser/extensions/file_manager_util.h" | 20 #include "chrome/browser/extensions/file_manager_util.h" |
20 #include "chrome/browser/profiles/profile.h" | 21 #include "chrome/browser/profiles/profile.h" |
21 #include "content/browser/browser_thread.h" | 22 #include "content/browser/browser_thread.h" |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 } | 162 } |
162 } | 163 } |
163 | 164 |
164 void ExtensionFileBrowserEventRouter::DiskChanged( | 165 void ExtensionFileBrowserEventRouter::DiskChanged( |
165 chromeos::MountLibraryEventType event, | 166 chromeos::MountLibraryEventType event, |
166 const chromeos::MountLibrary::Disk* disk) { | 167 const chromeos::MountLibrary::Disk* disk) { |
167 if (event == chromeos::MOUNT_DISK_ADDED) { | 168 if (event == chromeos::MOUNT_DISK_ADDED) { |
168 OnDiskAdded(disk); | 169 OnDiskAdded(disk); |
169 } else if (event == chromeos::MOUNT_DISK_REMOVED) { | 170 } else if (event == chromeos::MOUNT_DISK_REMOVED) { |
170 OnDiskRemoved(disk); | 171 OnDiskRemoved(disk); |
171 } else if (event == chromeos::MOUNT_DISK_CHANGED) { | |
172 OnDiskChanged(disk); | |
173 } | 172 } |
174 } | 173 } |
175 | 174 |
176 void ExtensionFileBrowserEventRouter::DeviceChanged( | 175 void ExtensionFileBrowserEventRouter::DeviceChanged( |
177 chromeos::MountLibraryEventType event, | 176 chromeos::MountLibraryEventType event, |
178 const std::string& device_path) { | 177 const std::string& device_path) { |
179 if (event == chromeos::MOUNT_DEVICE_ADDED) { | 178 if (event == chromeos::MOUNT_DEVICE_ADDED) { |
180 OnDeviceAdded(device_path); | 179 OnDeviceAdded(device_path); |
181 } else if (event == chromeos::MOUNT_DEVICE_REMOVED) { | 180 } else if (event == chromeos::MOUNT_DEVICE_REMOVED) { |
182 OnDeviceRemoved(device_path); | 181 OnDeviceRemoved(device_path); |
183 } else if (event == chromeos::MOUNT_DEVICE_SCANNED) { | 182 } else if (event == chromeos::MOUNT_DEVICE_SCANNED) { |
184 OnDeviceScanned(device_path); | 183 OnDeviceScanned(device_path); |
185 } else if (event == chromeos::MOUNT_FORMATTING_STARTED) { | 184 } else if (event == chromeos::MOUNT_FORMATTING_STARTED) { |
186 OnFormattingStarted(device_path); | 185 OnFormattingStarted(device_path); |
187 } else if (event == chromeos::MOUNT_FORMATTING_FINISHED) { | 186 } else if (event == chromeos::MOUNT_FORMATTING_FINISHED) { |
188 OnFormattingFinished(device_path); | 187 OnFormattingFinished(device_path); |
189 } | 188 } |
190 } | 189 } |
191 void ExtensionFileBrowserEventRouter::MountCompleted( | 190 void ExtensionFileBrowserEventRouter::MountCompleted( |
192 chromeos::MountLibrary::MountEvent event_type, | 191 chromeos::MountLibrary::MountEvent event_type, |
193 chromeos::MountError error_code, | 192 chromeos::MountError error_code, |
194 const chromeos::MountLibrary::MountPointInfo& mount_info) { | 193 const chromeos::MountLibrary::MountPointInfo& mount_info) { |
| 194 if (mount_info.mount_type == chromeos::MOUNT_TYPE_DEVICE && |
| 195 event_type == chromeos::MountLibrary::MOUNTING) { |
| 196 chromeos::MountLibrary* mount_lib = |
| 197 chromeos::CrosLibrary::Get()->GetMountLibrary(); |
| 198 chromeos::MountLibrary::Disk* disk = |
| 199 mount_lib->disks().find(mount_info.source_path)->second; |
| 200 |
| 201 if (!error_code) { |
| 202 HideFileBrowserNotification("MOUNT", disk->system_path()); |
| 203 } else if (error_code == chromeos::MOUNT_ERROR_UNSUPORTED_FILESYSTEM) { |
| 204 HideFileBrowserNotification("MOUNT", disk->system_path()); |
| 205 if (!disk->drive_label().empty()) { |
| 206 ShowFileBrowserNotification("MOUNT", disk->system_path(), |
| 207 IDR_PAGEINFO_INFO, |
| 208 l10n_util::GetStringUTF16(IDS_REMOVABLE_DEVICE_DETECTION_TITLE), |
| 209 l10n_util::GetStringFUTF16(IDS_DEVICE_UNSUPPORTED_MESSAGE, |
| 210 ASCIIToUTF16(disk->drive_label()))); |
| 211 } else { |
| 212 ShowFileBrowserNotification("MOUNT", disk->system_path(), |
| 213 IDR_PAGEINFO_INFO, |
| 214 l10n_util::GetStringUTF16(IDS_REMOVABLE_DEVICE_DETECTION_TITLE), |
| 215 l10n_util::GetStringUTF16(IDS_DEVICE_UNSUPPORTED_DEFAULT_MESSAGE)); |
| 216 } |
| 217 } |
| 218 } |
| 219 |
195 DispatchMountCompletedEvent(event_type, error_code, mount_info); | 220 DispatchMountCompletedEvent(event_type, error_code, mount_info); |
196 } | 221 } |
197 | 222 |
198 void ExtensionFileBrowserEventRouter::HandleFileWatchNotification( | 223 void ExtensionFileBrowserEventRouter::HandleFileWatchNotification( |
199 const FilePath& local_path, bool got_error) { | 224 const FilePath& local_path, bool got_error) { |
200 base::AutoLock lock(lock_); | 225 base::AutoLock lock(lock_); |
201 WatcherMap::const_iterator iter = file_watchers_.find(local_path); | 226 WatcherMap::const_iterator iter = file_watchers_.find(local_path); |
202 if (iter == file_watchers_.end()) { | 227 if (iter == file_watchers_.end()) { |
203 NOTREACHED(); | 228 NOTREACHED(); |
204 return; | 229 return; |
(...skipping 26 matching lines...) Expand all Loading... |
231 | 256 |
232 std::string args_json; | 257 std::string args_json; |
233 base::JSONWriter::Write(&args, false /* pretty_print */, &args_json); | 258 base::JSONWriter::Write(&args, false /* pretty_print */, &args_json); |
234 | 259 |
235 profile_->GetExtensionEventRouter()->DispatchEventToExtension( | 260 profile_->GetExtensionEventRouter()->DispatchEventToExtension( |
236 *iter, extension_event_names::kOnFileChanged, args_json, | 261 *iter, extension_event_names::kOnFileChanged, args_json, |
237 NULL, GURL()); | 262 NULL, GURL()); |
238 } | 263 } |
239 } | 264 } |
240 | 265 |
241 void ExtensionFileBrowserEventRouter::DispatchMountEvent( | 266 void ExtensionFileBrowserEventRouter::DispatchDiskEvent( |
242 const chromeos::MountLibrary::Disk* disk, bool added) { | 267 const chromeos::MountLibrary::Disk* disk, bool added) { |
243 if (!profile_) { | 268 if (!profile_) { |
244 NOTREACHED(); | 269 NOTREACHED(); |
245 return; | 270 return; |
246 } | 271 } |
247 | 272 |
248 ListValue args; | 273 ListValue args; |
249 DictionaryValue* mount_info = new DictionaryValue(); | 274 DictionaryValue* mount_info = new DictionaryValue(); |
250 args.Append(mount_info); | 275 args.Append(mount_info); |
251 mount_info->SetString("eventType", | 276 mount_info->SetString("eventType", |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
286 if (FileManagerUtil::ConvertFileToFileSystemUrl(profile_, | 311 if (FileManagerUtil::ConvertFileToFileSystemUrl(profile_, |
287 FilePath(mount_info.source_path), | 312 FilePath(mount_info.source_path), |
288 FileManagerUtil::GetFileBrowserExtensionUrl().GetOrigin(), | 313 FileManagerUtil::GetFileBrowserExtensionUrl().GetOrigin(), |
289 &source_url)) { | 314 &source_url)) { |
290 mount_info_value->SetString("sourceUrl", source_url.spec()); | 315 mount_info_value->SetString("sourceUrl", source_url.spec()); |
291 } | 316 } |
292 } else { | 317 } else { |
293 mount_info_value->SetString("sourceUrl", mount_info.source_path); | 318 mount_info_value->SetString("sourceUrl", mount_info.source_path); |
294 } | 319 } |
295 | 320 |
| 321 FilePath relative_mount_path; |
| 322 bool relative_mount_path_set = false; |
296 // If there were no error, add mountPath to the event. | 323 // If there were no error, add mountPath to the event. |
297 if (error_code == chromeos::MOUNT_ERROR_NONE) { | 324 if (error_code == chromeos::MOUNT_ERROR_NONE) { |
298 FilePath relative_mount_path; | |
299 // Convert mount point path to relative path with the external file system | 325 // Convert mount point path to relative path with the external file system |
300 // exposed within File API. | 326 // exposed within File API. |
301 if (FileManagerUtil::ConvertFileToRelativeFileSystemPath(profile_, | 327 if (FileManagerUtil::ConvertFileToRelativeFileSystemPath(profile_, |
302 FilePath(mount_info.mount_path), | 328 FilePath(mount_info.mount_path), |
303 &relative_mount_path)) { | 329 &relative_mount_path)) { |
304 mount_info_value->SetString("mountPath", relative_mount_path.value()); | 330 mount_info_value->SetString("mountPath", |
| 331 "/" + relative_mount_path.value()); |
| 332 relative_mount_path_set = true; |
305 } | 333 } |
306 } | 334 } |
307 | 335 |
308 std::string args_json; | 336 std::string args_json; |
309 base::JSONWriter::Write(&args, false /* pretty_print */, &args_json); | 337 base::JSONWriter::Write(&args, false /* pretty_print */, &args_json); |
310 profile_->GetExtensionEventRouter()->DispatchEventToRenderers( | 338 profile_->GetExtensionEventRouter()->DispatchEventToRenderers( |
311 extension_event_names::kOnFileBrowserMountCompleted, args_json, NULL, | 339 extension_event_names::kOnFileBrowserMountCompleted, args_json, NULL, |
312 GURL()); | 340 GURL()); |
| 341 |
| 342 if (relative_mount_path_set && |
| 343 mount_info.mount_type == chromeos::MOUNT_TYPE_DEVICE && |
| 344 event == chromeos::MountLibrary::MOUNTING) { |
| 345 FileManagerUtil::ShowFullTabUrl(profile_, relative_mount_path); |
| 346 } |
313 } | 347 } |
314 | 348 |
315 void ExtensionFileBrowserEventRouter::OnDiskAdded( | 349 void ExtensionFileBrowserEventRouter::OnDiskAdded( |
316 const chromeos::MountLibrary::Disk* disk) { | 350 const chromeos::MountLibrary::Disk* disk) { |
317 VLOG(1) << "Disk added: " << disk->device_path(); | 351 VLOG(1) << "Disk added: " << disk->device_path(); |
318 if (disk->device_path().empty()) { | 352 if (disk->device_path().empty()) { |
319 VLOG(1) << "Empty system path for " << disk->device_path(); | 353 VLOG(1) << "Empty system path for " << disk->device_path(); |
320 return; | 354 return; |
321 } | 355 } |
322 if (disk->is_parent()) { | |
323 if (!disk->has_media()) { | |
324 HideFileBrowserNotification("MOUNT", disk->system_path()); | |
325 return; | |
326 } | |
327 } | |
328 | 356 |
329 // If disk is not mounted yet, give it a try. | 357 // If disk is not mounted yet, give it a try. |
330 if (disk->mount_path().empty()) { | 358 if (disk->mount_path().empty()) { |
331 // Initiate disk mount operation. | 359 // Initiate disk mount operation. |
332 chromeos::MountLibrary* lib = | 360 chromeos::MountLibrary* lib = |
333 chromeos::CrosLibrary::Get()->GetMountLibrary(); | 361 chromeos::CrosLibrary::Get()->GetMountLibrary(); |
334 lib->MountPath(disk->device_path().c_str(), | 362 lib->MountPath(disk->device_path().c_str(), |
335 chromeos::MOUNT_TYPE_DEVICE, | 363 chromeos::MOUNT_TYPE_DEVICE, |
336 chromeos::MountPathOptions()); // Unused. | 364 chromeos::MountPathOptions()); // Unused. |
337 } | 365 } |
338 DispatchMountEvent(disk, true); | 366 DispatchDiskEvent(disk, true); |
339 } | 367 } |
340 | 368 |
341 void ExtensionFileBrowserEventRouter::OnDiskRemoved( | 369 void ExtensionFileBrowserEventRouter::OnDiskRemoved( |
342 const chromeos::MountLibrary::Disk* disk) { | 370 const chromeos::MountLibrary::Disk* disk) { |
343 VLOG(1) << "Disk removed: " << disk->device_path(); | 371 VLOG(1) << "Disk removed: " << disk->device_path(); |
344 HideFileBrowserNotification("MOUNT", disk->system_path()); | 372 HideFileBrowserNotification("MOUNT", disk->system_path()); |
345 MountPointMap::iterator iter = mounted_devices_.find(disk->device_path()); | |
346 if (iter == mounted_devices_.end()) | |
347 return; | |
348 | 373 |
349 chromeos::MountLibrary* lib = | |
350 chromeos::CrosLibrary::Get()->GetMountLibrary(); | |
351 // TODO(zelidrag): This for some reason does not work as advertized. | |
352 // we might need to clean up mount directory on FILE thread here as well. | |
353 lib->UnmountPath(disk->device_path().c_str()); | |
354 DispatchMountEvent(disk, false); | |
355 mounted_devices_.erase(iter); | |
356 } | |
357 | |
358 void ExtensionFileBrowserEventRouter::OnDiskChanged( | |
359 const chromeos::MountLibrary::Disk* disk) { | |
360 VLOG(1) << "Disk changed : " << disk->device_path(); | |
361 if (!disk->mount_path().empty()) { | 374 if (!disk->mount_path().empty()) { |
362 HideFileBrowserNotification("MOUNT", disk->system_path()); | 375 chromeos::MountLibrary* lib = |
363 // Remember this mount point. | 376 chromeos::CrosLibrary::Get()->GetMountLibrary(); |
364 if (mounted_devices_.find(disk->device_path()) == mounted_devices_.end()) { | 377 lib->UnmountPath(disk->mount_path().c_str()); |
365 mounted_devices_.insert( | |
366 std::pair<std::string, std::string>(disk->device_path(), | |
367 disk->mount_path())); | |
368 DispatchMountEvent(disk, true); | |
369 HideFileBrowserNotification("MOUNT", disk->system_path()); | |
370 FileManagerUtil::ShowFullTabUrl(profile_, FilePath(disk->mount_path())); | |
371 } | |
372 } | 378 } |
| 379 DispatchDiskEvent(disk, false); |
373 } | 380 } |
374 | 381 |
375 void ExtensionFileBrowserEventRouter::OnDeviceAdded( | 382 void ExtensionFileBrowserEventRouter::OnDeviceAdded( |
376 const std::string& device_path) { | 383 const std::string& device_path) { |
377 VLOG(1) << "Device added : " << device_path; | 384 VLOG(1) << "Device added : " << device_path; |
378 // TODO(zelidrag): Find better icon here. | 385 // TODO(zelidrag): Find better icon here. |
379 ShowFileBrowserNotification("MOUNT", device_path, IDR_PAGEINFO_INFO, | 386 ShowFileBrowserNotification("MOUNT", device_path, IDR_PAGEINFO_INFO, |
380 l10n_util::GetStringUTF16(IDS_REMOVABLE_DEVICE_DETECTION_TITLE), | 387 l10n_util::GetStringUTF16(IDS_REMOVABLE_DEVICE_DETECTION_TITLE), |
381 l10n_util::GetStringUTF16(IDS_REMOVABLE_DEVICE_SCANNING_MESSAGE)); | 388 l10n_util::GetStringUTF16(IDS_REMOVABLE_DEVICE_SCANNING_MESSAGE)); |
382 } | 389 } |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
505 &FileWatcherDelegate::HandleFileWatchOnUIThread, | 512 &FileWatcherDelegate::HandleFileWatchOnUIThread, |
506 local_path, | 513 local_path, |
507 true)); // got_error | 514 true)); // got_error |
508 } | 515 } |
509 | 516 |
510 void | 517 void |
511 ExtensionFileBrowserEventRouter::FileWatcherDelegate::HandleFileWatchOnUIThread( | 518 ExtensionFileBrowserEventRouter::FileWatcherDelegate::HandleFileWatchOnUIThread( |
512 const FilePath& local_path, bool got_error) { | 519 const FilePath& local_path, bool got_error) { |
513 router_->HandleFileWatchNotification(local_path, got_error); | 520 router_->HandleFileWatchNotification(local_path, got_error); |
514 } | 521 } |
OLD | NEW |