blob: 209d202fd3441f06390e12a47706bcdfc754027b [file] [log] [blame]
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/child/pdf_child_init.h"
#include "build/build_config.h"
#if defined(OS_WIN)
#include "base/no_destructor.h"
#include "base/win/current_module.h"
#include "base/win/iat_patch_function.h"
#include "base/win/windows_version.h"
#include "content/public/child/child_thread.h"
namespace {
#if defined(OS_WIN)
HDC WINAPI CreateDCAPatch(LPCSTR driver_name,
LPCSTR device_name,
LPCSTR output,
const void* init_data) {
DCHECK(std::string("DISPLAY") == std::string(driver_name));
// CreateDC fails behind the sandbox, but not CreateCompatibleDC.
return CreateCompatibleDC(NULL);
typedef DWORD (WINAPI* GetFontDataPtr) (HDC hdc,
DWORD table,
DWORD offset,
LPVOID buffer,
DWORD length);
GetFontDataPtr g_original_get_font_data = nullptr;
DWORD WINAPI GetFontDataPatch(HDC hdc,
DWORD table,
DWORD offset,
LPVOID buffer,
DWORD length) {
DWORD rv = g_original_get_font_data(hdc, table, offset, buffer, length);
if (rv == GDI_ERROR && hdc) {
HFONT font = static_cast<HFONT>(GetCurrentObject(hdc, OBJ_FONT));
LOGFONT logfont;
if (GetObject(font, sizeof(LOGFONT), &logfont)) {
if (content::ChildThread::Get())
rv = g_original_get_font_data(hdc, table, offset, buffer, length);
if (content::ChildThread::Get())
return rv;
#endif // defined(OS_WIN)
} // namespace
void InitializePDF() {
#if defined(OS_WIN)
// Need to patch a few functions for font loading to work correctly. This can
// be removed once we switch PDF to use Skia
// (
#if defined(COMPONENT_BUILD)
HMODULE module = ::GetModuleHandleA("pdfium.dll");
#endif // defined(COMPONENT_BUILD)
// When GDI is delayloaded, calls into real GDI after these patches are
// created result in the delayload handler overwriting the patches when it
// resolves the delayed imports with the full import information. To
// workaround this, we first call into GDI so that the delayed imports get
// resolved, THEN we apply the patches.
// On Win10 this isn't necessary because these methods are not used.
// TODO( Implement a generic IATPatchFunction fix to handle
// these situations.
// TODO( This workaround shouldn't be necessary on Win8
// either, but is because the win32k process mitigations aren't working
// properly.
if (base::win::GetVersion() < base::win::Version::WIN10)
GetFontData(nullptr, 0, 0, nullptr, 0);
static base::NoDestructor<base::win::IATPatchFunction> patch_createdca;
patch_createdca->PatchFromModule(module, "gdi32.dll", "CreateDCA",
static base::NoDestructor<base::win::IATPatchFunction> patch_get_font_data;
module, "gdi32.dll", "GetFontData",
g_original_get_font_data = reinterpret_cast<GetFontDataPtr>(
#endif // defined(OS_WIN)