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 | |
26 mswr::ComPtr<DWriteFontCollectionProxy> g_font_collection; | |
27 IPC::Sender* g_sender_override = nullptr; | |
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); | |
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 // Needed as a function for Bind() | |
80 IPC::Sender* GetSenderOverride() { | |
81 return g_sender_override; | |
82 } | |
83 | |
84 } // namespace | |
85 | |
86 void InitializeDWriteFontProxy( | |
87 const base::Callback<IPC::Sender*(void)>& sender) { | |
88 mswr::ComPtr<IDWriteFactory> factory; | |
89 | |
90 CreateDirectWriteFactory(&factory); | |
91 | |
92 if (!g_font_collection) { | |
93 if (g_sender_override) { | |
94 mswr::MakeAndInitialize<DWriteFontCollectionProxy>( | |
95 &g_font_collection, factory.Get(), base::Bind(&GetSenderOverride)); | |
96 } else { | |
97 mswr::MakeAndInitialize<DWriteFontCollectionProxy>(&g_font_collection, | |
98 factory.Get(), sender); | |
99 } | |
100 } | |
101 | |
102 PatchDWriteFactory(factory.Get()); | |
103 | |
104 blink::WebFontRendering::setDirectWriteFactory(factory.Get()); | |
105 SkFontMgr* skia_font_manager = SkFontMgr_New_DirectWrite(factory.Get()); | |
106 SetDefaultSkiaFactory(skia_font_manager); | |
107 } | |
108 | |
109 void UninitializeDWriteFontProxy() { | |
110 if (g_font_collection) | |
111 g_font_collection->Unregister(); | |
112 } | |
113 | |
114 void SetDWriteFontProxySenderForTesting(IPC::Sender* sender) { | |
115 g_sender_override = sender; | |
116 } | |
117 | |
118 } // namespace content | |
OLD | NEW |