Chromium Code Reviews| 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" | |
| 10 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 11 #include "base/json/json_reader.h" | 12 #include "base/json/json_reader.h" |
| 12 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/path_service.h" | 15 #include "base/path_service.h" |
| 16 #include "base/scoped_native_library.h" | |
| 15 #include "base/time/time.h" | 17 #include "base/time/time.h" |
| 18 #include "chrome/common/chrome_paths.h" | |
| 16 #include "chrome/common/chrome_utility_messages.h" | 19 #include "chrome/common/chrome_utility_messages.h" |
| 17 #include "chrome/common/extensions/chrome_extensions_client.h" | 20 #include "chrome/common/extensions/chrome_extensions_client.h" |
| 18 #include "chrome/common/extensions/extension.h" | 21 #include "chrome/common/extensions/extension.h" |
| 19 #include "chrome/common/extensions/extension_l10n_util.h" | 22 #include "chrome/common/extensions/extension_l10n_util.h" |
| 20 #include "chrome/common/extensions/update_manifest.h" | 23 #include "chrome/common/extensions/update_manifest.h" |
| 21 #include "chrome/common/safe_browsing/zip_analyzer.h" | 24 #include "chrome/common/safe_browsing/zip_analyzer.h" |
| 22 #include "chrome/utility/extensions/unpacker.h" | 25 #include "chrome/utility/extensions/unpacker.h" |
| 23 #include "chrome/utility/profile_import_handler.h" | 26 #include "chrome/utility/profile_import_handler.h" |
| 24 #include "chrome/utility/web_resource_unpacker.h" | 27 #include "chrome/utility/web_resource_unpacker.h" |
| 25 #include "content/public/child/image_decoder_utils.h" | 28 #include "content/public/child/image_decoder_utils.h" |
| 26 #include "content/public/common/content_paths.h" | 29 #include "content/public/common/content_paths.h" |
| 27 #include "content/public/utility/utility_thread.h" | 30 #include "content/public/utility/utility_thread.h" |
| 28 #include "extensions/common/manifest.h" | 31 #include "extensions/common/manifest.h" |
| 29 #include "media/base/media.h" | 32 #include "media/base/media.h" |
| 30 #include "media/base/media_file_checker.h" | 33 #include "media/base/media_file_checker.h" |
| 31 #include "printing/page_range.h" | 34 #include "printing/page_range.h" |
| 32 #include "third_party/skia/include/core/SkBitmap.h" | 35 #include "third_party/skia/include/core/SkBitmap.h" |
| 33 #include "third_party/zlib/google/zip.h" | 36 #include "third_party/zlib/google/zip.h" |
| 34 #include "ui/base/ui_base_switches.h" | 37 #include "ui/base/ui_base_switches.h" |
| 35 #include "ui/gfx/codec/jpeg_codec.h" | 38 #include "ui/gfx/codec/jpeg_codec.h" |
| 36 #include "ui/gfx/rect.h" | 39 #include "ui/gfx/rect.h" |
| 37 #include "ui/gfx/size.h" | 40 #include "ui/gfx/size.h" |
| 38 | 41 |
| 39 #if defined(OS_WIN) | 42 #if defined(OS_WIN) |
| 40 #include "base/file_util.h" | |
| 41 #include "base/path_service.h" | |
| 42 #include "base/win/iat_patch_function.h" | 43 #include "base/win/iat_patch_function.h" |
| 43 #include "base/win/scoped_handle.h" | 44 #include "base/win/scoped_handle.h" |
| 44 #include "chrome/common/chrome_paths.h" | |
| 45 #include "chrome/utility/media_galleries/itunes_pref_parser_win.h" | 45 #include "chrome/utility/media_galleries/itunes_pref_parser_win.h" |
| 46 #include "printing/emf_win.h" | 46 #include "printing/emf_win.h" |
| 47 #include "ui/gfx/gdi_util.h" | 47 #include "ui/gfx/gdi_util.h" |
| 48 #endif // defined(OS_WIN) | 48 #endif // defined(OS_WIN) |
| 49 | 49 |
| 50 #if defined(OS_MACOSX) | 50 #if defined(OS_MACOSX) |
| 51 #include "chrome/utility/media_galleries/iphoto_library_parser.h" | 51 #include "chrome/utility/media_galleries/iphoto_library_parser.h" |
| 52 #endif // defined(OS_MACOSX) | 52 #endif // defined(OS_MACOSX) |
| 53 | 53 |
| 54 #if defined(OS_WIN) || defined(OS_MACOSX) | 54 #if defined(OS_WIN) || defined(OS_MACOSX) |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 73 namespace { | 73 namespace { |
| 74 | 74 |
| 75 bool Send(IPC::Message* message) { | 75 bool Send(IPC::Message* message) { |
| 76 return content::UtilityThread::Get()->Send(message); | 76 return content::UtilityThread::Get()->Send(message); |
| 77 } | 77 } |
| 78 | 78 |
| 79 void ReleaseProcessIfNeeded() { | 79 void ReleaseProcessIfNeeded() { |
| 80 content::UtilityThread::Get()->ReleaseProcessIfNeeded(); | 80 content::UtilityThread::Get()->ReleaseProcessIfNeeded(); |
| 81 } | 81 } |
| 82 | 82 |
| 83 class PdfFunctionsBase { | |
| 84 public: | |
| 85 PdfFunctionsBase() : render_pdf_to_bitmap_(NULL) {} | |
| 86 | |
| 87 bool Init() { | |
| 88 base::FilePath pdf_module_path; | |
| 89 if (!PathService::Get(chrome::FILE_PDF_PLUGIN, &pdf_module_path) && | |
|
Lei Zhang
2013/11/14 21:56:54
Shouldn't this be:
if (!PathService::Get() || !Pa
Vitaly Buka (NO REVIEWS)
2013/11/14 22:10:20
Done.
| |
| 90 base::PathExists(pdf_module_path)) { | |
| 91 return false; | |
| 92 } | |
| 93 | |
| 94 pdf_lib_.Reset(base::LoadNativeLibrary(pdf_module_path, NULL)); | |
| 95 if (!pdf_lib_.is_valid()) { | |
| 96 LOG(WARNING) << "Couldn't load PDF plugin"; | |
| 97 return false; | |
| 98 } | |
| 99 | |
| 100 render_pdf_to_bitmap_ = | |
| 101 reinterpret_cast<RenderPDFPageToBitmapProc>( | |
| 102 pdf_lib_.GetFunctionPointer("RenderPDFPageToBitmap")); | |
| 103 LOG_IF(WARNING, !render_pdf_to_bitmap_) << "Missing RenderPDFPageToBitmap"; | |
| 104 | |
| 105 if (!render_pdf_to_bitmap_ || !PlatformInit(pdf_module_path, pdf_lib_)) | |
| 106 Reset(); | |
| 107 | |
| 108 return IsValid(); | |
| 109 } | |
| 110 | |
| 111 bool IsValid() const { | |
| 112 return pdf_lib_.is_valid(); | |
| 113 } | |
| 114 | |
| 115 void Reset() { | |
| 116 pdf_lib_.Reset(NULL); | |
| 117 } | |
| 118 | |
| 119 bool RenderPDFPageToBitmap(const unsigned char* pdf_buffer, | |
| 120 int buffer_size, | |
| 121 int page_number, | |
| 122 unsigned char* bitmap_buffer, | |
| 123 int bitmap_height, | |
| 124 int bitmap_width, | |
| 125 int dpi_x, | |
| 126 int dpi_y, | |
| 127 bool autorotate) { | |
| 128 if (!render_pdf_to_bitmap_) | |
| 129 return false; | |
| 130 return render_pdf_to_bitmap_(pdf_buffer, buffer_size, page_number, | |
| 131 bitmap_buffer, bitmap_height, bitmap_width, | |
| 132 dpi_x, dpi_y, autorotate); | |
| 133 } | |
| 134 | |
| 135 protected: | |
| 136 virtual bool PlatformInit( | |
| 137 const base::FilePath& pdf_module_path, | |
| 138 const base::ScopedNativeLibrary& pdf_lib) { | |
| 139 return true; | |
| 140 }; | |
| 141 | |
| 142 private: | |
| 143 // Exported by pdf.dll | |
|
Lei Zhang
2013/11/14 21:56:54
nit: "PDF plugin", since this is non-Windows as we
Vitaly Buka (NO REVIEWS)
2013/11/14 22:10:20
Done.
| |
| 144 typedef bool (*RenderPDFPageToBitmapProc)(const unsigned char* pdf_buffer, | |
| 145 int buffer_size, | |
| 146 int page_number, | |
| 147 unsigned char* bitmap_buffer, | |
| 148 int bitmap_height, | |
| 149 int bitmap_width, | |
| 150 int dpi_x, | |
| 151 int dpi_y, | |
| 152 bool autorotate); | |
| 153 RenderPDFPageToBitmapProc render_pdf_to_bitmap_; | |
|
Lei Zhang
2013/11/14 21:56:54
render_pdf_to_bitmap_func_ ?
"render_pdf_to_bitmap
Vitaly Buka (NO REVIEWS)
2013/11/14 22:10:20
Done.
| |
| 154 | |
| 155 base::ScopedNativeLibrary pdf_lib_; | |
| 156 DISALLOW_COPY_AND_ASSIGN(PdfFunctionsBase); | |
| 157 }; | |
| 158 | |
| 159 #if defined(OS_WIN) | |
| 160 // The 2 below IAT patch functions are almost identical to the code in | |
| 161 // render_process_impl.cc. This is needed to work around specific Windows APIs | |
| 162 // used by the Chrome PDF plugin that will fail in the sandbox. | |
| 163 static base::win::IATPatchFunction g_iat_patch_createdca; | |
| 164 HDC WINAPI UtilityProcess_CreateDCAPatch(LPCSTR driver_name, | |
| 165 LPCSTR device_name, | |
| 166 LPCSTR output, | |
| 167 const DEVMODEA* init_data) { | |
| 168 if (driver_name && (std::string("DISPLAY") == driver_name)) { | |
| 169 // CreateDC fails behind the sandbox, but not CreateCompatibleDC. | |
| 170 return CreateCompatibleDC(NULL); | |
| 171 } | |
| 172 | |
| 173 NOTREACHED(); | |
| 174 return CreateDCA(driver_name, device_name, output, init_data); | |
| 175 } | |
| 176 | |
| 177 static base::win::IATPatchFunction g_iat_patch_get_font_data; | |
| 178 DWORD WINAPI UtilityProcess_GetFontDataPatch( | |
| 179 HDC hdc, DWORD table, DWORD offset, LPVOID buffer, DWORD length) { | |
| 180 int rv = GetFontData(hdc, table, offset, buffer, length); | |
| 181 if (rv == GDI_ERROR && hdc) { | |
| 182 HFONT font = static_cast<HFONT>(GetCurrentObject(hdc, OBJ_FONT)); | |
| 183 | |
| 184 LOGFONT logfont; | |
| 185 if (GetObject(font, sizeof(LOGFONT), &logfont)) { | |
| 186 content::UtilityThread::Get()->PreCacheFont(logfont); | |
| 187 rv = GetFontData(hdc, table, offset, buffer, length); | |
| 188 content::UtilityThread::Get()->ReleaseCachedFonts(); | |
| 189 } | |
| 190 } | |
| 191 return rv; | |
| 192 } | |
| 193 | |
| 194 class PdfFunctionsWin : public PdfFunctionsBase { | |
| 195 public: | |
| 196 PdfFunctionsWin() : render_pdf_to_dc_(NULL), | |
| 197 get_pdf_doc_info_(NULL) { | |
| 198 } | |
| 199 | |
| 200 bool PlatformInit( | |
| 201 const base::FilePath& pdf_module_path, | |
| 202 const base::ScopedNativeLibrary& pdf_lib) OVERRIDE { | |
| 203 // Patch the IAT for handling specific APIs known to fail in the sandbox. | |
| 204 if (!g_iat_patch_createdca.is_patched()) { | |
| 205 g_iat_patch_createdca.Patch(pdf_module_path.value().c_str(), | |
| 206 "gdi32.dll", "CreateDCA", | |
| 207 UtilityProcess_CreateDCAPatch); | |
| 208 } | |
| 209 | |
| 210 if (!g_iat_patch_get_font_data.is_patched()) { | |
| 211 g_iat_patch_get_font_data.Patch(pdf_module_path.value().c_str(), | |
| 212 "gdi32.dll", "GetFontData", | |
| 213 UtilityProcess_GetFontDataPatch); | |
| 214 } | |
| 215 render_pdf_to_dc_ = | |
| 216 reinterpret_cast<RenderPDFPageToDCProc>( | |
| 217 pdf_lib.GetFunctionPointer("RenderPDFPageToDC")); | |
| 218 LOG_IF(WARNING, !render_pdf_to_dc_) << "Missing RenderPDFPageToDC"; | |
| 219 | |
| 220 get_pdf_doc_info_ = | |
| 221 reinterpret_cast<GetPDFDocInfoProc>( | |
| 222 pdf_lib.GetFunctionPointer("GetPDFDocInfo")); | |
| 223 LOG_IF(WARNING, !get_pdf_doc_info_) << "Missing GetPDFDocInfo"; | |
| 224 return render_pdf_to_dc_ && get_pdf_doc_info_; | |
| 225 } | |
| 226 | |
| 227 bool RenderPDFPageToDC(const unsigned char* pdf_buffer, | |
| 228 int buffer_size, | |
| 229 int page_number, | |
| 230 HDC dc, | |
| 231 int dpi_x, | |
| 232 int dpi_y, | |
| 233 int bounds_origin_x, | |
| 234 int bounds_origin_y, | |
| 235 int bounds_width, | |
| 236 int bounds_height, | |
| 237 bool fit_to_bounds, | |
| 238 bool stretch_to_bounds, | |
| 239 bool keep_aspect_ratio, | |
| 240 bool center_in_bounds, | |
| 241 bool autorotate) { | |
| 242 if (!render_pdf_to_dc_) | |
| 243 return false; | |
| 244 return render_pdf_to_dc_(pdf_buffer, buffer_size, page_number, | |
| 245 dc, dpi_x, dpi_y, bounds_origin_x, bounds_origin_y, | |
| 246 bounds_width, bounds_height, fit_to_bounds, | |
| 247 stretch_to_bounds, keep_aspect_ratio, | |
| 248 center_in_bounds, autorotate); | |
| 249 } | |
| 250 | |
| 251 bool GetPDFDocInfo(const unsigned char* pdf_buffer, | |
| 252 int buffer_size, | |
| 253 int* page_count, | |
| 254 double* max_page_width) { | |
| 255 if (!get_pdf_doc_info_) | |
| 256 return false; | |
| 257 return get_pdf_doc_info_(pdf_buffer, buffer_size, page_count, | |
| 258 max_page_width); | |
| 259 } | |
| 260 | |
| 261 private: | |
| 262 // Exported by pdf.dll | |
| 263 typedef bool (*RenderPDFPageToDCProc)( | |
| 264 const unsigned char* pdf_buffer, int buffer_size, int page_number, HDC dc, | |
| 265 int dpi_x, int dpi_y, int bounds_origin_x, int bounds_origin_y, | |
| 266 int bounds_width, int bounds_height, bool fit_to_bounds, | |
| 267 bool stretch_to_bounds, bool keep_aspect_ratio, bool center_in_bounds, | |
| 268 bool autorotate); | |
| 269 typedef bool (*GetPDFDocInfoProc)(const unsigned char* pdf_buffer, | |
| 270 int buffer_size, int* page_count, | |
| 271 double* max_page_width); | |
| 272 RenderPDFPageToDCProc render_pdf_to_dc_; | |
| 273 GetPDFDocInfoProc get_pdf_doc_info_; | |
| 274 | |
| 275 DISALLOW_COPY_AND_ASSIGN(PdfFunctionsWin); | |
| 276 }; | |
| 277 | |
| 278 typedef PdfFunctionsWin PdfFunctions; | |
| 279 #else // OS_WIN | |
| 280 typedef PdfFunctionsBase PdfFunctions; | |
| 281 #endif // OS_WIN | |
| 282 | |
| 283 static base::LazyInstance<PdfFunctions> g_pdf_lib = LAZY_INSTANCE_INITIALIZER; | |
| 284 | |
| 83 } // namespace | 285 } // namespace |
| 84 | 286 |
| 85 ChromeContentUtilityClient::ChromeContentUtilityClient() { | 287 ChromeContentUtilityClient::ChromeContentUtilityClient() { |
| 86 #if !defined(OS_ANDROID) | 288 #if !defined(OS_ANDROID) |
| 87 handlers_.push_back(new ProfileImportHandler()); | 289 handlers_.push_back(new ProfileImportHandler()); |
| 88 #endif // OS_ANDROID | 290 #endif // OS_ANDROID |
| 89 | 291 |
| 90 #if defined(ENABLE_MDNS) | 292 #if defined(ENABLE_MDNS) |
| 91 if (CommandLine::ForCurrentProcess()->HasSwitch( | 293 if (CommandLine::ForCurrentProcess()->HasSwitch( |
| 92 switches::kUtilityProcessEnableMDns)) { | 294 switches::kUtilityProcessEnableMDns)) { |
| 93 handlers_.push_back(new local_discovery::ServiceDiscoveryMessageHandler()); | 295 handlers_.push_back(new local_discovery::ServiceDiscoveryMessageHandler()); |
| 94 } | 296 } |
| 95 #endif // ENABLE_MDNS | 297 #endif // ENABLE_MDNS |
| 96 } | 298 } |
| 97 | 299 |
| 98 ChromeContentUtilityClient::~ChromeContentUtilityClient() { | 300 ChromeContentUtilityClient::~ChromeContentUtilityClient() { |
| 99 } | 301 } |
| 100 | 302 |
| 101 void ChromeContentUtilityClient::UtilityThreadStarted() { | 303 void ChromeContentUtilityClient::UtilityThreadStarted() { |
| 102 #if defined(OS_WIN) | |
| 103 // Load the pdf plugin before the sandbox is turned on. This is for Windows | |
| 104 // only because we need this DLL only on Windows. | |
| 105 base::FilePath pdf; | |
| 106 if (PathService::Get(chrome::FILE_PDF_PLUGIN, &pdf) && | |
| 107 base::PathExists(pdf)) { | |
| 108 bool rv = !!LoadLibrary(pdf.value().c_str()); | |
| 109 DCHECK(rv) << "Couldn't load PDF plugin"; | |
| 110 } | |
| 111 #endif | |
| 112 | |
| 113 CommandLine* command_line = CommandLine::ForCurrentProcess(); | 304 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 114 std::string lang = command_line->GetSwitchValueASCII(switches::kLang); | 305 std::string lang = command_line->GetSwitchValueASCII(switches::kLang); |
| 115 if (!lang.empty()) | 306 if (!lang.empty()) |
| 116 extension_l10n_util::SetProcessLocale(lang); | 307 extension_l10n_util::SetProcessLocale(lang); |
| 117 } | 308 } |
| 118 | 309 |
| 119 bool ChromeContentUtilityClient::OnMessageReceived( | 310 bool ChromeContentUtilityClient::OnMessageReceived( |
| 120 const IPC::Message& message) { | 311 const IPC::Message& message) { |
| 121 bool handled = true; | 312 bool handled = true; |
| 122 IPC_BEGIN_MESSAGE_MAP(ChromeContentUtilityClient, message) | 313 IPC_BEGIN_MESSAGE_MAP(ChromeContentUtilityClient, message) |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 175 | 366 |
| 176 return handled; | 367 return handled; |
| 177 } | 368 } |
| 178 | 369 |
| 179 // static | 370 // static |
| 180 void ChromeContentUtilityClient::PreSandboxStartup() { | 371 void ChromeContentUtilityClient::PreSandboxStartup() { |
| 181 #if defined(ENABLE_MDNS) | 372 #if defined(ENABLE_MDNS) |
| 182 local_discovery::ServiceDiscoveryMessageHandler::PreSandboxStartup(); | 373 local_discovery::ServiceDiscoveryMessageHandler::PreSandboxStartup(); |
| 183 #endif // ENABLE_MDNS | 374 #endif // ENABLE_MDNS |
| 184 | 375 |
| 376 g_pdf_lib.Get().Init(); | |
| 377 | |
| 185 // Load media libraries for media file validation. | 378 // Load media libraries for media file validation. |
| 186 base::FilePath media_path; | 379 base::FilePath media_path; |
| 187 PathService::Get(content::DIR_MEDIA_LIBS, &media_path); | 380 PathService::Get(content::DIR_MEDIA_LIBS, &media_path); |
| 188 if (!media_path.empty()) | 381 if (!media_path.empty()) |
| 189 media::InitializeMediaLibrary(media_path); | 382 media::InitializeMediaLibrary(media_path); |
| 190 } | 383 } |
| 191 | 384 |
| 192 void ChromeContentUtilityClient::OnUnpackExtension( | 385 void ChromeContentUtilityClient::OnUnpackExtension( |
| 193 const base::FilePath& extension_path, | 386 const base::FilePath& extension_path, |
| 194 const std::string& extension_id, | 387 const std::string& extension_id, |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 325 highest_rendered_page_number, scale_factor)); | 518 highest_rendered_page_number, scale_factor)); |
| 326 } | 519 } |
| 327 #endif // defined(OS_WIN) | 520 #endif // defined(OS_WIN) |
| 328 if (!succeeded) { | 521 if (!succeeded) { |
| 329 Send(new ChromeUtilityHostMsg_RenderPDFPagesToMetafile_Failed()); | 522 Send(new ChromeUtilityHostMsg_RenderPDFPagesToMetafile_Failed()); |
| 330 } | 523 } |
| 331 ReleaseProcessIfNeeded(); | 524 ReleaseProcessIfNeeded(); |
| 332 } | 525 } |
| 333 | 526 |
| 334 #if defined(OS_WIN) | 527 #if defined(OS_WIN) |
| 335 // Exported by pdf.dll | |
| 336 typedef bool (*RenderPDFPageToDCProc)( | |
| 337 const unsigned char* pdf_buffer, int buffer_size, int page_number, HDC dc, | |
| 338 int dpi_x, int dpi_y, int bounds_origin_x, int bounds_origin_y, | |
| 339 int bounds_width, int bounds_height, bool fit_to_bounds, | |
| 340 bool stretch_to_bounds, bool keep_aspect_ratio, bool center_in_bounds, | |
| 341 bool autorotate); | |
| 342 | |
| 343 typedef bool (*GetPDFDocInfoProc)(const unsigned char* pdf_buffer, | |
| 344 int buffer_size, int* page_count, | |
| 345 double* max_page_width); | |
| 346 | |
| 347 // The 2 below IAT patch functions are almost identical to the code in | |
| 348 // render_process_impl.cc. This is needed to work around specific Windows APIs | |
| 349 // used by the Chrome PDF plugin that will fail in the sandbox. | |
| 350 static base::win::IATPatchFunction g_iat_patch_createdca; | |
| 351 HDC WINAPI UtilityProcess_CreateDCAPatch(LPCSTR driver_name, | |
| 352 LPCSTR device_name, | |
| 353 LPCSTR output, | |
| 354 const DEVMODEA* init_data) { | |
| 355 if (driver_name && | |
| 356 (std::string("DISPLAY") == std::string(driver_name))) | |
| 357 // CreateDC fails behind the sandbox, but not CreateCompatibleDC. | |
| 358 return CreateCompatibleDC(NULL); | |
| 359 | |
| 360 NOTREACHED(); | |
| 361 return CreateDCA(driver_name, device_name, output, init_data); | |
| 362 } | |
| 363 | |
| 364 static base::win::IATPatchFunction g_iat_patch_get_font_data; | |
| 365 DWORD WINAPI UtilityProcess_GetFontDataPatch( | |
| 366 HDC hdc, DWORD table, DWORD offset, LPVOID buffer, DWORD length) { | |
| 367 int rv = GetFontData(hdc, table, offset, buffer, length); | |
| 368 if (rv == GDI_ERROR && hdc) { | |
| 369 HFONT font = static_cast<HFONT>(GetCurrentObject(hdc, OBJ_FONT)); | |
| 370 | |
| 371 LOGFONT logfont; | |
| 372 if (GetObject(font, sizeof(LOGFONT), &logfont)) { | |
| 373 content::UtilityThread::Get()->PreCacheFont(logfont); | |
| 374 rv = GetFontData(hdc, table, offset, buffer, length); | |
| 375 content::UtilityThread::Get()->ReleaseCachedFonts(); | |
| 376 } | |
| 377 } | |
| 378 return rv; | |
| 379 } | |
| 380 | 528 |
| 381 bool ChromeContentUtilityClient::RenderPDFToWinMetafile( | 529 bool ChromeContentUtilityClient::RenderPDFToWinMetafile( |
| 382 base::PlatformFile pdf_file, | 530 base::PlatformFile pdf_file, |
| 383 const base::FilePath& metafile_path, | 531 const base::FilePath& metafile_path, |
| 384 const gfx::Rect& render_area, | 532 const gfx::Rect& render_area, |
| 385 int render_dpi, | 533 int render_dpi, |
| 386 bool autorotate, | 534 bool autorotate, |
| 387 const std::vector<printing::PageRange>& page_ranges, | 535 const std::vector<printing::PageRange>& page_ranges, |
| 388 int* highest_rendered_page_number, | 536 int* highest_rendered_page_number, |
| 389 double* scale_factor) { | 537 double* scale_factor) { |
| 390 *highest_rendered_page_number = -1; | 538 *highest_rendered_page_number = -1; |
| 391 *scale_factor = 1.0; | 539 *scale_factor = 1.0; |
| 392 base::win::ScopedHandle file(pdf_file); | 540 base::win::ScopedHandle file(pdf_file); |
| 393 base::FilePath pdf_module_path; | 541 |
| 394 PathService::Get(chrome::FILE_PDF_PLUGIN, &pdf_module_path); | 542 if (!g_pdf_lib.Get().IsValid()) |
| 395 HMODULE pdf_module = GetModuleHandle(pdf_module_path.value().c_str()); | |
| 396 if (!pdf_module) | |
| 397 return false; | 543 return false; |
| 398 | 544 |
| 399 RenderPDFPageToDCProc render_proc = | |
| 400 reinterpret_cast<RenderPDFPageToDCProc>( | |
| 401 GetProcAddress(pdf_module, "RenderPDFPageToDC")); | |
| 402 if (!render_proc) | |
| 403 return false; | |
| 404 | |
| 405 GetPDFDocInfoProc get_info_proc = reinterpret_cast<GetPDFDocInfoProc>( | |
| 406 GetProcAddress(pdf_module, "GetPDFDocInfo")); | |
| 407 if (!get_info_proc) | |
| 408 return false; | |
| 409 | |
| 410 // Patch the IAT for handling specific APIs known to fail in the sandbox. | |
| 411 if (!g_iat_patch_createdca.is_patched()) | |
| 412 g_iat_patch_createdca.Patch(pdf_module_path.value().c_str(), | |
| 413 "gdi32.dll", "CreateDCA", | |
| 414 UtilityProcess_CreateDCAPatch); | |
| 415 | |
| 416 if (!g_iat_patch_get_font_data.is_patched()) | |
| 417 g_iat_patch_get_font_data.Patch(pdf_module_path.value().c_str(), | |
| 418 "gdi32.dll", "GetFontData", | |
| 419 UtilityProcess_GetFontDataPatch); | |
| 420 | |
| 421 // TODO(sanjeevr): Add a method to the PDF DLL that takes in a file handle | 545 // TODO(sanjeevr): Add a method to the PDF DLL that takes in a file handle |
| 422 // and a page range array. That way we don't need to read the entire PDF into | 546 // and a page range array. That way we don't need to read the entire PDF into |
| 423 // memory. | 547 // memory. |
| 424 DWORD length = ::GetFileSize(file, NULL); | 548 DWORD length = ::GetFileSize(file, NULL); |
| 425 if (length == INVALID_FILE_SIZE) | 549 if (length == INVALID_FILE_SIZE) |
| 426 return false; | 550 return false; |
| 427 | 551 |
| 428 std::vector<uint8> buffer; | 552 std::vector<uint8> buffer; |
| 429 buffer.resize(length); | 553 buffer.resize(length); |
| 430 DWORD bytes_read = 0; | 554 DWORD bytes_read = 0; |
| 431 if (!ReadFile(pdf_file, &buffer.front(), length, &bytes_read, NULL) || | 555 if (!ReadFile(pdf_file, &buffer.front(), length, &bytes_read, NULL) || |
| 432 (bytes_read != length)) | 556 (bytes_read != length)) { |
| 433 return false; | 557 return false; |
| 558 } | |
| 434 | 559 |
| 435 int total_page_count = 0; | 560 int total_page_count = 0; |
| 436 if (!get_info_proc(&buffer.front(), buffer.size(), &total_page_count, NULL)) | 561 if (!g_pdf_lib.Get().GetPDFDocInfo(&buffer.front(), buffer.size(), |
| 562 &total_page_count, NULL)) { | |
| 437 return false; | 563 return false; |
| 564 } | |
| 438 | 565 |
| 439 printing::Emf metafile; | 566 printing::Emf metafile; |
| 440 metafile.InitToFile(metafile_path); | 567 metafile.InitToFile(metafile_path); |
| 441 // We need to scale down DC to fit an entire page into DC available area. | 568 // We need to scale down DC to fit an entire page into DC available area. |
| 442 // Current metafile is based on screen DC and have current screen size. | 569 // Current metafile is based on screen DC and have current screen size. |
| 443 // Writing outside of those boundaries will result in the cut-off output. | 570 // Writing outside of those boundaries will result in the cut-off output. |
| 444 // On metafiles (this is the case here), scaling down will still record | 571 // On metafiles (this is the case here), scaling down will still record |
| 445 // original coordinates and we'll be able to print in full resolution. | 572 // original coordinates and we'll be able to print in full resolution. |
| 446 // Before playback we'll need to counter the scaling up that will happen | 573 // Before playback we'll need to counter the scaling up that will happen |
| 447 // in the service (print_system_win.cc). | 574 // in the service (print_system_win.cc). |
| 448 *scale_factor = gfx::CalculatePageScale(metafile.context(), | 575 *scale_factor = gfx::CalculatePageScale(metafile.context(), |
| 449 render_area.right(), | 576 render_area.right(), |
| 450 render_area.bottom()); | 577 render_area.bottom()); |
| 451 gfx::ScaleDC(metafile.context(), *scale_factor); | 578 gfx::ScaleDC(metafile.context(), *scale_factor); |
| 452 | 579 |
| 453 bool ret = false; | 580 bool ret = false; |
| 454 std::vector<printing::PageRange>::const_iterator iter; | 581 std::vector<printing::PageRange>::const_iterator iter; |
| 455 for (iter = page_ranges.begin(); iter != page_ranges.end(); ++iter) { | 582 for (iter = page_ranges.begin(); iter != page_ranges.end(); ++iter) { |
| 456 for (int page_number = iter->from; page_number <= iter->to; ++page_number) { | 583 for (int page_number = iter->from; page_number <= iter->to; ++page_number) { |
| 457 if (page_number >= total_page_count) | 584 if (page_number >= total_page_count) |
| 458 break; | 585 break; |
| 459 // The underlying metafile is of type Emf and ignores the arguments passed | 586 // The underlying metafile is of type Emf and ignores the arguments passed |
| 460 // to StartPage. | 587 // to StartPage. |
| 461 metafile.StartPage(gfx::Size(), gfx::Rect(), 1); | 588 metafile.StartPage(gfx::Size(), gfx::Rect(), 1); |
| 462 if (render_proc(&buffer.front(), buffer.size(), page_number, | 589 if (g_pdf_lib.Get().RenderPDFPageToDC( |
| 463 metafile.context(), render_dpi, render_dpi, | 590 &buffer.front(), buffer.size(), page_number, metafile.context(), |
| 464 render_area.x(), render_area.y(), render_area.width(), | 591 render_dpi, render_dpi, render_area.x(), render_area.y(), |
| 465 render_area.height(), true, false, true, true, | 592 render_area.width(), render_area.height(), true, false, true, |
| 466 autorotate)) | 593 true, autorotate)) { |
| 467 if (*highest_rendered_page_number < page_number) | 594 if (*highest_rendered_page_number < page_number) |
| 468 *highest_rendered_page_number = page_number; | 595 *highest_rendered_page_number = page_number; |
| 469 ret = true; | 596 ret = true; |
| 597 } | |
| 470 metafile.FinishPage(); | 598 metafile.FinishPage(); |
| 471 } | 599 } |
| 472 } | 600 } |
| 473 metafile.FinishDocument(); | 601 metafile.FinishDocument(); |
| 474 return ret; | 602 return ret; |
| 475 } | 603 } |
| 476 #endif // defined(OS_WIN) | 604 #endif // defined(OS_WIN) |
| 477 | 605 |
| 478 void ChromeContentUtilityClient::OnRobustJPEGDecodeImage( | 606 void ChromeContentUtilityClient::OnRobustJPEGDecodeImage( |
| 479 const std::vector<unsigned char>& encoded_data) { | 607 const std::vector<unsigned char>& encoded_data) { |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 623 picasa::PicasaAlbumsIndexer indexer(album_uids); | 751 picasa::PicasaAlbumsIndexer indexer(album_uids); |
| 624 indexer.ParseFolderINI(folders_inis); | 752 indexer.ParseFolderINI(folders_inis); |
| 625 | 753 |
| 626 Send(new ChromeUtilityHostMsg_IndexPicasaAlbumsContents_Finished( | 754 Send(new ChromeUtilityHostMsg_IndexPicasaAlbumsContents_Finished( |
| 627 indexer.albums_images())); | 755 indexer.albums_images())); |
| 628 ReleaseProcessIfNeeded(); | 756 ReleaseProcessIfNeeded(); |
| 629 } | 757 } |
| 630 #endif // defined(OS_WIN) || defined(OS_MACOSX) | 758 #endif // defined(OS_WIN) || defined(OS_MACOSX) |
| 631 | 759 |
| 632 } // namespace chrome | 760 } // namespace chrome |
| OLD | NEW |