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