OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "content/child/dwrite_font_proxy/dwrite_font_proxy_init_win.h" |
| 6 |
| 7 #include <dwrite.h> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/callback.h" |
| 11 #include "base/debug/alias.h" |
| 12 #include "base/win/iat_patch_function.h" |
| 13 #include "base/win/windows_version.h" |
| 14 #include "content/child/dwrite_font_proxy/dwrite_font_proxy_win.h" |
| 15 #include "content/common/font_warmup_win.h" |
| 16 #include "skia/ext/fontmgr_default_win.h" |
| 17 #include "third_party/WebKit/public/web/win/WebFontRendering.h" |
| 18 #include "third_party/skia/include/ports/SkTypeface_win.h" |
| 19 |
| 20 namespace mswr = Microsoft::WRL; |
| 21 |
| 22 namespace content { |
| 23 |
| 24 namespace { |
| 25 mswr::ComPtr<DWriteFontCollectionProxy> g_font_collection; |
| 26 IPC::Sender* g_sender_override = nullptr; |
| 27 |
| 28 // Windows-only DirectWrite support. These warm up the DirectWrite paths |
| 29 // before sandbox lock down to allow Skia access to the Font Manager service. |
| 30 void CreateDirectWriteFactory(IDWriteFactory** factory) { |
| 31 typedef decltype(DWriteCreateFactory)* DWriteCreateFactoryProc; |
| 32 HMODULE dwrite_dll = LoadLibraryW(L"dwrite.dll"); |
| 33 // TODO(scottmg): Temporary code to track crash in http://crbug.com/387867. |
| 34 if (!dwrite_dll) { |
| 35 DWORD load_library_get_last_error = GetLastError(); |
| 36 base::debug::Alias(&dwrite_dll); |
| 37 base::debug::Alias(&load_library_get_last_error); |
| 38 CHECK(false); |
| 39 } |
| 40 |
| 41 // This shouldn't be necessary, but not having this causes breakage in |
| 42 // content_browsertests, and possibly other high-stress cases. |
| 43 PatchServiceManagerCalls(); |
| 44 |
| 45 DWriteCreateFactoryProc dwrite_create_factory_proc = |
| 46 reinterpret_cast<DWriteCreateFactoryProc>( |
| 47 GetProcAddress(dwrite_dll, "DWriteCreateFactory")); |
| 48 // TODO(scottmg): Temporary code to track crash in http://crbug.com/387867. |
| 49 if (!dwrite_create_factory_proc) { |
| 50 DWORD get_proc_address_get_last_error = GetLastError(); |
| 51 base::debug::Alias(&dwrite_create_factory_proc); |
| 52 base::debug::Alias(&get_proc_address_get_last_error); |
| 53 CHECK(false); |
| 54 } |
| 55 CHECK(SUCCEEDED(dwrite_create_factory_proc( |
| 56 DWRITE_FACTORY_TYPE_ISOLATED, __uuidof(IDWriteFactory), |
| 57 reinterpret_cast<IUnknown**>(factory)))); |
| 58 } |
| 59 |
| 60 HRESULT STDMETHODCALLTYPE StubFontCollection(IDWriteFactory* factory, |
| 61 IDWriteFontCollection** col, |
| 62 BOOL checkUpdates) { |
| 63 DCHECK(g_font_collection != nullptr); |
| 64 g_font_collection.CopyTo(col); |
| 65 return S_OK; |
| 66 } |
| 67 |
| 68 // Copied from content/common/font_warmup_win.cc |
| 69 void PatchDWriteFactory(IDWriteFactory* factory) { |
| 70 const unsigned int kGetSystemFontCollectionVTableIndex = 3; |
| 71 |
| 72 PROC* vtable = *reinterpret_cast<PROC**>(factory); |
| 73 PROC* function_ptr = &vtable[kGetSystemFontCollectionVTableIndex]; |
| 74 void* stub_function = &StubFontCollection; |
| 75 base::win::ModifyCode(function_ptr, &stub_function, sizeof(PROC)); |
| 76 } |
| 77 |
| 78 // Needed as a function for Bind() |
| 79 IPC::Sender* GetSenderOverride() { |
| 80 return g_sender_override; |
| 81 } |
| 82 |
| 83 } // namespace |
| 84 |
| 85 void InitializeDWriteFontProxy( |
| 86 const base::Callback<IPC::Sender*(void)>& sender) { |
| 87 mswr::ComPtr<IDWriteFactory> factory; |
| 88 |
| 89 CreateDirectWriteFactory(&factory); |
| 90 |
| 91 if (g_font_collection == nullptr) { |
| 92 if (g_sender_override != nullptr) { |
| 93 mswr::MakeAndInitialize<DWriteFontCollectionProxy>( |
| 94 &g_font_collection, factory.Get(), base::Bind(&GetSenderOverride)); |
| 95 } else { |
| 96 mswr::MakeAndInitialize<DWriteFontCollectionProxy>(&g_font_collection, |
| 97 factory.Get(), sender); |
| 98 } |
| 99 } |
| 100 |
| 101 PatchDWriteFactory(factory.Get()); |
| 102 |
| 103 blink::WebFontRendering::setDirectWriteFactory(factory.Get()); |
| 104 SkFontMgr* skia_font_manager = SkFontMgr_New_DirectWrite(factory.Get()); |
| 105 SetDefaultSkiaFactory(skia_font_manager); |
| 106 } |
| 107 |
| 108 void UninitializeDWriteFontProxy() { |
| 109 if (g_font_collection != nullptr) |
| 110 g_font_collection->Unregister(); |
| 111 } |
| 112 |
| 113 void SetDWriteFontProxySenderForTesting(IPC::Sender* sender) { |
| 114 g_sender_override = sender; |
| 115 } |
| 116 |
| 117 } // namespace content |
OLD | NEW |