|
OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
Matt Perry
2011/06/09 01:40:33
I only skimmed this file, assuming most of it was
jam
2011/06/09 02:45:57
yep all moved
| |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/utility/chrome_content_utility_client.h" | |
6 | |
7 #include "base/base64.h" | |
8 #include "base/command_line.h" | |
9 #include "base/json/json_reader.h" | |
10 #include "chrome/common/chrome_paths.h" | |
11 #include "chrome/common/chrome_utility_messages.h" | |
12 #include "chrome/common/extensions/extension_l10n_util.h" | |
13 #include "chrome/common/extensions/extension_unpacker.h" | |
14 #include "chrome/common/extensions/update_manifest.h" | |
15 #include "chrome/common/web_resource/web_resource_unpacker.h" | |
16 #include "content/utility/utility_thread.h" | |
17 #include "printing/backend/print_backend.h" | |
18 #include "printing/page_range.h" | |
19 #include "third_party/skia/include/core/SkBitmap.h" | |
20 #include "ui/base/ui_base_switches.h" | |
21 #include "ui/gfx/rect.h" | |
22 #include "webkit/glue/image_decoder.h" | |
23 | |
24 #if defined(OS_WIN) | |
25 #include "app/win/iat_patch_function.h" | |
26 #include "base/file_util.h" | |
27 #include "base/memory/scoped_ptr.h" | |
28 #include "base/path_service.h" | |
29 #include "base/win/scoped_handle.h" | |
30 #include "content/common/content_switches.h" | |
31 #include "content/common/sandbox_init_wrapper.h" | |
32 #include "printing/emf_win.h" | |
33 #endif // defined(OS_WIN) | |
34 | |
35 namespace chrome { | |
36 | |
37 ChromeContentUtilityClient::ChromeContentUtilityClient() { | |
38 } | |
39 | |
40 ChromeContentUtilityClient::~ChromeContentUtilityClient() { | |
41 } | |
42 | |
43 void ChromeContentUtilityClient::UtilityThreadStarted() { | |
44 #if defined(OS_WIN) | |
45 // Load the pdf plugin before the sandbox is turned on. This is for Windows | |
46 // only because we need this DLL only on Windows. | |
47 FilePath pdf; | |
48 if (PathService::Get(chrome::FILE_PDF_PLUGIN, &pdf) && | |
49 file_util::PathExists(pdf)) { | |
50 bool rv = !!LoadLibrary(pdf.value().c_str()); | |
51 DCHECK(rv) << "Couldn't load PDF plugin"; | |
52 } | |
53 #endif | |
54 | |
55 CommandLine* command_line = CommandLine::ForCurrentProcess(); | |
56 std::string lang = command_line->GetSwitchValueASCII(switches::kLang); | |
57 if (!lang.empty()) | |
58 extension_l10n_util::SetProcessLocale(lang); | |
59 } | |
60 | |
61 bool ChromeContentUtilityClient::OnMessageReceived( | |
62 const IPC::Message& message) { | |
63 bool handled = true; | |
64 IPC_BEGIN_MESSAGE_MAP(ChromeContentUtilityClient, message) | |
65 IPC_MESSAGE_HANDLER(UtilityMsg_UnpackExtension, OnUnpackExtension) | |
66 IPC_MESSAGE_HANDLER(UtilityMsg_UnpackWebResource, OnUnpackWebResource) | |
67 IPC_MESSAGE_HANDLER(UtilityMsg_ParseUpdateManifest, OnParseUpdateManifest) | |
68 IPC_MESSAGE_HANDLER(UtilityMsg_DecodeImage, OnDecodeImage) | |
69 IPC_MESSAGE_HANDLER(UtilityMsg_DecodeImageBase64, OnDecodeImageBase64) | |
70 IPC_MESSAGE_HANDLER(UtilityMsg_RenderPDFPagesToMetafile, | |
71 OnRenderPDFPagesToMetafile) | |
72 IPC_MESSAGE_HANDLER(UtilityMsg_ParseJSON, OnParseJSON) | |
73 IPC_MESSAGE_HANDLER(UtilityMsg_GetPrinterCapsAndDefaults, | |
74 OnGetPrinterCapsAndDefaults) | |
75 IPC_MESSAGE_UNHANDLED(handled = false) | |
76 IPC_END_MESSAGE_MAP() | |
77 return handled; | |
78 } | |
79 | |
80 bool ChromeContentUtilityClient::Send(IPC::Message* message) { | |
81 return UtilityThread::current()->Send(message); | |
82 } | |
83 | |
84 void ChromeContentUtilityClient::OnUnpackExtension( | |
85 const FilePath& extension_path) { | |
86 ExtensionUnpacker unpacker(extension_path); | |
87 if (unpacker.Run() && unpacker.DumpImagesToFile() && | |
88 unpacker.DumpMessageCatalogsToFile()) { | |
89 Send(new UtilityHostMsg_UnpackExtension_Succeeded( | |
90 *unpacker.parsed_manifest())); | |
Matt Perry
2011/06/09 01:40:33
indent +1
| |
91 } else { | |
92 Send(new UtilityHostMsg_UnpackExtension_Failed(unpacker.error_message())); | |
93 } | |
94 | |
95 UtilityThread::current()->ReleaseProcessIfNeeded(); | |
96 } | |
97 | |
98 void ChromeContentUtilityClient::OnUnpackWebResource( | |
99 const std::string& resource_data) { | |
100 // Parse json data. | |
101 // TODO(mrc): Add the possibility of a template that controls parsing, and | |
102 // the ability to download and verify images. | |
103 WebResourceUnpacker unpacker(resource_data); | |
104 if (unpacker.Run()) { | |
105 Send(new UtilityHostMsg_UnpackWebResource_Succeeded( | |
106 *unpacker.parsed_json())); | |
107 } else { | |
108 Send(new UtilityHostMsg_UnpackWebResource_Failed( | |
109 unpacker.error_message())); | |
110 } | |
111 | |
112 UtilityThread::current()->ReleaseProcessIfNeeded(); | |
113 } | |
114 | |
115 void ChromeContentUtilityClient::OnParseUpdateManifest(const std::string& xml) { | |
116 UpdateManifest manifest; | |
117 if (!manifest.Parse(xml)) { | |
118 Send(new UtilityHostMsg_ParseUpdateManifest_Failed(manifest.errors())); | |
119 } else { | |
120 Send(new UtilityHostMsg_ParseUpdateManifest_Succeeded(manifest.results())); | |
121 } | |
122 UtilityThread::current()->ReleaseProcessIfNeeded(); | |
123 } | |
124 | |
125 void ChromeContentUtilityClient::OnDecodeImage( | |
126 const std::vector<unsigned char>& encoded_data) { | |
127 webkit_glue::ImageDecoder decoder; | |
128 const SkBitmap& decoded_image = decoder.Decode(&encoded_data[0], | |
129 encoded_data.size()); | |
130 if (decoded_image.empty()) { | |
131 Send(new UtilityHostMsg_DecodeImage_Failed()); | |
132 } else { | |
133 Send(new UtilityHostMsg_DecodeImage_Succeeded(decoded_image)); | |
134 } | |
135 UtilityThread::current()->ReleaseProcessIfNeeded(); | |
136 } | |
137 | |
138 void ChromeContentUtilityClient::OnDecodeImageBase64( | |
139 const std::string& encoded_string) { | |
140 std::string decoded_string; | |
141 | |
142 if (!base::Base64Decode(encoded_string, &decoded_string)) { | |
143 Send(new UtilityHostMsg_DecodeImage_Failed()); | |
144 return; | |
145 } | |
146 | |
147 std::vector<unsigned char> decoded_vector(decoded_string.size()); | |
148 for (size_t i = 0; i < decoded_string.size(); ++i) { | |
149 decoded_vector[i] = static_cast<unsigned char>(decoded_string[i]); | |
150 } | |
151 | |
152 OnDecodeImage(decoded_vector); | |
153 } | |
154 | |
155 void ChromeContentUtilityClient::OnRenderPDFPagesToMetafile( | |
156 base::PlatformFile pdf_file, | |
157 const FilePath& metafile_path, | |
158 const gfx::Rect& render_area, | |
159 int render_dpi, | |
160 const std::vector<printing::PageRange>& page_ranges) { | |
161 bool succeeded = false; | |
162 #if defined(OS_WIN) | |
163 int highest_rendered_page_number = 0; | |
164 succeeded = RenderPDFToWinMetafile(pdf_file, | |
165 metafile_path, | |
166 render_area, | |
167 render_dpi, | |
168 page_ranges, | |
169 &highest_rendered_page_number); | |
170 if (succeeded) { | |
171 Send(new UtilityHostMsg_RenderPDFPagesToMetafile_Succeeded( | |
172 highest_rendered_page_number)); | |
173 } | |
174 #endif // defined(OS_WIN) | |
175 if (!succeeded) { | |
176 Send(new UtilityHostMsg_RenderPDFPagesToMetafile_Failed()); | |
177 } | |
178 UtilityThread::current()->ReleaseProcessIfNeeded(); | |
179 } | |
180 | |
181 #if defined(OS_WIN) | |
182 // Exported by pdf.dll | |
183 typedef bool (*RenderPDFPageToDCProc)( | |
184 const unsigned char* pdf_buffer, int buffer_size, int page_number, HDC dc, | |
185 int dpi_x, int dpi_y, int bounds_origin_x, int bounds_origin_y, | |
186 int bounds_width, int bounds_height, bool fit_to_bounds, | |
187 bool stretch_to_bounds, bool keep_aspect_ratio, bool center_in_bounds); | |
188 | |
189 typedef bool (*GetPDFDocInfoProc)(const unsigned char* pdf_buffer, | |
190 int buffer_size, int* page_count, | |
191 double* max_page_width); | |
192 | |
193 // The 2 below IAT patch functions are almost identical to the code in | |
194 // render_process_impl.cc. This is needed to work around specific Windows APIs | |
195 // used by the Chrome PDF plugin that will fail in the sandbox. | |
196 static app::win::IATPatchFunction g_iat_patch_createdca; | |
197 HDC WINAPI UtilityProcess_CreateDCAPatch(LPCSTR driver_name, | |
198 LPCSTR device_name, | |
199 LPCSTR output, | |
200 const DEVMODEA* init_data) { | |
201 if (driver_name && | |
202 (std::string("DISPLAY") == std::string(driver_name))) | |
203 // CreateDC fails behind the sandbox, but not CreateCompatibleDC. | |
204 return CreateCompatibleDC(NULL); | |
205 | |
206 NOTREACHED(); | |
207 return CreateDCA(driver_name, device_name, output, init_data); | |
208 } | |
209 | |
210 static app::win::IATPatchFunction g_iat_patch_get_font_data; | |
211 DWORD WINAPI UtilityProcess_GetFontDataPatch( | |
212 HDC hdc, DWORD table, DWORD offset, LPVOID buffer, DWORD length) { | |
213 int rv = GetFontData(hdc, table, offset, buffer, length); | |
214 if (rv == GDI_ERROR && hdc) { | |
215 HFONT font = static_cast<HFONT>(GetCurrentObject(hdc, OBJ_FONT)); | |
216 | |
217 LOGFONT logfont; | |
218 if (GetObject(font, sizeof(LOGFONT), &logfont)) { | |
219 std::vector<char> font_data; | |
220 if (UtilityThread::current()->Send( | |
221 new UtilityHostMsg_PreCacheFont(logfont))) | |
222 rv = GetFontData(hdc, table, offset, buffer, length); | |
223 } | |
224 } | |
225 return rv; | |
226 } | |
227 | |
228 bool ChromeContentUtilityClient::RenderPDFToWinMetafile( | |
229 base::PlatformFile pdf_file, | |
230 const FilePath& metafile_path, | |
231 const gfx::Rect& render_area, | |
232 int render_dpi, | |
233 const std::vector<printing::PageRange>& page_ranges, | |
234 int* highest_rendered_page_number) { | |
235 *highest_rendered_page_number = -1; | |
236 base::win::ScopedHandle file(pdf_file); | |
237 FilePath pdf_module_path; | |
238 PathService::Get(chrome::FILE_PDF_PLUGIN, &pdf_module_path); | |
239 HMODULE pdf_module = GetModuleHandle(pdf_module_path.value().c_str()); | |
240 if (!pdf_module) | |
241 return false; | |
242 | |
243 RenderPDFPageToDCProc render_proc = | |
244 reinterpret_cast<RenderPDFPageToDCProc>( | |
245 GetProcAddress(pdf_module, "RenderPDFPageToDC")); | |
246 if (!render_proc) | |
247 return false; | |
248 | |
249 GetPDFDocInfoProc get_info_proc = reinterpret_cast<GetPDFDocInfoProc>( | |
250 GetProcAddress(pdf_module, "GetPDFDocInfo")); | |
251 if (!get_info_proc) | |
252 return false; | |
253 | |
254 // Patch the IAT for handling specific APIs known to fail in the sandbox. | |
255 if (!g_iat_patch_createdca.is_patched()) | |
256 g_iat_patch_createdca.Patch(pdf_module_path.value().c_str(), | |
257 "gdi32.dll", "CreateDCA", | |
258 UtilityProcess_CreateDCAPatch); | |
259 | |
260 if (!g_iat_patch_get_font_data.is_patched()) | |
261 g_iat_patch_get_font_data.Patch(pdf_module_path.value().c_str(), | |
262 "gdi32.dll", "GetFontData", | |
263 UtilityProcess_GetFontDataPatch); | |
264 | |
265 // TODO(sanjeevr): Add a method to the PDF DLL that takes in a file handle | |
266 // and a page range array. That way we don't need to read the entire PDF into | |
267 // memory. | |
268 DWORD length = ::GetFileSize(file, NULL); | |
269 if (length == INVALID_FILE_SIZE) | |
270 return false; | |
271 | |
272 std::vector<uint8> buffer; | |
273 buffer.resize(length); | |
274 DWORD bytes_read = 0; | |
275 if (!ReadFile(pdf_file, &buffer.front(), length, &bytes_read, NULL) || | |
276 (bytes_read != length)) | |
277 return false; | |
278 | |
279 int total_page_count = 0; | |
280 if (!get_info_proc(&buffer.front(), buffer.size(), &total_page_count, NULL)) | |
281 return false; | |
282 | |
283 printing::Emf metafile; | |
284 metafile.InitToFile(metafile_path); | |
285 // Since we created the metafile using the screen DPI (but we actually want | |
286 // the PDF DLL to print using the passed in render_dpi, we apply the following | |
287 // transformation. | |
288 SetGraphicsMode(metafile.context(), GM_ADVANCED); | |
289 XFORM xform = {0}; | |
290 int screen_dpi = GetDeviceCaps(GetDC(NULL), LOGPIXELSX); | |
291 xform.eM11 = xform.eM22 = | |
292 static_cast<float>(screen_dpi) / static_cast<float>(render_dpi); | |
293 ModifyWorldTransform(metafile.context(), &xform, MWT_LEFTMULTIPLY); | |
294 | |
295 bool ret = false; | |
296 std::vector<printing::PageRange>::const_iterator iter; | |
297 for (iter = page_ranges.begin(); iter != page_ranges.end(); ++iter) { | |
298 for (int page_number = iter->from; page_number <= iter->to; ++page_number) { | |
299 if (page_number >= total_page_count) | |
300 break; | |
301 // The underlying metafile is of type Emf and ignores the arguments passed | |
302 // to StartPage. | |
303 metafile.StartPage(gfx::Size(), gfx::Rect(), 1); | |
304 if (render_proc(&buffer.front(), buffer.size(), page_number, | |
305 metafile.context(), render_dpi, render_dpi, | |
306 render_area.x(), render_area.y(), render_area.width(), | |
307 render_area.height(), true, false, true, true)) | |
308 if (*highest_rendered_page_number < page_number) | |
309 *highest_rendered_page_number = page_number; | |
310 ret = true; | |
311 metafile.FinishPage(); | |
312 } | |
313 } | |
314 metafile.FinishDocument(); | |
315 return ret; | |
316 } | |
317 #endif // defined(OS_WIN) | |
318 | |
319 | |
320 void ChromeContentUtilityClient::OnParseJSON(const std::string& json) { | |
321 int error_code; | |
322 std::string error; | |
323 Value* value = | |
324 base::JSONReader::ReadAndReturnError(json, false, &error_code, &error); | |
325 if (value) { | |
326 ListValue wrapper; | |
327 wrapper.Append(value); | |
328 Send(new UtilityHostMsg_ParseJSON_Succeeded(wrapper)); | |
329 } else { | |
330 Send(new UtilityHostMsg_ParseJSON_Failed(error)); | |
331 } | |
332 UtilityThread::current()->ReleaseProcessIfNeeded(); | |
333 } | |
334 | |
335 void ChromeContentUtilityClient::OnGetPrinterCapsAndDefaults( | |
336 const std::string& printer_name) { | |
337 scoped_refptr<printing::PrintBackend> print_backend = | |
338 printing::PrintBackend::CreateInstance(NULL); | |
339 printing::PrinterCapsAndDefaults printer_info; | |
340 if (print_backend->GetPrinterCapsAndDefaults(printer_name, &printer_info)) { | |
341 Send(new UtilityHostMsg_GetPrinterCapsAndDefaults_Succeeded(printer_name, | |
342 printer_info)); | |
343 } else { | |
344 Send(new UtilityHostMsg_GetPrinterCapsAndDefaults_Failed(printer_name)); | |
345 } | |
346 UtilityThread::current()->ReleaseProcessIfNeeded(); | |
347 } | |
348 | |
349 } // namespace chrome | |
OLD | NEW |