| 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 <map> | 7 #include <map> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 } | 23 } |
| 24 | 24 |
| 25 void PreCacheFont(const LOGFONT& font, FontCacheDispatcher* dispatcher) { | 25 void PreCacheFont(const LOGFONT& font, FontCacheDispatcher* dispatcher) { |
| 26 typedef std::map<base::string16, FontCache::CacheElement> FontNameToElement; | 26 typedef std::map<base::string16, FontCache::CacheElement> FontNameToElement; |
| 27 | 27 |
| 28 base::AutoLock lock(mutex_); | 28 base::AutoLock lock(mutex_); |
| 29 | 29 |
| 30 // Fetch the font into memory. | 30 // Fetch the font into memory. |
| 31 // No matter the font is cached or not, we load it to avoid GDI swapping out | 31 // No matter the font is cached or not, we load it to avoid GDI swapping out |
| 32 // that font file. | 32 // that font file. |
| 33 HDC hdc = GetDC(NULL); | 33 HDC hdc = GetDC(nullptr); |
| 34 HFONT font_handle = CreateFontIndirect(&font); | 34 HFONT font_handle = CreateFontIndirect(&font); |
| 35 DCHECK(NULL != font_handle); | 35 DCHECK(nullptr != font_handle); |
| 36 | 36 |
| 37 HGDIOBJ old_font = SelectObject(hdc, font_handle); | 37 HGDIOBJ old_font = SelectObject(hdc, font_handle); |
| 38 DCHECK(NULL != old_font); | 38 DCHECK(nullptr != old_font); |
| 39 | 39 |
| 40 TEXTMETRIC tm; | 40 TEXTMETRIC tm; |
| 41 BOOL ret = GetTextMetrics(hdc, &tm); | 41 BOOL ret = GetTextMetrics(hdc, &tm); |
| 42 DCHECK(ret); | 42 DCHECK(ret); |
| 43 | 43 |
| 44 base::string16 font_name = font.lfFaceName; | 44 base::string16 font_name = font.lfFaceName; |
| 45 int ref_count_inc = 1; | 45 int ref_count_inc = 1; |
| 46 FontNameVector::iterator it = | 46 FontNameVector::iterator it = |
| 47 std::find(dispatcher_font_map_[dispatcher].begin(), | 47 std::find(dispatcher_font_map_[dispatcher].begin(), |
| 48 dispatcher_font_map_[dispatcher].end(), | 48 dispatcher_font_map_[dispatcher].end(), |
| 49 font_name); | 49 font_name); |
| 50 if (it == dispatcher_font_map_[dispatcher].end()) { | 50 if (it == dispatcher_font_map_[dispatcher].end()) { |
| 51 // Requested font is new to cache. | 51 // Requested font is new to cache. |
| 52 dispatcher_font_map_[dispatcher].push_back(font_name); | 52 dispatcher_font_map_[dispatcher].push_back(font_name); |
| 53 } else { | 53 } else { |
| 54 ref_count_inc = 0; | 54 ref_count_inc = 0; |
| 55 } | 55 } |
| 56 | 56 |
| 57 if (cache_[font_name].ref_count_ == 0) { // Requested font is new to cache. | 57 if (cache_[font_name].ref_count_ == 0) { // Requested font is new to cache. |
| 58 cache_[font_name].ref_count_ = 1; | 58 cache_[font_name].ref_count_ = 1; |
| 59 } else { // Requested font is already in cache, release old handles. | 59 } else { // Requested font is already in cache, release old handles. |
| 60 SelectObject(cache_[font_name].dc_, cache_[font_name].old_font_); | 60 SelectObject(cache_[font_name].dc_, cache_[font_name].old_font_); |
| 61 DeleteObject(cache_[font_name].font_); | 61 DeleteObject(cache_[font_name].font_); |
| 62 ReleaseDC(NULL, cache_[font_name].dc_); | 62 ReleaseDC(nullptr, cache_[font_name].dc_); |
| 63 } | 63 } |
| 64 cache_[font_name].font_ = font_handle; | 64 cache_[font_name].font_ = font_handle; |
| 65 cache_[font_name].dc_ = hdc; | 65 cache_[font_name].dc_ = hdc; |
| 66 cache_[font_name].old_font_ = old_font; | 66 cache_[font_name].old_font_ = old_font; |
| 67 cache_[font_name].ref_count_ += ref_count_inc; | 67 cache_[font_name].ref_count_ += ref_count_inc; |
| 68 } | 68 } |
| 69 | 69 |
| 70 void ReleaseCachedFonts(FontCacheDispatcher* dispatcher) { | 70 void ReleaseCachedFonts(FontCacheDispatcher* dispatcher) { |
| 71 typedef std::map<base::string16, FontCache::CacheElement> FontNameToElement; | 71 typedef std::map<base::string16, FontCache::CacheElement> FontNameToElement; |
| 72 | 72 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 93 cache_.erase(i++); | 93 cache_.erase(i++); |
| 94 } else { | 94 } else { |
| 95 ++i; | 95 ++i; |
| 96 } | 96 } |
| 97 } | 97 } |
| 98 } | 98 } |
| 99 | 99 |
| 100 private: | 100 private: |
| 101 struct CacheElement { | 101 struct CacheElement { |
| 102 CacheElement() | 102 CacheElement() |
| 103 : font_(NULL), old_font_(NULL), dc_(NULL), ref_count_(0) { | 103 : font_(nullptr), old_font_(nullptr), dc_(nullptr), ref_count_(0) { |
| 104 } | 104 } |
| 105 | 105 |
| 106 ~CacheElement() { | 106 ~CacheElement() { |
| 107 if (font_) { | 107 if (font_) { |
| 108 if (dc_ && old_font_) { | 108 if (dc_ && old_font_) { |
| 109 SelectObject(dc_, old_font_); | 109 SelectObject(dc_, old_font_); |
| 110 } | 110 } |
| 111 DeleteObject(font_); | 111 DeleteObject(font_); |
| 112 } | 112 } |
| 113 if (dc_) { | 113 if (dc_) { |
| 114 ReleaseDC(NULL, dc_); | 114 ReleaseDC(nullptr, dc_); |
| 115 } | 115 } |
| 116 } | 116 } |
| 117 | 117 |
| 118 HFONT font_; | 118 HFONT font_; |
| 119 HGDIOBJ old_font_; | 119 HGDIOBJ old_font_; |
| 120 HDC dc_; | 120 HDC dc_; |
| 121 int ref_count_; | 121 int ref_count_; |
| 122 }; | 122 }; |
| 123 friend struct DefaultSingletonTraits<FontCache>; | 123 friend struct DefaultSingletonTraits<FontCache>; |
| 124 | 124 |
| 125 FontCache() { | 125 FontCache() { |
| 126 } | 126 } |
| 127 | 127 |
| 128 std::map<base::string16, CacheElement> cache_; | 128 std::map<base::string16, CacheElement> cache_; |
| 129 DispatcherToFontNames dispatcher_font_map_; | 129 DispatcherToFontNames dispatcher_font_map_; |
| 130 base::Lock mutex_; | 130 base::Lock mutex_; |
| 131 | 131 |
| 132 DISALLOW_COPY_AND_ASSIGN(FontCache); | 132 DISALLOW_COPY_AND_ASSIGN(FontCache); |
| 133 }; | 133 }; |
| 134 | 134 |
| 135 } | 135 } |
| 136 | 136 |
| 137 FontCacheDispatcher::FontCacheDispatcher() | 137 FontCacheDispatcher::FontCacheDispatcher() |
| 138 : sender_(NULL) { | 138 : sender_(nullptr) { |
| 139 } | 139 } |
| 140 | 140 |
| 141 FontCacheDispatcher::~FontCacheDispatcher() { | 141 FontCacheDispatcher::~FontCacheDispatcher() { |
| 142 } | 142 } |
| 143 | 143 |
| 144 void FontCacheDispatcher::OnFilterAdded(IPC::Sender* sender) { | 144 void FontCacheDispatcher::OnFilterAdded(IPC::Sender* sender) { |
| 145 sender_ = sender; | 145 sender_ = sender; |
| 146 } | 146 } |
| 147 | 147 |
| 148 bool FontCacheDispatcher::OnMessageReceived(const IPC::Message& message) { | 148 bool FontCacheDispatcher::OnMessageReceived(const IPC::Message& message) { |
| 149 bool handled = true; | 149 bool handled = true; |
| 150 IPC_BEGIN_MESSAGE_MAP(FontCacheDispatcher, message) | 150 IPC_BEGIN_MESSAGE_MAP(FontCacheDispatcher, message) |
| 151 IPC_MESSAGE_HANDLER(ChildProcessHostMsg_PreCacheFont, OnPreCacheFont) | 151 IPC_MESSAGE_HANDLER(ChildProcessHostMsg_PreCacheFont, OnPreCacheFont) |
| 152 IPC_MESSAGE_HANDLER(ChildProcessHostMsg_ReleaseCachedFonts, | 152 IPC_MESSAGE_HANDLER(ChildProcessHostMsg_ReleaseCachedFonts, |
| 153 OnReleaseCachedFonts) | 153 OnReleaseCachedFonts) |
| 154 IPC_MESSAGE_UNHANDLED(handled = false) | 154 IPC_MESSAGE_UNHANDLED(handled = false) |
| 155 IPC_END_MESSAGE_MAP() | 155 IPC_END_MESSAGE_MAP() |
| 156 return handled; | 156 return handled; |
| 157 } | 157 } |
| 158 | 158 |
| 159 void FontCacheDispatcher::OnChannelClosing() { | 159 void FontCacheDispatcher::OnChannelClosing() { |
| 160 sender_ = NULL; | 160 sender_ = nullptr; |
| 161 } | 161 } |
| 162 | 162 |
| 163 bool FontCacheDispatcher::Send(IPC::Message* message) { | 163 bool FontCacheDispatcher::Send(IPC::Message* message) { |
| 164 if (sender_) | 164 if (sender_) |
| 165 return sender_->Send(message); | 165 return sender_->Send(message); |
| 166 | 166 |
| 167 delete message; | 167 delete message; |
| 168 return false; | 168 return false; |
| 169 } | 169 } |
| 170 | 170 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 185 FontCache::GetInstance()->PreCacheFont(font, this); | 185 FontCache::GetInstance()->PreCacheFont(font, this); |
| 186 } | 186 } |
| 187 | 187 |
| 188 void FontCacheDispatcher::OnReleaseCachedFonts() { | 188 void FontCacheDispatcher::OnReleaseCachedFonts() { |
| 189 // Release cached fonts that requested from a pid by decrementing the ref | 189 // Release cached fonts that requested from a pid by decrementing the ref |
| 190 // count. When ref count is zero, the handles are released. | 190 // count. When ref count is zero, the handles are released. |
| 191 FontCache::GetInstance()->ReleaseCachedFonts(this); | 191 FontCache::GetInstance()->ReleaseCachedFonts(this); |
| 192 } | 192 } |
| 193 | 193 |
| 194 } // namespace content | 194 } // namespace content |
| OLD | NEW |