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