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 "ui/base/ui_base_switches.h"
26 #include "chrome/common/extensions/api/networking_private/networking_private_crypto.h"
27 #include "chrome/utility/media_galleries/itunes_pref_parser_win.h"
28 #include "components/wifi/wifi_service.h"
29 #endif // defined(OS_WIN)
31 #if defined(OS_MACOSX)
32 #include "chrome/utility/media_galleries/iphoto_library_parser.h"
33 #endif // defined(OS_MACOSX)
35 #if defined(OS_WIN) || defined(OS_MACOSX)
36 #include "chrome/utility/media_galleries/iapps_xml_utils.h"
37 #include "chrome/utility/media_galleries/itunes_library_parser.h"
38 #include "chrome/utility/media_galleries/picasa_album_table_reader.h"
39 #include "chrome/utility/media_galleries/picasa_albums_indexer.h"
40 #endif // defined(OS_WIN) || defined(OS_MACOSX)
42 namespace extensions
{
46 bool Send(IPC::Message
* message
) {
47 return content::UtilityThread::Get()->Send(message
);
50 void ReleaseProcessIfNeeded() {
51 content::UtilityThread::Get()->ReleaseProcessIfNeeded();
56 ExtensionsHandler::ExtensionsHandler() {
57 ExtensionsClient::Set(ChromeExtensionsClient::GetInstance());
60 ExtensionsHandler::~ExtensionsHandler() {
64 void ExtensionsHandler::PreSandboxStartup() {
65 // Initialize libexif for image metadata parsing.
66 metadata::ImageMetadataExtractor::InitializeLibrary();
68 // Initialize media libraries for media file validation.
69 media::InitializeMediaLibrary();
72 bool ExtensionsHandler::OnMessageReceived(const IPC::Message
& message
) {
74 IPC_BEGIN_MESSAGE_MAP(ExtensionsHandler
, message
)
75 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_CheckMediaFile
, OnCheckMediaFile
)
77 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ParseITunesPrefXml
,
79 #endif // defined(OS_WIN)
81 #if defined(OS_MACOSX)
82 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ParseIPhotoLibraryXmlFile
,
83 OnParseIPhotoLibraryXmlFile
)
84 #endif // defined(OS_MACOSX)
86 #if defined(OS_WIN) || defined(OS_MACOSX)
87 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ParseITunesLibraryXmlFile
,
88 OnParseITunesLibraryXmlFile
)
89 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ParsePicasaPMPDatabase
,
90 OnParsePicasaPMPDatabase
)
91 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_IndexPicasaAlbumsContents
,
92 OnIndexPicasaAlbumsContents
)
93 #endif // defined(OS_WIN) || defined(OS_MACOSX)
96 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_GetWiFiCredentials
,
98 #endif // defined(OS_WIN)
100 IPC_MESSAGE_UNHANDLED(handled
= false)
101 IPC_END_MESSAGE_MAP()
102 return handled
|| utility_handler_
.OnMessageReceived(message
);
105 void ExtensionsHandler::OnCheckMediaFile(
106 int64 milliseconds_of_decoding
,
107 const IPC::PlatformFileForTransit
& media_file
) {
108 media::MediaFileChecker
checker(
109 IPC::PlatformFileForTransitToFile(media_file
));
110 const bool check_success
= checker
.Start(
111 base::TimeDelta::FromMilliseconds(milliseconds_of_decoding
));
112 Send(new ChromeUtilityHostMsg_CheckMediaFile_Finished(check_success
));
113 ReleaseProcessIfNeeded();
117 void ExtensionsHandler::OnParseITunesPrefXml(
118 const std::string
& itunes_xml_data
) {
119 base::FilePath
library_path(
120 itunes::FindLibraryLocationInPrefXml(itunes_xml_data
));
121 Send(new ChromeUtilityHostMsg_GotITunesDirectory(library_path
));
122 ReleaseProcessIfNeeded();
124 #endif // defined(OS_WIN)
126 #if defined(OS_MACOSX)
127 void ExtensionsHandler::OnParseIPhotoLibraryXmlFile(
128 const IPC::PlatformFileForTransit
& iphoto_library_file
) {
129 iphoto::IPhotoLibraryParser parser
;
130 base::File file
= IPC::PlatformFileForTransitToFile(iphoto_library_file
);
131 bool result
= parser
.Parse(iapps::ReadFileAsString(file
.Pass()));
132 Send(new ChromeUtilityHostMsg_GotIPhotoLibrary(result
, parser
.library()));
133 ReleaseProcessIfNeeded();
135 #endif // defined(OS_MACOSX)
137 #if defined(OS_WIN) || defined(OS_MACOSX)
138 void ExtensionsHandler::OnParseITunesLibraryXmlFile(
139 const IPC::PlatformFileForTransit
& itunes_library_file
) {
140 itunes::ITunesLibraryParser parser
;
141 base::File file
= IPC::PlatformFileForTransitToFile(itunes_library_file
);
142 bool result
= parser
.Parse(iapps::ReadFileAsString(file
.Pass()));
143 Send(new ChromeUtilityHostMsg_GotITunesLibrary(result
, parser
.library()));
144 ReleaseProcessIfNeeded();
147 void ExtensionsHandler::OnParsePicasaPMPDatabase(
148 const picasa::AlbumTableFilesForTransit
& album_table_files
) {
149 picasa::AlbumTableFiles files
;
150 files
.indicator_file
=
151 IPC::PlatformFileForTransitToFile(album_table_files
.indicator_file
);
152 files
.category_file
=
153 IPC::PlatformFileForTransitToFile(album_table_files
.category_file
);
155 IPC::PlatformFileForTransitToFile(album_table_files
.date_file
);
156 files
.filename_file
=
157 IPC::PlatformFileForTransitToFile(album_table_files
.filename_file
);
159 IPC::PlatformFileForTransitToFile(album_table_files
.name_file
);
161 IPC::PlatformFileForTransitToFile(album_table_files
.token_file
);
163 IPC::PlatformFileForTransitToFile(album_table_files
.uid_file
);
165 picasa::PicasaAlbumTableReader
reader(files
.Pass());
166 bool parse_success
= reader
.Init();
167 Send(new ChromeUtilityHostMsg_ParsePicasaPMPDatabase_Finished(
168 parse_success
, reader
.albums(), reader
.folders()));
169 ReleaseProcessIfNeeded();
172 void ExtensionsHandler::OnIndexPicasaAlbumsContents(
173 const picasa::AlbumUIDSet
& album_uids
,
174 const std::vector
<picasa::FolderINIContents
>& folders_inis
) {
175 picasa::PicasaAlbumsIndexer
indexer(album_uids
);
176 indexer
.ParseFolderINI(folders_inis
);
178 Send(new ChromeUtilityHostMsg_IndexPicasaAlbumsContents_Finished(
179 indexer
.albums_images()));
180 ReleaseProcessIfNeeded();
182 #endif // defined(OS_WIN) || defined(OS_MACOSX)
185 void ExtensionsHandler::OnGetWiFiCredentials(const std::string
& network_guid
) {
186 scoped_ptr
<wifi::WiFiService
> wifi_service(wifi::WiFiService::Create());
187 wifi_service
->Initialize(NULL
);
189 std::string key_data
;
191 wifi_service
->GetKeyFromSystem(network_guid
, &key_data
, &error
);
193 Send(new ChromeUtilityHostMsg_GotWiFiCredentials(key_data
, error
.empty()));
195 #endif // defined(OS_WIN)
197 } // namespace extensions