OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/utility/chrome_content_utility_client.h" | 5 #include "chrome/utility/chrome_content_utility_client.h" |
6 | 6 |
7 #include "base/base64.h" | 7 #include "base/base64.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/file_util.h" | |
11 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
12 #include "base/json/json_reader.h" | 11 #include "base/json/json_reader.h" |
13 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
14 #include "base/path_service.h" | 13 #include "base/path_service.h" |
15 #include "base/scoped_native_library.h" | |
16 #include "base/time/time.h" | 14 #include "base/time/time.h" |
17 #include "chrome/common/chrome_paths.h" | |
18 #include "chrome/common/chrome_utility_messages.h" | 15 #include "chrome/common/chrome_utility_messages.h" |
19 #include "chrome/common/extensions/chrome_extensions_client.h" | 16 #include "chrome/common/extensions/chrome_extensions_client.h" |
20 #include "chrome/common/extensions/update_manifest.h" | 17 #include "chrome/common/extensions/update_manifest.h" |
21 #include "chrome/common/safe_browsing/zip_analyzer.h" | 18 #include "chrome/common/safe_browsing/zip_analyzer.h" |
22 #include "chrome/utility/chrome_content_utility_ipc_whitelist.h" | 19 #include "chrome/utility/chrome_content_utility_ipc_whitelist.h" |
23 #include "chrome/utility/cloud_print/bitmap_image.h" | |
24 #include "chrome/utility/cloud_print/pwg_encoder.h" | |
25 #include "chrome/utility/extensions/unpacker.h" | 20 #include "chrome/utility/extensions/unpacker.h" |
26 #include "chrome/utility/image_writer/image_writer_handler.h" | 21 #include "chrome/utility/image_writer/image_writer_handler.h" |
27 #include "chrome/utility/profile_import_handler.h" | 22 #include "chrome/utility/profile_import_handler.h" |
28 #include "chrome/utility/web_resource_unpacker.h" | 23 #include "chrome/utility/web_resource_unpacker.h" |
29 #include "content/public/child/image_decoder_utils.h" | 24 #include "content/public/child/image_decoder_utils.h" |
30 #include "content/public/common/content_paths.h" | 25 #include "content/public/common/content_paths.h" |
31 #include "content/public/common/content_switches.h" | 26 #include "content/public/common/content_switches.h" |
32 #include "content/public/utility/utility_thread.h" | 27 #include "content/public/utility/utility_thread.h" |
33 #include "courgette/courgette.h" | 28 #include "courgette/courgette.h" |
34 #include "courgette/third_party/bsdiff.h" | 29 #include "courgette/third_party/bsdiff.h" |
35 #include "extensions/common/extension.h" | 30 #include "extensions/common/extension.h" |
36 #include "extensions/common/extension_l10n_util.h" | 31 #include "extensions/common/extension_l10n_util.h" |
37 #include "extensions/common/manifest.h" | 32 #include "extensions/common/manifest.h" |
38 #include "media/base/media.h" | 33 #include "media/base/media.h" |
39 #include "media/base/media_file_checker.h" | 34 #include "media/base/media_file_checker.h" |
40 #include "printing/page_range.h" | |
41 #include "printing/pdf_render_settings.h" | |
42 #include "third_party/skia/include/core/SkBitmap.h" | 35 #include "third_party/skia/include/core/SkBitmap.h" |
43 #include "third_party/zlib/google/zip.h" | 36 #include "third_party/zlib/google/zip.h" |
44 #include "ui/base/ui_base_switches.h" | 37 #include "ui/base/ui_base_switches.h" |
45 #include "ui/gfx/codec/jpeg_codec.h" | 38 #include "ui/gfx/codec/jpeg_codec.h" |
46 #include "ui/gfx/rect.h" | 39 #include "ui/gfx/rect.h" |
47 #include "ui/gfx/size.h" | 40 #include "ui/gfx/size.h" |
48 | 41 |
49 #if defined(OS_WIN) | 42 #if defined(OS_WIN) |
50 #include "base/win/iat_patch_function.h" | |
51 #include "base/win/scoped_handle.h" | |
52 #include "chrome/common/extensions/api/networking_private/networking_private_cry
pto.h" | 43 #include "chrome/common/extensions/api/networking_private/networking_private_cry
pto.h" |
53 #include "chrome/utility/media_galleries/itunes_pref_parser_win.h" | 44 #include "chrome/utility/media_galleries/itunes_pref_parser_win.h" |
54 #include "components/wifi/wifi_service.h" | 45 #include "components/wifi/wifi_service.h" |
55 #include "printing/emf_win.h" | |
56 #include "ui/gfx/gdi_util.h" | |
57 #endif // defined(OS_WIN) | 46 #endif // defined(OS_WIN) |
58 | 47 |
59 #if defined(OS_MACOSX) | 48 #if defined(OS_MACOSX) |
60 #include "chrome/utility/media_galleries/iphoto_library_parser.h" | 49 #include "chrome/utility/media_galleries/iphoto_library_parser.h" |
61 #endif // defined(OS_MACOSX) | 50 #endif // defined(OS_MACOSX) |
62 | 51 |
63 #if defined(OS_WIN) || defined(OS_MACOSX) | 52 #if defined(OS_WIN) || defined(OS_MACOSX) |
64 #include "chrome/utility/media_galleries/iapps_xml_utils.h" | 53 #include "chrome/utility/media_galleries/iapps_xml_utils.h" |
65 #include "chrome/utility/media_galleries/itunes_library_parser.h" | 54 #include "chrome/utility/media_galleries/itunes_library_parser.h" |
66 #include "chrome/utility/media_galleries/picasa_album_table_reader.h" | 55 #include "chrome/utility/media_galleries/picasa_album_table_reader.h" |
67 #include "chrome/utility/media_galleries/picasa_albums_indexer.h" | 56 #include "chrome/utility/media_galleries/picasa_albums_indexer.h" |
68 #endif // defined(OS_WIN) || defined(OS_MACOSX) | 57 #endif // defined(OS_WIN) || defined(OS_MACOSX) |
69 | 58 |
70 #if !defined(OS_ANDROID) && !defined(OS_IOS) | 59 #if !defined(OS_ANDROID) && !defined(OS_IOS) |
71 #include "chrome/common/media_galleries/metadata_types.h" | 60 #include "chrome/common/media_galleries/metadata_types.h" |
72 #include "chrome/utility/media_galleries/image_metadata_extractor.h" | 61 #include "chrome/utility/media_galleries/image_metadata_extractor.h" |
73 #include "chrome/utility/media_galleries/ipc_data_source.h" | 62 #include "chrome/utility/media_galleries/ipc_data_source.h" |
74 #include "chrome/utility/media_galleries/media_metadata_parser.h" | 63 #include "chrome/utility/media_galleries/media_metadata_parser.h" |
75 #endif // !defined(OS_ANDROID) && !defined(OS_IOS) | 64 #endif // !defined(OS_ANDROID) && !defined(OS_IOS) |
76 | 65 |
77 #if defined(ENABLE_FULL_PRINTING) | 66 #if defined(ENABLE_FULL_PRINTING) |
78 #include "chrome/common/crash_keys.h" | 67 #include "chrome/utility/printing_handler.h" |
79 #include "printing/backend/print_backend.h" | |
80 #endif | 68 #endif |
81 | 69 |
82 #if defined(ENABLE_MDNS) | 70 #if defined(ENABLE_MDNS) |
83 #include "chrome/utility/local_discovery/service_discovery_message_handler.h" | 71 #include "chrome/utility/local_discovery/service_discovery_message_handler.h" |
84 #endif // ENABLE_MDNS | 72 #endif |
85 | |
86 namespace chrome { | |
87 | 73 |
88 namespace { | 74 namespace { |
89 | 75 |
90 bool Send(IPC::Message* message) { | 76 bool Send(IPC::Message* message) { |
91 return content::UtilityThread::Get()->Send(message); | 77 return content::UtilityThread::Get()->Send(message); |
92 } | 78 } |
93 | 79 |
94 void ReleaseProcessIfNeeded() { | 80 void ReleaseProcessIfNeeded() { |
95 content::UtilityThread::Get()->ReleaseProcessIfNeeded(); | 81 content::UtilityThread::Get()->ReleaseProcessIfNeeded(); |
96 } | 82 } |
97 | 83 |
98 class PdfFunctionsBase { | |
99 public: | |
100 PdfFunctionsBase() : render_pdf_to_bitmap_func_(NULL), | |
101 get_pdf_doc_info_func_(NULL) {} | |
102 | |
103 bool Init() { | |
104 base::FilePath pdf_module_path; | |
105 if (!PathService::Get(chrome::FILE_PDF_PLUGIN, &pdf_module_path) || | |
106 !base::PathExists(pdf_module_path)) { | |
107 return false; | |
108 } | |
109 | |
110 pdf_lib_.Reset(base::LoadNativeLibrary(pdf_module_path, NULL)); | |
111 if (!pdf_lib_.is_valid()) { | |
112 LOG(WARNING) << "Couldn't load PDF plugin"; | |
113 return false; | |
114 } | |
115 | |
116 render_pdf_to_bitmap_func_ = | |
117 reinterpret_cast<RenderPDFPageToBitmapProc>( | |
118 pdf_lib_.GetFunctionPointer("RenderPDFPageToBitmap")); | |
119 LOG_IF(WARNING, !render_pdf_to_bitmap_func_) << | |
120 "Missing RenderPDFPageToBitmap"; | |
121 | |
122 get_pdf_doc_info_func_ = | |
123 reinterpret_cast<GetPDFDocInfoProc>( | |
124 pdf_lib_.GetFunctionPointer("GetPDFDocInfo")); | |
125 LOG_IF(WARNING, !get_pdf_doc_info_func_) << "Missing GetPDFDocInfo"; | |
126 | |
127 if (!render_pdf_to_bitmap_func_ || !get_pdf_doc_info_func_ || | |
128 !PlatformInit(pdf_module_path, pdf_lib_)) { | |
129 Reset(); | |
130 } | |
131 | |
132 return IsValid(); | |
133 } | |
134 | |
135 bool IsValid() const { | |
136 return pdf_lib_.is_valid(); | |
137 } | |
138 | |
139 void Reset() { | |
140 pdf_lib_.Reset(NULL); | |
141 } | |
142 | |
143 bool RenderPDFPageToBitmap(const void* pdf_buffer, | |
144 int pdf_buffer_size, | |
145 int page_number, | |
146 void* bitmap_buffer, | |
147 int bitmap_width, | |
148 int bitmap_height, | |
149 int dpi_x, | |
150 int dpi_y, | |
151 bool autorotate) { | |
152 if (!render_pdf_to_bitmap_func_) | |
153 return false; | |
154 return render_pdf_to_bitmap_func_(pdf_buffer, pdf_buffer_size, page_number, | |
155 bitmap_buffer, bitmap_width, | |
156 bitmap_height, dpi_x, dpi_y, autorotate); | |
157 } | |
158 | |
159 bool GetPDFDocInfo(const void* pdf_buffer, | |
160 int buffer_size, | |
161 int* page_count, | |
162 double* max_page_width) { | |
163 if (!get_pdf_doc_info_func_) | |
164 return false; | |
165 return get_pdf_doc_info_func_(pdf_buffer, buffer_size, page_count, | |
166 max_page_width); | |
167 } | |
168 | |
169 protected: | |
170 virtual bool PlatformInit( | |
171 const base::FilePath& pdf_module_path, | |
172 const base::ScopedNativeLibrary& pdf_lib) { | |
173 return true; | |
174 } | |
175 | |
176 private: | |
177 // Exported by PDF plugin. | |
178 typedef bool (*RenderPDFPageToBitmapProc)(const void* pdf_buffer, | |
179 int pdf_buffer_size, | |
180 int page_number, | |
181 void* bitmap_buffer, | |
182 int bitmap_width, | |
183 int bitmap_height, | |
184 int dpi_x, | |
185 int dpi_y, | |
186 bool autorotate); | |
187 typedef bool (*GetPDFDocInfoProc)(const void* pdf_buffer, | |
188 int buffer_size, int* page_count, | |
189 double* max_page_width); | |
190 | |
191 RenderPDFPageToBitmapProc render_pdf_to_bitmap_func_; | |
192 GetPDFDocInfoProc get_pdf_doc_info_func_; | |
193 | |
194 base::ScopedNativeLibrary pdf_lib_; | |
195 DISALLOW_COPY_AND_ASSIGN(PdfFunctionsBase); | |
196 }; | |
197 | |
198 #if defined(OS_WIN) | |
199 // The 2 below IAT patch functions are almost identical to the code in | |
200 // render_process_impl.cc. This is needed to work around specific Windows APIs | |
201 // used by the Chrome PDF plugin that will fail in the sandbox. | |
202 static base::win::IATPatchFunction g_iat_patch_createdca; | |
203 HDC WINAPI UtilityProcess_CreateDCAPatch(LPCSTR driver_name, | |
204 LPCSTR device_name, | |
205 LPCSTR output, | |
206 const DEVMODEA* init_data) { | |
207 if (driver_name && (std::string("DISPLAY") == driver_name)) { | |
208 // CreateDC fails behind the sandbox, but not CreateCompatibleDC. | |
209 return CreateCompatibleDC(NULL); | |
210 } | |
211 | |
212 NOTREACHED(); | |
213 return CreateDCA(driver_name, device_name, output, init_data); | |
214 } | |
215 | |
216 static base::win::IATPatchFunction g_iat_patch_get_font_data; | |
217 DWORD WINAPI UtilityProcess_GetFontDataPatch( | |
218 HDC hdc, DWORD table, DWORD offset, LPVOID buffer, DWORD length) { | |
219 int rv = GetFontData(hdc, table, offset, buffer, length); | |
220 if (rv == GDI_ERROR && hdc) { | |
221 HFONT font = static_cast<HFONT>(GetCurrentObject(hdc, OBJ_FONT)); | |
222 | |
223 LOGFONT logfont; | |
224 if (GetObject(font, sizeof(LOGFONT), &logfont)) { | |
225 content::UtilityThread::Get()->PreCacheFont(logfont); | |
226 rv = GetFontData(hdc, table, offset, buffer, length); | |
227 content::UtilityThread::Get()->ReleaseCachedFonts(); | |
228 } | |
229 } | |
230 return rv; | |
231 } | |
232 | |
233 class PdfFunctionsWin : public PdfFunctionsBase { | |
234 public: | |
235 PdfFunctionsWin() : render_pdf_to_dc_func_(NULL) { | |
236 } | |
237 | |
238 bool PlatformInit( | |
239 const base::FilePath& pdf_module_path, | |
240 const base::ScopedNativeLibrary& pdf_lib) OVERRIDE { | |
241 // Patch the IAT for handling specific APIs known to fail in the sandbox. | |
242 if (!g_iat_patch_createdca.is_patched()) { | |
243 g_iat_patch_createdca.Patch(pdf_module_path.value().c_str(), | |
244 "gdi32.dll", "CreateDCA", | |
245 UtilityProcess_CreateDCAPatch); | |
246 } | |
247 | |
248 if (!g_iat_patch_get_font_data.is_patched()) { | |
249 g_iat_patch_get_font_data.Patch(pdf_module_path.value().c_str(), | |
250 "gdi32.dll", "GetFontData", | |
251 UtilityProcess_GetFontDataPatch); | |
252 } | |
253 render_pdf_to_dc_func_ = | |
254 reinterpret_cast<RenderPDFPageToDCProc>( | |
255 pdf_lib.GetFunctionPointer("RenderPDFPageToDC")); | |
256 LOG_IF(WARNING, !render_pdf_to_dc_func_) << "Missing RenderPDFPageToDC"; | |
257 | |
258 return render_pdf_to_dc_func_ != NULL; | |
259 } | |
260 | |
261 bool RenderPDFPageToDC(const void* pdf_buffer, | |
262 int buffer_size, | |
263 int page_number, | |
264 HDC dc, | |
265 int dpi_x, | |
266 int dpi_y, | |
267 int bounds_origin_x, | |
268 int bounds_origin_y, | |
269 int bounds_width, | |
270 int bounds_height, | |
271 bool fit_to_bounds, | |
272 bool stretch_to_bounds, | |
273 bool keep_aspect_ratio, | |
274 bool center_in_bounds, | |
275 bool autorotate) { | |
276 if (!render_pdf_to_dc_func_) | |
277 return false; | |
278 return render_pdf_to_dc_func_(pdf_buffer, buffer_size, page_number, | |
279 dc, dpi_x, dpi_y, bounds_origin_x, | |
280 bounds_origin_y, bounds_width, bounds_height, | |
281 fit_to_bounds, stretch_to_bounds, | |
282 keep_aspect_ratio, center_in_bounds, | |
283 autorotate); | |
284 } | |
285 | |
286 private: | |
287 // Exported by PDF plugin. | |
288 typedef bool (*RenderPDFPageToDCProc)( | |
289 const void* pdf_buffer, int buffer_size, int page_number, HDC dc, | |
290 int dpi_x, int dpi_y, int bounds_origin_x, int bounds_origin_y, | |
291 int bounds_width, int bounds_height, bool fit_to_bounds, | |
292 bool stretch_to_bounds, bool keep_aspect_ratio, bool center_in_bounds, | |
293 bool autorotate); | |
294 RenderPDFPageToDCProc render_pdf_to_dc_func_; | |
295 | |
296 DISALLOW_COPY_AND_ASSIGN(PdfFunctionsWin); | |
297 }; | |
298 | |
299 typedef PdfFunctionsWin PdfFunctions; | |
300 #else // OS_WIN | |
301 typedef PdfFunctionsBase PdfFunctions; | |
302 #endif // OS_WIN | |
303 | |
304 #if !defined(OS_ANDROID) && !defined(OS_IOS) | 84 #if !defined(OS_ANDROID) && !defined(OS_IOS) |
305 void FinishParseMediaMetadata( | 85 void FinishParseMediaMetadata( |
306 metadata::MediaMetadataParser* parser, | 86 metadata::MediaMetadataParser* parser, |
307 const extensions::api::media_galleries::MediaMetadata& metadata, | 87 const extensions::api::media_galleries::MediaMetadata& metadata, |
308 const std::vector<metadata::AttachedImage>& attached_images) { | 88 const std::vector<metadata::AttachedImage>& attached_images) { |
309 Send(new ChromeUtilityHostMsg_ParseMediaMetadata_Finished( | 89 Send(new ChromeUtilityHostMsg_ParseMediaMetadata_Finished( |
310 true, *metadata.ToValue(), attached_images)); | 90 true, *metadata.ToValue(), attached_images)); |
311 ReleaseProcessIfNeeded(); | 91 ReleaseProcessIfNeeded(); |
312 } | 92 } |
313 #endif // !defined(OS_ANDROID) && !defined(OS_IOS) | 93 #endif // !defined(OS_ANDROID) && !defined(OS_IOS) |
314 | 94 |
315 static base::LazyInstance<PdfFunctions> g_pdf_lib = LAZY_INSTANCE_INITIALIZER; | |
316 | |
317 } // namespace | 95 } // namespace |
318 | 96 |
319 ChromeContentUtilityClient::ChromeContentUtilityClient() | 97 ChromeContentUtilityClient::ChromeContentUtilityClient() |
320 : filter_messages_(false) { | 98 : filter_messages_(false) { |
321 #if !defined(OS_ANDROID) | 99 #if !defined(OS_ANDROID) |
322 handlers_.push_back(new ProfileImportHandler()); | 100 handlers_.push_back(new ProfileImportHandler()); |
323 #endif // OS_ANDROID | 101 #endif |
| 102 |
| 103 #if defined(ENABLE_FULL_PRINTING) |
| 104 handlers_.push_back(new PrintingHandler()); |
| 105 #endif |
324 | 106 |
325 #if defined(ENABLE_MDNS) | 107 #if defined(ENABLE_MDNS) |
326 if (CommandLine::ForCurrentProcess()->HasSwitch( | 108 if (CommandLine::ForCurrentProcess()->HasSwitch( |
327 switches::kUtilityProcessEnableMDns)) { | 109 switches::kUtilityProcessEnableMDns)) { |
328 handlers_.push_back(new local_discovery::ServiceDiscoveryMessageHandler()); | 110 handlers_.push_back(new local_discovery::ServiceDiscoveryMessageHandler()); |
329 } | 111 } |
330 #endif // ENABLE_MDNS | 112 #endif |
331 | 113 |
332 handlers_.push_back(new image_writer::ImageWriterHandler()); | 114 handlers_.push_back(new image_writer::ImageWriterHandler()); |
333 } | 115 } |
334 | 116 |
335 ChromeContentUtilityClient::~ChromeContentUtilityClient() { | 117 ChromeContentUtilityClient::~ChromeContentUtilityClient() { |
336 } | 118 } |
337 | 119 |
338 void ChromeContentUtilityClient::UtilityThreadStarted() { | 120 void ChromeContentUtilityClient::UtilityThreadStarted() { |
339 CommandLine* command_line = CommandLine::ForCurrentProcess(); | 121 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
340 std::string lang = command_line->GetSwitchValueASCII(switches::kLang); | 122 std::string lang = command_line->GetSwitchValueASCII(switches::kLang); |
341 if (!lang.empty()) | 123 if (!lang.empty()) |
342 extension_l10n_util::SetProcessLocale(lang); | 124 extension_l10n_util::SetProcessLocale(lang); |
343 | 125 |
344 if (command_line->HasSwitch(switches::kUtilityProcessRunningElevated)) { | 126 if (command_line->HasSwitch(switches::kUtilityProcessRunningElevated)) { |
345 message_id_whitelist_.insert(kMessageWhitelist, | 127 message_id_whitelist_.insert(kMessageWhitelist, |
346 kMessageWhitelist + kMessageWhitelistSize); | 128 kMessageWhitelist + kMessageWhitelistSize); |
347 filter_messages_ = true; | 129 filter_messages_ = true; |
348 } | 130 } |
349 } | 131 } |
350 | 132 |
351 bool ChromeContentUtilityClient::OnMessageReceived( | 133 bool ChromeContentUtilityClient::OnMessageReceived( |
352 const IPC::Message& message) { | 134 const IPC::Message& message) { |
353 if (filter_messages_ && | 135 if (filter_messages_ && !ContainsKey(message_id_whitelist_, message.type())) |
354 (message_id_whitelist_.find(message.type()) == | |
355 message_id_whitelist_.end())) { | |
356 return false; | 136 return false; |
357 } | |
358 | 137 |
359 bool handled = true; | 138 bool handled = true; |
360 IPC_BEGIN_MESSAGE_MAP(ChromeContentUtilityClient, message) | 139 IPC_BEGIN_MESSAGE_MAP(ChromeContentUtilityClient, message) |
361 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_UnpackExtension, OnUnpackExtension) | 140 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_UnpackExtension, OnUnpackExtension) |
362 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_UnpackWebResource, | 141 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_UnpackWebResource, |
363 OnUnpackWebResource) | 142 OnUnpackWebResource) |
364 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ParseUpdateManifest, | 143 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ParseUpdateManifest, |
365 OnParseUpdateManifest) | 144 OnParseUpdateManifest) |
366 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_DecodeImage, OnDecodeImage) | 145 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_DecodeImage, OnDecodeImage) |
367 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_DecodeImageBase64, OnDecodeImageBase64) | 146 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_DecodeImageBase64, OnDecodeImageBase64) |
368 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToMetafiles, | |
369 OnRenderPDFPagesToMetafile) | |
370 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToPWGRaster, | |
371 OnRenderPDFPagesToPWGRaster) | |
372 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RobustJPEGDecodeImage, | 147 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RobustJPEGDecodeImage, |
373 OnRobustJPEGDecodeImage) | 148 OnRobustJPEGDecodeImage) |
374 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ParseJSON, OnParseJSON) | 149 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ParseJSON, OnParseJSON) |
375 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_GetPrinterCapsAndDefaults, | |
376 OnGetPrinterCapsAndDefaults) | |
377 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_GetPrinterSemanticCapsAndDefaults, | |
378 OnGetPrinterSemanticCapsAndDefaults) | |
379 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_PatchFileBsdiff, | 150 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_PatchFileBsdiff, |
380 OnPatchFileBsdiff) | 151 OnPatchFileBsdiff) |
381 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_PatchFileCourgette, | 152 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_PatchFileCourgette, |
382 OnPatchFileCourgette) | 153 OnPatchFileCourgette) |
383 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_StartupPing, OnStartupPing) | 154 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_StartupPing, OnStartupPing) |
384 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_AnalyzeZipFileForDownloadProtection, | 155 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_AnalyzeZipFileForDownloadProtection, |
385 OnAnalyzeZipFileForDownloadProtection) | 156 OnAnalyzeZipFileForDownloadProtection) |
386 | 157 |
387 #if !defined(OS_ANDROID) && !defined(OS_IOS) | 158 #if !defined(OS_ANDROID) && !defined(OS_IOS) |
388 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_CheckMediaFile, OnCheckMediaFile) | 159 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_CheckMediaFile, OnCheckMediaFile) |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
424 for (Handlers::iterator it = handlers_.begin(); | 195 for (Handlers::iterator it = handlers_.begin(); |
425 !handled && it != handlers_.end(); ++it) { | 196 !handled && it != handlers_.end(); ++it) { |
426 handled = (*it)->OnMessageReceived(message); | 197 handled = (*it)->OnMessageReceived(message); |
427 } | 198 } |
428 | 199 |
429 return handled; | 200 return handled; |
430 } | 201 } |
431 | 202 |
432 // static | 203 // static |
433 void ChromeContentUtilityClient::PreSandboxStartup() { | 204 void ChromeContentUtilityClient::PreSandboxStartup() { |
| 205 #if defined(ENABLE_FULL_PRINTING) |
| 206 PrintingHandler::PreSandboxStartup(); |
| 207 #endif |
| 208 |
434 #if defined(ENABLE_MDNS) | 209 #if defined(ENABLE_MDNS) |
435 if (CommandLine::ForCurrentProcess()->HasSwitch( | 210 if (CommandLine::ForCurrentProcess()->HasSwitch( |
436 switches::kUtilityProcessEnableMDns)) { | 211 switches::kUtilityProcessEnableMDns)) { |
437 local_discovery::ServiceDiscoveryMessageHandler::PreSandboxStartup(); | 212 local_discovery::ServiceDiscoveryMessageHandler::PreSandboxStartup(); |
438 } | 213 } |
439 #endif // ENABLE_MDNS | 214 #endif // ENABLE_MDNS |
440 | 215 |
441 g_pdf_lib.Get().Init(); | |
442 | |
443 #if !defined(OS_ANDROID) && !defined(OS_IOS) | 216 #if !defined(OS_ANDROID) && !defined(OS_IOS) |
444 // Initialize libexif for image metadata parsing. | 217 // Initialize libexif for image metadata parsing. |
445 metadata::ImageMetadataExtractor::InitializeLibrary(); | 218 metadata::ImageMetadataExtractor::InitializeLibrary(); |
446 #endif // !defined(OS_ANDROID) && !defined(OS_IOS) | 219 #endif // !defined(OS_ANDROID) && !defined(OS_IOS) |
447 | 220 |
448 // Load media libraries for media file validation. | 221 // Load media libraries for media file validation. |
449 base::FilePath media_path; | 222 base::FilePath media_path; |
450 PathService::Get(content::DIR_MEDIA_LIBS, &media_path); | 223 PathService::Get(content::DIR_MEDIA_LIBS, &media_path); |
451 if (!media_path.empty()) | 224 if (!media_path.empty()) |
452 media::InitializeMediaLibrary(media_path); | 225 media::InitializeMediaLibrary(media_path); |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
559 succeeded = zip::ZipFiles(src_dir, src_relative_paths, dest_fd.fd); | 332 succeeded = zip::ZipFiles(src_dir, src_relative_paths, dest_fd.fd); |
560 | 333 |
561 if (succeeded) | 334 if (succeeded) |
562 Send(new ChromeUtilityHostMsg_CreateZipFile_Succeeded()); | 335 Send(new ChromeUtilityHostMsg_CreateZipFile_Succeeded()); |
563 else | 336 else |
564 Send(new ChromeUtilityHostMsg_CreateZipFile_Failed()); | 337 Send(new ChromeUtilityHostMsg_CreateZipFile_Failed()); |
565 ReleaseProcessIfNeeded(); | 338 ReleaseProcessIfNeeded(); |
566 } | 339 } |
567 #endif // defined(OS_CHROMEOS) | 340 #endif // defined(OS_CHROMEOS) |
568 | 341 |
569 void ChromeContentUtilityClient::OnRenderPDFPagesToMetafile( | |
570 IPC::PlatformFileForTransit pdf_transit, | |
571 const base::FilePath& metafile_path, | |
572 const printing::PdfRenderSettings& settings, | |
573 const std::vector<printing::PageRange>& page_ranges_const) { | |
574 bool succeeded = false; | |
575 #if defined(OS_WIN) | |
576 base::File pdf_file = IPC::PlatformFileForTransitToFile(pdf_transit); | |
577 int highest_rendered_page_number = 0; | |
578 double scale_factor = 1.0; | |
579 std::vector<printing::PageRange> page_ranges = page_ranges_const; | |
580 succeeded = RenderPDFToWinMetafile(pdf_file.Pass(), | |
581 metafile_path, | |
582 settings, | |
583 &page_ranges, | |
584 &highest_rendered_page_number, | |
585 &scale_factor); | |
586 if (succeeded) { | |
587 // TODO(vitalybuka|scottmg): http://crbug.com/170859. These could | |
588 // potentially be sent as each page is converted so that the spool could | |
589 // start sooner. | |
590 Send(new ChromeUtilityHostMsg_RenderPDFPagesToMetafiles_Succeeded( | |
591 page_ranges, scale_factor)); | |
592 } | |
593 #endif // defined(OS_WIN) | |
594 if (!succeeded) { | |
595 Send(new ChromeUtilityHostMsg_RenderPDFPagesToMetafile_Failed()); | |
596 } | |
597 ReleaseProcessIfNeeded(); | |
598 } | |
599 | |
600 void ChromeContentUtilityClient::OnRenderPDFPagesToPWGRaster( | |
601 IPC::PlatformFileForTransit pdf_transit, | |
602 const printing::PdfRenderSettings& settings, | |
603 const printing::PwgRasterSettings& bitmap_settings, | |
604 IPC::PlatformFileForTransit bitmap_transit) { | |
605 base::File pdf = IPC::PlatformFileForTransitToFile(pdf_transit); | |
606 base::File bitmap = IPC::PlatformFileForTransitToFile(bitmap_transit); | |
607 if (RenderPDFPagesToPWGRaster(pdf.Pass(), settings, bitmap_settings, | |
608 bitmap.Pass())) { | |
609 Send(new ChromeUtilityHostMsg_RenderPDFPagesToPWGRaster_Succeeded()); | |
610 } else { | |
611 Send(new ChromeUtilityHostMsg_RenderPDFPagesToPWGRaster_Failed()); | |
612 } | |
613 ReleaseProcessIfNeeded(); | |
614 } | |
615 | |
616 #if defined(OS_WIN) | |
617 bool ChromeContentUtilityClient::RenderPDFToWinMetafile( | |
618 base::File pdf_file, | |
619 const base::FilePath& metafile_path, | |
620 const printing::PdfRenderSettings& settings, | |
621 std::vector<printing::PageRange>* page_ranges, | |
622 int* highest_rendered_page_number, | |
623 double* scale_factor) { | |
624 DCHECK(page_ranges); | |
625 *highest_rendered_page_number = -1; | |
626 *scale_factor = 1.0; | |
627 | |
628 if (!g_pdf_lib.Get().IsValid()) | |
629 return false; | |
630 | |
631 // TODO(sanjeevr): Add a method to the PDF DLL that takes in a file handle | |
632 // and a page range array. That way we don't need to read the entire PDF into | |
633 // memory. | |
634 int64 length = pdf_file.GetLength(); | |
635 if (length < 0) | |
636 return false; | |
637 | |
638 std::vector<char> buffer; | |
639 buffer.resize(length); | |
640 if (length != pdf_file.Read(0, &buffer.front(), length)) | |
641 return false; | |
642 | |
643 int total_page_count = 0; | |
644 if (!g_pdf_lib.Get().GetPDFDocInfo(&buffer.front(), buffer.size(), | |
645 &total_page_count, NULL)) { | |
646 return false; | |
647 } | |
648 | |
649 // If no range supplied, do all pages. | |
650 if (page_ranges->empty()) { | |
651 printing::PageRange page_range_all; | |
652 page_range_all.from = 0; | |
653 page_range_all.to = total_page_count - 1; | |
654 page_ranges->push_back(page_range_all); | |
655 } | |
656 | |
657 bool ret = false; | |
658 std::vector<printing::PageRange>::const_iterator iter; | |
659 for (iter = page_ranges->begin(); iter != page_ranges->end(); ++iter) { | |
660 for (int page_number = iter->from; page_number <= iter->to; ++page_number) { | |
661 if (page_number >= total_page_count) | |
662 break; | |
663 | |
664 printing::Emf metafile; | |
665 metafile.InitToFile(metafile_path.InsertBeforeExtensionASCII( | |
666 base::StringPrintf(".%d", page_number))); | |
667 | |
668 // We need to scale down DC to fit an entire page into DC available area. | |
669 // Current metafile is based on screen DC and have current screen size. | |
670 // Writing outside of those boundaries will result in the cut-off output. | |
671 // On metafiles (this is the case here), scaling down will still record | |
672 // original coordinates and we'll be able to print in full resolution. | |
673 // Before playback we'll need to counter the scaling up that will happen | |
674 // in the service (print_system_win.cc). | |
675 *scale_factor = gfx::CalculatePageScale(metafile.context(), | |
676 settings.area().right(), | |
677 settings.area().bottom()); | |
678 gfx::ScaleDC(metafile.context(), *scale_factor); | |
679 | |
680 // The underlying metafile is of type Emf and ignores the arguments passed | |
681 // to StartPage. | |
682 metafile.StartPage(gfx::Size(), gfx::Rect(), 1); | |
683 if (g_pdf_lib.Get().RenderPDFPageToDC( | |
684 &buffer.front(), buffer.size(), page_number, metafile.context(), | |
685 settings.dpi(), settings.dpi(), settings.area().x(), | |
686 settings.area().y(), settings.area().width(), | |
687 settings.area().height(), true, false, true, true, | |
688 settings.autorotate())) { | |
689 if (*highest_rendered_page_number < page_number) | |
690 *highest_rendered_page_number = page_number; | |
691 ret = true; | |
692 } | |
693 metafile.FinishPage(); | |
694 metafile.FinishDocument(); | |
695 } | |
696 } | |
697 return ret; | |
698 } | |
699 #endif // defined(OS_WIN) | |
700 | |
701 bool ChromeContentUtilityClient::RenderPDFPagesToPWGRaster( | |
702 base::File pdf_file, | |
703 const printing::PdfRenderSettings& settings, | |
704 const printing::PwgRasterSettings& bitmap_settings, | |
705 base::File bitmap_file) { | |
706 bool autoupdate = true; | |
707 if (!g_pdf_lib.Get().IsValid()) | |
708 return false; | |
709 | |
710 base::File::Info info; | |
711 if (!pdf_file.GetInfo(&info) || info.size <= 0) | |
712 return false; | |
713 | |
714 std::string data(info.size, 0); | |
715 int data_size = pdf_file.Read(0, &data[0], data.size()); | |
716 if (data_size != static_cast<int>(data.size())) | |
717 return false; | |
718 | |
719 int total_page_count = 0; | |
720 if (!g_pdf_lib.Get().GetPDFDocInfo(data.data(), data.size(), | |
721 &total_page_count, NULL)) { | |
722 return false; | |
723 } | |
724 | |
725 cloud_print::PwgEncoder encoder; | |
726 std::string pwg_header; | |
727 encoder.EncodeDocumentHeader(&pwg_header); | |
728 int bytes_written = bitmap_file.WriteAtCurrentPos(pwg_header.data(), | |
729 pwg_header.size()); | |
730 if (bytes_written != static_cast<int>(pwg_header.size())) | |
731 return false; | |
732 | |
733 cloud_print::BitmapImage image(settings.area().size(), | |
734 cloud_print::BitmapImage::BGRA); | |
735 for (int i = 0; i < total_page_count; ++i) { | |
736 int page_number = i; | |
737 | |
738 if (bitmap_settings.reverse_page_order) { | |
739 page_number = total_page_count - 1 - page_number; | |
740 } | |
741 | |
742 if (!g_pdf_lib.Get().RenderPDFPageToBitmap(data.data(), | |
743 data.size(), | |
744 page_number, | |
745 image.pixel_data(), | |
746 image.size().width(), | |
747 image.size().height(), | |
748 settings.dpi(), | |
749 settings.dpi(), | |
750 autoupdate)) { | |
751 return false; | |
752 } | |
753 | |
754 cloud_print::PwgHeaderInfo header_info; | |
755 header_info.dpi = settings.dpi(); | |
756 header_info.total_pages = total_page_count; | |
757 | |
758 // Transform odd pages. | |
759 if (page_number % 2) { | |
760 switch (bitmap_settings.odd_page_transform) { | |
761 case printing::TRANSFORM_NORMAL: | |
762 break; | |
763 case printing::TRANSFORM_ROTATE_180: | |
764 header_info.flipx = true; | |
765 header_info.flipy = true; | |
766 break; | |
767 case printing::TRANSFORM_FLIP_HORIZONTAL: | |
768 header_info.flipx = true; | |
769 break; | |
770 case printing::TRANSFORM_FLIP_VERTICAL: | |
771 header_info.flipy = true; | |
772 break; | |
773 } | |
774 } | |
775 | |
776 if (bitmap_settings.rotate_all_pages) { | |
777 header_info.flipx = !header_info.flipx; | |
778 header_info.flipy = !header_info.flipy; | |
779 } | |
780 | |
781 std::string pwg_page; | |
782 if (!encoder.EncodePage(image, header_info, &pwg_page)) | |
783 return false; | |
784 bytes_written = bitmap_file.WriteAtCurrentPos(pwg_page.data(), | |
785 pwg_page.size()); | |
786 if (bytes_written != static_cast<int>(pwg_page.size())) | |
787 return false; | |
788 } | |
789 return true; | |
790 } | |
791 | |
792 void ChromeContentUtilityClient::OnRobustJPEGDecodeImage( | 342 void ChromeContentUtilityClient::OnRobustJPEGDecodeImage( |
793 const std::vector<unsigned char>& encoded_data) { | 343 const std::vector<unsigned char>& encoded_data) { |
794 // Our robust jpeg decoding is using IJG libjpeg. | 344 // Our robust jpeg decoding is using IJG libjpeg. |
795 if (gfx::JPEGCodec::JpegLibraryVariant() == gfx::JPEGCodec::IJG_LIBJPEG) { | 345 if (gfx::JPEGCodec::JpegLibraryVariant() == gfx::JPEGCodec::IJG_LIBJPEG) { |
796 scoped_ptr<SkBitmap> decoded_image(gfx::JPEGCodec::Decode( | 346 scoped_ptr<SkBitmap> decoded_image(gfx::JPEGCodec::Decode( |
797 &encoded_data[0], encoded_data.size())); | 347 &encoded_data[0], encoded_data.size())); |
798 if (!decoded_image.get() || decoded_image->empty()) { | 348 if (!decoded_image.get() || decoded_image->empty()) { |
799 Send(new ChromeUtilityHostMsg_DecodeImage_Failed()); | 349 Send(new ChromeUtilityHostMsg_DecodeImage_Failed()); |
800 } else { | 350 } else { |
801 Send(new ChromeUtilityHostMsg_DecodeImage_Succeeded(*decoded_image)); | 351 Send(new ChromeUtilityHostMsg_DecodeImage_Succeeded(*decoded_image)); |
(...skipping 12 matching lines...) Expand all Loading... |
814 if (value) { | 364 if (value) { |
815 base::ListValue wrapper; | 365 base::ListValue wrapper; |
816 wrapper.Append(value); | 366 wrapper.Append(value); |
817 Send(new ChromeUtilityHostMsg_ParseJSON_Succeeded(wrapper)); | 367 Send(new ChromeUtilityHostMsg_ParseJSON_Succeeded(wrapper)); |
818 } else { | 368 } else { |
819 Send(new ChromeUtilityHostMsg_ParseJSON_Failed(error)); | 369 Send(new ChromeUtilityHostMsg_ParseJSON_Failed(error)); |
820 } | 370 } |
821 ReleaseProcessIfNeeded(); | 371 ReleaseProcessIfNeeded(); |
822 } | 372 } |
823 | 373 |
824 void ChromeContentUtilityClient::OnGetPrinterCapsAndDefaults( | |
825 const std::string& printer_name) { | |
826 #if defined(ENABLE_FULL_PRINTING) | |
827 scoped_refptr<printing::PrintBackend> print_backend = | |
828 printing::PrintBackend::CreateInstance(NULL); | |
829 printing::PrinterCapsAndDefaults printer_info; | |
830 | |
831 crash_keys::ScopedPrinterInfo crash_key( | |
832 print_backend->GetPrinterDriverInfo(printer_name)); | |
833 | |
834 if (print_backend->GetPrinterCapsAndDefaults(printer_name, &printer_info)) { | |
835 Send(new ChromeUtilityHostMsg_GetPrinterCapsAndDefaults_Succeeded( | |
836 printer_name, printer_info)); | |
837 } else // NOLINT | |
838 #endif | |
839 { | |
840 Send(new ChromeUtilityHostMsg_GetPrinterCapsAndDefaults_Failed( | |
841 printer_name)); | |
842 } | |
843 ReleaseProcessIfNeeded(); | |
844 } | |
845 | |
846 void ChromeContentUtilityClient::OnGetPrinterSemanticCapsAndDefaults( | |
847 const std::string& printer_name) { | |
848 #if defined(ENABLE_FULL_PRINTING) | |
849 scoped_refptr<printing::PrintBackend> print_backend = | |
850 printing::PrintBackend::CreateInstance(NULL); | |
851 printing::PrinterSemanticCapsAndDefaults printer_info; | |
852 | |
853 crash_keys::ScopedPrinterInfo crash_key( | |
854 print_backend->GetPrinterDriverInfo(printer_name)); | |
855 | |
856 if (print_backend->GetPrinterSemanticCapsAndDefaults(printer_name, | |
857 &printer_info)) { | |
858 Send(new ChromeUtilityHostMsg_GetPrinterSemanticCapsAndDefaults_Succeeded( | |
859 printer_name, printer_info)); | |
860 } else // NOLINT | |
861 #endif | |
862 { | |
863 Send(new ChromeUtilityHostMsg_GetPrinterSemanticCapsAndDefaults_Failed( | |
864 printer_name)); | |
865 } | |
866 ReleaseProcessIfNeeded(); | |
867 } | |
868 | |
869 void ChromeContentUtilityClient::OnPatchFileBsdiff( | 374 void ChromeContentUtilityClient::OnPatchFileBsdiff( |
870 const base::FilePath& input_file, | 375 const base::FilePath& input_file, |
871 const base::FilePath& patch_file, | 376 const base::FilePath& patch_file, |
872 const base::FilePath& output_file) { | 377 const base::FilePath& output_file) { |
873 if (input_file.empty() || patch_file.empty() || output_file.empty()) { | 378 if (input_file.empty() || patch_file.empty() || output_file.empty()) { |
874 Send(new ChromeUtilityHostMsg_PatchFile_Failed(-1)); | 379 Send(new ChromeUtilityHostMsg_PatchFile_Failed(-1)); |
875 } else { | 380 } else { |
876 const int patch_status = courgette::ApplyBinaryPatch(input_file, | 381 const int patch_status = courgette::ApplyBinaryPatch(input_file, |
877 patch_file, | 382 patch_file, |
878 output_file); | 383 output_file); |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1027 bool success = error.empty() && !key_data.empty(); | 532 bool success = error.empty() && !key_data.empty(); |
1028 if (success) { | 533 if (success) { |
1029 NetworkingPrivateCrypto crypto; | 534 NetworkingPrivateCrypto crypto; |
1030 success = crypto.EncryptByteString(public_key, key_data, &ciphertext); | 535 success = crypto.EncryptByteString(public_key, key_data, &ciphertext); |
1031 } | 536 } |
1032 | 537 |
1033 Send(new ChromeUtilityHostMsg_GotEncryptedWiFiCredentials(ciphertext, | 538 Send(new ChromeUtilityHostMsg_GotEncryptedWiFiCredentials(ciphertext, |
1034 success)); | 539 success)); |
1035 } | 540 } |
1036 #endif // defined(OS_WIN) | 541 #endif // defined(OS_WIN) |
1037 | |
1038 } // namespace chrome | |
OLD | NEW |