| 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" |
| 14 #include "content/common/view_messages.h" |
| 13 | 15 |
| 14 namespace { | 16 namespace { |
| 15 typedef std::vector<string16> FontNameVector; | 17 typedef std::vector<string16> FontNameVector; |
| 16 typedef std::map<FontCacheDispatcher*, FontNameVector> DispatcherToFontNames; | 18 typedef std::map<FontCacheDispatcher*, FontNameVector> DispatcherToFontNames; |
| 17 | 19 |
| 18 class FontCache { | 20 class FontCache { |
| 19 public: | 21 public: |
| 20 static FontCache* GetInstance() { | 22 static FontCache* GetInstance() { |
| 21 return Singleton<FontCache>::get(); | 23 return Singleton<FontCache>::get(); |
| 22 } | 24 } |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 dispatcher_font_map_.erase(it); | 91 dispatcher_font_map_.erase(it); |
| 90 for (FontNameToElement::iterator i = cache_.begin(); i != cache_.end(); ) { | 92 for (FontNameToElement::iterator i = cache_.begin(); i != cache_.end(); ) { |
| 91 if (i->second.ref_count_ == 0) { | 93 if (i->second.ref_count_ == 0) { |
| 92 cache_.erase(i++); | 94 cache_.erase(i++); |
| 93 } else { | 95 } else { |
| 94 ++i; | 96 ++i; |
| 95 } | 97 } |
| 96 } | 98 } |
| 97 } | 99 } |
| 98 | 100 |
| 101 void PreCacheFontCharacters(const LOGFONT& font, |
| 102 const std::wstring& str, |
| 103 FontCacheDispatcher* dispatcher) { |
| 104 // The only way windows seem to load properly, it is to create a similar |
| 105 // device (like the one in which we print), then do an ExtTextOut, |
| 106 // as we do in the printing thread, which is sandboxed. |
| 107 HDC hdc = CreateEnhMetaFile(NULL, NULL, NULL, NULL); |
| 108 HFONT font_handle = CreateFontIndirect(&font); |
| 109 DCHECK(NULL != font_handle); |
| 110 |
| 111 HGDIOBJ old_font = SelectObject(hdc, font_handle); |
| 112 DCHECK(NULL != old_font); |
| 113 |
| 114 ExtTextOut(hdc, 0, 0, ETO_GLYPH_INDEX, 0, str.c_str(), str.length(), NULL); |
| 115 |
| 116 SelectObject(hdc, old_font); |
| 117 DeleteObject(font_handle); |
| 118 |
| 119 HENHMETAFILE metafile = CloseEnhMetaFile(hdc); |
| 120 |
| 121 if (metafile) { |
| 122 DeleteEnhMetaFile(metafile); |
| 123 } |
| 124 } |
| 125 |
| 99 private: | 126 private: |
| 100 struct CacheElement { | 127 struct CacheElement { |
| 101 CacheElement() | 128 CacheElement() |
| 102 : font_(NULL), old_font_(NULL), dc_(NULL), ref_count_(0) { | 129 : font_(NULL), old_font_(NULL), dc_(NULL), ref_count_(0) { |
| 103 } | 130 } |
| 104 | 131 |
| 105 ~CacheElement() { | 132 ~CacheElement() { |
| 106 if (font_) { | 133 if (font_) { |
| 107 if (dc_ && old_font_) { | 134 if (dc_ && old_font_) { |
| 108 SelectObject(dc_, old_font_); | 135 SelectObject(dc_, old_font_); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 void FontCacheDispatcher::OnFilterAdded(IPC::Channel* channel) { | 170 void FontCacheDispatcher::OnFilterAdded(IPC::Channel* channel) { |
| 144 channel_ = channel; | 171 channel_ = channel; |
| 145 } | 172 } |
| 146 | 173 |
| 147 bool FontCacheDispatcher::OnMessageReceived(const IPC::Message& message) { | 174 bool FontCacheDispatcher::OnMessageReceived(const IPC::Message& message) { |
| 148 bool handled = true; | 175 bool handled = true; |
| 149 IPC_BEGIN_MESSAGE_MAP(FontCacheDispatcher, message) | 176 IPC_BEGIN_MESSAGE_MAP(FontCacheDispatcher, message) |
| 150 IPC_MESSAGE_HANDLER(ChildProcessHostMsg_PreCacheFont, OnPreCacheFont) | 177 IPC_MESSAGE_HANDLER(ChildProcessHostMsg_PreCacheFont, OnPreCacheFont) |
| 151 IPC_MESSAGE_HANDLER(ChildProcessHostMsg_ReleaseCachedFonts, | 178 IPC_MESSAGE_HANDLER(ChildProcessHostMsg_ReleaseCachedFonts, |
| 152 OnReleaseCachedFonts) | 179 OnReleaseCachedFonts) |
| 180 IPC_MESSAGE_HANDLER(ViewHostMsg_PreCacheFontCharacters, |
| 181 OnPreCacheFontCharacters) |
| 153 IPC_MESSAGE_UNHANDLED(handled = false) | 182 IPC_MESSAGE_UNHANDLED(handled = false) |
| 154 IPC_END_MESSAGE_MAP() | 183 IPC_END_MESSAGE_MAP() |
| 155 return handled; | 184 return handled; |
| 156 } | 185 } |
| 157 | 186 |
| 158 void FontCacheDispatcher::OnChannelClosing() { | 187 void FontCacheDispatcher::OnChannelClosing() { |
| 159 channel_ = NULL; | 188 channel_ = NULL; |
| 160 } | 189 } |
| 161 | 190 |
| 162 bool FontCacheDispatcher::Send(IPC::Message* message) { | 191 bool FontCacheDispatcher::Send(IPC::Message* message) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 177 // font for us by using a dummy call to GetTextMetrics of | 206 // font for us by using a dummy call to GetTextMetrics of |
| 178 // the same font. | 207 // the same font. |
| 179 // This means the browser process just loads the font into memory so that | 208 // 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 | 209 // 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, | 210 // 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 | 211 // 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. | 212 // to avoid the case that an in-cache font is swapped out by GDI. |
| 184 FontCache::GetInstance()->PreCacheFont(font, this); | 213 FontCache::GetInstance()->PreCacheFont(font, this); |
| 185 } | 214 } |
| 186 | 215 |
| 216 void FontCacheDispatcher::OnPreCacheFontCharacters(const LOGFONT& font, |
| 217 const std::wstring& str) { |
| 218 // First, comments from FontCacheDispatcher::OnPreCacheFont do apply here too. |
| 219 // Except that for True Type fonts, |
| 220 // GetTextMetrics will not load the font in memory. |
| 221 // The only way windows seem to load properly, it is to create a similar |
| 222 // device (like the one in which we print), then do an ExtTextOut, |
| 223 // as we do in the printing thread, which is sandboxed. |
| 224 FontCache::GetInstance()->PreCacheFontCharacters(font, str, this); |
| 225 } |
| 226 |
| 187 void FontCacheDispatcher::OnReleaseCachedFonts() { | 227 void FontCacheDispatcher::OnReleaseCachedFonts() { |
| 188 // Release cached fonts that requested from a pid by decrementing the ref | 228 // Release cached fonts that requested from a pid by decrementing the ref |
| 189 // count. When ref count is zero, the handles are released. | 229 // count. When ref count is zero, the handles are released. |
| 190 FontCache::GetInstance()->ReleaseCachedFonts(this); | 230 FontCache::GetInstance()->ReleaseCachedFonts(this); |
| 191 } | 231 } |
| OLD | NEW |