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