OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/common/font_cache_dispatcher_win.h" | 5 #include "content/common/font_cache_dispatcher_win.h" |
6 | 6 |
| 7 #include <string> |
7 #include <vector> | 8 #include <vector> |
8 #include <map> | 9 #include <map> |
9 | 10 |
10 #include "base/logging.h" | 11 #include "base/logging.h" |
11 #include "base/string16.h" | 12 #include "base/string16.h" |
12 #include "content/common/child_process_messages.h" | 13 #include "content/common/child_process_messages.h" |
13 | 14 |
14 namespace { | 15 namespace { |
15 typedef std::vector<string16> FontNameVector; | 16 typedef std::vector<string16> FontNameVector; |
16 typedef std::map<FontCacheDispatcher*, FontNameVector> DispatcherToFontNames; | 17 typedef std::map<FontCacheDispatcher*, FontNameVector> DispatcherToFontNames; |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 dispatcher_font_map_.erase(it); | 90 dispatcher_font_map_.erase(it); |
90 for (FontNameToElement::iterator i = cache_.begin(); i != cache_.end(); ) { | 91 for (FontNameToElement::iterator i = cache_.begin(); i != cache_.end(); ) { |
91 if (i->second.ref_count_ == 0) { | 92 if (i->second.ref_count_ == 0) { |
92 cache_.erase(i++); | 93 cache_.erase(i++); |
93 } else { | 94 } else { |
94 ++i; | 95 ++i; |
95 } | 96 } |
96 } | 97 } |
97 } | 98 } |
98 | 99 |
| 100 void PreCacheFontCharacters(const LOGFONT& font, |
| 101 const std::wstring& str, |
| 102 FontCacheDispatcher* dispatcher) { |
| 103 // The only way windows seem to load properly, it is to create a similar |
| 104 // device (like the one in which we print), then do an ExtTextOut, |
| 105 // as we do in the printing thread, which is sandboxed. |
| 106 HDC hdc = CreateEnhMetaFile(NULL, NULL, NULL, NULL); |
| 107 HFONT font_handle = CreateFontIndirect(&font); |
| 108 DCHECK(NULL != font_handle); |
| 109 |
| 110 HGDIOBJ old_font = SelectObject(hdc, font_handle); |
| 111 DCHECK(NULL != old_font); |
| 112 |
| 113 ExtTextOut(hdc, 0, 0, ETO_GLYPH_INDEX, 0, str.c_str(), str.length(), NULL); |
| 114 |
| 115 SelectObject(hdc, old_font); |
| 116 DeleteObject(font_handle); |
| 117 |
| 118 HENHMETAFILE metafile = CloseEnhMetaFile(hdc); |
| 119 |
| 120 if (metafile) { |
| 121 DeleteEnhMetaFile(metafile); |
| 122 } |
| 123 } |
| 124 |
99 private: | 125 private: |
100 struct CacheElement { | 126 struct CacheElement { |
101 CacheElement() | 127 CacheElement() |
102 : font_(NULL), old_font_(NULL), dc_(NULL), ref_count_(0) { | 128 : font_(NULL), old_font_(NULL), dc_(NULL), ref_count_(0) { |
103 } | 129 } |
104 | 130 |
105 ~CacheElement() { | 131 ~CacheElement() { |
106 if (font_) { | 132 if (font_) { |
107 if (dc_ && old_font_) { | 133 if (dc_ && old_font_) { |
108 SelectObject(dc_, old_font_); | 134 SelectObject(dc_, old_font_); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 } | 167 } |
142 | 168 |
143 void FontCacheDispatcher::OnFilterAdded(IPC::Channel* channel) { | 169 void FontCacheDispatcher::OnFilterAdded(IPC::Channel* channel) { |
144 channel_ = channel; | 170 channel_ = channel; |
145 } | 171 } |
146 | 172 |
147 bool FontCacheDispatcher::OnMessageReceived(const IPC::Message& message) { | 173 bool FontCacheDispatcher::OnMessageReceived(const IPC::Message& message) { |
148 bool handled = true; | 174 bool handled = true; |
149 IPC_BEGIN_MESSAGE_MAP(FontCacheDispatcher, message) | 175 IPC_BEGIN_MESSAGE_MAP(FontCacheDispatcher, message) |
150 IPC_MESSAGE_HANDLER(ChildProcessHostMsg_PreCacheFont, OnPreCacheFont) | 176 IPC_MESSAGE_HANDLER(ChildProcessHostMsg_PreCacheFont, OnPreCacheFont) |
| 177 IPC_MESSAGE_HANDLER(ChildProcessHostMsg_PreCacheFontCharacters, |
| 178 OnPreCacheFontCharacters) |
151 IPC_MESSAGE_HANDLER(ChildProcessHostMsg_ReleaseCachedFonts, | 179 IPC_MESSAGE_HANDLER(ChildProcessHostMsg_ReleaseCachedFonts, |
152 OnReleaseCachedFonts) | 180 OnReleaseCachedFonts) |
153 IPC_MESSAGE_UNHANDLED(handled = false) | 181 IPC_MESSAGE_UNHANDLED(handled = false) |
154 IPC_END_MESSAGE_MAP() | 182 IPC_END_MESSAGE_MAP() |
155 return handled; | 183 return handled; |
156 } | 184 } |
157 | 185 |
158 void FontCacheDispatcher::OnChannelClosing() { | 186 void FontCacheDispatcher::OnChannelClosing() { |
159 channel_ = NULL; | 187 channel_ = NULL; |
160 } | 188 } |
(...skipping 16 matching lines...) Expand all Loading... |
177 // font for us by using a dummy call to GetTextMetrics of | 205 // font for us by using a dummy call to GetTextMetrics of |
178 // the same font. | 206 // the same font. |
179 // This means the browser process just loads the font into memory so that | 207 // This means the browser process just loads the font into memory so that |
180 // when GDI attempt to query that font info in child process, it does not | 208 // when GDI attempt to query that font info in child process, it does not |
181 // need to load that file, hence no permission issues there. Therefore, | 209 // need to load that file, hence no permission issues there. Therefore, |
182 // when a font is asked to be cached, we always recreates the font object | 210 // when a font is asked to be cached, we always recreates the font object |
183 // to avoid the case that an in-cache font is swapped out by GDI. | 211 // to avoid the case that an in-cache font is swapped out by GDI. |
184 FontCache::GetInstance()->PreCacheFont(font, this); | 212 FontCache::GetInstance()->PreCacheFont(font, this); |
185 } | 213 } |
186 | 214 |
| 215 void FontCacheDispatcher::OnPreCacheFontCharacters(const LOGFONT& font, |
| 216 const std::wstring& str) { |
| 217 // First, comments from FontCacheDispatcher::OnPreCacheFont do apply here too. |
| 218 // Except that for True Type fonts, |
| 219 // GetTextMetrics will not load the font in memory. |
| 220 // The only way windows seem to load properly, it is to create a similar |
| 221 // device (like the one in which we print), then do an ExtTextOut, |
| 222 // as we do in the printing thread, which is sandboxed. |
| 223 FontCache::GetInstance()->PreCacheFontCharacters(font, str, this); |
| 224 } |
| 225 |
187 void FontCacheDispatcher::OnReleaseCachedFonts() { | 226 void FontCacheDispatcher::OnReleaseCachedFonts() { |
188 // Release cached fonts that requested from a pid by decrementing the ref | 227 // Release cached fonts that requested from a pid by decrementing the ref |
189 // count. When ref count is zero, the handles are released. | 228 // count. When ref count is zero, the handles are released. |
190 FontCache::GetInstance()->ReleaseCachedFonts(this); | 229 FontCache::GetInstance()->ReleaseCachedFonts(this); |
191 } | 230 } |
OLD | NEW |