| 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" |
| 11 #include "base/strings/string16.h" | 11 #include "base/strings/string16.h" |
| 12 #include "content/common/child_process_messages.h" | 12 #include "content/common/child_process_messages.h" |
| 13 #include "ipc/ipc_channel.h" |
| 13 | 14 |
| 14 namespace content { | 15 namespace content { |
| 15 namespace { | 16 namespace { |
| 16 typedef std::vector<base::string16> FontNameVector; | 17 typedef std::vector<base::string16> FontNameVector; |
| 17 typedef std::map<FontCacheDispatcher*, FontNameVector> DispatcherToFontNames; | 18 typedef std::map<FontCacheDispatcher*, FontNameVector> DispatcherToFontNames; |
| 18 | 19 |
| 19 class FontCache { | 20 class FontCache { |
| 20 public: | 21 public: |
| 21 static FontCache* GetInstance() { | 22 static FontCache* GetInstance() { |
| 22 return Singleton<FontCache>::get(); | 23 return Singleton<FontCache>::get(); |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 std::map<base::string16, CacheElement> cache_; | 129 std::map<base::string16, CacheElement> cache_; |
| 129 DispatcherToFontNames dispatcher_font_map_; | 130 DispatcherToFontNames dispatcher_font_map_; |
| 130 base::Lock mutex_; | 131 base::Lock mutex_; |
| 131 | 132 |
| 132 DISALLOW_COPY_AND_ASSIGN(FontCache); | 133 DISALLOW_COPY_AND_ASSIGN(FontCache); |
| 133 }; | 134 }; |
| 134 | 135 |
| 135 } | 136 } |
| 136 | 137 |
| 137 FontCacheDispatcher::FontCacheDispatcher() | 138 FontCacheDispatcher::FontCacheDispatcher() |
| 138 : sender_(NULL) { | 139 : channel_(NULL) { |
| 139 } | 140 } |
| 140 | 141 |
| 141 FontCacheDispatcher::~FontCacheDispatcher() { | 142 FontCacheDispatcher::~FontCacheDispatcher() { |
| 142 } | 143 } |
| 143 | 144 |
| 144 void FontCacheDispatcher::OnFilterAdded(IPC::Sender* sender) { | 145 void FontCacheDispatcher::OnFilterAdded(IPC::Channel* channel) { |
| 145 sender_ = sender; | 146 channel_ = channel; |
| 146 } | 147 } |
| 147 | 148 |
| 148 bool FontCacheDispatcher::OnMessageReceived(const IPC::Message& message) { | 149 bool FontCacheDispatcher::OnMessageReceived(const IPC::Message& message) { |
| 149 bool handled = true; | 150 bool handled = true; |
| 150 IPC_BEGIN_MESSAGE_MAP(FontCacheDispatcher, message) | 151 IPC_BEGIN_MESSAGE_MAP(FontCacheDispatcher, message) |
| 151 IPC_MESSAGE_HANDLER(ChildProcessHostMsg_PreCacheFont, OnPreCacheFont) | 152 IPC_MESSAGE_HANDLER(ChildProcessHostMsg_PreCacheFont, OnPreCacheFont) |
| 152 IPC_MESSAGE_HANDLER(ChildProcessHostMsg_ReleaseCachedFonts, | 153 IPC_MESSAGE_HANDLER(ChildProcessHostMsg_ReleaseCachedFonts, |
| 153 OnReleaseCachedFonts) | 154 OnReleaseCachedFonts) |
| 154 IPC_MESSAGE_UNHANDLED(handled = false) | 155 IPC_MESSAGE_UNHANDLED(handled = false) |
| 155 IPC_END_MESSAGE_MAP() | 156 IPC_END_MESSAGE_MAP() |
| 156 return handled; | 157 return handled; |
| 157 } | 158 } |
| 158 | 159 |
| 159 void FontCacheDispatcher::OnChannelClosing() { | 160 void FontCacheDispatcher::OnChannelClosing() { |
| 160 sender_ = NULL; | 161 channel_ = NULL; |
| 161 } | 162 } |
| 162 | 163 |
| 163 bool FontCacheDispatcher::Send(IPC::Message* message) { | 164 bool FontCacheDispatcher::Send(IPC::Message* message) { |
| 164 if (sender_) | 165 if (channel_) |
| 165 return sender_->Send(message); | 166 return channel_->Send(message); |
| 166 | 167 |
| 167 delete message; | 168 delete message; |
| 168 return false; | 169 return false; |
| 169 } | 170 } |
| 170 | 171 |
| 171 void FontCacheDispatcher::OnPreCacheFont(const LOGFONT& font) { | 172 void FontCacheDispatcher::OnPreCacheFont(const LOGFONT& font) { |
| 172 // If a child process is running in a sandbox, GetTextMetrics() | 173 // If a child process is running in a sandbox, GetTextMetrics() |
| 173 // can sometimes fail. If a font has not been loaded | 174 // can sometimes fail. If a font has not been loaded |
| 174 // previously, GetTextMetrics() will try to load the font | 175 // previously, GetTextMetrics() will try to load the font |
| 175 // from the font file. However, the sandboxed process does | 176 // from the font file. However, the sandboxed process does |
| 176 // not have permissions to access any font files and | 177 // not have permissions to access any font files and |
| 177 // the call fails. So we make the browser pre-load the | 178 // the call fails. So we make the browser pre-load the |
| 178 // font for us by using a dummy call to GetTextMetrics of | 179 // font for us by using a dummy call to GetTextMetrics of |
| 179 // the same font. | 180 // the same font. |
| 180 // This means the browser process just loads the font into memory so that | 181 // This means the browser process just loads the font into memory so that |
| 181 // when GDI attempt to query that font info in child process, it does not | 182 // when GDI attempt to query that font info in child process, it does not |
| 182 // need to load that file, hence no permission issues there. Therefore, | 183 // need to load that file, hence no permission issues there. Therefore, |
| 183 // when a font is asked to be cached, we always recreates the font object | 184 // when a font is asked to be cached, we always recreates the font object |
| 184 // to avoid the case that an in-cache font is swapped out by GDI. | 185 // to avoid the case that an in-cache font is swapped out by GDI. |
| 185 FontCache::GetInstance()->PreCacheFont(font, this); | 186 FontCache::GetInstance()->PreCacheFont(font, this); |
| 186 } | 187 } |
| 187 | 188 |
| 188 void FontCacheDispatcher::OnReleaseCachedFonts() { | 189 void FontCacheDispatcher::OnReleaseCachedFonts() { |
| 189 // Release cached fonts that requested from a pid by decrementing the ref | 190 // Release cached fonts that requested from a pid by decrementing the ref |
| 190 // count. When ref count is zero, the handles are released. | 191 // count. When ref count is zero, the handles are released. |
| 191 FontCache::GetInstance()->ReleaseCachedFonts(this); | 192 FontCache::GetInstance()->ReleaseCachedFonts(this); |
| 192 } | 193 } |
| 193 | 194 |
| 194 } // namespace content | 195 } // namespace content |
| OLD | NEW |