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