1 // Copyright 2015 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/child/pdf_child_init.h"
7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h"
9 #include "base/path_service.h"
10 #include "chrome/common/chrome_paths.h"
11 #include "content/public/child/child_thread.h"
14 #include "base/win/iat_patch_function.h"
20 static base::win::IATPatchFunction g_iat_patch_createdca
;
21 HDC WINAPI
CreateDCAPatch(LPCSTR driver_name
,
24 const void* init_data
) {
25 DCHECK(std::string("DISPLAY") == std::string(driver_name
));
30 // CreateDC fails behind the sandbox, but not CreateCompatibleDC.
31 return CreateCompatibleDC(NULL
);
34 typedef DWORD (WINAPI
* GetFontDataPtr
) (HDC hdc
,
39 GetFontDataPtr g_original_get_font_data
= NULL
;
40 static base::win::IATPatchFunction g_iat_patch_get_font_data
;
41 DWORD WINAPI
GetFontDataPatch(HDC hdc
,
46 int rv
= g_original_get_font_data(hdc
, table
, offset
, buffer
, length
);
47 if (rv
== GDI_ERROR
&& hdc
) {
48 HFONT font
= static_cast<HFONT
>(GetCurrentObject(hdc
, OBJ_FONT
));
51 if (GetObject(font
, sizeof(LOGFONT
), &logfont
)) {
52 std::vector
<char> font_data
;
53 if (content::ChildThread::Get())
54 content::ChildThread::Get()->PreCacheFont(logfont
);
55 rv
= g_original_get_font_data(hdc
, table
, offset
, buffer
, length
);
56 if (content::ChildThread::Get())
57 content::ChildThread::Get()->ReleaseCachedFonts();
66 void InitializePDF() {
68 // Need to patch a few functions for font loading to work correctly. This can
69 // be removed once we switch PDF to use Skia.
70 HMODULE current_module
= NULL
;
71 wchar_t current_module_name
[MAX_PATH
];
72 CHECK(GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
,
73 reinterpret_cast<LPCWSTR
>(InitializePDF
),
75 DWORD result
= GetModuleFileNameW(current_module
, current_module_name
,
77 if (!result
|| result
== MAX_PATH
)
79 g_iat_patch_createdca
.Patch(current_module_name
, "gdi32.dll", "CreateDCA",
81 g_iat_patch_get_font_data
.Patch(current_module_name
, "gdi32.dll",
82 "GetFontData", GetFontDataPatch
);
83 g_original_get_font_data
= reinterpret_cast<GetFontDataPtr
>(
84 g_iat_patch_get_font_data
.original_function());