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 // Load media libraries for media file validation.
69 base::FilePath media_path
;
70 PathService::Get(content::DIR_MEDIA_LIBS
, &media_path
);
71 if (!media_path
.empty())
72 media::InitializeMediaLibrary(media_path
);
75 bool ExtensionsHandler::OnMessageReceived(const IPC::Message
& message
) {
77 IPC_BEGIN_MESSAGE_MAP(ExtensionsHandler
, message
)
78 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_CheckMediaFile
, OnCheckMediaFile
)
80 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ParseITunesPrefXml
,
82 #endif // defined(OS_WIN)
84 #if defined(OS_MACOSX)
85 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ParseIPhotoLibraryXmlFile
,
86 OnParseIPhotoLibraryXmlFile
)
87 #endif // defined(OS_MACOSX)
89 #if defined(OS_WIN) || defined(OS_MACOSX)
90 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ParseITunesLibraryXmlFile
,
91 OnParseITunesLibraryXmlFile
)
92 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ParsePicasaPMPDatabase
,
93 OnParsePicasaPMPDatabase
)
94 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_IndexPicasaAlbumsContents
,
95 OnIndexPicasaAlbumsContents
)
96 #endif // defined(OS_WIN) || defined(OS_MACOSX)
99 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_GetWiFiCredentials
,
100 OnGetWiFiCredentials
)
101 #endif // defined(OS_WIN)
103 IPC_MESSAGE_UNHANDLED(handled
= false)
104 IPC_END_MESSAGE_MAP()
105 return handled
|| utility_handler_
.OnMessageReceived(message
);
108 void ExtensionsHandler::OnCheckMediaFile(
109 int64 milliseconds_of_decoding
,
110 const IPC::PlatformFileForTransit
& media_file
) {
111 media::MediaFileChecker
checker(
112 IPC::PlatformFileForTransitToFile(media_file
));
113 const bool check_success
= checker
.Start(
114 base::TimeDelta::FromMilliseconds(milliseconds_of_decoding
));
115 Send(new ChromeUtilityHostMsg_CheckMediaFile_Finished(check_success
));
116 ReleaseProcessIfNeeded();
120 void ExtensionsHandler::OnParseITunesPrefXml(
121 const std::string
& itunes_xml_data
) {
122 base::FilePath
library_path(
123 itunes::FindLibraryLocationInPrefXml(itunes_xml_data
));
124 Send(new ChromeUtilityHostMsg_GotITunesDirectory(library_path
));
125 ReleaseProcessIfNeeded();
127 #endif // defined(OS_WIN)
129 #if defined(OS_MACOSX)
130 void ExtensionsHandler::OnParseIPhotoLibraryXmlFile(
131 const IPC::PlatformFileForTransit
& iphoto_library_file
) {
132 iphoto::IPhotoLibraryParser parser
;
133 base::File file
= IPC::PlatformFileForTransitToFile(iphoto_library_file
);
134 bool result
= parser
.Parse(iapps::ReadFileAsString(file
.Pass()));
135 Send(new ChromeUtilityHostMsg_GotIPhotoLibrary(result
, parser
.library()));
136 ReleaseProcessIfNeeded();
138 #endif // defined(OS_MACOSX)
140 #if defined(OS_WIN) || defined(OS_MACOSX)
141 void ExtensionsHandler::OnParseITunesLibraryXmlFile(
142 const IPC::PlatformFileForTransit
& itunes_library_file
) {
143 itunes::ITunesLibraryParser parser
;
144 base::File file
= IPC::PlatformFileForTransitToFile(itunes_library_file
);
145 bool result
= parser
.Parse(iapps::ReadFileAsString(file
.Pass()));
146 Send(new ChromeUtilityHostMsg_GotITunesLibrary(result
, parser
.library()));
147 ReleaseProcessIfNeeded();
150 void ExtensionsHandler::OnParsePicasaPMPDatabase(
151 const picasa::AlbumTableFilesForTransit
& album_table_files
) {
152 picasa::AlbumTableFiles files
;
153 files
.indicator_file
=
154 IPC::PlatformFileForTransitToFile(album_table_files
.indicator_file
);
155 files
.category_file
=
156 IPC::PlatformFileForTransitToFile(album_table_files
.category_file
);
158 IPC::PlatformFileForTransitToFile(album_table_files
.date_file
);
159 files
.filename_file
=
160 IPC::PlatformFileForTransitToFile(album_table_files
.filename_file
);
162 IPC::PlatformFileForTransitToFile(album_table_files
.name_file
);
164 IPC::PlatformFileForTransitToFile(album_table_files
.token_file
);
166 IPC::PlatformFileForTransitToFile(album_table_files
.uid_file
);
168 picasa::PicasaAlbumTableReader
reader(files
.Pass());
169 bool parse_success
= reader
.Init();
170 Send(new ChromeUtilityHostMsg_ParsePicasaPMPDatabase_Finished(
171 parse_success
, reader
.albums(), reader
.folders()));
172 ReleaseProcessIfNeeded();
175 void ExtensionsHandler::OnIndexPicasaAlbumsContents(
176 const picasa::AlbumUIDSet
& album_uids
,
177 const std::vector
<picasa::FolderINIContents
>& folders_inis
) {
178 picasa::PicasaAlbumsIndexer
indexer(album_uids
);
179 indexer
.ParseFolderINI(folders_inis
);
181 Send(new ChromeUtilityHostMsg_IndexPicasaAlbumsContents_Finished(
182 indexer
.albums_images()));
183 ReleaseProcessIfNeeded();
185 #endif // defined(OS_WIN) || defined(OS_MACOSX)
188 void ExtensionsHandler::OnGetWiFiCredentials(const std::string
& network_guid
) {
189 scoped_ptr
<wifi::WiFiService
> wifi_service(wifi::WiFiService::Create());
190 wifi_service
->Initialize(NULL
);
192 std::string key_data
;
194 wifi_service
->GetKeyFromSystem(network_guid
, &key_data
, &error
);
196 Send(new ChromeUtilityHostMsg_GotWiFiCredentials(key_data
, error
.empty()));
198 #endif // defined(OS_WIN)
200 } // namespace extensions