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