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.
5 #include "chrome/utility/extensions/extensions_handler.h"
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"
27 #include "chrome/common/extensions/api/networking_private/networking_private_crypto.h"
28 #include "chrome/utility/media_galleries/itunes_pref_parser_win.h"
29 #include "components/wifi/wifi_service.h"
30 #endif // defined(OS_WIN)
32 #if defined(OS_MACOSX)
33 #include "chrome/utility/media_galleries/iphoto_library_parser.h"
34 #endif // defined(OS_MACOSX)
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)
43 namespace extensions
{
47 bool Send(IPC::Message
* message
) {
48 return content::UtilityThread::Get()->Send(message
);
51 void ReleaseProcessIfNeeded() {
52 content::UtilityThread::Get()->ReleaseProcessIfNeeded();
57 ExtensionsHandler::ExtensionsHandler() {}
59 ExtensionsHandler::~ExtensionsHandler() {}
62 void ExtensionsHandler::PreSandboxStartup() {
63 // Initialize libexif for image metadata parsing.
64 metadata::ImageMetadataExtractor::InitializeLibrary();
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
);
73 void ExtensionsHandler::UtilityThreadStarted() {
74 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
75 std::string lang
= command_line
->GetSwitchValueASCII(switches::kLang
);
77 extension_l10n_util::SetProcessLocale(lang
);
80 bool ExtensionsHandler::OnMessageReceived(const IPC::Message
& message
) {
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
)
90 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ParseITunesPrefXml
,
92 #endif // defined(OS_WIN)
94 #if defined(OS_MACOSX)
95 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ParseIPhotoLibraryXmlFile
,
96 OnParseIPhotoLibraryXmlFile
)
97 #endif // defined(OS_MACOSX)
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)
109 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_GetAndEncryptWiFiCredentials
,
110 OnGetAndEncryptWiFiCredentials
)
111 #endif // defined(OS_WIN)
113 IPC_MESSAGE_UNHANDLED(handled
= false)
114 IPC_END_MESSAGE_MAP()
118 void ExtensionsHandler::OnUnpackExtension(
119 const base::FilePath
& extension_path
,
120 const std::string
& extension_id
,
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
,
128 static_cast<Manifest::Location
>(location
),
130 if (unpacker
.Run() && unpacker
.DumpImagesToFile() &&
131 unpacker
.DumpMessageCatalogsToFile()) {
132 Send(new ChromeUtilityHostMsg_UnpackExtension_Succeeded(
133 *unpacker
.parsed_manifest()));
135 Send(new ChromeUtilityHostMsg_UnpackExtension_Failed(
136 unpacker
.error_message()));
139 ReleaseProcessIfNeeded();
142 void ExtensionsHandler::OnParseUpdateManifest(const std::string
& xml
) {
143 UpdateManifest manifest
;
144 if (!manifest
.Parse(xml
)) {
145 Send(new ChromeUtilityHostMsg_ParseUpdateManifest_Failed(
148 Send(new ChromeUtilityHostMsg_ParseUpdateManifest_Succeeded(
149 manifest
.results()));
151 ReleaseProcessIfNeeded();
154 void ExtensionsHandler::OnDecodeImageBase64(
155 const std::string
& encoded_string
) {
156 std::string decoded_string
;
158 if (!base::Base64Decode(encoded_string
, &decoded_string
)) {
159 Send(new ChromeUtilityHostMsg_DecodeImage_Failed());
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
]);
168 ChromeContentUtilityClient::DecodeImage(decoded_vector
);
171 void ExtensionsHandler::OnParseJSON(const std::string
& json
) {
174 base::Value
* value
= base::JSONReader::ReadAndReturnError(
175 json
, base::JSON_PARSE_RFC
, &error_code
, &error
);
177 base::ListValue wrapper
;
178 wrapper
.Append(value
);
179 Send(new ChromeUtilityHostMsg_ParseJSON_Succeeded(wrapper
));
181 Send(new ChromeUtilityHostMsg_ParseJSON_Failed(error
));
183 ReleaseProcessIfNeeded();
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();
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();
205 #endif // defined(OS_WIN)
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();
216 #endif // defined(OS_MACOSX)
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();
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
);
236 IPC::PlatformFileForTransitToFile(album_table_files
.date_file
);
237 files
.filename_file
=
238 IPC::PlatformFileForTransitToFile(album_table_files
.filename_file
);
240 IPC::PlatformFileForTransitToFile(album_table_files
.name_file
);
242 IPC::PlatformFileForTransitToFile(album_table_files
.token_file
);
244 IPC::PlatformFileForTransitToFile(album_table_files
.uid_file
);
246 picasa::PicasaAlbumTableReader
reader(files
.Pass());
247 bool parse_success
= reader
.Init();
248 Send(new ChromeUtilityHostMsg_ParsePicasaPMPDatabase_Finished(
252 ReleaseProcessIfNeeded();
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
);
261 Send(new ChromeUtilityHostMsg_IndexPicasaAlbumsContents_Finished(
262 indexer
.albums_images()));
263 ReleaseProcessIfNeeded();
265 #endif // defined(OS_WIN) || defined(OS_MACOSX)
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
);
274 std::string key_data
;
276 wifi_service
->GetKeyFromSystem(network_guid
, &key_data
, &error
);
278 std::vector
<uint8
> ciphertext
;
279 bool success
= error
.empty() && !key_data
.empty();
281 NetworkingPrivateCrypto crypto
;
282 success
= crypto
.EncryptByteString(public_key
, key_data
, &ciphertext
);
285 Send(new ChromeUtilityHostMsg_GotEncryptedWiFiCredentials(ciphertext
,
288 #endif // defined(OS_WIN)
290 } // namespace extensions