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/media_galleries/metadata_types.h"
13 #include "chrome/utility/chrome_content_utility_client.h"
14 #include "chrome/utility/media_galleries/image_metadata_extractor.h"
15 #include "content/public/common/content_paths.h"
16 #include "content/public/utility/utility_thread.h"
17 #include "extensions/common/extension.h"
18 #include "extensions/common/extension_l10n_util.h"
19 #include "extensions/common/extension_utility_messages.h"
20 #include "extensions/utility/unpacker.h"
21 #include "media/base/media.h"
22 #include "media/base/media_file_checker.h"
23 #include "third_party/zlib/google/zip.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();
55 const char kExtensionHandlerUnzipError
[] =
56 "Could not unzip extension for install.";
60 ExtensionsHandler::ExtensionsHandler() {
61 ExtensionsClient::Set(ChromeExtensionsClient::GetInstance());
64 ExtensionsHandler::~ExtensionsHandler() {
68 void ExtensionsHandler::PreSandboxStartup() {
69 // Initialize libexif for image metadata parsing.
70 metadata::ImageMetadataExtractor::InitializeLibrary();
72 // Load media libraries for media file validation.
73 base::FilePath media_path
;
74 PathService::Get(content::DIR_MEDIA_LIBS
, &media_path
);
75 if (!media_path
.empty())
76 media::InitializeMediaLibrary(media_path
);
79 bool ExtensionsHandler::OnMessageReceived(const IPC::Message
& message
) {
81 IPC_BEGIN_MESSAGE_MAP(ExtensionsHandler
, message
)
82 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_UnzipToDir
, OnUnzipToDir
)
83 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_DecodeImageBase64
, OnDecodeImageBase64
)
84 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_CheckMediaFile
, OnCheckMediaFile
)
86 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ParseITunesPrefXml
,
88 #endif // defined(OS_WIN)
90 #if defined(OS_MACOSX)
91 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ParseIPhotoLibraryXmlFile
,
92 OnParseIPhotoLibraryXmlFile
)
93 #endif // defined(OS_MACOSX)
95 #if defined(OS_WIN) || defined(OS_MACOSX)
96 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ParseITunesLibraryXmlFile
,
97 OnParseITunesLibraryXmlFile
)
98 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ParsePicasaPMPDatabase
,
99 OnParsePicasaPMPDatabase
)
100 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_IndexPicasaAlbumsContents
,
101 OnIndexPicasaAlbumsContents
)
102 #endif // defined(OS_WIN) || defined(OS_MACOSX)
105 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_GetWiFiCredentials
,
106 OnGetWiFiCredentials
)
107 #endif // defined(OS_WIN)
109 IPC_MESSAGE_UNHANDLED(handled
= false)
110 IPC_END_MESSAGE_MAP()
111 return handled
|| utility_handler_
.OnMessageReceived(message
);
114 void ExtensionsHandler::OnUnzipToDir(const base::FilePath
& zip_path
,
115 const base::FilePath
& dir
) {
116 if (!zip::Unzip(zip_path
, dir
)) {
117 Send(new ChromeUtilityHostMsg_UnzipToDir_Failed(
118 std::string(kExtensionHandlerUnzipError
)));
120 Send(new ChromeUtilityHostMsg_UnzipToDir_Succeeded(dir
));
123 ReleaseProcessIfNeeded();
126 void ExtensionsHandler::OnDecodeImageBase64(
127 const std::string
& encoded_string
) {
128 std::string decoded_string
;
130 if (!base::Base64Decode(encoded_string
, &decoded_string
)) {
131 Send(new ChromeUtilityHostMsg_DecodeImage_Failed());
135 std::vector
<unsigned char> decoded_vector(decoded_string
.size());
136 for (size_t i
= 0; i
< decoded_string
.size(); ++i
) {
137 decoded_vector
[i
] = static_cast<unsigned char>(decoded_string
[i
]);
140 ChromeContentUtilityClient::DecodeImageAndSend(decoded_vector
, false);
143 void ExtensionsHandler::OnCheckMediaFile(
144 int64 milliseconds_of_decoding
,
145 const IPC::PlatformFileForTransit
& media_file
) {
146 media::MediaFileChecker
checker(
147 IPC::PlatformFileForTransitToFile(media_file
));
148 const bool check_success
= checker
.Start(
149 base::TimeDelta::FromMilliseconds(milliseconds_of_decoding
));
150 Send(new ChromeUtilityHostMsg_CheckMediaFile_Finished(check_success
));
151 ReleaseProcessIfNeeded();
155 void ExtensionsHandler::OnParseITunesPrefXml(
156 const std::string
& itunes_xml_data
) {
157 base::FilePath
library_path(
158 itunes::FindLibraryLocationInPrefXml(itunes_xml_data
));
159 Send(new ChromeUtilityHostMsg_GotITunesDirectory(library_path
));
160 ReleaseProcessIfNeeded();
162 #endif // defined(OS_WIN)
164 #if defined(OS_MACOSX)
165 void ExtensionsHandler::OnParseIPhotoLibraryXmlFile(
166 const IPC::PlatformFileForTransit
& iphoto_library_file
) {
167 iphoto::IPhotoLibraryParser parser
;
168 base::File file
= IPC::PlatformFileForTransitToFile(iphoto_library_file
);
169 bool result
= parser
.Parse(iapps::ReadFileAsString(file
.Pass()));
170 Send(new ChromeUtilityHostMsg_GotIPhotoLibrary(result
, parser
.library()));
171 ReleaseProcessIfNeeded();
173 #endif // defined(OS_MACOSX)
175 #if defined(OS_WIN) || defined(OS_MACOSX)
176 void ExtensionsHandler::OnParseITunesLibraryXmlFile(
177 const IPC::PlatformFileForTransit
& itunes_library_file
) {
178 itunes::ITunesLibraryParser parser
;
179 base::File file
= IPC::PlatformFileForTransitToFile(itunes_library_file
);
180 bool result
= parser
.Parse(iapps::ReadFileAsString(file
.Pass()));
181 Send(new ChromeUtilityHostMsg_GotITunesLibrary(result
, parser
.library()));
182 ReleaseProcessIfNeeded();
185 void ExtensionsHandler::OnParsePicasaPMPDatabase(
186 const picasa::AlbumTableFilesForTransit
& album_table_files
) {
187 picasa::AlbumTableFiles files
;
188 files
.indicator_file
=
189 IPC::PlatformFileForTransitToFile(album_table_files
.indicator_file
);
190 files
.category_file
=
191 IPC::PlatformFileForTransitToFile(album_table_files
.category_file
);
193 IPC::PlatformFileForTransitToFile(album_table_files
.date_file
);
194 files
.filename_file
=
195 IPC::PlatformFileForTransitToFile(album_table_files
.filename_file
);
197 IPC::PlatformFileForTransitToFile(album_table_files
.name_file
);
199 IPC::PlatformFileForTransitToFile(album_table_files
.token_file
);
201 IPC::PlatformFileForTransitToFile(album_table_files
.uid_file
);
203 picasa::PicasaAlbumTableReader
reader(files
.Pass());
204 bool parse_success
= reader
.Init();
205 Send(new ChromeUtilityHostMsg_ParsePicasaPMPDatabase_Finished(
206 parse_success
, reader
.albums(), reader
.folders()));
207 ReleaseProcessIfNeeded();
210 void ExtensionsHandler::OnIndexPicasaAlbumsContents(
211 const picasa::AlbumUIDSet
& album_uids
,
212 const std::vector
<picasa::FolderINIContents
>& folders_inis
) {
213 picasa::PicasaAlbumsIndexer
indexer(album_uids
);
214 indexer
.ParseFolderINI(folders_inis
);
216 Send(new ChromeUtilityHostMsg_IndexPicasaAlbumsContents_Finished(
217 indexer
.albums_images()));
218 ReleaseProcessIfNeeded();
220 #endif // defined(OS_WIN) || defined(OS_MACOSX)
223 void ExtensionsHandler::OnGetWiFiCredentials(const std::string
& network_guid
) {
224 scoped_ptr
<wifi::WiFiService
> wifi_service(wifi::WiFiService::Create());
225 wifi_service
->Initialize(NULL
);
227 std::string key_data
;
229 wifi_service
->GetKeyFromSystem(network_guid
, &key_data
, &error
);
231 Send(new ChromeUtilityHostMsg_GotWiFiCredentials(key_data
, error
.empty()));
233 #endif // defined(OS_WIN)
235 } // namespace extensions