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_config_ipc_linux.h" | 5 #include "content/common/font_config_ipc_linux.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
9 #include <sys/mman.h> | 9 #include <sys/mman.h> |
10 #include <sys/socket.h> | 10 #include <sys/socket.h> |
11 #include <sys/stat.h> | 11 #include <sys/stat.h> |
12 #include <sys/uio.h> | 12 #include <sys/uio.h> |
13 #include <unistd.h> | 13 #include <unistd.h> |
14 | 14 |
15 #include "base/debug/trace_event.h" | 15 #include "base/debug/trace_event.h" |
16 #include "base/files/file_util.h" | 16 #include "base/files/file_util.h" |
17 #include "base/pickle.h" | 17 #include "base/pickle.h" |
18 #include "base/posix/unix_domain_socket_linux.h" | 18 #include "base/posix/unix_domain_socket_linux.h" |
19 #include "skia/ext/refptr.h" | 19 #include "skia/ext/refptr.h" |
20 #include "skia/ext/skia_utils_base.h" | 20 #include "skia/ext/skia_utils_base.h" |
21 #include "third_party/skia/include/core/SkData.h" | 21 #include "third_party/skia/include/core/SkData.h" |
22 #include "third_party/skia/include/core/SkStream.h" | 22 #include "third_party/skia/include/core/SkStream.h" |
23 | 23 |
24 namespace content { | 24 namespace content { |
25 | 25 |
26 class FontConfigIPC::FontMemoryStream | |
27 : public SkMemoryStream, | |
28 public base::SupportsWeakPtr<FontConfigIPC::FontMemoryStream> { | |
Daniel Erat
2014/11/06 20:33:55
do you actually need SupportsWeakPtr here and the
Krzysztof Olczyk
2014/11/07 11:38:00
Yes, there was also the problem with the fact that
| |
29 public: | |
30 explicit FontMemoryStream(SkData* data) : SkMemoryStream(data) {} | |
31 ~FontMemoryStream() override {} | |
32 }; | |
33 | |
26 // Return a stream from the file descriptor, or NULL on failure. | 34 // Return a stream from the file descriptor, or NULL on failure. |
27 SkStream* StreamFromFD(int fd) { | 35 FontConfigIPC::FontMemoryStream* FontConfigIPC::StreamFromFD(int fd) { |
28 skia::RefPtr<SkData> data = skia::AdoptRef(SkData::NewFromFD(fd)); | 36 skia::RefPtr<SkData> data = skia::AdoptRef(SkData::NewFromFD(fd)); |
29 if (!data) { | 37 if (!data) { |
30 return NULL; | 38 return NULL; |
31 } | 39 } |
32 return new SkMemoryStream(data.get()); | 40 return new FontMemoryStream(data.get()); |
33 } | 41 } |
34 | 42 |
35 void CloseFD(int fd) { | 43 void CloseFD(int fd) { |
36 int err = IGNORE_EINTR(close(fd)); | 44 int err = IGNORE_EINTR(close(fd)); |
37 DCHECK(!err); | 45 DCHECK(!err); |
38 } | 46 } |
39 | 47 |
40 FontConfigIPC::FontConfigIPC(int fd) | 48 FontConfigIPC::FontConfigIPC(int fd) |
41 : fd_(fd) { | 49 : fd_(fd) { |
42 } | 50 } |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
89 if (outFamilyName) | 97 if (outFamilyName) |
90 *outFamilyName = reply_family; | 98 *outFamilyName = reply_family; |
91 if (outStyle) | 99 if (outStyle) |
92 *outStyle = static_cast<SkTypeface::Style>(reply_style); | 100 *outStyle = static_cast<SkTypeface::Style>(reply_style); |
93 | 101 |
94 return true; | 102 return true; |
95 } | 103 } |
96 | 104 |
97 SkStream* FontConfigIPC::openStream(const FontIdentity& identity) { | 105 SkStream* FontConfigIPC::openStream(const FontIdentity& identity) { |
98 TRACE_EVENT0("sandbox_ipc", "FontConfigIPC::openStream"); | 106 TRACE_EVENT0("sandbox_ipc", "FontConfigIPC::openStream"); |
107 | |
108 auto stream_cache_it = font_stream_cache_.find(identity.fID); | |
109 if (stream_cache_it != font_stream_cache_.end() && stream_cache_it->second) | |
110 return stream_cache_it->second.get(); | |
111 | |
112 PurgeFontStreamCache(); | |
113 | |
99 Pickle request; | 114 Pickle request; |
100 request.WriteInt(METHOD_OPEN); | 115 request.WriteInt(METHOD_OPEN); |
101 request.WriteUInt32(identity.fID); | 116 request.WriteUInt32(identity.fID); |
102 | 117 |
103 int result_fd = -1; | 118 int result_fd = -1; |
104 uint8_t reply_buf[256]; | 119 uint8_t reply_buf[256]; |
105 const ssize_t r = UnixDomainSocket::SendRecvMsg(fd_, reply_buf, | 120 const ssize_t r = UnixDomainSocket::SendRecvMsg(fd_, reply_buf, |
106 sizeof(reply_buf), | 121 sizeof(reply_buf), |
107 &result_fd, request); | 122 &result_fd, request); |
108 | 123 |
109 if (r == -1) | 124 if (r == -1) |
110 return NULL; | 125 return NULL; |
111 | 126 |
112 Pickle reply(reinterpret_cast<char*>(reply_buf), r); | 127 Pickle reply(reinterpret_cast<char*>(reply_buf), r); |
113 bool result; | 128 bool result; |
114 PickleIterator iter(reply); | 129 PickleIterator iter(reply); |
115 if (!reply.ReadBool(&iter, &result) || | 130 if (!reply.ReadBool(&iter, &result) || |
116 !result) { | 131 !result) { |
117 if (result_fd) | 132 if (result_fd) |
118 CloseFD(result_fd); | 133 CloseFD(result_fd); |
119 return NULL; | 134 return NULL; |
120 } | 135 } |
121 | 136 |
122 SkStream* stream = StreamFromFD(result_fd); | 137 FontMemoryStream* stream = StreamFromFD(result_fd); |
123 CloseFD(result_fd); | 138 CloseFD(result_fd); |
139 font_stream_cache_[identity.fID] = base::AsWeakPtr(stream); | |
124 return stream; | 140 return stream; |
125 } | 141 } |
126 | 142 |
143 void FontConfigIPC::PurgeFontStreamCache() { | |
144 auto stream_cache_it = font_stream_cache_.begin(); | |
145 while (stream_cache_it != font_stream_cache_.end()) { | |
146 if (!stream_cache_it->second) | |
147 font_stream_cache_.erase(stream_cache_it++); | |
148 else | |
149 ++stream_cache_it; | |
150 } | |
151 } | |
152 | |
127 } // namespace content | 153 } // namespace content |
128 | 154 |
OLD | NEW |