Chromium Code Reviews| 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 // Implements the Chrome Extensions Media Galleries API. | 5 // Implements the Chrome Extensions Media Galleries API. |
| 6 | 6 |
| 7 #include "chrome/browser/extensions/api/media_galleries/media_galleries_api.h" | 7 #include "chrome/browser/extensions/api/media_galleries/media_galleries_api.h" |
| 8 | 8 |
| 9 #include <set> | 9 #include <set> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 61 using storage_monitor::MediaStorageUtil; | 61 using storage_monitor::MediaStorageUtil; |
| 62 using storage_monitor::StorageInfo; | 62 using storage_monitor::StorageInfo; |
| 63 using web_modal::WebContentsModalDialogManager; | 63 using web_modal::WebContentsModalDialogManager; |
| 64 | 64 |
| 65 namespace extensions { | 65 namespace extensions { |
| 66 | 66 |
| 67 namespace MediaGalleries = api::media_galleries; | 67 namespace MediaGalleries = api::media_galleries; |
| 68 namespace DropPermissionForMediaFileSystem = | 68 namespace DropPermissionForMediaFileSystem = |
| 69 MediaGalleries::DropPermissionForMediaFileSystem; | 69 MediaGalleries::DropPermissionForMediaFileSystem; |
| 70 namespace GetMediaFileSystems = MediaGalleries::GetMediaFileSystems; | 70 namespace GetMediaFileSystems = MediaGalleries::GetMediaFileSystems; |
| 71 namespace AddGalleryWatch = MediaGalleries::AddGalleryWatch; | |
| 72 namespace RemoveGalleryWatch = MediaGalleries::RemoveGalleryWatch; | |
| 73 namespace GetAllGalleryWatch = MediaGalleries::GetAllGalleryWatch; | |
| 71 | 74 |
| 72 namespace { | 75 namespace { |
| 73 | 76 |
| 74 const char kDisallowedByPolicy[] = | 77 const char kDisallowedByPolicy[] = |
| 75 "Media Galleries API is disallowed by policy: "; | 78 "Media Galleries API is disallowed by policy: "; |
| 76 const char kFailedToSetGalleryPermission[] = | 79 const char kFailedToSetGalleryPermission[] = |
| 77 "Failed to set gallery permission."; | 80 "Failed to set gallery permission."; |
| 78 const char kInvalidGalleryId[] = "Invalid gallery id."; | 81 const char kInvalidGalleryIdMsg[] = "Invalid gallery id."; |
| 79 const char kMissingEventListener[] = "Missing event listener registration."; | 82 const char kMissingEventListener[] = "Missing event listener registration."; |
| 80 const char kNonExistentGalleryId[] = "Non-existent gallery id."; | 83 const char kNonExistentGalleryId[] = "Non-existent gallery id."; |
| 81 const char kNoScanPermission[] = "No permission to scan."; | 84 const char kNoScanPermission[] = "No permission to scan."; |
| 82 | 85 |
| 83 const char kDeviceIdKey[] = "deviceId"; | 86 const char kDeviceIdKey[] = "deviceId"; |
| 84 const char kGalleryIdKey[] = "galleryId"; | 87 const char kGalleryIdKey[] = "galleryId"; |
| 85 const char kIsAvailableKey[] = "isAvailable"; | 88 const char kIsAvailableKey[] = "isAvailable"; |
| 86 const char kIsMediaDeviceKey[] = "isMediaDevice"; | 89 const char kIsMediaDeviceKey[] = "isMediaDevice"; |
| 87 const char kIsRemovableKey[] = "isRemovable"; | 90 const char kIsRemovableKey[] = "isRemovable"; |
| 88 const char kNameKey[] = "name"; | 91 const char kNameKey[] = "name"; |
| 89 | 92 |
| 90 const char kMetadataKey[] = "metadata"; | 93 const char kMetadataKey[] = "metadata"; |
| 91 const char kAttachedImagesBlobInfoKey[] = "attachedImagesBlobInfo"; | 94 const char kAttachedImagesBlobInfoKey[] = "attachedImagesBlobInfo"; |
| 92 const char kBlobUUIDKey[] = "blobUUID"; | 95 const char kBlobUUIDKey[] = "blobUUID"; |
| 93 const char kTypeKey[] = "type"; | 96 const char kTypeKey[] = "type"; |
| 94 const char kSizeKey[] = "size"; | 97 const char kSizeKey[] = "size"; |
| 95 | 98 |
| 99 const char kInvalidGalleryId[] = "-1"; | |
| 100 | |
| 96 MediaFileSystemRegistry* media_file_system_registry() { | 101 MediaFileSystemRegistry* media_file_system_registry() { |
| 97 return g_browser_process->media_file_system_registry(); | 102 return g_browser_process->media_file_system_registry(); |
| 98 } | 103 } |
| 99 | 104 |
| 100 GalleryWatchManager* gallery_watch_manager() { | 105 GalleryWatchManager* gallery_watch_manager() { |
| 101 return media_file_system_registry()->gallery_watch_manager(); | 106 return media_file_system_registry()->gallery_watch_manager(); |
| 102 } | 107 } |
| 103 | 108 |
| 104 MediaScanManager* media_scan_manager() { | 109 MediaScanManager* media_scan_manager() { |
| 105 return media_file_system_registry()->media_scan_manager(); | 110 return media_file_system_registry()->media_scan_manager(); |
| 106 } | 111 } |
| 107 | 112 |
| 108 // Checks whether the MediaGalleries API is currently accessible (it may be | 113 // Checks whether the MediaGalleries API is currently accessible (it may be |
| 109 // disallowed even if an extension has the requisite permission). Then | 114 // disallowed even if an extension has the requisite permission). Then |
| 110 // initializes the MediaGalleriesPreferences | 115 // initializes the MediaGalleriesPreferences |
| 111 bool Setup(Profile* profile, std::string* error, base::Closure callback) { | 116 bool Setup(Profile* profile, std::string* error, base::Closure callback) { |
| 112 if (!ChromeSelectFilePolicy::FileSelectDialogsAllowed()) { | 117 if (!ChromeSelectFilePolicy::FileSelectDialogsAllowed()) { |
| 113 *error = std::string(kDisallowedByPolicy) + | 118 *error = std::string(kDisallowedByPolicy) + |
| 114 prefs::kAllowFileSelectionDialogs; | 119 prefs::kAllowFileSelectionDialogs; |
| 115 return false; | 120 return false; |
| 116 } | 121 } |
| 117 | 122 |
| 118 MediaGalleriesPreferences* preferences = | 123 MediaGalleriesPreferences* preferences = |
| 119 media_file_system_registry()->GetPreferences(profile); | 124 media_file_system_registry()->GetPreferences(profile); |
| 120 preferences->EnsureInitialized(callback); | 125 preferences->EnsureInitialized(callback); |
| 121 return true; | 126 return true; |
| 122 } | 127 } |
| 123 | 128 |
| 129 // Returns true and sets |gallery_file_path| and |gallery_pref_id| if the | |
| 130 // |gallery_id| is valid and returns false otherwise. | |
| 131 bool GetGalleryFilePathAndId(const std::string& gallery_id, | |
| 132 Profile* profile, | |
| 133 const Extension* extension, | |
| 134 base::FilePath* gallery_file_path, | |
| 135 MediaGalleryPrefId* gallery_pref_id) { | |
| 136 MediaGalleryPrefId pref_id; | |
| 137 if (!base::StringToUint64(gallery_id, &pref_id)) | |
| 138 return false; | |
| 139 MediaGalleriesPreferences* preferences = | |
| 140 g_browser_process->media_file_system_registry()->GetPreferences(profile); | |
| 141 base::FilePath file_path( | |
| 142 preferences->LookUpGalleryPathForExtension(pref_id, extension, false)); | |
| 143 if (file_path.empty()) | |
| 144 return false; | |
| 145 *gallery_pref_id = pref_id; | |
| 146 *gallery_file_path = file_path; | |
| 147 return true; | |
| 148 } | |
| 149 | |
| 124 WebContents* GetWebContents(content::RenderViewHost* rvh, | 150 WebContents* GetWebContents(content::RenderViewHost* rvh, |
| 125 Profile* profile, | 151 Profile* profile, |
| 126 const std::string& app_id) { | 152 const std::string& app_id) { |
| 127 WebContents* contents = WebContents::FromRenderViewHost(rvh); | 153 WebContents* contents = WebContents::FromRenderViewHost(rvh); |
| 128 WebContentsModalDialogManager* web_contents_modal_dialog_manager = | 154 WebContentsModalDialogManager* web_contents_modal_dialog_manager = |
| 129 WebContentsModalDialogManager::FromWebContents(contents); | 155 WebContentsModalDialogManager::FromWebContents(contents); |
| 130 if (!web_contents_modal_dialog_manager) { | 156 if (!web_contents_modal_dialog_manager) { |
| 131 // If there is no WebContentsModalDialogManager, then this contents is | 157 // If there is no WebContentsModalDialogManager, then this contents is |
| 132 // probably the background page for an app. Try to find a app window to | 158 // probably the background page for an app. Try to find a app window to |
| 133 // host the dialog. | 159 // host the dialog. |
| (...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 392 | 418 |
| 393 scoped_ptr<extensions::Event> event( | 419 scoped_ptr<extensions::Event> event( |
| 394 new extensions::Event(event_name, event_args.Pass())); | 420 new extensions::Event(event_name, event_args.Pass())); |
| 395 router->DispatchEventToExtension(extension_id, event.Pass()); | 421 router->DispatchEventToExtension(extension_id, event.Pass()); |
| 396 } | 422 } |
| 397 | 423 |
| 398 void MediaGalleriesEventRouter::OnGalleryChanged( | 424 void MediaGalleriesEventRouter::OnGalleryChanged( |
| 399 const std::string& extension_id, MediaGalleryPrefId gallery_id) { | 425 const std::string& extension_id, MediaGalleryPrefId gallery_id) { |
| 400 MediaGalleries::GalleryChangeDetails details; | 426 MediaGalleries::GalleryChangeDetails details; |
| 401 details.type = MediaGalleries::GALLERY_CHANGE_TYPE_CONTENTS_CHANGED; | 427 details.type = MediaGalleries::GALLERY_CHANGE_TYPE_CONTENTS_CHANGED; |
| 402 details.gallery_id = gallery_id; | 428 details.gallery_id = base::Uint64ToString(gallery_id); |
| 403 DispatchEventToExtension( | 429 DispatchEventToExtension( |
| 404 extension_id, | 430 extension_id, |
| 405 MediaGalleries::OnGalleryChanged::kEventName, | 431 MediaGalleries::OnGalleryChanged::kEventName, |
| 406 MediaGalleries::OnGalleryChanged::Create(details).Pass()); | 432 MediaGalleries::OnGalleryChanged::Create(details).Pass()); |
| 407 } | 433 } |
| 408 | 434 |
| 409 void MediaGalleriesEventRouter::OnGalleryWatchDropped( | 435 void MediaGalleriesEventRouter::OnGalleryWatchDropped( |
| 410 const std::string& extension_id, MediaGalleryPrefId gallery_id) { | 436 const std::string& extension_id, MediaGalleryPrefId gallery_id) { |
| 411 MediaGalleries::GalleryChangeDetails details; | 437 MediaGalleries::GalleryChangeDetails details; |
| 412 details.type = MediaGalleries::GALLERY_CHANGE_TYPE_WATCH_DROPPED; | 438 details.type = MediaGalleries::GALLERY_CHANGE_TYPE_WATCH_DROPPED; |
| 413 details.gallery_id = gallery_id; | 439 details.gallery_id = gallery_id; |
| 414 DispatchEventToExtension( | 440 DispatchEventToExtension( |
| 415 extension_id, | 441 extension_id, |
| 416 MediaGalleries::OnGalleryChanged::kEventName, | 442 MediaGalleries::OnGalleryChanged::kEventName, |
| 417 MediaGalleries::OnGalleryChanged::Create(details).Pass()); | 443 MediaGalleries::OnGalleryChanged::Create(details).Pass()); |
| 418 } | 444 } |
| 419 | 445 |
| 420 void MediaGalleriesEventRouter::OnListenerRemoved( | 446 void MediaGalleriesEventRouter::OnListenerRemoved( |
| 421 const EventListenerInfo& details) { | 447 const EventListenerInfo& details) { |
| 422 if (details.event_name == MediaGalleries::OnGalleryChanged::kEventName && | 448 if (details.event_name == MediaGalleries::OnGalleryChanged::kEventName && |
| 423 !ExtensionHasGalleryChangeListener(details.extension_id)) { | 449 !ExtensionHasGalleryChangeListener(details.extension_id)) { |
| 424 gallery_watch_manager()->RemoveAllWatches(profile_, details.extension_id); | 450 gallery_watch_manager()->RemoveAllWatches(profile_, details.extension_id); |
| 425 } | 451 } |
| 426 } | 452 } |
| 427 | 453 |
| 454 /////////////////////////////////////////////////////////////////////////////// | |
| 455 // MediaGalleriesGetMediaFileSystemsFunction // | |
| 456 /////////////////////////////////////////////////////////////////////////////// | |
| 428 MediaGalleriesGetMediaFileSystemsFunction:: | 457 MediaGalleriesGetMediaFileSystemsFunction:: |
| 429 ~MediaGalleriesGetMediaFileSystemsFunction() {} | 458 ~MediaGalleriesGetMediaFileSystemsFunction() {} |
| 430 | 459 |
| 431 bool MediaGalleriesGetMediaFileSystemsFunction::RunAsync() { | 460 bool MediaGalleriesGetMediaFileSystemsFunction::RunAsync() { |
| 432 media_galleries::UsageCount(media_galleries::GET_MEDIA_FILE_SYSTEMS); | 461 media_galleries::UsageCount(media_galleries::GET_MEDIA_FILE_SYSTEMS); |
| 433 scoped_ptr<GetMediaFileSystems::Params> params( | 462 scoped_ptr<GetMediaFileSystems::Params> params( |
| 434 GetMediaFileSystems::Params::Create(*args_)); | 463 GetMediaFileSystems::Params::Create(*args_)); |
| 435 EXTENSION_FUNCTION_VALIDATE(params.get()); | 464 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 436 MediaGalleries::GetMediaFileSystemsInteractivity interactive = | 465 MediaGalleries::GetMediaFileSystemsInteractivity interactive = |
| 437 MediaGalleries::GET_MEDIA_FILE_SYSTEMS_INTERACTIVITY_NO; | 466 MediaGalleries::GET_MEDIA_FILE_SYSTEMS_INTERACTIVITY_NO; |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 525 if (!render_view_host()) { | 554 if (!render_view_host()) { |
| 526 cb.Run(std::vector<MediaFileSystemInfo>()); | 555 cb.Run(std::vector<MediaFileSystemInfo>()); |
| 527 return; | 556 return; |
| 528 } | 557 } |
| 529 MediaFileSystemRegistry* registry = media_file_system_registry(); | 558 MediaFileSystemRegistry* registry = media_file_system_registry(); |
| 530 DCHECK(registry->GetPreferences(GetProfile())->IsInitialized()); | 559 DCHECK(registry->GetPreferences(GetProfile())->IsInitialized()); |
| 531 registry->GetMediaFileSystemsForExtension( | 560 registry->GetMediaFileSystemsForExtension( |
| 532 render_view_host(), extension(), cb); | 561 render_view_host(), extension(), cb); |
| 533 } | 562 } |
| 534 | 563 |
| 564 | |
| 565 /////////////////////////////////////////////////////////////////////////////// | |
| 566 // MediaGalleriesGetAllMediaFileSystemMetadataFunction // | |
| 567 /////////////////////////////////////////////////////////////////////////////// | |
| 535 MediaGalleriesGetAllMediaFileSystemMetadataFunction:: | 568 MediaGalleriesGetAllMediaFileSystemMetadataFunction:: |
| 536 ~MediaGalleriesGetAllMediaFileSystemMetadataFunction() {} | 569 ~MediaGalleriesGetAllMediaFileSystemMetadataFunction() {} |
| 537 | 570 |
| 538 bool MediaGalleriesGetAllMediaFileSystemMetadataFunction::RunAsync() { | 571 bool MediaGalleriesGetAllMediaFileSystemMetadataFunction::RunAsync() { |
| 539 media_galleries::UsageCount( | 572 media_galleries::UsageCount( |
| 540 media_galleries::GET_ALL_MEDIA_FILE_SYSTEM_METADATA); | 573 media_galleries::GET_ALL_MEDIA_FILE_SYSTEM_METADATA); |
| 541 return Setup(GetProfile(), &error_, base::Bind( | 574 return Setup(GetProfile(), &error_, base::Bind( |
| 542 &MediaGalleriesGetAllMediaFileSystemMetadataFunction::OnPreferencesInit, | 575 &MediaGalleriesGetAllMediaFileSystemMetadataFunction::OnPreferencesInit, |
| 543 this)); | 576 this)); |
| 544 } | 577 } |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 587 metadata.is_removable = StorageInfo::IsRemovableDevice(gallery.device_id); | 620 metadata.is_removable = StorageInfo::IsRemovableDevice(gallery.device_id); |
| 588 metadata.is_media_device = StorageInfo::IsMediaDevice(gallery.device_id); | 621 metadata.is_media_device = StorageInfo::IsMediaDevice(gallery.device_id); |
| 589 metadata.is_available = ContainsKey(*available_devices, gallery.device_id); | 622 metadata.is_available = ContainsKey(*available_devices, gallery.device_id); |
| 590 list->Append(metadata.ToValue().release()); | 623 list->Append(metadata.ToValue().release()); |
| 591 } | 624 } |
| 592 | 625 |
| 593 SetResult(list); | 626 SetResult(list); |
| 594 SendResponse(true); | 627 SendResponse(true); |
| 595 } | 628 } |
| 596 | 629 |
| 630 /////////////////////////////////////////////////////////////////////////////// | |
| 631 // MediaGalleriesAddUserSelectedFolderFunction // | |
| 632 /////////////////////////////////////////////////////////////////////////////// | |
| 597 MediaGalleriesAddUserSelectedFolderFunction:: | 633 MediaGalleriesAddUserSelectedFolderFunction:: |
| 598 ~MediaGalleriesAddUserSelectedFolderFunction() {} | 634 ~MediaGalleriesAddUserSelectedFolderFunction() {} |
| 599 | 635 |
| 600 bool MediaGalleriesAddUserSelectedFolderFunction::RunAsync() { | 636 bool MediaGalleriesAddUserSelectedFolderFunction::RunAsync() { |
| 601 media_galleries::UsageCount(media_galleries::ADD_USER_SELECTED_FOLDER); | 637 media_galleries::UsageCount(media_galleries::ADD_USER_SELECTED_FOLDER); |
| 602 return Setup(GetProfile(), &error_, base::Bind( | 638 return Setup(GetProfile(), &error_, base::Bind( |
| 603 &MediaGalleriesAddUserSelectedFolderFunction::OnPreferencesInit, this)); | 639 &MediaGalleriesAddUserSelectedFolderFunction::OnPreferencesInit, this)); |
| 604 } | 640 } |
| 605 | 641 |
| 606 void MediaGalleriesAddUserSelectedFolderFunction::OnPreferencesInit() { | 642 void MediaGalleriesAddUserSelectedFolderFunction::OnPreferencesInit() { |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 696 if (!render_view_host()) { | 732 if (!render_view_host()) { |
| 697 cb.Run(std::vector<MediaFileSystemInfo>()); | 733 cb.Run(std::vector<MediaFileSystemInfo>()); |
| 698 return; | 734 return; |
| 699 } | 735 } |
| 700 MediaFileSystemRegistry* registry = media_file_system_registry(); | 736 MediaFileSystemRegistry* registry = media_file_system_registry(); |
| 701 DCHECK(registry->GetPreferences(GetProfile())->IsInitialized()); | 737 DCHECK(registry->GetPreferences(GetProfile())->IsInitialized()); |
| 702 registry->GetMediaFileSystemsForExtension( | 738 registry->GetMediaFileSystemsForExtension( |
| 703 render_view_host(), extension(), cb); | 739 render_view_host(), extension(), cb); |
| 704 } | 740 } |
| 705 | 741 |
| 742 /////////////////////////////////////////////////////////////////////////////// | |
| 743 // MediaGalleriesDropPermissionForMediaFileSystemFunction // | |
| 744 /////////////////////////////////////////////////////////////////////////////// | |
| 706 MediaGalleriesDropPermissionForMediaFileSystemFunction:: | 745 MediaGalleriesDropPermissionForMediaFileSystemFunction:: |
| 707 ~MediaGalleriesDropPermissionForMediaFileSystemFunction() {} | 746 ~MediaGalleriesDropPermissionForMediaFileSystemFunction() {} |
| 708 | 747 |
| 709 bool MediaGalleriesDropPermissionForMediaFileSystemFunction::RunAsync() { | 748 bool MediaGalleriesDropPermissionForMediaFileSystemFunction::RunAsync() { |
| 710 media_galleries::UsageCount( | 749 media_galleries::UsageCount( |
| 711 media_galleries::DROP_PERMISSION_FOR_MEDIA_FILE_SYSTEM); | 750 media_galleries::DROP_PERMISSION_FOR_MEDIA_FILE_SYSTEM); |
| 712 | 751 |
| 713 scoped_ptr<DropPermissionForMediaFileSystem::Params> params( | 752 scoped_ptr<DropPermissionForMediaFileSystem::Params> params( |
| 714 DropPermissionForMediaFileSystem::Params::Create(*args_)); | 753 DropPermissionForMediaFileSystem::Params::Create(*args_)); |
| 715 EXTENSION_FUNCTION_VALIDATE(params.get()); | 754 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 716 MediaGalleryPrefId pref_id; | 755 MediaGalleryPrefId pref_id; |
| 717 if (!base::StringToUint64(params->gallery_id, &pref_id)) { | 756 if (!base::StringToUint64(params->gallery_id, &pref_id)) { |
| 718 error_ = kInvalidGalleryId; | 757 error_ = kInvalidGalleryIdMsg; |
| 719 return false; | 758 return false; |
| 720 } | 759 } |
| 721 | 760 |
| 722 base::Closure callback = base::Bind( | 761 base::Closure callback = base::Bind( |
| 723 &MediaGalleriesDropPermissionForMediaFileSystemFunction:: | 762 &MediaGalleriesDropPermissionForMediaFileSystemFunction:: |
| 724 OnPreferencesInit, | 763 OnPreferencesInit, |
| 725 this, | 764 this, |
| 726 pref_id); | 765 pref_id); |
| 727 return Setup(GetProfile(), &error_, callback); | 766 return Setup(GetProfile(), &error_, callback); |
| 728 } | 767 } |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 739 | 778 |
| 740 bool dropped = preferences->SetGalleryPermissionForExtension( | 779 bool dropped = preferences->SetGalleryPermissionForExtension( |
| 741 *extension(), pref_id, false); | 780 *extension(), pref_id, false); |
| 742 if (dropped) | 781 if (dropped) |
| 743 SetResult(new base::StringValue(base::Uint64ToString(pref_id))); | 782 SetResult(new base::StringValue(base::Uint64ToString(pref_id))); |
| 744 else | 783 else |
| 745 error_ = kFailedToSetGalleryPermission; | 784 error_ = kFailedToSetGalleryPermission; |
| 746 SendResponse(dropped); | 785 SendResponse(dropped); |
| 747 } | 786 } |
| 748 | 787 |
| 788 /////////////////////////////////////////////////////////////////////////////// | |
| 789 // MediaGalleriesStartMediaScanFunction // | |
| 790 /////////////////////////////////////////////////////////////////////////////// | |
| 749 MediaGalleriesStartMediaScanFunction::~MediaGalleriesStartMediaScanFunction() {} | 791 MediaGalleriesStartMediaScanFunction::~MediaGalleriesStartMediaScanFunction() {} |
| 750 | 792 |
| 751 bool MediaGalleriesStartMediaScanFunction::RunAsync() { | 793 bool MediaGalleriesStartMediaScanFunction::RunAsync() { |
| 752 media_galleries::UsageCount(media_galleries::START_MEDIA_SCAN); | 794 media_galleries::UsageCount(media_galleries::START_MEDIA_SCAN); |
| 753 if (!CheckScanPermission(extension(), &error_)) { | 795 if (!CheckScanPermission(extension(), &error_)) { |
| 754 MediaGalleriesEventRouter::Get(GetProfile()) | 796 MediaGalleriesEventRouter::Get(GetProfile()) |
| 755 ->OnScanError(extension()->id()); | 797 ->OnScanError(extension()->id()); |
| 756 return false; | 798 return false; |
| 757 } | 799 } |
| 758 return Setup(GetProfile(), &error_, base::Bind( | 800 return Setup(GetProfile(), &error_, base::Bind( |
| 759 &MediaGalleriesStartMediaScanFunction::OnPreferencesInit, this)); | 801 &MediaGalleriesStartMediaScanFunction::OnPreferencesInit, this)); |
| 760 } | 802 } |
| 761 | 803 |
| 762 void MediaGalleriesStartMediaScanFunction::OnPreferencesInit() { | 804 void MediaGalleriesStartMediaScanFunction::OnPreferencesInit() { |
| 763 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 805 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 764 MediaGalleriesEventRouter* api = MediaGalleriesEventRouter::Get(GetProfile()); | 806 MediaGalleriesEventRouter* api = MediaGalleriesEventRouter::Get(GetProfile()); |
| 765 if (!api->ExtensionHasScanProgressListener(extension()->id())) { | 807 if (!api->ExtensionHasScanProgressListener(extension()->id())) { |
| 766 error_ = kMissingEventListener; | 808 error_ = kMissingEventListener; |
| 767 SendResponse(false); | 809 SendResponse(false); |
| 768 return; | 810 return; |
| 769 } | 811 } |
| 770 | 812 |
| 771 media_scan_manager()->StartScan(GetProfile(), extension(), user_gesture()); | 813 media_scan_manager()->StartScan(GetProfile(), extension(), user_gesture()); |
| 772 SendResponse(true); | 814 SendResponse(true); |
| 773 } | 815 } |
| 774 | 816 |
| 817 /////////////////////////////////////////////////////////////////////////////// | |
| 818 // MediaGalleriesCancelMediaScanFunction // | |
| 819 /////////////////////////////////////////////////////////////////////////////// | |
| 775 MediaGalleriesCancelMediaScanFunction:: | 820 MediaGalleriesCancelMediaScanFunction:: |
| 776 ~MediaGalleriesCancelMediaScanFunction() { | 821 ~MediaGalleriesCancelMediaScanFunction() { |
| 777 } | 822 } |
| 778 | 823 |
| 779 bool MediaGalleriesCancelMediaScanFunction::RunAsync() { | 824 bool MediaGalleriesCancelMediaScanFunction::RunAsync() { |
| 780 media_galleries::UsageCount(media_galleries::CANCEL_MEDIA_SCAN); | 825 media_galleries::UsageCount(media_galleries::CANCEL_MEDIA_SCAN); |
| 781 if (!CheckScanPermission(extension(), &error_)) { | 826 if (!CheckScanPermission(extension(), &error_)) { |
| 782 MediaGalleriesEventRouter::Get(GetProfile()) | 827 MediaGalleriesEventRouter::Get(GetProfile()) |
| 783 ->OnScanError(extension()->id()); | 828 ->OnScanError(extension()->id()); |
| 784 return false; | 829 return false; |
| 785 } | 830 } |
| 786 return Setup(GetProfile(), &error_, base::Bind( | 831 return Setup(GetProfile(), &error_, base::Bind( |
| 787 &MediaGalleriesCancelMediaScanFunction::OnPreferencesInit, this)); | 832 &MediaGalleriesCancelMediaScanFunction::OnPreferencesInit, this)); |
| 788 } | 833 } |
| 789 | 834 |
| 790 void MediaGalleriesCancelMediaScanFunction::OnPreferencesInit() { | 835 void MediaGalleriesCancelMediaScanFunction::OnPreferencesInit() { |
| 791 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 836 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 792 media_scan_manager()->CancelScan(GetProfile(), extension()); | 837 media_scan_manager()->CancelScan(GetProfile(), extension()); |
| 793 SendResponse(true); | 838 SendResponse(true); |
| 794 } | 839 } |
| 795 | 840 |
| 841 /////////////////////////////////////////////////////////////////////////////// | |
| 842 // MediaGalleriesAddScanResultsFunction // | |
| 843 /////////////////////////////////////////////////////////////////////////////// | |
| 796 MediaGalleriesAddScanResultsFunction::~MediaGalleriesAddScanResultsFunction() {} | 844 MediaGalleriesAddScanResultsFunction::~MediaGalleriesAddScanResultsFunction() {} |
| 797 | 845 |
| 798 bool MediaGalleriesAddScanResultsFunction::RunAsync() { | 846 bool MediaGalleriesAddScanResultsFunction::RunAsync() { |
| 799 media_galleries::UsageCount(media_galleries::ADD_SCAN_RESULTS); | 847 media_galleries::UsageCount(media_galleries::ADD_SCAN_RESULTS); |
| 800 if (!CheckScanPermission(extension(), &error_)) { | 848 if (!CheckScanPermission(extension(), &error_)) { |
| 801 // We don't fire a scan progress error here, as it would be unintuitive. | 849 // We don't fire a scan progress error here, as it would be unintuitive. |
| 802 return false; | 850 return false; |
| 803 } | 851 } |
| 804 if (!user_gesture()) | 852 if (!user_gesture()) |
| 805 return false; | 853 return false; |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 860 if (!list.get()) { | 908 if (!list.get()) { |
| 861 SendResponse(false); | 909 SendResponse(false); |
| 862 return; | 910 return; |
| 863 } | 911 } |
| 864 | 912 |
| 865 // The custom JS binding will use this list to create DOMFileSystem objects. | 913 // The custom JS binding will use this list to create DOMFileSystem objects. |
| 866 SetResult(list.release()); | 914 SetResult(list.release()); |
| 867 SendResponse(true); | 915 SendResponse(true); |
| 868 } | 916 } |
| 869 | 917 |
| 918 /////////////////////////////////////////////////////////////////////////////// | |
| 919 // MediaGalleriesGetMetadataFunction // | |
| 920 /////////////////////////////////////////////////////////////////////////////// | |
| 870 MediaGalleriesGetMetadataFunction::~MediaGalleriesGetMetadataFunction() {} | 921 MediaGalleriesGetMetadataFunction::~MediaGalleriesGetMetadataFunction() {} |
| 871 | 922 |
| 872 bool MediaGalleriesGetMetadataFunction::RunAsync() { | 923 bool MediaGalleriesGetMetadataFunction::RunAsync() { |
| 873 std::string blob_uuid; | 924 std::string blob_uuid; |
| 874 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &blob_uuid)); | 925 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &blob_uuid)); |
| 875 | 926 |
| 876 const base::Value* options_value = NULL; | 927 const base::Value* options_value = NULL; |
| 877 if (!args_->Get(1, &options_value)) | 928 if (!args_->Get(1, &options_value)) |
| 878 return false; | 929 return false; |
| 879 scoped_ptr<MediaGalleries::MediaMetadataOptions> options = | 930 scoped_ptr<MediaGalleries::MediaMetadataOptions> options = |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1027 base::Passed(&attached_images), base::Passed(&blob_uuids))); | 1078 base::Passed(&attached_images), base::Passed(&blob_uuids))); |
| 1028 return; | 1079 return; |
| 1029 } | 1080 } |
| 1030 | 1081 |
| 1031 // All Blobs have been constructed. The renderer will take ownership. | 1082 // All Blobs have been constructed. The renderer will take ownership. |
| 1032 SetResult(result_dictionary.release()); | 1083 SetResult(result_dictionary.release()); |
| 1033 SetTransferredBlobUUIDs(*blob_uuids); | 1084 SetTransferredBlobUUIDs(*blob_uuids); |
| 1034 SendResponse(true); | 1085 SendResponse(true); |
| 1035 } | 1086 } |
| 1036 | 1087 |
| 1088 /////////////////////////////////////////////////////////////////////////////// | |
| 1089 // MediaGalleriesAddGalleryWatchFunction // | |
| 1090 /////////////////////////////////////////////////////////////////////////////// | |
| 1091 MediaGalleriesAddGalleryWatchFunction:: | |
| 1092 ~MediaGalleriesAddGalleryWatchFunction() { | |
| 1093 } | |
| 1094 | |
| 1095 bool MediaGalleriesAddGalleryWatchFunction::RunAsync() { | |
| 1096 DCHECK(GetProfile()); | |
| 1097 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
|
Lei Zhang
2014/08/25 22:45:21
nit: we usually check what thread we are on before
Oren Blasberg
2014/08/26 00:40:49
Done.
| |
| 1098 if (!render_view_host() || !render_view_host()->GetProcess()) | |
| 1099 return false; | |
| 1100 | |
| 1101 scoped_ptr<AddGalleryWatch::Params> params( | |
| 1102 AddGalleryWatch::Params::Create(*args_)); | |
| 1103 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
| 1104 | |
| 1105 MediaGalleriesPreferences* preferences = | |
| 1106 g_browser_process->media_file_system_registry()->GetPreferences( | |
| 1107 GetProfile()); | |
| 1108 preferences->EnsureInitialized( | |
| 1109 base::Bind(&MediaGalleriesAddGalleryWatchFunction::OnPreferencesInit, | |
| 1110 this, | |
| 1111 params->gallery_id)); | |
| 1112 | |
| 1113 return true; | |
| 1114 } | |
| 1115 | |
| 1116 void MediaGalleriesAddGalleryWatchFunction::OnPreferencesInit( | |
| 1117 const std::string& pref_id) { | |
| 1118 base::FilePath gallery_file_path; | |
| 1119 MediaGalleryPrefId gallery_pref_id = 0; | |
|
Lei Zhang
2014/08/25 22:45:21
Use kInvalidMediaGalleryPrefId instead of 0
Oren Blasberg
2014/08/26 00:40:49
Done.
| |
| 1120 if (!GetGalleryFilePathAndId(pref_id, | |
| 1121 GetProfile(), | |
| 1122 extension(), | |
| 1123 &gallery_file_path, | |
| 1124 &gallery_pref_id)) { | |
| 1125 api::media_galleries::AddGalleryWatchResult result; | |
| 1126 error_ = kInvalidGalleryIdMsg; | |
| 1127 result.gallery_id = kInvalidGalleryId; | |
| 1128 result.success = false; | |
| 1129 SetResult(result.ToValue().release()); | |
| 1130 SendResponse(false); | |
| 1131 return; | |
| 1132 } | |
| 1133 | |
| 1134 gallery_watch_manager()->AddWatch( | |
| 1135 GetProfile(), | |
| 1136 extension(), | |
| 1137 gallery_pref_id, | |
| 1138 base::Bind(&MediaGalleriesAddGalleryWatchFunction::HandleResponse, | |
| 1139 this, | |
| 1140 gallery_pref_id)); | |
| 1141 } | |
| 1142 | |
| 1143 void MediaGalleriesAddGalleryWatchFunction::HandleResponse( | |
| 1144 MediaGalleryPrefId gallery_id, | |
| 1145 const std::string& error) { | |
| 1146 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 1147 | |
| 1148 // If an app added a file watch without any event listeners on the | |
| 1149 // onGalleryChanged event, that's an error. | |
| 1150 MediaGalleriesEventRouter* api = MediaGalleriesEventRouter::Get(GetProfile()); | |
| 1151 if (!api->ExtensionHasGalleryChangeListener(extension()->id())) { | |
| 1152 api::media_galleries::AddGalleryWatchResult result; | |
| 1153 result.gallery_id = base::Uint64ToString(gallery_id); | |
| 1154 result.success = false; | |
| 1155 SetResult(result.ToValue().release()); | |
| 1156 error_ = kMissingEventListener; | |
| 1157 SendResponse(false); | |
| 1158 return; | |
| 1159 } | |
| 1160 | |
| 1161 api::media_galleries::AddGalleryWatchResult result; | |
|
Lei Zhang
2014/08/25 22:45:21
These two lines are redundant with lines 1152/1153
Oren Blasberg
2014/08/26 00:40:49
Done.
| |
| 1162 result.gallery_id = base::Uint64ToString(gallery_id); | |
| 1163 result.success = error.empty(); | |
| 1164 SetResult(result.ToValue().release()); | |
| 1165 if (error.empty()) { | |
| 1166 SendResponse(true); | |
| 1167 } else { | |
| 1168 error_ = error.c_str(); | |
| 1169 SendResponse(false); | |
| 1170 } | |
| 1171 } | |
| 1172 | |
| 1173 /////////////////////////////////////////////////////////////////////////////// | |
| 1174 // MediaGalleriesRemoveGalleryWatchFunction // | |
| 1175 /////////////////////////////////////////////////////////////////////////////// | |
| 1176 | |
| 1177 MediaGalleriesRemoveGalleryWatchFunction:: | |
| 1178 ~MediaGalleriesRemoveGalleryWatchFunction() { | |
| 1179 } | |
| 1180 | |
| 1181 bool MediaGalleriesRemoveGalleryWatchFunction::RunAsync() { | |
| 1182 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 1183 if (!render_view_host() || !render_view_host()->GetProcess()) | |
| 1184 return false; | |
| 1185 | |
| 1186 scoped_ptr<RemoveGalleryWatch::Params> params( | |
| 1187 RemoveGalleryWatch::Params::Create(*args_)); | |
| 1188 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
| 1189 | |
| 1190 MediaGalleriesPreferences* preferences = | |
| 1191 g_browser_process->media_file_system_registry()->GetPreferences( | |
| 1192 GetProfile()); | |
| 1193 preferences->EnsureInitialized( | |
| 1194 base::Bind(&MediaGalleriesRemoveGalleryWatchFunction::OnPreferencesInit, | |
| 1195 this, | |
| 1196 params->gallery_id)); | |
| 1197 return true; | |
| 1198 } | |
| 1199 | |
| 1200 void MediaGalleriesRemoveGalleryWatchFunction::OnPreferencesInit( | |
| 1201 const std::string& pref_id) { | |
| 1202 base::FilePath gallery_file_path; | |
| 1203 MediaGalleryPrefId gallery_pref_id = 0; | |
| 1204 if (!GetGalleryFilePathAndId(pref_id, | |
| 1205 GetProfile(), | |
| 1206 extension(), | |
| 1207 &gallery_file_path, | |
| 1208 &gallery_pref_id)) { | |
| 1209 error_ = kInvalidGalleryIdMsg; | |
| 1210 SendResponse(false); | |
| 1211 return; | |
| 1212 } | |
| 1213 | |
| 1214 gallery_watch_manager()->RemoveWatch( | |
| 1215 GetProfile(), extension_id(), gallery_pref_id); | |
| 1216 SendResponse(true); | |
| 1217 } | |
| 1218 | |
| 1219 /////////////////////////////////////////////////////////////////////////////// | |
| 1220 // MediaGalleriesGetAllGalleryWatchFunction // | |
| 1221 /////////////////////////////////////////////////////////////////////////////// | |
| 1222 | |
| 1223 MediaGalleriesGetAllGalleryWatchFunction:: | |
| 1224 ~MediaGalleriesGetAllGalleryWatchFunction() { | |
| 1225 } | |
| 1226 | |
| 1227 bool MediaGalleriesGetAllGalleryWatchFunction::RunAsync() { | |
| 1228 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 1229 if (!render_view_host() || !render_view_host()->GetProcess()) | |
| 1230 return false; | |
| 1231 | |
| 1232 MediaGalleriesPreferences* preferences = | |
| 1233 g_browser_process->media_file_system_registry()->GetPreferences( | |
| 1234 GetProfile()); | |
| 1235 preferences->EnsureInitialized(base::Bind( | |
| 1236 &MediaGalleriesGetAllGalleryWatchFunction::OnPreferencesInit, this)); | |
| 1237 return true; | |
| 1238 } | |
| 1239 | |
| 1240 void MediaGalleriesGetAllGalleryWatchFunction::OnPreferencesInit() { | |
| 1241 std::vector<std::string> result; | |
| 1242 MediaGalleryPrefIdSet gallery_ids = | |
| 1243 gallery_watch_manager()->GetWatchSet(GetProfile(), extension_id()); | |
| 1244 for (MediaGalleryPrefIdSet::const_iterator iter = gallery_ids.begin(); | |
| 1245 iter != gallery_ids.end(); | |
| 1246 ++iter) { | |
| 1247 result.push_back(base::Uint64ToString(*iter)); | |
| 1248 } | |
| 1249 results_ = GetAllGalleryWatch::Results::Create(result); | |
| 1250 SendResponse(true); | |
| 1251 } | |
| 1252 | |
| 1253 /////////////////////////////////////////////////////////////////////////////// | |
| 1254 // MediaGalleriesRemoveAllGalleryWatchFunction // | |
| 1255 /////////////////////////////////////////////////////////////////////////////// | |
| 1256 | |
| 1257 MediaGalleriesRemoveAllGalleryWatchFunction:: | |
| 1258 ~MediaGalleriesRemoveAllGalleryWatchFunction() { | |
| 1259 } | |
| 1260 | |
| 1261 bool MediaGalleriesRemoveAllGalleryWatchFunction::RunAsync() { | |
| 1262 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 1263 if (!render_view_host() || !render_view_host()->GetProcess()) | |
| 1264 return false; | |
| 1265 | |
| 1266 MediaGalleriesPreferences* preferences = | |
| 1267 g_browser_process->media_file_system_registry()->GetPreferences( | |
| 1268 GetProfile()); | |
| 1269 preferences->EnsureInitialized(base::Bind( | |
| 1270 &MediaGalleriesRemoveAllGalleryWatchFunction::OnPreferencesInit, this)); | |
| 1271 return true; | |
| 1272 } | |
| 1273 | |
| 1274 void MediaGalleriesRemoveAllGalleryWatchFunction::OnPreferencesInit() { | |
| 1275 gallery_watch_manager()->RemoveAllWatches(GetProfile(), extension_id()); | |
| 1276 SendResponse(true); | |
| 1277 } | |
| 1278 | |
| 1037 } // namespace extensions | 1279 } // namespace extensions |
| OLD | NEW |