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/media_galleries/media_file_system_registry.h" | 5 #include "chrome/browser/media_galleries/media_file_system_registry.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
13 #include "base/prefs/pref_service.h" | 13 #include "base/prefs/pref_service.h" |
14 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
15 #include "chrome/browser/extensions/extension_service.h" | 15 #include "chrome/browser/extensions/extension_service.h" |
| 16 #include "chrome/browser/media_galleries/fileapi/media_file_system_backend.h" |
16 #include "chrome/browser/media_galleries/fileapi/mtp_device_map_service.h" | 17 #include "chrome/browser/media_galleries/fileapi/mtp_device_map_service.h" |
17 #include "chrome/browser/media_galleries/imported_media_gallery_registry.h" | 18 #include "chrome/browser/media_galleries/imported_media_gallery_registry.h" |
18 #include "chrome/browser/media_galleries/media_file_system_context.h" | 19 #include "chrome/browser/media_galleries/media_file_system_context.h" |
19 #include "chrome/browser/media_galleries/media_galleries_dialog_controller.h" | 20 #include "chrome/browser/media_galleries/media_galleries_dialog_controller.h" |
20 #include "chrome/browser/media_galleries/media_galleries_histograms.h" | 21 #include "chrome/browser/media_galleries/media_galleries_histograms.h" |
21 #include "chrome/browser/media_galleries/media_galleries_preferences_factory.h" | 22 #include "chrome/browser/media_galleries/media_galleries_preferences_factory.h" |
22 #include "chrome/browser/media_galleries/media_scan_manager.h" | 23 #include "chrome/browser/media_galleries/media_scan_manager.h" |
23 #include "chrome/browser/profiles/profile.h" | 24 #include "chrome/browser/profiles/profile.h" |
24 #include "chrome/common/chrome_paths.h" | 25 #include "chrome/common/chrome_paths.h" |
25 #include "chrome/common/extensions/extension_constants.h" | 26 #include "chrome/common/extensions/extension_constants.h" |
26 #include "chrome/common/pref_names.h" | 27 #include "chrome/common/pref_names.h" |
27 #include "components/storage_monitor/media_storage_util.h" | 28 #include "components/storage_monitor/media_storage_util.h" |
28 #include "components/storage_monitor/storage_monitor.h" | 29 #include "components/storage_monitor/storage_monitor.h" |
29 #include "content/public/browser/browser_thread.h" | 30 #include "content/public/browser/browser_thread.h" |
30 #include "content/public/browser/navigation_details.h" | 31 #include "content/public/browser/navigation_details.h" |
31 #include "content/public/browser/render_process_host.h" | 32 #include "content/public/browser/render_process_host.h" |
32 #include "content/public/browser/render_process_host_observer.h" | 33 #include "content/public/browser/render_process_host_observer.h" |
33 #include "content/public/browser/render_view_host.h" | 34 #include "content/public/browser/render_view_host.h" |
34 #include "content/public/browser/web_contents.h" | 35 #include "content/public/browser/web_contents.h" |
35 #include "content/public/browser/web_contents_observer.h" | 36 #include "content/public/browser/web_contents_observer.h" |
36 #include "extensions/browser/extension_system.h" | 37 #include "extensions/browser/extension_system.h" |
37 #include "extensions/common/extension.h" | 38 #include "extensions/common/extension.h" |
38 #include "extensions/common/extension_set.h" | 39 #include "extensions/common/extension_set.h" |
39 #include "webkit/browser/fileapi/isolated_context.h" | 40 #include "webkit/browser/fileapi/external_mount_points.h" |
| 41 #include "webkit/common/fileapi/file_system_mount_option.h" |
40 #include "webkit/common/fileapi/file_system_types.h" | 42 #include "webkit/common/fileapi/file_system_types.h" |
41 | 43 |
42 using content::BrowserThread; | 44 using content::BrowserThread; |
43 using content::NavigationController; | 45 using content::NavigationController; |
44 using content::RenderProcessHost; | 46 using content::RenderProcessHost; |
45 using content::WebContents; | 47 using content::WebContents; |
46 using fileapi::IsolatedContext; | 48 using fileapi::ExternalMountPoints; |
47 using storage_monitor::MediaStorageUtil; | 49 using storage_monitor::MediaStorageUtil; |
48 using storage_monitor::StorageInfo; | 50 using storage_monitor::StorageInfo; |
49 using storage_monitor::StorageMonitor; | 51 using storage_monitor::StorageMonitor; |
50 | 52 |
51 namespace { | 53 namespace { |
52 | 54 |
53 struct InvalidatedGalleriesInfo { | 55 struct InvalidatedGalleriesInfo { |
54 std::set<ExtensionGalleriesHost*> extension_hosts; | 56 std::set<ExtensionGalleriesHost*> extension_hosts; |
55 std::set<MediaGalleryPrefId> pref_ids; | 57 std::set<MediaGalleryPrefId> pref_ids; |
56 }; | 58 }; |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 | 256 |
255 // The main owner of this class is | 257 // The main owner of this class is |
256 // |MediaFileSystemRegistry::extension_hosts_map_|, but a callback may | 258 // |MediaFileSystemRegistry::extension_hosts_map_|, but a callback may |
257 // temporarily hold a reference. | 259 // temporarily hold a reference. |
258 class ExtensionGalleriesHost | 260 class ExtensionGalleriesHost |
259 : public base::RefCountedThreadSafe<ExtensionGalleriesHost> { | 261 : public base::RefCountedThreadSafe<ExtensionGalleriesHost> { |
260 public: | 262 public: |
261 // |no_references_callback| is called when the last RenderViewHost reference | 263 // |no_references_callback| is called when the last RenderViewHost reference |
262 // goes away. RenderViewHost references are added through ReferenceFromRVH(). | 264 // goes away. RenderViewHost references are added through ReferenceFromRVH(). |
263 ExtensionGalleriesHost(MediaFileSystemContext* file_system_context, | 265 ExtensionGalleriesHost(MediaFileSystemContext* file_system_context, |
| 266 const base::FilePath& profile_path, |
| 267 const std::string& extension_id, |
264 const base::Closure& no_references_callback) | 268 const base::Closure& no_references_callback) |
265 : file_system_context_(file_system_context), | 269 : file_system_context_(file_system_context), |
| 270 profile_path_(profile_path), |
| 271 extension_id_(extension_id), |
266 no_references_callback_(no_references_callback), | 272 no_references_callback_(no_references_callback), |
267 rph_refs_(base::Bind(&ExtensionGalleriesHost::CleanUp, | 273 rph_refs_(base::Bind(&ExtensionGalleriesHost::CleanUp, |
268 base::Unretained(this))) { | 274 base::Unretained(this))) { |
269 } | 275 } |
270 | 276 |
271 // For each gallery in the list of permitted |galleries|, checks if the | 277 // For each gallery in the list of permitted |galleries|, checks if the |
272 // device is attached and if so looks up or creates a file system id and | 278 // device is attached and if so looks up or creates a file system name and |
273 // passes the information needed for the renderer to create those file | 279 // passes the information needed for the renderer to create those file |
274 // system objects to the |callback|. | 280 // system objects to the |callback|. |
275 void GetMediaFileSystems(const MediaGalleryPrefIdSet& galleries, | 281 void GetMediaFileSystems(const MediaGalleryPrefIdSet& galleries, |
276 const MediaGalleriesPrefInfoMap& galleries_info, | 282 const MediaGalleriesPrefInfoMap& galleries_info, |
277 const MediaFileSystemsCallback& callback) { | 283 const MediaFileSystemsCallback& callback) { |
278 // Extract all the device ids so we can make sure they are attached. | 284 // Extract all the device ids so we can make sure they are attached. |
279 MediaStorageUtil::DeviceIdSet* device_ids = | 285 MediaStorageUtil::DeviceIdSet* device_ids = |
280 new MediaStorageUtil::DeviceIdSet; | 286 new MediaStorageUtil::DeviceIdSet; |
281 for (std::set<MediaGalleryPrefId>::const_iterator id = galleries.begin(); | 287 for (std::set<MediaGalleryPrefId>::const_iterator id = galleries.begin(); |
282 id != galleries.end(); | 288 id != galleries.end(); |
283 ++id) { | 289 ++id) { |
284 device_ids->insert(galleries_info.find(*id)->second.device_id); | 290 device_ids->insert(galleries_info.find(*id)->second.device_id); |
285 } | 291 } |
286 MediaStorageUtil::FilterAttachedDevices(device_ids, base::Bind( | 292 MediaStorageUtil::FilterAttachedDevices(device_ids, base::Bind( |
287 &ExtensionGalleriesHost::GetMediaFileSystemsForAttachedDevices, this, | 293 &ExtensionGalleriesHost::GetMediaFileSystemsForAttachedDevices, this, |
288 base::Owned(device_ids), galleries, galleries_info, callback)); | 294 base::Owned(device_ids), galleries, galleries_info, callback)); |
289 } | 295 } |
290 | 296 |
| 297 // Checks if |gallery| is attached and if so, registers the file system and |
| 298 // then calls |callback| with the result. |
| 299 void RegisterMediaFileSystem( |
| 300 const MediaGalleryPrefInfo& gallery, |
| 301 const base::Callback<void(base::File::Error result)>& callback) { |
| 302 // Extract all the device ids so we can make sure they are attached. |
| 303 MediaStorageUtil::DeviceIdSet* device_ids = |
| 304 new MediaStorageUtil::DeviceIdSet; |
| 305 device_ids->insert(gallery.device_id); |
| 306 MediaStorageUtil::FilterAttachedDevices(device_ids, base::Bind( |
| 307 &ExtensionGalleriesHost::RegisterAttachedMediaFileSystem, this, |
| 308 base::Owned(device_ids), gallery, callback)); |
| 309 } |
| 310 |
291 // Revoke the file system for |id| if this extension has created one for |id|. | 311 // Revoke the file system for |id| if this extension has created one for |id|. |
292 void RevokeGalleryByPrefId(MediaGalleryPrefId id) { | 312 void RevokeGalleryByPrefId(MediaGalleryPrefId id) { |
293 PrefIdFsInfoMap::iterator gallery = pref_id_map_.find(id); | 313 PrefIdFsInfoMap::iterator gallery = pref_id_map_.find(id); |
294 if (gallery == pref_id_map_.end()) | 314 if (gallery == pref_id_map_.end()) |
295 return; | 315 return; |
296 | 316 |
297 file_system_context_->RevokeFileSystem(gallery->second.fsid); | 317 file_system_context_->RevokeFileSystem(gallery->second.fsid); |
298 pref_id_map_.erase(gallery); | 318 pref_id_map_.erase(gallery); |
299 | 319 |
300 if (pref_id_map_.empty()) { | 320 if (pref_id_map_.empty()) { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
350 pref_id_map_.find(pref_id); | 370 pref_id_map_.find(pref_id); |
351 if (existing_info != pref_id_map_.end()) { | 371 if (existing_info != pref_id_map_.end()) { |
352 result.push_back(existing_info->second); | 372 result.push_back(existing_info->second); |
353 continue; | 373 continue; |
354 } | 374 } |
355 | 375 |
356 base::FilePath path = gallery_info.AbsolutePath(); | 376 base::FilePath path = gallery_info.AbsolutePath(); |
357 if (!MediaStorageUtil::CanCreateFileSystem(device_id, path)) | 377 if (!MediaStorageUtil::CanCreateFileSystem(device_id, path)) |
358 continue; | 378 continue; |
359 | 379 |
360 std::string fsid = | 380 std::string fs_name = MediaFileSystemBackend::ConstructMountName( |
361 file_system_context_->RegisterFileSystem(device_id, path); | 381 profile_path_, extension_id_, pref_id); |
362 if (fsid.empty()) | 382 if (!file_system_context_->RegisterFileSystem(device_id, fs_name, path)) |
363 continue; | 383 continue; |
364 | 384 |
365 MediaFileSystemInfo new_entry( | 385 MediaFileSystemInfo new_entry( |
366 gallery_info.GetGalleryDisplayName(), | 386 gallery_info.GetGalleryDisplayName(), |
367 path, | 387 file_system_context_->GetRegisteredPath(fs_name), |
368 fsid, | 388 fs_name, |
369 pref_id, | 389 pref_id, |
370 GetTransientIdForRemovableDeviceId(device_id), | 390 GetTransientIdForRemovableDeviceId(device_id), |
371 StorageInfo::IsRemovableDevice(device_id), | 391 StorageInfo::IsRemovableDevice(device_id), |
372 StorageInfo::IsMediaDevice(device_id)); | 392 StorageInfo::IsMediaDevice(device_id)); |
373 result.push_back(new_entry); | 393 result.push_back(new_entry); |
374 pref_id_map_[pref_id] = new_entry; | 394 pref_id_map_[pref_id] = new_entry; |
375 } | 395 } |
376 | 396 |
377 if (result.size() == 0) { | 397 if (result.size() == 0) { |
378 rph_refs_.Reset(); | 398 rph_refs_.Reset(); |
379 CleanUp(); | 399 CleanUp(); |
380 } | 400 } |
381 | 401 |
382 DCHECK_EQ(pref_id_map_.size(), result.size()); | 402 DCHECK_EQ(pref_id_map_.size(), result.size()); |
383 callback.Run(result); | 403 callback.Run(result); |
384 } | 404 } |
385 | 405 |
| 406 void RegisterAttachedMediaFileSystem( |
| 407 const MediaStorageUtil::DeviceIdSet* attached_device, |
| 408 const MediaGalleryPrefInfo& gallery, |
| 409 const base::Callback<void(base::File::Error result)>& callback) { |
| 410 base::File::Error result = base::File::FILE_ERROR_NOT_FOUND; |
| 411 |
| 412 // If rph_refs is empty then we're actually in the middle of shutdown, and |
| 413 // Filter...() lagging which can invoke this method interleaved in the |
| 414 // destruction callback sequence and re-populate pref_id_map_. |
| 415 if (!attached_device->empty() && !rph_refs_.empty()) { |
| 416 std::string fs_name = MediaFileSystemBackend::ConstructMountName( |
| 417 profile_path_, extension_id_, gallery.pref_id); |
| 418 base::FilePath path = gallery.AbsolutePath(); |
| 419 const std::string& device_id = gallery.device_id; |
| 420 |
| 421 if (ContainsKey(pref_id_map_, gallery.pref_id)) { |
| 422 result = base::File::FILE_OK; |
| 423 } else if (MediaStorageUtil::CanCreateFileSystem(device_id, path) && |
| 424 file_system_context_->RegisterFileSystem(device_id, fs_name, |
| 425 path)) { |
| 426 result = base::File::FILE_OK; |
| 427 pref_id_map_[gallery.pref_id] = MediaFileSystemInfo( |
| 428 gallery.GetGalleryDisplayName(), |
| 429 file_system_context_->GetRegisteredPath(fs_name), |
| 430 fs_name, |
| 431 gallery.pref_id, |
| 432 GetTransientIdForRemovableDeviceId(device_id), |
| 433 StorageInfo::IsRemovableDevice(device_id), |
| 434 StorageInfo::IsMediaDevice(device_id)); |
| 435 } |
| 436 } |
| 437 |
| 438 if (pref_id_map_.empty()) { |
| 439 rph_refs_.Reset(); |
| 440 CleanUp(); |
| 441 } |
| 442 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 443 base::Bind(callback, result)); |
| 444 } |
| 445 |
386 std::string GetTransientIdForRemovableDeviceId(const std::string& device_id) { | 446 std::string GetTransientIdForRemovableDeviceId(const std::string& device_id) { |
387 if (!StorageInfo::IsRemovableDevice(device_id)) | 447 if (!StorageInfo::IsRemovableDevice(device_id)) |
388 return std::string(); | 448 return std::string(); |
389 | 449 |
390 return StorageMonitor::GetInstance()->GetTransientIdForDeviceId(device_id); | 450 return StorageMonitor::GetInstance()->GetTransientIdForDeviceId(device_id); |
391 } | 451 } |
392 | 452 |
393 void CleanUp() { | 453 void CleanUp() { |
394 DCHECK(rph_refs_.empty()); | 454 DCHECK(rph_refs_.empty()); |
395 for (PrefIdFsInfoMap::const_iterator it = pref_id_map_.begin(); | 455 for (PrefIdFsInfoMap::const_iterator it = pref_id_map_.begin(); |
396 it != pref_id_map_.end(); | 456 it != pref_id_map_.end(); |
397 ++it) { | 457 ++it) { |
398 file_system_context_->RevokeFileSystem(it->second.fsid); | 458 file_system_context_->RevokeFileSystem(it->second.fsid); |
399 } | 459 } |
400 pref_id_map_.clear(); | 460 pref_id_map_.clear(); |
401 | 461 |
402 no_references_callback_.Run(); | 462 no_references_callback_.Run(); |
403 } | 463 } |
404 | 464 |
405 // MediaFileSystemRegistry owns |this| and |file_system_context_|, so it's | 465 // MediaFileSystemRegistry owns |this| and |file_system_context_|, so it's |
406 // safe to store a raw pointer. | 466 // safe to store a raw pointer. |
407 MediaFileSystemContext* file_system_context_; | 467 MediaFileSystemContext* file_system_context_; |
408 | 468 |
| 469 // Path for the active profile. |
| 470 const base::FilePath profile_path_; |
| 471 |
| 472 // Id of the extension this host belongs to. |
| 473 const std::string extension_id_; |
| 474 |
409 // A callback to call when the last RVH reference goes away. | 475 // A callback to call when the last RVH reference goes away. |
410 base::Closure no_references_callback_; | 476 base::Closure no_references_callback_; |
411 | 477 |
412 // A map from the gallery preferences id to the file system information. | 478 // A map from the gallery preferences id to the file system information. |
413 PrefIdFsInfoMap pref_id_map_; | 479 PrefIdFsInfoMap pref_id_map_; |
414 | 480 |
415 // The set of render processes and web contents that may have references to | 481 // The set of render processes and web contents that may have references to |
416 // the file system ids this instance manages. | 482 // the file system ids this instance manages. |
417 RPHReferenceManager rph_refs_; | 483 RPHReferenceManager rph_refs_; |
418 | 484 |
(...skipping 14 matching lines...) Expand all Loading... |
433 Profile::FromBrowserContext(rvh->GetProcess()->GetBrowserContext()); | 499 Profile::FromBrowserContext(rvh->GetProcess()->GetBrowserContext()); |
434 MediaGalleriesPreferences* preferences = GetPreferences(profile); | 500 MediaGalleriesPreferences* preferences = GetPreferences(profile); |
435 MediaGalleryPrefIdSet galleries = | 501 MediaGalleryPrefIdSet galleries = |
436 preferences->GalleriesForExtension(*extension); | 502 preferences->GalleriesForExtension(*extension); |
437 | 503 |
438 if (galleries.empty()) { | 504 if (galleries.empty()) { |
439 callback.Run(std::vector<MediaFileSystemInfo>()); | 505 callback.Run(std::vector<MediaFileSystemInfo>()); |
440 return; | 506 return; |
441 } | 507 } |
442 | 508 |
443 ExtensionGalleriesHostMap::iterator extension_hosts = | 509 ExtensionGalleriesHost* extension_host = |
444 extension_hosts_map_.find(profile); | 510 GetExtensionGalleryHost(profile, preferences, extension->id()); |
445 if (extension_hosts->second.empty()) | |
446 preferences->AddGalleryChangeObserver(this); | |
447 | 511 |
448 ExtensionGalleriesHost* extension_host = | |
449 extension_hosts->second[extension->id()].get(); | |
450 if (!extension_host) { | |
451 extension_host = new ExtensionGalleriesHost( | |
452 file_system_context_.get(), | |
453 base::Bind(&MediaFileSystemRegistry::OnExtensionGalleriesHostEmpty, | |
454 base::Unretained(this), | |
455 profile, | |
456 extension->id())); | |
457 extension_hosts_map_[profile][extension->id()] = extension_host; | |
458 } | |
459 // This must come before the GetMediaFileSystems call to make sure the | 512 // This must come before the GetMediaFileSystems call to make sure the |
460 // RVH of the context is referenced before the filesystems are retrieved. | 513 // RVH of the context is referenced before the filesystems are retrieved. |
461 extension_host->ReferenceFromRVH(rvh); | 514 extension_host->ReferenceFromRVH(rvh); |
462 | 515 |
463 extension_host->GetMediaFileSystems(galleries, preferences->known_galleries(), | 516 extension_host->GetMediaFileSystems(galleries, preferences->known_galleries(), |
464 callback); | 517 callback); |
465 } | 518 } |
466 | 519 |
| 520 void MediaFileSystemRegistry::RegisterMediaFileSystemForExtension( |
| 521 const content::RenderViewHost* rvh, |
| 522 const extensions::Extension* extension, |
| 523 MediaGalleryPrefId pref_id, |
| 524 const base::Callback<void(base::File::Error result)>& callback) { |
| 525 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 526 DCHECK_NE(kInvalidMediaGalleryPrefId, pref_id); |
| 527 |
| 528 Profile* profile = |
| 529 Profile::FromBrowserContext(rvh->GetProcess()->GetBrowserContext()); |
| 530 MediaGalleriesPreferences* preferences = GetPreferences(profile); |
| 531 MediaGalleriesPrefInfoMap::const_iterator gallery = |
| 532 preferences->known_galleries().find(pref_id); |
| 533 MediaGalleryPrefIdSet permitted_galleries = |
| 534 preferences->GalleriesForExtension(*extension); |
| 535 |
| 536 if (gallery == preferences->known_galleries().end() || |
| 537 !ContainsKey(permitted_galleries, pref_id)) { |
| 538 BrowserThread::PostTask( |
| 539 BrowserThread::IO, FROM_HERE, |
| 540 base::Bind(callback, base::File::FILE_ERROR_NOT_FOUND)); |
| 541 return; |
| 542 } |
| 543 |
| 544 ExtensionGalleriesHost* extension_host = |
| 545 GetExtensionGalleryHost(profile, preferences, extension->id()); |
| 546 |
| 547 // This must come before the GetMediaFileSystems call to make sure the |
| 548 // RVH of the context is referenced before the filesystems are retrieved. |
| 549 extension_host->ReferenceFromRVH(rvh); |
| 550 |
| 551 extension_host->RegisterMediaFileSystem(gallery->second, callback); |
| 552 } |
| 553 |
467 MediaGalleriesPreferences* MediaFileSystemRegistry::GetPreferences( | 554 MediaGalleriesPreferences* MediaFileSystemRegistry::GetPreferences( |
468 Profile* profile) { | 555 Profile* profile) { |
469 // Create an empty ExtensionHostMap for this profile on first initialization. | 556 // Create an empty ExtensionHostMap for this profile on first initialization. |
470 if (!ContainsKey(extension_hosts_map_, profile)) { | 557 if (!ContainsKey(extension_hosts_map_, profile)) { |
471 extension_hosts_map_[profile] = ExtensionHostMap(); | 558 extension_hosts_map_[profile] = ExtensionHostMap(); |
472 media_galleries::UsageCount(media_galleries::PROFILES_WITH_USAGE); | 559 media_galleries::UsageCount(media_galleries::PROFILES_WITH_USAGE); |
473 } | 560 } |
474 | 561 |
475 return MediaGalleriesPreferencesFactory::GetForProfile(profile); | 562 return MediaGalleriesPreferencesFactory::GetForProfile(profile); |
476 } | 563 } |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
531 } | 618 } |
532 } | 619 } |
533 | 620 |
534 /****************** | 621 /****************** |
535 * Private methods | 622 * Private methods |
536 ******************/ | 623 ******************/ |
537 | 624 |
538 class MediaFileSystemRegistry::MediaFileSystemContextImpl | 625 class MediaFileSystemRegistry::MediaFileSystemContextImpl |
539 : public MediaFileSystemContext { | 626 : public MediaFileSystemContext { |
540 public: | 627 public: |
541 explicit MediaFileSystemContextImpl(MediaFileSystemRegistry* registry) | 628 MediaFileSystemContextImpl() {} |
542 : registry_(registry) { | |
543 DCHECK(registry_); // Suppresses unused warning on Android. | |
544 } | |
545 virtual ~MediaFileSystemContextImpl() {} | 629 virtual ~MediaFileSystemContextImpl() {} |
546 | 630 |
547 virtual std::string RegisterFileSystem( | 631 virtual bool RegisterFileSystem(const std::string& device_id, |
548 const std::string& device_id, const base::FilePath& path) OVERRIDE { | 632 const std::string& fs_name, |
| 633 const base::FilePath& path) OVERRIDE { |
549 if (StorageInfo::IsMassStorageDevice(device_id)) { | 634 if (StorageInfo::IsMassStorageDevice(device_id)) { |
550 return RegisterFileSystemForMassStorage(device_id, path); | 635 return RegisterFileSystemForMassStorage(device_id, fs_name, path); |
551 } else { | 636 } else { |
552 return RegisterFileSystemForMTPDevice(device_id, path); | 637 return RegisterFileSystemForMTPDevice(device_id, fs_name, path); |
553 } | 638 } |
554 } | 639 } |
555 | 640 |
556 virtual void RevokeFileSystem(const std::string& fsid) OVERRIDE { | 641 virtual void RevokeFileSystem(const std::string& fs_name) OVERRIDE { |
557 ImportedMediaGalleryRegistry* imported_registry = | 642 ImportedMediaGalleryRegistry* imported_registry = |
558 ImportedMediaGalleryRegistry::GetInstance(); | 643 ImportedMediaGalleryRegistry::GetInstance(); |
559 if (imported_registry->RevokeImportedFilesystemOnUIThread(fsid)) | 644 if (imported_registry->RevokeImportedFilesystemOnUIThread(fs_name)) |
560 return; | 645 return; |
561 | 646 |
562 IsolatedContext::GetInstance()->RevokeFileSystem(fsid); | 647 ExternalMountPoints::GetSystemInstance()->RevokeFileSystem(fs_name); |
563 | 648 |
564 content::BrowserThread::PostTask( | 649 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind( |
565 content::BrowserThread::IO, FROM_HERE, base::Bind( | 650 &MTPDeviceMapService::RevokeMTPFileSystem, |
566 &MTPDeviceMapService::RevokeMTPFileSystem, | 651 base::Unretained(MTPDeviceMapService::GetInstance()), |
567 base::Unretained(MTPDeviceMapService::GetInstance()), | 652 fs_name)); |
568 fsid)); | 653 } |
| 654 |
| 655 virtual base::FilePath GetRegisteredPath( |
| 656 const std::string& fs_name) const OVERRIDE { |
| 657 base::FilePath result; |
| 658 if (!ExternalMountPoints::GetSystemInstance()->GetRegisteredPath(fs_name, |
| 659 &result)) { |
| 660 return base::FilePath(); |
| 661 } |
| 662 return result; |
569 } | 663 } |
570 | 664 |
571 private: | 665 private: |
572 // Registers and returns the file system id for the mass storage device | 666 // Registers and returns the file system id for the mass storage device |
573 // specified by |device_id| and |path|. | 667 // specified by |device_id| and |path|. |
574 std::string RegisterFileSystemForMassStorage( | 668 bool RegisterFileSystemForMassStorage(const std::string& device_id, |
575 const std::string& device_id, const base::FilePath& path) { | 669 const std::string& fs_name, |
| 670 const base::FilePath& path) { |
576 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 671 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
577 DCHECK(StorageInfo::IsMassStorageDevice(device_id)); | 672 DCHECK(StorageInfo::IsMassStorageDevice(device_id)); |
578 | 673 |
579 // Sanity checks for |path|. | 674 // Sanity checks for |path|. |
580 CHECK(path.IsAbsolute()); | 675 CHECK(path.IsAbsolute()); |
581 CHECK(!path.ReferencesParent()); | 676 CHECK(!path.ReferencesParent()); |
582 | 677 |
583 // TODO(gbillock): refactor ImportedMediaGalleryRegistry to delegate this | 678 // TODO(gbillock): refactor ImportedMediaGalleryRegistry to delegate this |
584 // call tree, probably by having it figure out by device id what | 679 // call tree, probably by having it figure out by device id what |
585 // registration is needed, or having per-device-type handlers at the | 680 // registration is needed, or having per-device-type handlers at the |
586 // next higher level. | 681 // next higher level. |
587 std::string fsid; | 682 bool result = false; |
588 if (StorageInfo::IsITunesDevice(device_id)) { | 683 if (StorageInfo::IsITunesDevice(device_id)) { |
589 ImportedMediaGalleryRegistry* imported_registry = | 684 ImportedMediaGalleryRegistry* registry = |
590 ImportedMediaGalleryRegistry::GetInstance(); | 685 ImportedMediaGalleryRegistry::GetInstance(); |
591 fsid = imported_registry->RegisterITunesFilesystemOnUIThread(path); | 686 result = registry->RegisterITunesFilesystemOnUIThread(fs_name, path); |
592 } else if (StorageInfo::IsPicasaDevice(device_id)) { | 687 } else if (StorageInfo::IsPicasaDevice(device_id)) { |
593 ImportedMediaGalleryRegistry* imported_registry = | 688 ImportedMediaGalleryRegistry* registry = |
594 ImportedMediaGalleryRegistry::GetInstance(); | 689 ImportedMediaGalleryRegistry::GetInstance(); |
595 fsid = imported_registry->RegisterPicasaFilesystemOnUIThread( | 690 result = registry->RegisterPicasaFilesystemOnUIThread(fs_name, path); |
596 path); | |
597 } else if (StorageInfo::IsIPhotoDevice(device_id)) { | 691 } else if (StorageInfo::IsIPhotoDevice(device_id)) { |
598 ImportedMediaGalleryRegistry* imported_registry = | 692 ImportedMediaGalleryRegistry* registry = |
599 ImportedMediaGalleryRegistry::GetInstance(); | 693 ImportedMediaGalleryRegistry::GetInstance(); |
600 fsid = imported_registry->RegisterIPhotoFilesystemOnUIThread( | 694 result = registry->RegisterIPhotoFilesystemOnUIThread(fs_name, path); |
601 path); | |
602 } else { | 695 } else { |
603 std::string fs_name(extension_misc::kMediaFileSystemPathPart); | 696 result = ExternalMountPoints::GetSystemInstance()->RegisterFileSystem( |
604 fsid = IsolatedContext::GetInstance()->RegisterFileSystemForPath( | 697 fs_name, fileapi::kFileSystemTypeNativeMedia, |
605 fileapi::kFileSystemTypeNativeMedia, path, &fs_name); | 698 fileapi::FileSystemMountOption(), path); |
606 } | 699 } |
607 return fsid; | 700 return result; |
608 } | 701 } |
609 | 702 |
610 std::string RegisterFileSystemForMTPDevice( | 703 bool RegisterFileSystemForMTPDevice(const std::string& device_id, |
611 const std::string& device_id, const base::FilePath& path) { | 704 const std::string fs_name, |
| 705 const base::FilePath& path) { |
612 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 706 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
613 DCHECK(!StorageInfo::IsMassStorageDevice(device_id)); | 707 DCHECK(!StorageInfo::IsMassStorageDevice(device_id)); |
614 | 708 |
615 // Sanity checks for |path|. | 709 // Sanity checks for |path|. |
616 CHECK(MediaStorageUtil::CanCreateFileSystem(device_id, path)); | 710 CHECK(MediaStorageUtil::CanCreateFileSystem(device_id, path)); |
617 std::string fs_name(extension_misc::kMediaFileSystemPathPart); | 711 bool result = ExternalMountPoints::GetSystemInstance()->RegisterFileSystem( |
618 const std::string fsid = | 712 fs_name, fileapi::kFileSystemTypeDeviceMedia, |
619 IsolatedContext::GetInstance()->RegisterFileSystemForPath( | 713 fileapi::FileSystemMountOption(), path); |
620 fileapi::kFileSystemTypeDeviceMedia, path, &fs_name); | 714 CHECK(result); |
621 CHECK(!fsid.empty()); | 715 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind( |
622 content::BrowserThread::PostTask( | 716 &MTPDeviceMapService::RegisterMTPFileSystem, |
623 content::BrowserThread::IO, FROM_HERE, base::Bind( | 717 base::Unretained(MTPDeviceMapService::GetInstance()), |
624 &MTPDeviceMapService::RegisterMTPFileSystem, | 718 path.value(), fs_name)); |
625 base::Unretained(MTPDeviceMapService::GetInstance()), | 719 return result; |
626 path.value(), fsid)); | |
627 return fsid; | |
628 } | 720 } |
629 | 721 |
630 MediaFileSystemRegistry* registry_; | |
631 | |
632 DISALLOW_COPY_AND_ASSIGN(MediaFileSystemContextImpl); | 722 DISALLOW_COPY_AND_ASSIGN(MediaFileSystemContextImpl); |
633 }; | 723 }; |
634 | 724 |
635 // Constructor in 'private' section because depends on private class definition. | 725 // Constructor in 'private' section because depends on private class definition. |
636 MediaFileSystemRegistry::MediaFileSystemRegistry() | 726 MediaFileSystemRegistry::MediaFileSystemRegistry() |
637 : file_system_context_(new MediaFileSystemContextImpl(this)) { | 727 : file_system_context_(new MediaFileSystemContextImpl) { |
638 StorageMonitor::GetInstance()->AddObserver(this); | 728 StorageMonitor::GetInstance()->AddObserver(this); |
639 } | 729 } |
640 | 730 |
641 MediaFileSystemRegistry::~MediaFileSystemRegistry() { | 731 MediaFileSystemRegistry::~MediaFileSystemRegistry() { |
642 // TODO(gbillock): This is needed because the unit test uses the | 732 // TODO(gbillock): This is needed because the unit test uses the |
643 // g_browser_process registry. We should create one in the unit test, | 733 // g_browser_process registry. We should create one in the unit test, |
644 // and then can remove this. | 734 // and then can remove this. |
645 if (StorageMonitor::GetInstance()) | 735 if (StorageMonitor::GetInstance()) |
646 StorageMonitor::GetInstance()->RemoveObserver(this); | 736 StorageMonitor::GetInstance()->RemoveObserver(this); |
647 } | 737 } |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
691 if (!ContainsKey(extension_hosts_map_, profile)) | 781 if (!ContainsKey(extension_hosts_map_, profile)) |
692 break; | 782 break; |
693 ExtensionHostMap::const_iterator gallery_host_it = | 783 ExtensionHostMap::const_iterator gallery_host_it = |
694 extension_host_map.find(extensions[i]->id()); | 784 extension_host_map.find(extensions[i]->id()); |
695 if (gallery_host_it == extension_host_map.end()) | 785 if (gallery_host_it == extension_host_map.end()) |
696 continue; | 786 continue; |
697 gallery_host_it->second->RevokeGalleryByPrefId(pref_id); | 787 gallery_host_it->second->RevokeGalleryByPrefId(pref_id); |
698 } | 788 } |
699 } | 789 } |
700 | 790 |
| 791 ExtensionGalleriesHost* MediaFileSystemRegistry::GetExtensionGalleryHost( |
| 792 Profile* profile, |
| 793 MediaGalleriesPreferences* preferences, |
| 794 const std::string& extension_id) { |
| 795 ExtensionGalleriesHostMap::iterator extension_hosts = |
| 796 extension_hosts_map_.find(profile); |
| 797 // GetPreferences(), which had to be called because preferences is an |
| 798 // argument, ensures that profile is in the map. |
| 799 DCHECK(extension_hosts != extension_hosts_map_.end()); |
| 800 if (extension_hosts->second.empty()) |
| 801 preferences->AddGalleryChangeObserver(this); |
| 802 |
| 803 ExtensionGalleriesHost* result = extension_hosts->second[extension_id].get(); |
| 804 if (!result) { |
| 805 result = new ExtensionGalleriesHost( |
| 806 file_system_context_.get(), |
| 807 profile->GetPath(), |
| 808 extension_id, |
| 809 base::Bind(&MediaFileSystemRegistry::OnExtensionGalleriesHostEmpty, |
| 810 base::Unretained(this), |
| 811 profile, |
| 812 extension_id)); |
| 813 extension_hosts_map_[profile][extension_id] = result; |
| 814 } |
| 815 return result; |
| 816 } |
| 817 |
701 void MediaFileSystemRegistry::OnExtensionGalleriesHostEmpty( | 818 void MediaFileSystemRegistry::OnExtensionGalleriesHostEmpty( |
702 Profile* profile, const std::string& extension_id) { | 819 Profile* profile, const std::string& extension_id) { |
703 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 820 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
704 | 821 |
705 ExtensionGalleriesHostMap::iterator extension_hosts = | 822 ExtensionGalleriesHostMap::iterator extension_hosts = |
706 extension_hosts_map_.find(profile); | 823 extension_hosts_map_.find(profile); |
707 DCHECK(extension_hosts != extension_hosts_map_.end()); | 824 DCHECK(extension_hosts != extension_hosts_map_.end()); |
708 ExtensionHostMap::size_type erase_count = | 825 ExtensionHostMap::size_type erase_count = |
709 extension_hosts->second.erase(extension_id); | 826 extension_hosts->second.erase(extension_id); |
710 DCHECK_EQ(1U, erase_count); | 827 DCHECK_EQ(1U, erase_count); |
711 if (extension_hosts->second.empty()) { | 828 if (extension_hosts->second.empty()) { |
712 // When a profile has no ExtensionGalleriesHosts left, remove the | 829 // When a profile has no ExtensionGalleriesHosts left, remove the |
713 // matching gallery-change-watcher since it is no longer needed. Leave the | 830 // matching gallery-change-watcher since it is no longer needed. Leave the |
714 // |extension_hosts| entry alone, since it indicates the profile has been | 831 // |extension_hosts| entry alone, since it indicates the profile has been |
715 // previously used. | 832 // previously used. |
716 MediaGalleriesPreferences* preferences = GetPreferences(profile); | 833 MediaGalleriesPreferences* preferences = GetPreferences(profile); |
717 preferences->RemoveGalleryChangeObserver(this); | 834 preferences->RemoveGalleryChangeObserver(this); |
718 } | 835 } |
719 } | 836 } |
OLD | NEW |