OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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/font_cache_dispatcher_win.h" |
| 6 |
| 7 #include <vector> |
| 8 #include <map> |
| 9 |
| 10 #include "base/logging.h" |
| 11 #include "base/string16.h" |
| 12 #include "content/common/child_process_messages.h" |
| 13 |
| 14 namespace { |
| 15 typedef std::vector<string16> FontNameVector; |
| 16 typedef std::map<FontCacheDispatcher*, FontNameVector> DispatcherToFontNames; |
| 17 |
| 18 class FontCache { |
| 19 public: |
| 20 static FontCache* GetInstance() { |
| 21 return Singleton<FontCache>::get(); |
| 22 } |
| 23 |
| 24 void PreCacheFont(LOGFONT font, FontCacheDispatcher* dispatcher) { |
| 25 typedef std::map<string16, FontCache::CacheElement> FontNameToElement; |
| 26 |
| 27 base::AutoLock lock(mutex_); |
| 28 |
| 29 // Fetch the font into memory. |
| 30 // No matter the font is cached or not, we load it to avoid GDI swapping out |
| 31 // that font file. |
| 32 HDC hdc = GetDC(NULL); |
| 33 HFONT font_handle = CreateFontIndirect(&font); |
| 34 DCHECK(NULL != font_handle); |
| 35 |
| 36 HGDIOBJ old_font = SelectObject(hdc, font_handle); |
| 37 DCHECK(NULL != old_font); |
| 38 |
| 39 TEXTMETRIC tm; |
| 40 BOOL ret = GetTextMetrics(hdc, &tm); |
| 41 DCHECK(ret); |
| 42 |
| 43 string16 font_name = font.lfFaceName; |
| 44 int ref_count_inc = 1; |
| 45 FontNameVector::iterator it = |
| 46 std::find(dispatcher_font_map_[dispatcher].begin(), |
| 47 dispatcher_font_map_[dispatcher].end(), |
| 48 font_name); |
| 49 if (it == dispatcher_font_map_[dispatcher].end()) { |
| 50 // Requested font is new to cache. |
| 51 dispatcher_font_map_[dispatcher].push_back(font_name); |
| 52 } else { |
| 53 ref_count_inc = 0; |
| 54 } |
| 55 |
| 56 if (cache_[font_name].ref_count_ == 0) { // Requested font is new to cache. |
| 57 cache_[font_name].ref_count_ = 1; |
| 58 } else { // Requested font is already in cache, release old handles. |
| 59 DeleteObject(cache_[font_name].font_); |
| 60 DeleteDC(cache_[font_name].dc_); |
| 61 } |
| 62 cache_[font_name].font_ = font_handle; |
| 63 cache_[font_name].dc_ = hdc; |
| 64 cache_[font_name].ref_count_ += ref_count_inc; |
| 65 } |
| 66 |
| 67 void ReleaseCachedFonts(FontCacheDispatcher* dispatcher) { |
| 68 typedef std::map<string16, FontCache::CacheElement> FontNameToElement; |
| 69 |
| 70 base::AutoLock lock(mutex_); |
| 71 |
| 72 DispatcherToFontNames::iterator it; |
| 73 it = dispatcher_font_map_.find(dispatcher); |
| 74 if (it == dispatcher_font_map_.end()) { |
| 75 return; |
| 76 } |
| 77 |
| 78 for (FontNameVector::iterator i = it->second.begin(), e = it->second.end(); |
| 79 i != e; ++i) { |
| 80 FontNameToElement::iterator element; |
| 81 element = cache_.find(*i); |
| 82 if (element != cache_.end()) { |
| 83 --((*element).second.ref_count_); |
| 84 } |
| 85 } |
| 86 |
| 87 dispatcher_font_map_.erase(it); |
| 88 for (FontNameToElement::iterator i = cache_.begin(); i != cache_.end(); ) { |
| 89 if (i->second.ref_count_ == 0) { |
| 90 cache_.erase(i++); |
| 91 } else { |
| 92 ++i; |
| 93 } |
| 94 } |
| 95 } |
| 96 |
| 97 private: |
| 98 struct CacheElement { |
| 99 CacheElement() |
| 100 : font_(NULL), dc_(NULL), ref_count_(0) { |
| 101 } |
| 102 |
| 103 ~CacheElement() { |
| 104 if (font_) { |
| 105 DeleteObject(font_); |
| 106 } |
| 107 if (dc_) { |
| 108 DeleteDC(dc_); |
| 109 } |
| 110 } |
| 111 |
| 112 HFONT font_; |
| 113 HDC dc_; |
| 114 int ref_count_; |
| 115 }; |
| 116 friend struct DefaultSingletonTraits<FontCache>; |
| 117 |
| 118 FontCache() { |
| 119 } |
| 120 |
| 121 std::map<string16, CacheElement> cache_; |
| 122 DispatcherToFontNames dispatcher_font_map_; |
| 123 base::Lock mutex_; |
| 124 |
| 125 DISALLOW_COPY_AND_ASSIGN(FontCache); |
| 126 }; |
| 127 |
| 128 } |
| 129 |
| 130 FontCacheDispatcher::FontCacheDispatcher() |
| 131 : channel_(NULL) { |
| 132 } |
| 133 |
| 134 FontCacheDispatcher::~FontCacheDispatcher() { |
| 135 } |
| 136 |
| 137 void FontCacheDispatcher::OnFilterAdded(IPC::Channel* channel) { |
| 138 channel_ = channel; |
| 139 } |
| 140 |
| 141 bool FontCacheDispatcher::OnMessageReceived(const IPC::Message& message) { |
| 142 bool handled = true; |
| 143 IPC_BEGIN_MESSAGE_MAP(FontCacheDispatcher, message) |
| 144 IPC_MESSAGE_HANDLER(ChildProcessHostMsg_PreCacheFont, OnPreCacheFont) |
| 145 IPC_MESSAGE_HANDLER(ChildProcessHostMsg_ReleaseCachedFonts, |
| 146 OnReleaseCachedFonts) |
| 147 IPC_MESSAGE_UNHANDLED(handled = false) |
| 148 IPC_END_MESSAGE_MAP() |
| 149 return handled; |
| 150 } |
| 151 |
| 152 void FontCacheDispatcher::OnChannelClosing() { |
| 153 channel_ = NULL; |
| 154 } |
| 155 |
| 156 bool FontCacheDispatcher::Send(IPC::Message* message) { |
| 157 if (channel_) |
| 158 return channel_->Send(message); |
| 159 |
| 160 delete message; |
| 161 return false; |
| 162 } |
| 163 |
| 164 void FontCacheDispatcher::OnPreCacheFont(const LOGFONT& font) { |
| 165 // If a child process is running in a sandbox, GetTextMetrics() |
| 166 // can sometimes fail. If a font has not been loaded |
| 167 // previously, GetTextMetrics() will try to load the font |
| 168 // from the font file. However, the sandboxed process does |
| 169 // not have permissions to access any font files and |
| 170 // the call fails. So we make the browser pre-load the |
| 171 // font for us by using a dummy call to GetTextMetrics of |
| 172 // the same font. |
| 173 // This means the browser process just loads the font into memory so that |
| 174 // when GDI attempt to query that font info in child process, it does not |
| 175 // need to load that file, hence no permission issues there. Therefore, |
| 176 // when a font is asked to be cached, we always recreates the font object |
| 177 // to avoid the case that an in-cache font is swapped out by GDI. |
| 178 FontCache::GetInstance()->PreCacheFont(font, this); |
| 179 } |
| 180 |
| 181 void FontCacheDispatcher::OnReleaseCachedFonts() { |
| 182 // Release cached fonts that requested from a pid by decrementing the ref |
| 183 // count. When ref count is zero, the handles are released. |
| 184 FontCache::GetInstance()->ReleaseCachedFonts(this); |
| 185 } |
OLD | NEW |