Chromium Code Reviews| 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" |
| 21 #include "base/threading/thread_restrictions.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 | 26 |
| 24 namespace content { | 27 namespace content { |
| 25 | 28 |
| 26 // Return a stream from the file descriptor, or NULL on failure. | 29 class FontConfigIPC::MappedFontFile |
| 27 SkStream* StreamFromFD(int fd) { | 30 : public base::RefCountedThreadSafe<MappedFontFile> { |
| 28 skia::RefPtr<SkData> data = skia::AdoptRef(SkData::NewFromFD(fd)); | 31 public: |
| 29 if (!data) { | 32 explicit MappedFontFile(uint32_t font_id) : font_id_(font_id) {} |
| 30 return NULL; | 33 |
| 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)); | |
| 31 } | 39 } |
| 32 return new SkMemoryStream(data.get()); | 40 |
| 33 } | 41 SkMemoryStream* CreateMemoryStream() { |
| 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() { | |
|
Daniel Erat
2015/01/14 23:40:27
as i understand it, you should be using 'override'
Krzysztof Olczyk
2015/01/15 09:05:28
When it derives from base class which has virtual
Daniel Erat
2015/01/15 15:55:54
ah, thanks; i didn't realize that RefCountedThread
Krzysztof Olczyk
2015/01/15 17:44:11
It uses CRTP in order to match right destructor in
| |
| 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 }; | |
| 34 | 68 |
| 35 void CloseFD(int fd) { | 69 void CloseFD(int fd) { |
| 36 int err = IGNORE_EINTR(close(fd)); | 70 int err = IGNORE_EINTR(close(fd)); |
| 37 DCHECK(!err); | 71 DCHECK(!err); |
| 38 } | 72 } |
| 39 | 73 |
| 40 FontConfigIPC::FontConfigIPC(int fd) | 74 FontConfigIPC::FontConfigIPC(int fd) |
| 41 : fd_(fd) { | 75 : fd_(fd) { |
| 42 } | 76 } |
| 43 | 77 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 89 if (outFamilyName) | 123 if (outFamilyName) |
| 90 *outFamilyName = reply_family; | 124 *outFamilyName = reply_family; |
| 91 if (outStyle) | 125 if (outStyle) |
| 92 *outStyle = static_cast<SkTypeface::Style>(reply_style); | 126 *outStyle = static_cast<SkTypeface::Style>(reply_style); |
| 93 | 127 |
| 94 return true; | 128 return true; |
| 95 } | 129 } |
| 96 | 130 |
| 97 SkStream* FontConfigIPC::openStream(const FontIdentity& identity) { | 131 SkStream* FontConfigIPC::openStream(const FontIdentity& identity) { |
| 98 TRACE_EVENT0("sandbox_ipc", "FontConfigIPC::openStream"); | 132 TRACE_EVENT0("sandbox_ipc", "FontConfigIPC::openStream"); |
| 133 | |
| 134 { | |
| 135 base::AutoLock lock(stream_opening_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 | |
| 99 Pickle request; | 141 Pickle request; |
| 100 request.WriteInt(METHOD_OPEN); | 142 request.WriteInt(METHOD_OPEN); |
| 101 request.WriteUInt32(identity.fID); | 143 request.WriteUInt32(identity.fID); |
| 102 | 144 |
| 103 int result_fd = -1; | 145 int result_fd = -1; |
| 104 uint8_t reply_buf[256]; | 146 uint8_t reply_buf[256]; |
| 105 const ssize_t r = UnixDomainSocket::SendRecvMsg(fd_, reply_buf, | 147 const ssize_t r = UnixDomainSocket::SendRecvMsg(fd_, reply_buf, |
| 106 sizeof(reply_buf), | 148 sizeof(reply_buf), |
| 107 &result_fd, request); | 149 &result_fd, request); |
| 108 | 150 |
| 109 if (r == -1) | 151 if (r == -1) |
| 110 return NULL; | 152 return NULL; |
| 111 | 153 |
| 112 Pickle reply(reinterpret_cast<char*>(reply_buf), r); | 154 Pickle reply(reinterpret_cast<char*>(reply_buf), r); |
| 113 bool result; | 155 bool result; |
| 114 PickleIterator iter(reply); | 156 PickleIterator iter(reply); |
| 115 if (!iter.ReadBool(&result) || !result) { | 157 if (!iter.ReadBool(&result) || !result) { |
| 116 if (result_fd) | 158 if (result_fd) |
| 117 CloseFD(result_fd); | 159 CloseFD(result_fd); |
| 118 return NULL; | 160 return NULL; |
| 119 } | 161 } |
| 120 | 162 |
| 121 SkStream* stream = StreamFromFD(result_fd); | 163 scoped_refptr<MappedFontFile> mapped_font_file = |
| 122 CloseFD(result_fd); | 164 new MappedFontFile(identity.fID); |
| 123 return stream; | 165 if (!mapped_font_file->Initialize(result_fd)) |
| 166 return nullptr; | |
| 167 | |
| 168 { | |
| 169 base::AutoLock lock(stream_opening_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(stream_opening_lock_); | |
| 179 mapped_font_files_.erase(mapped_font_file->font_id()); | |
| 124 } | 180 } |
| 125 | 181 |
| 126 } // namespace content | 182 } // namespace content |
| 127 | 183 |
| OLD | NEW |