[email protected] | ddc6a12 | 2014-06-27 04:52:32 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/utility/extensions/extensions_handler.h" |
| 6 | |
dcheng | e73d8520c | 2015-12-27 01:19:09 | [diff] [blame^] | 7 | #include <utility> |
| 8 | |
[email protected] | ddc6a12 | 2014-06-27 04:52:32 | [diff] [blame] | 9 | #include "base/command_line.h" |
| 10 | #include "base/path_service.h" |
avi | 2729e44 | 2015-12-26 05:27:45 | [diff] [blame] | 11 | #include "build/build_config.h" |
[email protected] | ddc6a12 | 2014-06-27 04:52:32 | [diff] [blame] | 12 | #include "chrome/common/chrome_utility_messages.h" |
| 13 | #include "chrome/common/extensions/chrome_extensions_client.h" |
| 14 | #include "chrome/common/extensions/chrome_utility_extensions_messages.h" |
[email protected] | ddc6a12 | 2014-06-27 04:52:32 | [diff] [blame] | 15 | #include "chrome/common/media_galleries/metadata_types.h" |
| 16 | #include "chrome/utility/chrome_content_utility_client.h" |
[email protected] | ddc6a12 | 2014-06-27 04:52:32 | [diff] [blame] | 17 | #include "chrome/utility/media_galleries/image_metadata_extractor.h" |
thestig | bd29d52 | 2015-10-15 21:56:16 | [diff] [blame] | 18 | #include "chrome/utility/media_galleries/ipc_data_source.h" |
| 19 | #include "chrome/utility/media_galleries/media_metadata_parser.h" |
[email protected] | ddc6a12 | 2014-06-27 04:52:32 | [diff] [blame] | 20 | #include "content/public/common/content_paths.h" |
| 21 | #include "content/public/utility/utility_thread.h" |
| 22 | #include "extensions/common/extension.h" |
| 23 | #include "extensions/common/extension_l10n_util.h" |
rockot | eb6d47fa | 2014-09-24 20:31:53 | [diff] [blame] | 24 | #include "extensions/common/extension_utility_messages.h" |
asargent | 9156f029 | 2015-01-15 01:07:02 | [diff] [blame] | 25 | #include "extensions/utility/unpacker.h" |
[email protected] | ddc6a12 | 2014-06-27 04:52:32 | [diff] [blame] | 26 | #include "media/base/media.h" |
[email protected] | ddc6a12 | 2014-06-27 04:52:32 | [diff] [blame] | 27 | #include "ui/base/ui_base_switches.h" |
| 28 | |
j.isorce | f6778e65 | 2015-11-16 17:14:25 | [diff] [blame] | 29 | #if !defined(MEDIA_DISABLE_FFMPEG) |
| 30 | #include "media/base/media_file_checker.h" |
| 31 | #endif |
| 32 | |
[email protected] | ddc6a12 | 2014-06-27 04:52:32 | [diff] [blame] | 33 | #if defined(OS_WIN) |
| 34 | #include "chrome/common/extensions/api/networking_private/networking_private_crypto.h" |
| 35 | #include "chrome/utility/media_galleries/itunes_pref_parser_win.h" |
| 36 | #include "components/wifi/wifi_service.h" |
| 37 | #endif // defined(OS_WIN) |
| 38 | |
| 39 | #if defined(OS_MACOSX) |
| 40 | #include "chrome/utility/media_galleries/iphoto_library_parser.h" |
| 41 | #endif // defined(OS_MACOSX) |
| 42 | |
| 43 | #if defined(OS_WIN) || defined(OS_MACOSX) |
| 44 | #include "chrome/utility/media_galleries/iapps_xml_utils.h" |
| 45 | #include "chrome/utility/media_galleries/itunes_library_parser.h" |
| 46 | #include "chrome/utility/media_galleries/picasa_album_table_reader.h" |
| 47 | #include "chrome/utility/media_galleries/picasa_albums_indexer.h" |
| 48 | #endif // defined(OS_WIN) || defined(OS_MACOSX) |
| 49 | |
| 50 | namespace extensions { |
| 51 | |
| 52 | namespace { |
| 53 | |
| 54 | bool Send(IPC::Message* message) { |
| 55 | return content::UtilityThread::Get()->Send(message); |
| 56 | } |
| 57 | |
| 58 | void ReleaseProcessIfNeeded() { |
| 59 | content::UtilityThread::Get()->ReleaseProcessIfNeeded(); |
| 60 | } |
| 61 | |
thestig | bd29d52 | 2015-10-15 21:56:16 | [diff] [blame] | 62 | void FinishParseMediaMetadata( |
| 63 | metadata::MediaMetadataParser* /* parser */, |
| 64 | const extensions::api::media_galleries::MediaMetadata& metadata, |
| 65 | const std::vector<metadata::AttachedImage>& attached_images) { |
| 66 | Send(new ChromeUtilityHostMsg_ParseMediaMetadata_Finished( |
| 67 | true, *metadata.ToValue(), attached_images)); |
| 68 | ReleaseProcessIfNeeded(); |
| 69 | } |
| 70 | |
[email protected] | ddc6a12 | 2014-06-27 04:52:32 | [diff] [blame] | 71 | } // namespace |
| 72 | |
thestig | bd29d52 | 2015-10-15 21:56:16 | [diff] [blame] | 73 | ExtensionsHandler::ExtensionsHandler(ChromeContentUtilityClient* utility_client) |
| 74 | : utility_client_(utility_client) { |
asargent | 275faaa | 2015-01-27 23:43:29 | [diff] [blame] | 75 | ExtensionsClient::Set(ChromeExtensionsClient::GetInstance()); |
rockot | eb6d47fa | 2014-09-24 20:31:53 | [diff] [blame] | 76 | } |
[email protected] | ddc6a12 | 2014-06-27 04:52:32 | [diff] [blame] | 77 | |
rockot | eb6d47fa | 2014-09-24 20:31:53 | [diff] [blame] | 78 | ExtensionsHandler::~ExtensionsHandler() { |
| 79 | } |
[email protected] | ddc6a12 | 2014-06-27 04:52:32 | [diff] [blame] | 80 | |
| 81 | // static |
| 82 | void ExtensionsHandler::PreSandboxStartup() { |
| 83 | // Initialize libexif for image metadata parsing. |
| 84 | metadata::ImageMetadataExtractor::InitializeLibrary(); |
| 85 | |
chcunningham | fd11b3c | 2015-06-09 02:09:42 | [diff] [blame] | 86 | // Initialize media libraries for media file validation. |
| 87 | media::InitializeMediaLibrary(); |
[email protected] | ddc6a12 | 2014-06-27 04:52:32 | [diff] [blame] | 88 | } |
| 89 | |
[email protected] | ddc6a12 | 2014-06-27 04:52:32 | [diff] [blame] | 90 | bool ExtensionsHandler::OnMessageReceived(const IPC::Message& message) { |
| 91 | bool handled = true; |
| 92 | IPC_BEGIN_MESSAGE_MAP(ExtensionsHandler, message) |
[email protected] | ddc6a12 | 2014-06-27 04:52:32 | [diff] [blame] | 93 | IPC_MESSAGE_HANDLER(ChromeUtilityMsg_CheckMediaFile, OnCheckMediaFile) |
thestig | bd29d52 | 2015-10-15 21:56:16 | [diff] [blame] | 94 | IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ParseMediaMetadata, |
| 95 | OnParseMediaMetadata) |
| 96 | |
[email protected] | ddc6a12 | 2014-06-27 04:52:32 | [diff] [blame] | 97 | #if defined(OS_WIN) |
| 98 | IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ParseITunesPrefXml, |
| 99 | OnParseITunesPrefXml) |
| 100 | #endif // defined(OS_WIN) |
| 101 | |
| 102 | #if defined(OS_MACOSX) |
| 103 | IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ParseIPhotoLibraryXmlFile, |
| 104 | OnParseIPhotoLibraryXmlFile) |
| 105 | #endif // defined(OS_MACOSX) |
| 106 | |
| 107 | #if defined(OS_WIN) || defined(OS_MACOSX) |
| 108 | IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ParseITunesLibraryXmlFile, |
| 109 | OnParseITunesLibraryXmlFile) |
| 110 | IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ParsePicasaPMPDatabase, |
| 111 | OnParsePicasaPMPDatabase) |
| 112 | IPC_MESSAGE_HANDLER(ChromeUtilityMsg_IndexPicasaAlbumsContents, |
| 113 | OnIndexPicasaAlbumsContents) |
| 114 | #endif // defined(OS_WIN) || defined(OS_MACOSX) |
| 115 | |
| 116 | #if defined(OS_WIN) |
noamsml | 8383a98 | 2014-08-29 22:45:34 | [diff] [blame] | 117 | IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_GetWiFiCredentials, |
| 118 | OnGetWiFiCredentials) |
[email protected] | ddc6a12 | 2014-06-27 04:52:32 | [diff] [blame] | 119 | #endif // defined(OS_WIN) |
| 120 | |
| 121 | IPC_MESSAGE_UNHANDLED(handled = false) |
| 122 | IPC_END_MESSAGE_MAP() |
rockot | 3813023 | 2014-11-06 18:50:01 | [diff] [blame] | 123 | return handled || utility_handler_.OnMessageReceived(message); |
[email protected] | ddc6a12 | 2014-06-27 04:52:32 | [diff] [blame] | 124 | } |
| 125 | |
[email protected] | ddc6a12 | 2014-06-27 04:52:32 | [diff] [blame] | 126 | void ExtensionsHandler::OnCheckMediaFile( |
avi | 2729e44 | 2015-12-26 05:27:45 | [diff] [blame] | 127 | int64_t milliseconds_of_decoding, |
[email protected] | ddc6a12 | 2014-06-27 04:52:32 | [diff] [blame] | 128 | const IPC::PlatformFileForTransit& media_file) { |
j.isorce | f6778e65 | 2015-11-16 17:14:25 | [diff] [blame] | 129 | #if !defined(MEDIA_DISABLE_FFMPEG) |
[email protected] | ddc6a12 | 2014-06-27 04:52:32 | [diff] [blame] | 130 | media::MediaFileChecker checker( |
| 131 | IPC::PlatformFileForTransitToFile(media_file)); |
| 132 | const bool check_success = checker.Start( |
| 133 | base::TimeDelta::FromMilliseconds(milliseconds_of_decoding)); |
| 134 | Send(new ChromeUtilityHostMsg_CheckMediaFile_Finished(check_success)); |
j.isorce | f6778e65 | 2015-11-16 17:14:25 | [diff] [blame] | 135 | #else |
| 136 | Send(new ChromeUtilityHostMsg_CheckMediaFile_Finished(false)); |
| 137 | #endif |
[email protected] | ddc6a12 | 2014-06-27 04:52:32 | [diff] [blame] | 138 | ReleaseProcessIfNeeded(); |
| 139 | } |
| 140 | |
avi | 2729e44 | 2015-12-26 05:27:45 | [diff] [blame] | 141 | void ExtensionsHandler::OnParseMediaMetadata(const std::string& mime_type, |
| 142 | int64_t total_size, |
| 143 | bool get_attached_images) { |
thestig | bd29d52 | 2015-10-15 21:56:16 | [diff] [blame] | 144 | // Only one IPCDataSource may be created and added to the list of handlers. |
| 145 | scoped_ptr<metadata::IPCDataSource> source( |
| 146 | new metadata::IPCDataSource(total_size)); |
| 147 | metadata::MediaMetadataParser* parser = new metadata::MediaMetadataParser( |
| 148 | source.get(), mime_type, get_attached_images); |
dcheng | e73d8520c | 2015-12-27 01:19:09 | [diff] [blame^] | 149 | utility_client_->AddHandler(std::move(source)); |
thestig | bd29d52 | 2015-10-15 21:56:16 | [diff] [blame] | 150 | parser->Start(base::Bind(&FinishParseMediaMetadata, base::Owned(parser))); |
| 151 | } |
| 152 | |
[email protected] | ddc6a12 | 2014-06-27 04:52:32 | [diff] [blame] | 153 | #if defined(OS_WIN) |
| 154 | void ExtensionsHandler::OnParseITunesPrefXml( |
| 155 | const std::string& itunes_xml_data) { |
| 156 | base::FilePath library_path( |
| 157 | itunes::FindLibraryLocationInPrefXml(itunes_xml_data)); |
| 158 | Send(new ChromeUtilityHostMsg_GotITunesDirectory(library_path)); |
| 159 | ReleaseProcessIfNeeded(); |
| 160 | } |
| 161 | #endif // defined(OS_WIN) |
| 162 | |
| 163 | #if defined(OS_MACOSX) |
| 164 | void ExtensionsHandler::OnParseIPhotoLibraryXmlFile( |
| 165 | const IPC::PlatformFileForTransit& iphoto_library_file) { |
| 166 | iphoto::IPhotoLibraryParser parser; |
| 167 | base::File file = IPC::PlatformFileForTransitToFile(iphoto_library_file); |
| 168 | bool result = parser.Parse(iapps::ReadFileAsString(file.Pass())); |
| 169 | Send(new ChromeUtilityHostMsg_GotIPhotoLibrary(result, parser.library())); |
| 170 | ReleaseProcessIfNeeded(); |
| 171 | } |
| 172 | #endif // defined(OS_MACOSX) |
| 173 | |
| 174 | #if defined(OS_WIN) || defined(OS_MACOSX) |
| 175 | void ExtensionsHandler::OnParseITunesLibraryXmlFile( |
| 176 | const IPC::PlatformFileForTransit& itunes_library_file) { |
| 177 | itunes::ITunesLibraryParser parser; |
| 178 | base::File file = IPC::PlatformFileForTransitToFile(itunes_library_file); |
| 179 | bool result = parser.Parse(iapps::ReadFileAsString(file.Pass())); |
| 180 | Send(new ChromeUtilityHostMsg_GotITunesLibrary(result, parser.library())); |
| 181 | ReleaseProcessIfNeeded(); |
| 182 | } |
| 183 | |
| 184 | void ExtensionsHandler::OnParsePicasaPMPDatabase( |
| 185 | const picasa::AlbumTableFilesForTransit& album_table_files) { |
| 186 | picasa::AlbumTableFiles files; |
| 187 | files.indicator_file = |
| 188 | IPC::PlatformFileForTransitToFile(album_table_files.indicator_file); |
| 189 | files.category_file = |
| 190 | IPC::PlatformFileForTransitToFile(album_table_files.category_file); |
| 191 | files.date_file = |
| 192 | IPC::PlatformFileForTransitToFile(album_table_files.date_file); |
| 193 | files.filename_file = |
| 194 | IPC::PlatformFileForTransitToFile(album_table_files.filename_file); |
| 195 | files.name_file = |
| 196 | IPC::PlatformFileForTransitToFile(album_table_files.name_file); |
| 197 | files.token_file = |
| 198 | IPC::PlatformFileForTransitToFile(album_table_files.token_file); |
| 199 | files.uid_file = |
| 200 | IPC::PlatformFileForTransitToFile(album_table_files.uid_file); |
| 201 | |
| 202 | picasa::PicasaAlbumTableReader reader(files.Pass()); |
| 203 | bool parse_success = reader.Init(); |
| 204 | Send(new ChromeUtilityHostMsg_ParsePicasaPMPDatabase_Finished( |
rockot | eb6d47fa | 2014-09-24 20:31:53 | [diff] [blame] | 205 | parse_success, reader.albums(), reader.folders())); |
[email protected] | ddc6a12 | 2014-06-27 04:52:32 | [diff] [blame] | 206 | ReleaseProcessIfNeeded(); |
| 207 | } |
| 208 | |
| 209 | void ExtensionsHandler::OnIndexPicasaAlbumsContents( |
| 210 | const picasa::AlbumUIDSet& album_uids, |
| 211 | const std::vector<picasa::FolderINIContents>& folders_inis) { |
| 212 | picasa::PicasaAlbumsIndexer indexer(album_uids); |
| 213 | indexer.ParseFolderINI(folders_inis); |
| 214 | |
| 215 | Send(new ChromeUtilityHostMsg_IndexPicasaAlbumsContents_Finished( |
| 216 | indexer.albums_images())); |
| 217 | ReleaseProcessIfNeeded(); |
| 218 | } |
| 219 | #endif // defined(OS_WIN) || defined(OS_MACOSX) |
| 220 | |
| 221 | #if defined(OS_WIN) |
noamsml | 8383a98 | 2014-08-29 22:45:34 | [diff] [blame] | 222 | void ExtensionsHandler::OnGetWiFiCredentials(const std::string& network_guid) { |
[email protected] | ddc6a12 | 2014-06-27 04:52:32 | [diff] [blame] | 223 | scoped_ptr<wifi::WiFiService> wifi_service(wifi::WiFiService::Create()); |
| 224 | wifi_service->Initialize(NULL); |
| 225 | |
| 226 | std::string key_data; |
| 227 | std::string error; |
| 228 | wifi_service->GetKeyFromSystem(network_guid, &key_data, &error); |
| 229 | |
noamsml | 8383a98 | 2014-08-29 22:45:34 | [diff] [blame] | 230 | Send(new ChromeUtilityHostMsg_GotWiFiCredentials(key_data, error.empty())); |
[email protected] | ddc6a12 | 2014-06-27 04:52:32 | [diff] [blame] | 231 | } |
| 232 | #endif // defined(OS_WIN) |
| 233 | |
| 234 | } // namespace extensions |