| 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 <stddef.h> | 9 #include <stddef.h> |
| 10 | 10 |
| 11 #include <memory> |
| 11 #include <set> | 12 #include <set> |
| 12 #include <string> | 13 #include <string> |
| 13 #include <utility> | 14 #include <utility> |
| 14 #include <vector> | 15 #include <vector> |
| 15 | 16 |
| 16 #include "base/callback.h" | 17 #include "base/callback.h" |
| 17 #include "base/lazy_instance.h" | 18 #include "base/lazy_instance.h" |
| 18 #include "base/macros.h" | 19 #include "base/macros.h" |
| 19 #include "base/memory/ptr_util.h" | 20 #include "base/memory/ptr_util.h" |
| 20 #include "base/numerics/safe_conversions.h" | 21 #include "base/numerics/safe_conversions.h" |
| (...skipping 409 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 430 void MediaGalleriesGetMediaFileSystemsFunction::ReturnGalleries( | 431 void MediaGalleriesGetMediaFileSystemsFunction::ReturnGalleries( |
| 431 const std::vector<MediaFileSystemInfo>& filesystems) { | 432 const std::vector<MediaFileSystemInfo>& filesystems) { |
| 432 std::unique_ptr<base::ListValue> list( | 433 std::unique_ptr<base::ListValue> list( |
| 433 ConstructFileSystemList(render_frame_host(), extension(), filesystems)); | 434 ConstructFileSystemList(render_frame_host(), extension(), filesystems)); |
| 434 if (!list.get()) { | 435 if (!list.get()) { |
| 435 SendResponse(false); | 436 SendResponse(false); |
| 436 return; | 437 return; |
| 437 } | 438 } |
| 438 | 439 |
| 439 // The custom JS binding will use this list to create DOMFileSystem objects. | 440 // The custom JS binding will use this list to create DOMFileSystem objects. |
| 440 SetResult(list.release()); | 441 SetResult(std::move(list)); |
| 441 SendResponse(true); | 442 SendResponse(true); |
| 442 } | 443 } |
| 443 | 444 |
| 444 void MediaGalleriesGetMediaFileSystemsFunction::ShowDialog() { | 445 void MediaGalleriesGetMediaFileSystemsFunction::ShowDialog() { |
| 445 media_galleries::UsageCount(media_galleries::SHOW_DIALOG); | 446 media_galleries::UsageCount(media_galleries::SHOW_DIALOG); |
| 446 WebContents* contents = | 447 WebContents* contents = |
| 447 ChromeExtensionFunctionDetails(this).GetOriginWebContents(); | 448 ChromeExtensionFunctionDetails(this).GetOriginWebContents(); |
| 448 if (!contents) { | 449 if (!contents) { |
| 449 SendResponse(false); | 450 SendResponse(false); |
| 450 return; | 451 return; |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 546 | 547 |
| 547 int index = -1; | 548 int index = -1; |
| 548 if (pref_id != kInvalidMediaGalleryPrefId) { | 549 if (pref_id != kInvalidMediaGalleryPrefId) { |
| 549 for (size_t i = 0; i < filesystems.size(); ++i) { | 550 for (size_t i = 0; i < filesystems.size(); ++i) { |
| 550 if (filesystems[i].pref_id == pref_id) { | 551 if (filesystems[i].pref_id == pref_id) { |
| 551 index = i; | 552 index = i; |
| 552 break; | 553 break; |
| 553 } | 554 } |
| 554 } | 555 } |
| 555 } | 556 } |
| 556 base::DictionaryValue* results = new base::DictionaryValue; | 557 std::unique_ptr<base::DictionaryValue> results(new base::DictionaryValue); |
| 557 results->SetWithoutPathExpansion("mediaFileSystems", list.release()); | 558 results->SetWithoutPathExpansion("mediaFileSystems", list.release()); |
| 558 results->SetIntegerWithoutPathExpansion("selectedFileSystemIndex", index); | 559 results->SetIntegerWithoutPathExpansion("selectedFileSystemIndex", index); |
| 559 SetResult(results); | 560 SetResult(std::move(results)); |
| 560 SendResponse(true); | 561 SendResponse(true); |
| 561 } | 562 } |
| 562 | 563 |
| 563 void | 564 void |
| 564 MediaGalleriesAddUserSelectedFolderFunction::GetMediaFileSystemsForExtension( | 565 MediaGalleriesAddUserSelectedFolderFunction::GetMediaFileSystemsForExtension( |
| 565 const MediaFileSystemsCallback& cb) { | 566 const MediaFileSystemsCallback& cb) { |
| 566 if (!render_frame_host()) { | 567 if (!render_frame_host()) { |
| 567 cb.Run(std::vector<MediaFileSystemInfo>()); | 568 cb.Run(std::vector<MediaFileSystemInfo>()); |
| 568 return; | 569 return; |
| 569 } | 570 } |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 624 | 625 |
| 625 if (!mime_type_sniffed) { | 626 if (!mime_type_sniffed) { |
| 626 SendResponse(false); | 627 SendResponse(false); |
| 627 return; | 628 return; |
| 628 } | 629 } |
| 629 | 630 |
| 630 if (metadata_type == MediaGalleries::GET_METADATA_TYPE_MIMETYPEONLY) { | 631 if (metadata_type == MediaGalleries::GET_METADATA_TYPE_MIMETYPEONLY) { |
| 631 MediaGalleries::MediaMetadata metadata; | 632 MediaGalleries::MediaMetadata metadata; |
| 632 metadata.mime_type = mime_type; | 633 metadata.mime_type = mime_type; |
| 633 | 634 |
| 634 base::DictionaryValue* result_dictionary = new base::DictionaryValue; | 635 std::unique_ptr<base::DictionaryValue> result_dictionary( |
| 636 new base::DictionaryValue); |
| 635 result_dictionary->Set(kMetadataKey, metadata.ToValue().release()); | 637 result_dictionary->Set(kMetadataKey, metadata.ToValue().release()); |
| 636 SetResult(result_dictionary); | 638 SetResult(std::move(result_dictionary)); |
| 637 SendResponse(true); | 639 SendResponse(true); |
| 638 return; | 640 return; |
| 639 } | 641 } |
| 640 | 642 |
| 641 // We get attached images by default. GET_METADATA_TYPE_NONE is the default | 643 // We get attached images by default. GET_METADATA_TYPE_NONE is the default |
| 642 // value if the caller doesn't specify the metadata type. | 644 // value if the caller doesn't specify the metadata type. |
| 643 bool get_attached_images = | 645 bool get_attached_images = |
| 644 metadata_type == MediaGalleries::GET_METADATA_TYPE_ALL || | 646 metadata_type == MediaGalleries::GET_METADATA_TYPE_ALL || |
| 645 metadata_type == MediaGalleries::GET_METADATA_TYPE_NONE; | 647 metadata_type == MediaGalleries::GET_METADATA_TYPE_NONE; |
| 646 | 648 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 664 } | 666 } |
| 665 | 667 |
| 666 DCHECK(metadata_dictionary.get()); | 668 DCHECK(metadata_dictionary.get()); |
| 667 DCHECK(attached_images.get()); | 669 DCHECK(attached_images.get()); |
| 668 | 670 |
| 669 std::unique_ptr<base::DictionaryValue> result_dictionary( | 671 std::unique_ptr<base::DictionaryValue> result_dictionary( |
| 670 new base::DictionaryValue); | 672 new base::DictionaryValue); |
| 671 result_dictionary->Set(kMetadataKey, metadata_dictionary.release()); | 673 result_dictionary->Set(kMetadataKey, metadata_dictionary.release()); |
| 672 | 674 |
| 673 if (attached_images->empty()) { | 675 if (attached_images->empty()) { |
| 674 SetResult(result_dictionary.release()); | 676 SetResult(std::move(result_dictionary)); |
| 675 SendResponse(true); | 677 SendResponse(true); |
| 676 return; | 678 return; |
| 677 } | 679 } |
| 678 | 680 |
| 679 result_dictionary->Set(kAttachedImagesBlobInfoKey, new base::ListValue); | 681 result_dictionary->Set(kAttachedImagesBlobInfoKey, new base::ListValue); |
| 680 metadata::AttachedImage* first_image = &attached_images->front(); | 682 metadata::AttachedImage* first_image = &attached_images->front(); |
| 681 content::BrowserContext::CreateMemoryBackedBlob( | 683 content::BrowserContext::CreateMemoryBackedBlob( |
| 682 GetProfile(), first_image->data.c_str(), first_image->data.size(), | 684 GetProfile(), first_image->data.c_str(), first_image->data.size(), |
| 683 base::Bind(&MediaGalleriesGetMetadataFunction::ConstructNextBlob, this, | 685 base::Bind(&MediaGalleriesGetMetadataFunction::ConstructNextBlob, this, |
| 684 base::Passed(&result_dictionary), | 686 base::Passed(&result_dictionary), |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 732 GetProfile(), | 734 GetProfile(), |
| 733 next_image->data.c_str(), | 735 next_image->data.c_str(), |
| 734 next_image->data.size(), | 736 next_image->data.size(), |
| 735 base::Bind(&MediaGalleriesGetMetadataFunction::ConstructNextBlob, | 737 base::Bind(&MediaGalleriesGetMetadataFunction::ConstructNextBlob, |
| 736 this, base::Passed(&result_dictionary), | 738 this, base::Passed(&result_dictionary), |
| 737 base::Passed(&attached_images), base::Passed(&blob_uuids))); | 739 base::Passed(&attached_images), base::Passed(&blob_uuids))); |
| 738 return; | 740 return; |
| 739 } | 741 } |
| 740 | 742 |
| 741 // All Blobs have been constructed. The renderer will take ownership. | 743 // All Blobs have been constructed. The renderer will take ownership. |
| 742 SetResult(result_dictionary.release()); | 744 SetResult(std::move(result_dictionary)); |
| 743 SetTransferredBlobUUIDs(*blob_uuids); | 745 SetTransferredBlobUUIDs(*blob_uuids); |
| 744 SendResponse(true); | 746 SendResponse(true); |
| 745 } | 747 } |
| 746 | 748 |
| 747 /////////////////////////////////////////////////////////////////////////////// | 749 /////////////////////////////////////////////////////////////////////////////// |
| 748 // MediaGalleriesAddGalleryWatchFunction // | 750 // MediaGalleriesAddGalleryWatchFunction // |
| 749 /////////////////////////////////////////////////////////////////////////////// | 751 /////////////////////////////////////////////////////////////////////////////// |
| 750 MediaGalleriesAddGalleryWatchFunction:: | 752 MediaGalleriesAddGalleryWatchFunction:: |
| 751 ~MediaGalleriesAddGalleryWatchFunction() { | 753 ~MediaGalleriesAddGalleryWatchFunction() { |
| 752 } | 754 } |
| (...skipping 25 matching lines...) Expand all Loading... |
| 778 MediaGalleryPrefId gallery_pref_id = kInvalidMediaGalleryPrefId; | 780 MediaGalleryPrefId gallery_pref_id = kInvalidMediaGalleryPrefId; |
| 779 if (!GetGalleryFilePathAndId(pref_id, | 781 if (!GetGalleryFilePathAndId(pref_id, |
| 780 GetProfile(), | 782 GetProfile(), |
| 781 extension(), | 783 extension(), |
| 782 &gallery_file_path, | 784 &gallery_file_path, |
| 783 &gallery_pref_id)) { | 785 &gallery_pref_id)) { |
| 784 api::media_galleries::AddGalleryWatchResult result; | 786 api::media_galleries::AddGalleryWatchResult result; |
| 785 error_ = kInvalidGalleryIdMsg; | 787 error_ = kInvalidGalleryIdMsg; |
| 786 result.gallery_id = kInvalidGalleryId; | 788 result.gallery_id = kInvalidGalleryId; |
| 787 result.success = false; | 789 result.success = false; |
| 788 SetResult(result.ToValue().release()); | 790 SetResult(result.ToValue()); |
| 789 SendResponse(false); | 791 SendResponse(false); |
| 790 return; | 792 return; |
| 791 } | 793 } |
| 792 | 794 |
| 793 gallery_watch_manager()->AddWatch( | 795 gallery_watch_manager()->AddWatch( |
| 794 GetProfile(), | 796 GetProfile(), |
| 795 extension(), | 797 extension(), |
| 796 gallery_pref_id, | 798 gallery_pref_id, |
| 797 base::Bind(&MediaGalleriesAddGalleryWatchFunction::HandleResponse, | 799 base::Bind(&MediaGalleriesAddGalleryWatchFunction::HandleResponse, |
| 798 this, | 800 this, |
| 799 gallery_pref_id)); | 801 gallery_pref_id)); |
| 800 } | 802 } |
| 801 | 803 |
| 802 void MediaGalleriesAddGalleryWatchFunction::HandleResponse( | 804 void MediaGalleriesAddGalleryWatchFunction::HandleResponse( |
| 803 MediaGalleryPrefId gallery_id, | 805 MediaGalleryPrefId gallery_id, |
| 804 const std::string& error) { | 806 const std::string& error) { |
| 805 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 807 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 806 | 808 |
| 807 // If an app added a file watch without any event listeners on the | 809 // If an app added a file watch without any event listeners on the |
| 808 // onGalleryChanged event, that's an error. | 810 // onGalleryChanged event, that's an error. |
| 809 MediaGalleriesEventRouter* api = MediaGalleriesEventRouter::Get(GetProfile()); | 811 MediaGalleriesEventRouter* api = MediaGalleriesEventRouter::Get(GetProfile()); |
| 810 api::media_galleries::AddGalleryWatchResult result; | 812 api::media_galleries::AddGalleryWatchResult result; |
| 811 result.gallery_id = base::Uint64ToString(gallery_id); | 813 result.gallery_id = base::Uint64ToString(gallery_id); |
| 812 | 814 |
| 813 if (!api->ExtensionHasGalleryChangeListener(extension()->id())) { | 815 if (!api->ExtensionHasGalleryChangeListener(extension()->id())) { |
| 814 result.success = false; | 816 result.success = false; |
| 815 SetResult(result.ToValue().release()); | 817 SetResult(result.ToValue()); |
| 816 error_ = kMissingEventListener; | 818 error_ = kMissingEventListener; |
| 817 SendResponse(false); | 819 SendResponse(false); |
| 818 return; | 820 return; |
| 819 } | 821 } |
| 820 | 822 |
| 821 result.success = error.empty(); | 823 result.success = error.empty(); |
| 822 SetResult(result.ToValue().release()); | 824 SetResult(result.ToValue()); |
| 823 if (error.empty()) { | 825 if (error.empty()) { |
| 824 SendResponse(true); | 826 SendResponse(true); |
| 825 } else { | 827 } else { |
| 826 error_ = error.c_str(); | 828 error_ = error.c_str(); |
| 827 SendResponse(false); | 829 SendResponse(false); |
| 828 } | 830 } |
| 829 } | 831 } |
| 830 | 832 |
| 831 /////////////////////////////////////////////////////////////////////////////// | 833 /////////////////////////////////////////////////////////////////////////////// |
| 832 // MediaGalleriesRemoveGalleryWatchFunction // | 834 // MediaGalleriesRemoveGalleryWatchFunction // |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 868 SendResponse(false); | 870 SendResponse(false); |
| 869 return; | 871 return; |
| 870 } | 872 } |
| 871 | 873 |
| 872 gallery_watch_manager()->RemoveWatch( | 874 gallery_watch_manager()->RemoveWatch( |
| 873 GetProfile(), extension_id(), gallery_pref_id); | 875 GetProfile(), extension_id(), gallery_pref_id); |
| 874 SendResponse(true); | 876 SendResponse(true); |
| 875 } | 877 } |
| 876 | 878 |
| 877 } // namespace extensions | 879 } // namespace extensions |
| OLD | NEW |