Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / chrome / child / pdf_child_init.cc
blobfe9a1a4f29dc6a3cb90a6069260f3dc47af5a9e0
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"
13 #if defined(OS_WIN)
14 #include "base/win/iat_patch_function.h"
15 #endif
17 namespace chrome {
18 namespace {
19 #if defined(OS_WIN)
20 static base::win::IATPatchFunction g_iat_patch_createdca;
21 HDC WINAPI CreateDCAPatch(LPCSTR driver_name,
22 LPCSTR device_name,
23 LPCSTR output,
24 const void* init_data) {
25 DCHECK(std::string("DISPLAY") == std::string(driver_name));
26 DCHECK(!device_name);
27 DCHECK(!output);
28 DCHECK(!init_data);
30 // CreateDC fails behind the sandbox, but not CreateCompatibleDC.
31 return CreateCompatibleDC(NULL);
34 typedef DWORD (WINAPI* GetFontDataPtr) (HDC hdc,
35 DWORD table,
36 DWORD offset,
37 LPVOID buffer,
38 DWORD length);
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,
42 DWORD table,
43 DWORD offset,
44 LPVOID buffer,
45 DWORD length) {
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));
50 LOGFONT logfont;
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();
60 return rv;
62 #endif // OS_WIN
64 } // namespace
66 void InitializePDF() {
67 #if defined(OS_WIN)
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),
74 &current_module));
75 DWORD result = GetModuleFileNameW(current_module, current_module_name,
76 MAX_PATH);
77 if (!result || result == MAX_PATH)
78 return;
79 g_iat_patch_createdca.Patch(current_module_name, "gdi32.dll", "CreateDCA",
80 CreateDCAPatch);
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());
85 #endif // OS_WIN
88 } // namespace chrome