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_CheckMediaFile
, OnCheckMediaFile
)
85 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ParseITunesPrefXml
,
87 #endif // defined(OS_WIN)
89 #if defined(OS_MACOSX)
90 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ParseIPhotoLibraryXmlFile
,
91 OnParseIPhotoLibraryXmlFile
)
92 #endif // defined(OS_MACOSX)
94 #if defined(OS_WIN) || defined(OS_MACOSX)
95 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ParseITunesLibraryXmlFile
,
96 OnParseITunesLibraryXmlFile
)
97 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ParsePicasaPMPDatabase
,
98 OnParsePicasaPMPDatabase
)
99 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_IndexPicasaAlbumsContents
,
100 OnIndexPicasaAlbumsContents
)
101 #endif // defined(OS_WIN) || defined(OS_MACOSX)
104 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_GetWiFiCredentials
,
105 OnGetWiFiCredentials
)
106 #endif // defined(OS_WIN)
108 IPC_MESSAGE_UNHANDLED(handled
= false)
109 IPC_END_MESSAGE_MAP()
110 return handled
|| utility_handler_
.OnMessageReceived(message
);
113 void ExtensionsHandler::OnUnzipToDir(const base::FilePath
& zip_path
,
114 const base::FilePath
& dir
) {
115 if (!zip::Unzip(zip_path
, dir
)) {
116 Send(new ChromeUtilityHostMsg_UnzipToDir_Failed(
117 std::string(kExtensionHandlerUnzipError
)));
119 Send(new ChromeUtilityHostMsg_UnzipToDir_Succeeded(dir
));
122 ReleaseProcessIfNeeded();
125 void ExtensionsHandler::OnCheckMediaFile(
126 int64 milliseconds_of_decoding
,
127 const IPC::PlatformFileForTransit
& media_file
) {
128 media::MediaFileChecker
checker(
129 IPC::PlatformFileForTransitToFile(media_file
));
130 const bool check_success
= checker
.Start(
131 base::TimeDelta::FromMilliseconds(milliseconds_of_decoding
));
132 Send(new ChromeUtilityHostMsg_CheckMediaFile_Finished(check_success
));
133 ReleaseProcessIfNeeded();
137 void ExtensionsHandler::OnParseITunesPrefXml(
138 const std::string
& itunes_xml_data
) {
139 base::FilePath
library_path(
140 itunes::FindLibraryLocationInPrefXml(itunes_xml_data
));
141 Send(new ChromeUtilityHostMsg_GotITunesDirectory(library_path
));
142 ReleaseProcessIfNeeded();
144 #endif // defined(OS_WIN)
146 #if defined(OS_MACOSX)
147 void ExtensionsHandler::OnParseIPhotoLibraryXmlFile(
148 const IPC::PlatformFileForTransit
& iphoto_library_file
) {
149 iphoto::IPhotoLibraryParser parser
;
150 base::File file
= IPC::PlatformFileForTransitToFile(iphoto_library_file
);
151 bool result
= parser
.Parse(iapps::ReadFileAsString(file
.Pass()));
152 Send(new ChromeUtilityHostMsg_GotIPhotoLibrary(result
, parser
.library()));
153 ReleaseProcessIfNeeded();
155 #endif // defined(OS_MACOSX)
157 #if defined(OS_WIN) || defined(OS_MACOSX)
158 void ExtensionsHandler::OnParseITunesLibraryXmlFile(
159 const IPC::PlatformFileForTransit
& itunes_library_file
) {
160 itunes::ITunesLibraryParser parser
;
161 base::File file
= IPC::PlatformFileForTransitToFile(itunes_library_file
);
162 bool result
= parser
.Parse(iapps::ReadFileAsString(file
.Pass()));
163 Send(new ChromeUtilityHostMsg_GotITunesLibrary(result
, parser
.library()));
164 ReleaseProcessIfNeeded();
167 void ExtensionsHandler::OnParsePicasaPMPDatabase(
168 const picasa::AlbumTableFilesForTransit
& album_table_files
) {
169 picasa::AlbumTableFiles files
;
170 files
.indicator_file
=
171 IPC::PlatformFileForTransitToFile(album_table_files
.indicator_file
);
172 files
.category_file
=
173 IPC::PlatformFileForTransitToFile(album_table_files
.category_file
);
175 IPC::PlatformFileForTransitToFile(album_table_files
.date_file
);
176 files
.filename_file
=
177 IPC::PlatformFileForTransitToFile(album_table_files
.filename_file
);
179 IPC::PlatformFileForTransitToFile(album_table_files
.name_file
);
181 IPC::PlatformFileForTransitToFile(album_table_files
.token_file
);
183 IPC::PlatformFileForTransitToFile(album_table_files
.uid_file
);
185 picasa::PicasaAlbumTableReader
reader(files
.Pass());
186 bool parse_success
= reader
.Init();
187 Send(new ChromeUtilityHostMsg_ParsePicasaPMPDatabase_Finished(
188 parse_success
, reader
.albums(), reader
.folders()));
189 ReleaseProcessIfNeeded();
192 void ExtensionsHandler::OnIndexPicasaAlbumsContents(
193 const picasa::AlbumUIDSet
& album_uids
,
194 const std::vector
<picasa::FolderINIContents
>& folders_inis
) {
195 picasa::PicasaAlbumsIndexer
indexer(album_uids
);
196 indexer
.ParseFolderINI(folders_inis
);
198 Send(new ChromeUtilityHostMsg_IndexPicasaAlbumsContents_Finished(
199 indexer
.albums_images()));
200 ReleaseProcessIfNeeded();
202 #endif // defined(OS_WIN) || defined(OS_MACOSX)
205 void ExtensionsHandler::OnGetWiFiCredentials(const std::string
& network_guid
) {
206 scoped_ptr
<wifi::WiFiService
> wifi_service(wifi::WiFiService::Create());
207 wifi_service
->Initialize(NULL
);
209 std::string key_data
;
211 wifi_service
->GetKeyFromSystem(network_guid
, &key_data
, &error
);
213 Send(new ChromeUtilityHostMsg_GotWiFiCredentials(key_data
, error
.empty()));
215 #endif // defined(OS_WIN)
217 } // namespace extensions