OLD | NEW |
(Empty) | |
| 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 |
| 7 #include "base/command_line.h" |
| 8 #include "base/path_service.h" |
| 9 #include "chrome/common/chrome_utility_messages.h" |
| 10 #include "chrome/common/extensions/chrome_extensions_client.h" |
| 11 #include "chrome/common/extensions/chrome_utility_extensions_messages.h" |
| 12 #include "chrome/common/extensions/update_manifest.h" |
| 13 #include "chrome/common/media_galleries/metadata_types.h" |
| 14 #include "chrome/utility/chrome_content_utility_client.h" |
| 15 #include "chrome/utility/extensions/unpacker.h" |
| 16 #include "chrome/utility/media_galleries/image_metadata_extractor.h" |
| 17 #include "content/public/common/content_paths.h" |
| 18 #include "content/public/utility/utility_thread.h" |
| 19 #include "extensions/common/extension.h" |
| 20 #include "extensions/common/extension_l10n_util.h" |
| 21 #include "extensions/common/manifest.h" |
| 22 #include "media/base/media.h" |
| 23 #include "media/base/media_file_checker.h" |
| 24 #include "ui/base/ui_base_switches.h" |
| 25 |
| 26 #if defined(OS_WIN) |
| 27 #include "chrome/common/extensions/api/networking_private/networking_private_cry
pto.h" |
| 28 #include "chrome/utility/media_galleries/itunes_pref_parser_win.h" |
| 29 #include "components/wifi/wifi_service.h" |
| 30 #endif // defined(OS_WIN) |
| 31 |
| 32 #if defined(OS_MACOSX) |
| 33 #include "chrome/utility/media_galleries/iphoto_library_parser.h" |
| 34 #endif // defined(OS_MACOSX) |
| 35 |
| 36 #if defined(OS_WIN) || defined(OS_MACOSX) |
| 37 #include "chrome/utility/media_galleries/iapps_xml_utils.h" |
| 38 #include "chrome/utility/media_galleries/itunes_library_parser.h" |
| 39 #include "chrome/utility/media_galleries/picasa_album_table_reader.h" |
| 40 #include "chrome/utility/media_galleries/picasa_albums_indexer.h" |
| 41 #endif // defined(OS_WIN) || defined(OS_MACOSX) |
| 42 |
| 43 namespace extensions { |
| 44 |
| 45 namespace { |
| 46 |
| 47 bool Send(IPC::Message* message) { |
| 48 return content::UtilityThread::Get()->Send(message); |
| 49 } |
| 50 |
| 51 void ReleaseProcessIfNeeded() { |
| 52 content::UtilityThread::Get()->ReleaseProcessIfNeeded(); |
| 53 } |
| 54 |
| 55 } // namespace |
| 56 |
| 57 ExtensionsHandler::ExtensionsHandler() {} |
| 58 |
| 59 ExtensionsHandler::~ExtensionsHandler() {} |
| 60 |
| 61 // static |
| 62 void ExtensionsHandler::PreSandboxStartup() { |
| 63 // Initialize libexif for image metadata parsing. |
| 64 metadata::ImageMetadataExtractor::InitializeLibrary(); |
| 65 |
| 66 // Load media libraries for media file validation. |
| 67 base::FilePath media_path; |
| 68 PathService::Get(content::DIR_MEDIA_LIBS, &media_path); |
| 69 if (!media_path.empty()) |
| 70 media::InitializeMediaLibrary(media_path); |
| 71 } |
| 72 |
| 73 void ExtensionsHandler::UtilityThreadStarted() { |
| 74 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 75 std::string lang = command_line->GetSwitchValueASCII(switches::kLang); |
| 76 if (!lang.empty()) |
| 77 extension_l10n_util::SetProcessLocale(lang); |
| 78 } |
| 79 |
| 80 bool ExtensionsHandler::OnMessageReceived(const IPC::Message& message) { |
| 81 bool handled = true; |
| 82 IPC_BEGIN_MESSAGE_MAP(ExtensionsHandler, message) |
| 83 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_UnpackExtension, OnUnpackExtension) |
| 84 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ParseUpdateManifest, |
| 85 OnParseUpdateManifest) |
| 86 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_DecodeImageBase64, OnDecodeImageBase64) |
| 87 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ParseJSON, OnParseJSON) |
| 88 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_CheckMediaFile, OnCheckMediaFile) |
| 89 #if defined(OS_WIN) |
| 90 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ParseITunesPrefXml, |
| 91 OnParseITunesPrefXml) |
| 92 #endif // defined(OS_WIN) |
| 93 |
| 94 #if defined(OS_MACOSX) |
| 95 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ParseIPhotoLibraryXmlFile, |
| 96 OnParseIPhotoLibraryXmlFile) |
| 97 #endif // defined(OS_MACOSX) |
| 98 |
| 99 #if defined(OS_WIN) || defined(OS_MACOSX) |
| 100 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ParseITunesLibraryXmlFile, |
| 101 OnParseITunesLibraryXmlFile) |
| 102 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ParsePicasaPMPDatabase, |
| 103 OnParsePicasaPMPDatabase) |
| 104 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_IndexPicasaAlbumsContents, |
| 105 OnIndexPicasaAlbumsContents) |
| 106 #endif // defined(OS_WIN) || defined(OS_MACOSX) |
| 107 |
| 108 #if defined(OS_WIN) |
| 109 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_GetAndEncryptWiFiCredentials, |
| 110 OnGetAndEncryptWiFiCredentials) |
| 111 #endif // defined(OS_WIN) |
| 112 |
| 113 IPC_MESSAGE_UNHANDLED(handled = false) |
| 114 IPC_END_MESSAGE_MAP() |
| 115 return handled; |
| 116 } |
| 117 |
| 118 void ExtensionsHandler::OnUnpackExtension( |
| 119 const base::FilePath& extension_path, |
| 120 const std::string& extension_id, |
| 121 int location, |
| 122 int creation_flags) { |
| 123 CHECK_GT(location, Manifest::INVALID_LOCATION); |
| 124 CHECK_LT(location, Manifest::NUM_LOCATIONS); |
| 125 ExtensionsClient::Set(ChromeExtensionsClient::GetInstance()); |
| 126 Unpacker unpacker(extension_path, |
| 127 extension_id, |
| 128 static_cast<Manifest::Location>(location), |
| 129 creation_flags); |
| 130 if (unpacker.Run() && unpacker.DumpImagesToFile() && |
| 131 unpacker.DumpMessageCatalogsToFile()) { |
| 132 Send(new ChromeUtilityHostMsg_UnpackExtension_Succeeded( |
| 133 *unpacker.parsed_manifest())); |
| 134 } else { |
| 135 Send(new ChromeUtilityHostMsg_UnpackExtension_Failed( |
| 136 unpacker.error_message())); |
| 137 } |
| 138 |
| 139 ReleaseProcessIfNeeded(); |
| 140 } |
| 141 |
| 142 void ExtensionsHandler::OnParseUpdateManifest(const std::string& xml) { |
| 143 UpdateManifest manifest; |
| 144 if (!manifest.Parse(xml)) { |
| 145 Send(new ChromeUtilityHostMsg_ParseUpdateManifest_Failed( |
| 146 manifest.errors())); |
| 147 } else { |
| 148 Send(new ChromeUtilityHostMsg_ParseUpdateManifest_Succeeded( |
| 149 manifest.results())); |
| 150 } |
| 151 ReleaseProcessIfNeeded(); |
| 152 } |
| 153 |
| 154 void ExtensionsHandler::OnDecodeImageBase64( |
| 155 const std::string& encoded_string) { |
| 156 std::string decoded_string; |
| 157 |
| 158 if (!base::Base64Decode(encoded_string, &decoded_string)) { |
| 159 Send(new ChromeUtilityHostMsg_DecodeImage_Failed()); |
| 160 return; |
| 161 } |
| 162 |
| 163 std::vector<unsigned char> decoded_vector(decoded_string.size()); |
| 164 for (size_t i = 0; i < decoded_string.size(); ++i) { |
| 165 decoded_vector[i] = static_cast<unsigned char>(decoded_string[i]); |
| 166 } |
| 167 |
| 168 ChromeContentUtilityClient::DecodeImage(decoded_vector); |
| 169 } |
| 170 |
| 171 void ExtensionsHandler::OnParseJSON(const std::string& json) { |
| 172 int error_code; |
| 173 std::string error; |
| 174 base::Value* value = base::JSONReader::ReadAndReturnError( |
| 175 json, base::JSON_PARSE_RFC, &error_code, &error); |
| 176 if (value) { |
| 177 base::ListValue wrapper; |
| 178 wrapper.Append(value); |
| 179 Send(new ChromeUtilityHostMsg_ParseJSON_Succeeded(wrapper)); |
| 180 } else { |
| 181 Send(new ChromeUtilityHostMsg_ParseJSON_Failed(error)); |
| 182 } |
| 183 ReleaseProcessIfNeeded(); |
| 184 } |
| 185 |
| 186 void ExtensionsHandler::OnCheckMediaFile( |
| 187 int64 milliseconds_of_decoding, |
| 188 const IPC::PlatformFileForTransit& media_file) { |
| 189 media::MediaFileChecker checker( |
| 190 IPC::PlatformFileForTransitToFile(media_file)); |
| 191 const bool check_success = checker.Start( |
| 192 base::TimeDelta::FromMilliseconds(milliseconds_of_decoding)); |
| 193 Send(new ChromeUtilityHostMsg_CheckMediaFile_Finished(check_success)); |
| 194 ReleaseProcessIfNeeded(); |
| 195 } |
| 196 |
| 197 #if defined(OS_WIN) |
| 198 void ExtensionsHandler::OnParseITunesPrefXml( |
| 199 const std::string& itunes_xml_data) { |
| 200 base::FilePath library_path( |
| 201 itunes::FindLibraryLocationInPrefXml(itunes_xml_data)); |
| 202 Send(new ChromeUtilityHostMsg_GotITunesDirectory(library_path)); |
| 203 ReleaseProcessIfNeeded(); |
| 204 } |
| 205 #endif // defined(OS_WIN) |
| 206 |
| 207 #if defined(OS_MACOSX) |
| 208 void ExtensionsHandler::OnParseIPhotoLibraryXmlFile( |
| 209 const IPC::PlatformFileForTransit& iphoto_library_file) { |
| 210 iphoto::IPhotoLibraryParser parser; |
| 211 base::File file = IPC::PlatformFileForTransitToFile(iphoto_library_file); |
| 212 bool result = parser.Parse(iapps::ReadFileAsString(file.Pass())); |
| 213 Send(new ChromeUtilityHostMsg_GotIPhotoLibrary(result, parser.library())); |
| 214 ReleaseProcessIfNeeded(); |
| 215 } |
| 216 #endif // defined(OS_MACOSX) |
| 217 |
| 218 #if defined(OS_WIN) || defined(OS_MACOSX) |
| 219 void ExtensionsHandler::OnParseITunesLibraryXmlFile( |
| 220 const IPC::PlatformFileForTransit& itunes_library_file) { |
| 221 itunes::ITunesLibraryParser parser; |
| 222 base::File file = IPC::PlatformFileForTransitToFile(itunes_library_file); |
| 223 bool result = parser.Parse(iapps::ReadFileAsString(file.Pass())); |
| 224 Send(new ChromeUtilityHostMsg_GotITunesLibrary(result, parser.library())); |
| 225 ReleaseProcessIfNeeded(); |
| 226 } |
| 227 |
| 228 void ExtensionsHandler::OnParsePicasaPMPDatabase( |
| 229 const picasa::AlbumTableFilesForTransit& album_table_files) { |
| 230 picasa::AlbumTableFiles files; |
| 231 files.indicator_file = |
| 232 IPC::PlatformFileForTransitToFile(album_table_files.indicator_file); |
| 233 files.category_file = |
| 234 IPC::PlatformFileForTransitToFile(album_table_files.category_file); |
| 235 files.date_file = |
| 236 IPC::PlatformFileForTransitToFile(album_table_files.date_file); |
| 237 files.filename_file = |
| 238 IPC::PlatformFileForTransitToFile(album_table_files.filename_file); |
| 239 files.name_file = |
| 240 IPC::PlatformFileForTransitToFile(album_table_files.name_file); |
| 241 files.token_file = |
| 242 IPC::PlatformFileForTransitToFile(album_table_files.token_file); |
| 243 files.uid_file = |
| 244 IPC::PlatformFileForTransitToFile(album_table_files.uid_file); |
| 245 |
| 246 picasa::PicasaAlbumTableReader reader(files.Pass()); |
| 247 bool parse_success = reader.Init(); |
| 248 Send(new ChromeUtilityHostMsg_ParsePicasaPMPDatabase_Finished( |
| 249 parse_success, |
| 250 reader.albums(), |
| 251 reader.folders())); |
| 252 ReleaseProcessIfNeeded(); |
| 253 } |
| 254 |
| 255 void ExtensionsHandler::OnIndexPicasaAlbumsContents( |
| 256 const picasa::AlbumUIDSet& album_uids, |
| 257 const std::vector<picasa::FolderINIContents>& folders_inis) { |
| 258 picasa::PicasaAlbumsIndexer indexer(album_uids); |
| 259 indexer.ParseFolderINI(folders_inis); |
| 260 |
| 261 Send(new ChromeUtilityHostMsg_IndexPicasaAlbumsContents_Finished( |
| 262 indexer.albums_images())); |
| 263 ReleaseProcessIfNeeded(); |
| 264 } |
| 265 #endif // defined(OS_WIN) || defined(OS_MACOSX) |
| 266 |
| 267 #if defined(OS_WIN) |
| 268 void ExtensionsHandler::OnGetAndEncryptWiFiCredentials( |
| 269 const std::string& network_guid, |
| 270 const std::vector<uint8>& public_key) { |
| 271 scoped_ptr<wifi::WiFiService> wifi_service(wifi::WiFiService::Create()); |
| 272 wifi_service->Initialize(NULL); |
| 273 |
| 274 std::string key_data; |
| 275 std::string error; |
| 276 wifi_service->GetKeyFromSystem(network_guid, &key_data, &error); |
| 277 |
| 278 std::vector<uint8> ciphertext; |
| 279 bool success = error.empty() && !key_data.empty(); |
| 280 if (success) { |
| 281 NetworkingPrivateCrypto crypto; |
| 282 success = crypto.EncryptByteString(public_key, key_data, &ciphertext); |
| 283 } |
| 284 |
| 285 Send(new ChromeUtilityHostMsg_GotEncryptedWiFiCredentials(ciphertext, |
| 286 success)); |
| 287 } |
| 288 #endif // defined(OS_WIN) |
| 289 |
| 290 } // namespace extensions |
OLD | NEW |