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/files/memory_mapped_file.h" | |
18 #include "base/memory/ref_counted.h" | |
19 #include "base/pickle.h" | 17 #include "base/pickle.h" |
20 #include "base/posix/unix_domain_socket_linux.h" | 18 #include "base/posix/unix_domain_socket_linux.h" |
21 #include "base/threading/thread_restrictions.h" | |
22 #include "skia/ext/refptr.h" | 19 #include "skia/ext/refptr.h" |
23 #include "skia/ext/skia_utils_base.h" | 20 #include "skia/ext/skia_utils_base.h" |
24 #include "third_party/skia/include/core/SkData.h" | 21 #include "third_party/skia/include/core/SkData.h" |
25 #include "third_party/skia/include/core/SkStream.h" | 22 #include "third_party/skia/include/core/SkStream.h" |
26 | 23 |
27 namespace content { | 24 namespace content { |
28 | 25 |
29 class FontConfigIPC::MappedFontFile | 26 // Return a stream from the file descriptor, or NULL on failure. |
30 : public base::RefCountedThreadSafe<MappedFontFile> { | 27 SkStream* StreamFromFD(int fd) { |
31 public: | 28 skia::RefPtr<SkData> data = skia::AdoptRef(SkData::NewFromFD(fd)); |
32 explicit MappedFontFile(uint32_t font_id) : font_id_(font_id) {} | 29 if (!data) { |
33 | 30 return NULL; |
34 uint32_t font_id() const { return font_id_; } | |
35 | |
36 bool Initialize(int fd) { | |
37 base::ThreadRestrictions::ScopedAllowIO allow_mmap; | |
38 return mapped_font_file_.Initialize(base::File(fd)); | |
39 } | 31 } |
40 | 32 return new SkMemoryStream(data.get()); |
41 SkMemoryStream* CreateMemoryStream() { | 33 } |
42 DCHECK(mapped_font_file_.IsValid()); | |
43 auto data = skia::AdoptRef(SkData::NewWithProc( | |
44 mapped_font_file_.data(), mapped_font_file_.length(), | |
45 &MappedFontFile::ReleaseProc, this)); | |
46 if (!data) | |
47 return nullptr; | |
48 AddRef(); | |
49 return new SkMemoryStream(data.get()); | |
50 } | |
51 | |
52 private: | |
53 friend class base::RefCountedThreadSafe<MappedFontFile>; | |
54 | |
55 ~MappedFontFile() { | |
56 auto font_config = static_cast<FontConfigIPC*>(FontConfigIPC::RefGlobal()); | |
57 font_config->RemoveMappedFontFile(this); | |
58 } | |
59 | |
60 static void ReleaseProc(const void* ptr, size_t length, void* context) { | |
61 base::ThreadRestrictions::ScopedAllowIO allow_munmap; | |
62 static_cast<MappedFontFile*>(context)->Release(); | |
63 } | |
64 | |
65 uint32_t font_id_; | |
66 base::MemoryMappedFile mapped_font_file_; | |
67 }; | |
68 | 34 |
69 void CloseFD(int fd) { | 35 void CloseFD(int fd) { |
70 int err = IGNORE_EINTR(close(fd)); | 36 int err = IGNORE_EINTR(close(fd)); |
71 DCHECK(!err); | 37 DCHECK(!err); |
72 } | 38 } |
73 | 39 |
74 FontConfigIPC::FontConfigIPC(int fd) | 40 FontConfigIPC::FontConfigIPC(int fd) |
75 : fd_(fd) { | 41 : fd_(fd) { |
76 } | 42 } |
77 | 43 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 if (outFamilyName) | 89 if (outFamilyName) |
124 *outFamilyName = reply_family; | 90 *outFamilyName = reply_family; |
125 if (outStyle) | 91 if (outStyle) |
126 *outStyle = static_cast<SkTypeface::Style>(reply_style); | 92 *outStyle = static_cast<SkTypeface::Style>(reply_style); |
127 | 93 |
128 return true; | 94 return true; |
129 } | 95 } |
130 | 96 |
131 SkStream* FontConfigIPC::openStream(const FontIdentity& identity) { | 97 SkStream* FontConfigIPC::openStream(const FontIdentity& identity) { |
132 TRACE_EVENT0("sandbox_ipc", "FontConfigIPC::openStream"); | 98 TRACE_EVENT0("sandbox_ipc", "FontConfigIPC::openStream"); |
133 | |
134 { | |
135 base::AutoLock lock(lock_); | |
136 auto mapped_font_files_it = mapped_font_files_.find(identity.fID); | |
137 if (mapped_font_files_it != mapped_font_files_.end()) | |
138 return mapped_font_files_it->second->CreateMemoryStream(); | |
139 } | |
140 | |
141 Pickle request; | 99 Pickle request; |
142 request.WriteInt(METHOD_OPEN); | 100 request.WriteInt(METHOD_OPEN); |
143 request.WriteUInt32(identity.fID); | 101 request.WriteUInt32(identity.fID); |
144 | 102 |
145 int result_fd = -1; | 103 int result_fd = -1; |
146 uint8_t reply_buf[256]; | 104 uint8_t reply_buf[256]; |
147 const ssize_t r = UnixDomainSocket::SendRecvMsg(fd_, reply_buf, | 105 const ssize_t r = UnixDomainSocket::SendRecvMsg(fd_, reply_buf, |
148 sizeof(reply_buf), | 106 sizeof(reply_buf), |
149 &result_fd, request); | 107 &result_fd, request); |
150 | 108 |
151 if (r == -1) | 109 if (r == -1) |
152 return NULL; | 110 return NULL; |
153 | 111 |
154 Pickle reply(reinterpret_cast<char*>(reply_buf), r); | 112 Pickle reply(reinterpret_cast<char*>(reply_buf), r); |
155 bool result; | 113 bool result; |
156 PickleIterator iter(reply); | 114 PickleIterator iter(reply); |
157 if (!iter.ReadBool(&result) || !result) { | 115 if (!iter.ReadBool(&result) || !result) { |
158 if (result_fd) | 116 if (result_fd) |
159 CloseFD(result_fd); | 117 CloseFD(result_fd); |
160 return NULL; | 118 return NULL; |
161 } | 119 } |
162 | 120 |
163 scoped_refptr<MappedFontFile> mapped_font_file = | 121 SkStream* stream = StreamFromFD(result_fd); |
164 new MappedFontFile(identity.fID); | 122 CloseFD(result_fd); |
165 if (!mapped_font_file->Initialize(result_fd)) | 123 return stream; |
166 return nullptr; | |
167 | |
168 { | |
169 base::AutoLock lock(lock_); | |
170 auto mapped_font_files_it = | |
171 mapped_font_files_.insert(std::make_pair(mapped_font_file->font_id(), | |
172 mapped_font_file.get())).first; | |
173 return mapped_font_files_it->second->CreateMemoryStream(); | |
174 } | |
175 } | |
176 | |
177 void FontConfigIPC::RemoveMappedFontFile(MappedFontFile* mapped_font_file) { | |
178 base::AutoLock lock(lock_); | |
179 mapped_font_files_.erase(mapped_font_file->font_id()); | |
180 } | 124 } |
181 | 125 |
182 } // namespace content | 126 } // namespace content |
183 | 127 |
OLD | NEW |