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> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "apps/app_window.h" | 13 #include "apps/app_window.h" |
| 14 #include "apps/app_window_registry.h" | 14 #include "apps/app_window_registry.h" |
| 15 #include "base/callback.h" | 15 #include "base/callback.h" |
| 16 #include "base/lazy_instance.h" | 16 #include "base/lazy_instance.h" |
| 17 #include "base/numerics/safe_conversions.h" | |
| 17 #include "base/stl_util.h" | 18 #include "base/stl_util.h" |
| 18 #include "base/strings/string_number_conversions.h" | 19 #include "base/strings/string_number_conversions.h" |
| 19 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
| 20 #include "base/values.h" | 21 #include "base/values.h" |
| 21 #include "chrome/browser/browser_process.h" | 22 #include "chrome/browser/browser_process.h" |
| 22 #include "chrome/browser/extensions/api/file_system/file_system_api.h" | 23 #include "chrome/browser/extensions/api/file_system/file_system_api.h" |
| 24 #include "chrome/browser/extensions/blob_holder.h" | |
| 23 #include "chrome/browser/extensions/blob_reader.h" | 25 #include "chrome/browser/extensions/blob_reader.h" |
| 24 #include "chrome/browser/extensions/extension_tab_util.h" | 26 #include "chrome/browser/extensions/extension_tab_util.h" |
| 25 #include "chrome/browser/media_galleries/fileapi/safe_media_metadata_parser.h" | 27 #include "chrome/browser/media_galleries/fileapi/safe_media_metadata_parser.h" |
| 26 #include "chrome/browser/media_galleries/media_file_system_registry.h" | 28 #include "chrome/browser/media_galleries/media_file_system_registry.h" |
| 27 #include "chrome/browser/media_galleries/media_galleries_dialog_controller.h" | 29 #include "chrome/browser/media_galleries/media_galleries_dialog_controller.h" |
| 28 #include "chrome/browser/media_galleries/media_galleries_histograms.h" | 30 #include "chrome/browser/media_galleries/media_galleries_histograms.h" |
| 29 #include "chrome/browser/media_galleries/media_galleries_preferences.h" | 31 #include "chrome/browser/media_galleries/media_galleries_preferences.h" |
| 30 #include "chrome/browser/media_galleries/media_galleries_scan_result_dialog_cont roller.h" | 32 #include "chrome/browser/media_galleries/media_galleries_scan_result_dialog_cont roller.h" |
| 31 #include "chrome/browser/media_galleries/media_scan_manager.h" | 33 #include "chrome/browser/media_galleries/media_scan_manager.h" |
| 32 #include "chrome/browser/platform_util.h" | 34 #include "chrome/browser/platform_util.h" |
| 33 #include "chrome/browser/profiles/profile.h" | 35 #include "chrome/browser/profiles/profile.h" |
| 34 #include "chrome/browser/ui/chrome_select_file_policy.h" | 36 #include "chrome/browser/ui/chrome_select_file_policy.h" |
| 35 #include "chrome/common/extensions/api/media_galleries.h" | 37 #include "chrome/common/extensions/api/media_galleries.h" |
| 36 #include "chrome/common/pref_names.h" | 38 #include "chrome/common/pref_names.h" |
| 37 #include "components/storage_monitor/storage_info.h" | 39 #include "components/storage_monitor/storage_info.h" |
| 38 #include "components/web_modal/web_contents_modal_dialog_manager.h" | 40 #include "components/web_modal/web_contents_modal_dialog_manager.h" |
| 41 #include "content/public/browser/blob_handle.h" | |
| 42 #include "content/public/browser/browser_context.h" | |
| 39 #include "content/public/browser/browser_thread.h" | 43 #include "content/public/browser/browser_thread.h" |
| 40 #include "content/public/browser/child_process_security_policy.h" | 44 #include "content/public/browser/child_process_security_policy.h" |
| 41 #include "content/public/browser/render_process_host.h" | 45 #include "content/public/browser/render_process_host.h" |
| 42 #include "content/public/browser/render_view_host.h" | 46 #include "content/public/browser/render_view_host.h" |
| 43 #include "content/public/browser/web_contents.h" | 47 #include "content/public/browser/web_contents.h" |
| 44 #include "extensions/browser/event_router.h" | 48 #include "extensions/browser/event_router.h" |
| 45 #include "extensions/browser/extension_prefs.h" | 49 #include "extensions/browser/extension_prefs.h" |
| 46 #include "extensions/browser/extension_system.h" | 50 #include "extensions/browser/extension_system.h" |
| 47 #include "extensions/common/extension.h" | 51 #include "extensions/common/extension.h" |
| 48 #include "extensions/common/permissions/api_permission.h" | 52 #include "extensions/common/permissions/api_permission.h" |
| 49 #include "extensions/common/permissions/media_galleries_permission.h" | 53 #include "extensions/common/permissions/media_galleries_permission.h" |
| 50 #include "extensions/common/permissions/permissions_data.h" | 54 #include "extensions/common/permissions/permissions_data.h" |
| 51 #include "grit/generated_resources.h" | 55 #include "grit/generated_resources.h" |
| 52 #include "net/base/mime_sniffer.h" | 56 #include "net/base/mime_sniffer.h" |
| 53 #include "ui/base/l10n/l10n_util.h" | 57 #include "ui/base/l10n/l10n_util.h" |
| 58 #include "webkit/browser/blob/blob_data_handle.h" | |
| 54 | 59 |
| 55 using content::WebContents; | 60 using content::WebContents; |
| 56 using storage_monitor::MediaStorageUtil; | 61 using storage_monitor::MediaStorageUtil; |
| 57 using storage_monitor::StorageInfo; | 62 using storage_monitor::StorageInfo; |
| 58 using web_modal::WebContentsModalDialogManager; | 63 using web_modal::WebContentsModalDialogManager; |
| 59 | 64 |
| 60 namespace extensions { | 65 namespace extensions { |
| 61 | 66 |
| 62 namespace MediaGalleries = api::media_galleries; | 67 namespace MediaGalleries = api::media_galleries; |
| 63 namespace DropPermissionForMediaFileSystem = | 68 namespace DropPermissionForMediaFileSystem = |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 75 const char kNonExistentGalleryId[] = "Non-existent gallery id."; | 80 const char kNonExistentGalleryId[] = "Non-existent gallery id."; |
| 76 const char kNoScanPermission[] = "No permission to scan."; | 81 const char kNoScanPermission[] = "No permission to scan."; |
| 77 | 82 |
| 78 const char kDeviceIdKey[] = "deviceId"; | 83 const char kDeviceIdKey[] = "deviceId"; |
| 79 const char kGalleryIdKey[] = "galleryId"; | 84 const char kGalleryIdKey[] = "galleryId"; |
| 80 const char kIsAvailableKey[] = "isAvailable"; | 85 const char kIsAvailableKey[] = "isAvailable"; |
| 81 const char kIsMediaDeviceKey[] = "isMediaDevice"; | 86 const char kIsMediaDeviceKey[] = "isMediaDevice"; |
| 82 const char kIsRemovableKey[] = "isRemovable"; | 87 const char kIsRemovableKey[] = "isRemovable"; |
| 83 const char kNameKey[] = "name"; | 88 const char kNameKey[] = "name"; |
| 84 | 89 |
| 90 const char kMetadataKey[] = "metadata"; | |
| 91 const char kAttachedImagesBlobInfoKey[] = "attachedImagesBlobInfo"; | |
| 92 const char kBlobUUIDKey[] = "blobUUID"; | |
| 93 const char kTypeKey[] = "type"; | |
| 94 const char kSizeKey[] = "size"; | |
| 95 | |
| 85 MediaFileSystemRegistry* media_file_system_registry() { | 96 MediaFileSystemRegistry* media_file_system_registry() { |
| 86 return g_browser_process->media_file_system_registry(); | 97 return g_browser_process->media_file_system_registry(); |
| 87 } | 98 } |
| 88 | 99 |
| 89 MediaScanManager* media_scan_manager() { | 100 MediaScanManager* media_scan_manager() { |
| 90 return media_file_system_registry()->media_scan_manager(); | 101 return media_file_system_registry()->media_scan_manager(); |
| 91 } | 102 } |
| 92 | 103 |
| 93 // Checks whether the MediaGalleries API is currently accessible (it may be | 104 // Checks whether the MediaGalleries API is currently accessible (it may be |
| 94 // disallowed even if an extension has the requisite permission). Then | 105 // disallowed even if an extension has the requisite permission). Then |
| (...skipping 722 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 817 if (!args_->Get(1, &options_value)) | 828 if (!args_->Get(1, &options_value)) |
| 818 return false; | 829 return false; |
| 819 scoped_ptr<MediaGalleries::MediaMetadataOptions> options = | 830 scoped_ptr<MediaGalleries::MediaMetadataOptions> options = |
| 820 MediaGalleries::MediaMetadataOptions::FromValue(*options_value); | 831 MediaGalleries::MediaMetadataOptions::FromValue(*options_value); |
| 821 if (!options) | 832 if (!options) |
| 822 return false; | 833 return false; |
| 823 | 834 |
| 824 bool mime_type_only = options->metadata_type == | 835 bool mime_type_only = options->metadata_type == |
| 825 MediaGalleries::GET_METADATA_TYPE_MIMETYPEONLY; | 836 MediaGalleries::GET_METADATA_TYPE_MIMETYPEONLY; |
| 826 | 837 |
| 838 // Get attached images by default. | |
| 839 bool get_attached_images = | |
|
tommycli
2014/06/02 17:19:06
Should the default really be to get the attached i
vandebo (ex-Chrome)
2014/06/02 18:07:27
The IDL implies that we only get images for type A
tommycli
2014/06/02 19:34:38
Done.
| |
| 840 options->metadata_type == MediaGalleries::GET_METADATA_TYPE_ALL || | |
| 841 options->metadata_type == MediaGalleries::GET_METADATA_TYPE_NONE; | |
| 842 | |
| 827 return Setup(GetProfile(), &error_, base::Bind( | 843 return Setup(GetProfile(), &error_, base::Bind( |
| 828 &MediaGalleriesGetMetadataFunction::OnPreferencesInit, this, | 844 &MediaGalleriesGetMetadataFunction::OnPreferencesInit, this, |
| 829 mime_type_only, blob_uuid)); | 845 mime_type_only, get_attached_images, blob_uuid)); |
| 830 } | 846 } |
| 831 | 847 |
| 832 void MediaGalleriesGetMetadataFunction::OnPreferencesInit( | 848 void MediaGalleriesGetMetadataFunction::OnPreferencesInit( |
| 833 bool mime_type_only, const std::string& blob_uuid) { | 849 bool mime_type_only, bool get_attached_images, |
| 850 const std::string& blob_uuid) { | |
| 834 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 851 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 835 | 852 |
| 836 // BlobReader is self-deleting. | 853 // BlobReader is self-deleting. |
| 837 BlobReader* reader = new BlobReader( | 854 BlobReader* reader = new BlobReader( |
| 838 GetProfile(), | 855 GetProfile(), |
| 839 blob_uuid, | 856 blob_uuid, |
| 840 base::Bind(&MediaGalleriesGetMetadataFunction::SniffMimeType, this, | 857 base::Bind(&MediaGalleriesGetMetadataFunction::GetMetadata, this, |
| 841 mime_type_only, blob_uuid)); | 858 mime_type_only, get_attached_images, blob_uuid)); |
| 842 reader->SetByteRange(0, net::kMaxBytesToSniff); | 859 reader->SetByteRange(0, net::kMaxBytesToSniff); |
| 843 reader->Start(); | 860 reader->Start(); |
| 844 } | 861 } |
| 845 | 862 |
| 846 void MediaGalleriesGetMetadataFunction::SniffMimeType( | 863 void MediaGalleriesGetMetadataFunction::GetMetadata( |
| 847 bool mime_type_only, const std::string& blob_uuid, | 864 bool mime_type_only, bool get_attached_images, |
| 848 scoped_ptr<std::string> blob_header, int64 total_blob_length) { | 865 const std::string& blob_uuid, scoped_ptr<std::string> blob_header, |
| 866 int64 total_blob_length) { | |
| 849 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 867 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 850 | 868 |
| 851 std::string mime_type; | 869 std::string mime_type; |
| 852 bool mime_type_sniffed = net::SniffMimeTypeFromLocalData( | 870 bool mime_type_sniffed = net::SniffMimeTypeFromLocalData( |
| 853 blob_header->c_str(), blob_header->size(), &mime_type); | 871 blob_header->c_str(), blob_header->size(), &mime_type); |
| 854 | 872 |
| 855 if (!mime_type_sniffed) { | 873 if (!mime_type_sniffed) { |
| 856 SendResponse(false); | 874 SendResponse(false); |
| 857 return; | 875 return; |
| 858 } | 876 } |
| 859 | 877 |
| 860 if (mime_type_only) { | 878 if (mime_type_only) { |
| 861 MediaGalleries::MediaMetadata metadata; | 879 MediaGalleries::MediaMetadata metadata; |
| 862 metadata.mime_type = mime_type; | 880 metadata.mime_type = mime_type; |
| 863 SetResult(metadata.ToValue().release()); | 881 |
| 882 base::DictionaryValue* result_dictionary = new base::DictionaryValue; | |
| 883 result_dictionary->Set(kMetadataKey, metadata.ToValue().release()); | |
| 884 SetResult(result_dictionary); | |
| 864 SendResponse(true); | 885 SendResponse(true); |
| 865 return; | 886 return; |
| 866 } | 887 } |
| 867 | 888 |
| 868 // TODO(tommycli): Enable getting attached images. | |
| 869 scoped_refptr<metadata::SafeMediaMetadataParser> parser( | 889 scoped_refptr<metadata::SafeMediaMetadataParser> parser( |
| 870 new metadata::SafeMediaMetadataParser(GetProfile(), blob_uuid, | 890 new metadata::SafeMediaMetadataParser(GetProfile(), blob_uuid, |
| 871 total_blob_length, mime_type, | 891 total_blob_length, mime_type, |
| 872 false /* get_attached_images */)); | 892 get_attached_images)); |
| 873 parser->Start(base::Bind( | 893 parser->Start(base::Bind( |
| 874 &MediaGalleriesGetMetadataFunction::OnSafeMediaMetadataParserDone, this)); | 894 &MediaGalleriesGetMetadataFunction::OnSafeMediaMetadataParserDone, this)); |
| 875 } | 895 } |
| 876 | 896 |
| 877 void MediaGalleriesGetMetadataFunction::OnSafeMediaMetadataParserDone( | 897 void MediaGalleriesGetMetadataFunction::OnSafeMediaMetadataParserDone( |
| 878 bool parse_success, scoped_ptr<base::DictionaryValue> metadata_dictionary, | 898 bool parse_success, scoped_ptr<base::DictionaryValue> metadata_dictionary, |
| 879 scoped_ptr<std::vector<metadata::AttachedImage> > attached_images) { | 899 scoped_ptr<std::vector<metadata::AttachedImage> > attached_images) { |
| 900 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 901 | |
| 880 if (!parse_success) { | 902 if (!parse_success) { |
| 881 SendResponse(false); | 903 SendResponse(false); |
| 882 return; | 904 return; |
| 883 } | 905 } |
| 884 | 906 |
| 885 SetResult(metadata_dictionary->DeepCopy()); | 907 DCHECK(attached_images.get()); |
| 908 if (attached_images->empty()) { | |
| 909 base::DictionaryValue* result_dictionary = new base::DictionaryValue; | |
| 910 result_dictionary->Set(kMetadataKey, metadata_dictionary.release()); | |
| 911 SetResult(result_dictionary); | |
| 912 SendResponse(true); | |
| 913 return; | |
| 914 } | |
| 915 | |
| 916 metadata::AttachedImage* first_image = &attached_images->front(); | |
| 917 content::BrowserContext::CreateMemoryBackedBlob( | |
| 918 GetProfile(), | |
| 919 first_image->data.c_str(), | |
| 920 first_image->data.size(), | |
| 921 base::Bind(&MediaGalleriesGetMetadataFunction::ConstructNextBlob, | |
| 922 this, base::Passed(&metadata_dictionary), | |
| 923 base::Passed(&attached_images), | |
| 924 base::Passed(make_scoped_ptr(new std::vector<std::string>)))); | |
| 925 } | |
| 926 | |
| 927 void MediaGalleriesGetMetadataFunction::ConstructNextBlob( | |
| 928 scoped_ptr<base::DictionaryValue> metadata_dictionary, | |
| 929 scoped_ptr<std::vector<metadata::AttachedImage> > attached_images, | |
| 930 scoped_ptr<std::vector<std::string> > blob_uuids, | |
| 931 scoped_ptr<content::BlobHandle> next_blob) { | |
| 932 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 933 | |
| 934 DCHECK(metadata_dictionary.get()); | |
| 935 DCHECK(attached_images.get()); | |
| 936 DCHECK(blob_uuids.get()); | |
| 937 DCHECK(next_blob.get()); | |
| 938 | |
| 939 DCHECK(!attached_images->empty()); | |
| 940 DCHECK_LT(blob_uuids->size(), attached_images->size()); | |
| 941 | |
| 942 // For the newly constructed blob, add its UUID to list and store reference. | |
| 943 blob_uuids->push_back(next_blob->GetUUID()); | |
| 944 WebContents* contents = WebContents::FromRenderViewHost(render_view_host()); | |
| 945 extensions::BlobHolder::CreateForWebContents(contents); | |
| 946 extensions::BlobHolder* holder = | |
| 947 extensions::BlobHolder::FromWebContents(contents); | |
| 948 holder->HoldBlobReference(next_blob.Pass()); | |
| 949 | |
| 950 if (blob_uuids->size() == attached_images->size()) { | |
| 951 FinishRequest(metadata_dictionary.Pass(), attached_images.Pass(), | |
| 952 blob_uuids.Pass()); | |
| 953 return; | |
| 954 } | |
| 955 | |
| 956 // Construct the next Blob. | |
| 957 metadata::AttachedImage* next_image = &(*attached_images)[blob_uuids->size()]; | |
| 958 content::BrowserContext::CreateMemoryBackedBlob( | |
| 959 GetProfile(), | |
| 960 next_image->data.c_str(), | |
| 961 next_image->data.size(), | |
| 962 base::Bind(&MediaGalleriesGetMetadataFunction::ConstructNextBlob, | |
| 963 this, base::Passed(&metadata_dictionary), | |
| 964 base::Passed(&attached_images), base::Passed(&blob_uuids))); | |
| 965 } | |
| 966 | |
| 967 void MediaGalleriesGetMetadataFunction::FinishRequest( | |
| 968 scoped_ptr<base::DictionaryValue> metadata_dictionary, | |
| 969 scoped_ptr<std::vector<metadata::AttachedImage> > attached_images, | |
| 970 scoped_ptr<std::vector<std::string> > blob_uuids) { | |
| 971 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 972 DCHECK(metadata_dictionary.get()); | |
| 973 DCHECK(attached_images->size() == blob_uuids->size()); | |
| 974 | |
| 975 base::DictionaryValue* result_dictionary = new base::DictionaryValue; | |
| 976 result_dictionary->Set(kMetadataKey, metadata_dictionary->DeepCopy()); | |
| 977 | |
| 978 // The custom JS binding takes ownership of the Blobs in the renderer. | |
| 979 base::ListValue* attached_images_list = new base::ListValue; | |
| 980 for (size_t i = 0; i < attached_images->size(); ++i) { | |
| 981 base::DictionaryValue* attached_image = new base::DictionaryValue; | |
| 982 attached_image->Set(kBlobUUIDKey, new base::StringValue((*blob_uuids)[i])); | |
| 983 attached_image->Set(kTypeKey, new base::StringValue( | |
| 984 (*attached_images)[i].type)); | |
| 985 attached_image->Set(kSizeKey, new base::FundamentalValue( | |
| 986 base::checked_cast<int>((*attached_images)[i].data.size()))); | |
| 987 attached_images_list->Append(attached_image); | |
| 988 } | |
| 989 result_dictionary->Set(kAttachedImagesBlobInfoKey, attached_images_list); | |
| 990 | |
| 991 SetResult(result_dictionary); | |
| 992 SetTransferredBlobUUIDs(*blob_uuids); | |
| 886 SendResponse(true); | 993 SendResponse(true); |
| 887 } | 994 } |
| 888 | 995 |
| 889 } // namespace extensions | 996 } // namespace extensions |
| OLD | NEW |