[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | cf3ac397 | 2010-12-22 20:02:29 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 797c355 | 2011-03-17 00:26:18 | [diff] [blame] | 5 | #include "content/common/font_config_ipc_linux.h" |
[email protected] | cf3ac397 | 2010-12-22 20:02:29 | [diff] [blame] | 6 | |
| 7 | #include <errno.h> |
[email protected] | cf3ac397 | 2010-12-22 20:02:29 | [diff] [blame] | 8 | #include <fcntl.h> |
[email protected] | bf408906 | 2013-03-18 21:01:33 | [diff] [blame] | 9 | #include <sys/mman.h> |
[email protected] | cf3ac397 | 2010-12-22 20:02:29 | [diff] [blame] | 10 | #include <sys/socket.h> |
[email protected] | bf408906 | 2013-03-18 21:01:33 | [diff] [blame] | 11 | #include <sys/stat.h> |
[email protected] | cf3ac397 | 2010-12-22 20:02:29 | [diff] [blame] | 12 | #include <sys/uio.h> |
[email protected] | 8c89c59 | 2013-01-18 10:34:29 | [diff] [blame] | 13 | #include <unistd.h> |
[email protected] | cf3ac397 | 2010-12-22 20:02:29 | [diff] [blame] | 14 | |
[email protected] | 6a1048ab | 2013-08-03 08:03:51 | [diff] [blame] | 15 | #include "base/debug/trace_event.h" |
[email protected] | bf408906 | 2013-03-18 21:01:33 | [diff] [blame] | 16 | #include "base/file_util.h" |
[email protected] | cf3ac397 | 2010-12-22 20:02:29 | [diff] [blame] | 17 | #include "base/pickle.h" |
[email protected] | 8c89c59 | 2013-01-18 10:34:29 | [diff] [blame] | 18 | #include "base/posix/unix_domain_socket_linux.h" |
[email protected] | 8e4e8f5f | 2013-06-06 21:33:59 | [diff] [blame] | 19 | #include "skia/ext/refptr.h" |
[email protected] | 9628e0d | 2013-03-12 13:11:54 | [diff] [blame] | 20 | #include "skia/ext/skia_utils_base.h" |
[email protected] | 8e4e8f5f | 2013-06-06 21:33:59 | [diff] [blame] | 21 | #include "third_party/skia/include/core/SkData.h" |
[email protected] | 9628e0d | 2013-03-12 13:11:54 | [diff] [blame] | 22 | #include "third_party/skia/include/core/SkStream.h" |
[email protected] | cf3ac397 | 2010-12-22 20:02:29 | [diff] [blame] | 23 | |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 24 | namespace content { |
| 25 | |
[email protected] | bf408906 | 2013-03-18 21:01:33 | [diff] [blame] | 26 | // Return a stream from the file descriptor, or NULL on failure. |
| 27 | SkStream* StreamFromFD(int fd) { |
[email protected] | 8e4e8f5f | 2013-06-06 21:33:59 | [diff] [blame] | 28 | skia::RefPtr<SkData> data = skia::AdoptRef(SkData::NewFromFD(fd)); |
| 29 | if (!data) { |
[email protected] | bf408906 | 2013-03-18 21:01:33 | [diff] [blame] | 30 | return NULL; |
[email protected] | 8e4e8f5f | 2013-06-06 21:33:59 | [diff] [blame] | 31 | } |
| 32 | return new SkMemoryStream(data.get()); |
[email protected] | bf408906 | 2013-03-18 21:01:33 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | void CloseFD(int fd) { |
| 36 | int err = HANDLE_EINTR(close(fd)); |
| 37 | DCHECK(!err); |
| 38 | } |
| 39 | |
[email protected] | cf3ac397 | 2010-12-22 20:02:29 | [diff] [blame] | 40 | FontConfigIPC::FontConfigIPC(int fd) |
| 41 | : fd_(fd) { |
| 42 | } |
| 43 | |
| 44 | FontConfigIPC::~FontConfigIPC() { |
[email protected] | bf408906 | 2013-03-18 21:01:33 | [diff] [blame] | 45 | CloseFD(fd_); |
[email protected] | cf3ac397 | 2010-12-22 20:02:29 | [diff] [blame] | 46 | } |
| 47 | |
[email protected] | 9628e0d | 2013-03-12 13:11:54 | [diff] [blame] | 48 | bool FontConfigIPC::matchFamilyName(const char familyName[], |
| 49 | SkTypeface::Style requestedStyle, |
| 50 | FontIdentity* outFontIdentity, |
| 51 | SkString* outFamilyName, |
| 52 | SkTypeface::Style* outStyle) { |
[email protected] | 6a1048ab | 2013-08-03 08:03:51 | [diff] [blame] | 53 | TRACE_EVENT0("sandbox_ipc", "FontConfigIPC::matchFamilyName"); |
[email protected] | 9628e0d | 2013-03-12 13:11:54 | [diff] [blame] | 54 | size_t familyNameLen = familyName ? strlen(familyName) : 0; |
| 55 | if (familyNameLen > kMaxFontFamilyLength) |
[email protected] | cf3ac397 | 2010-12-22 20:02:29 | [diff] [blame] | 56 | return false; |
| 57 | |
| 58 | Pickle request; |
| 59 | request.WriteInt(METHOD_MATCH); |
[email protected] | 9628e0d | 2013-03-12 13:11:54 | [diff] [blame] | 60 | request.WriteData(familyName, familyNameLen); |
| 61 | request.WriteUInt32(requestedStyle); |
[email protected] | cf3ac397 | 2010-12-22 20:02:29 | [diff] [blame] | 62 | |
[email protected] | 9628e0d | 2013-03-12 13:11:54 | [diff] [blame] | 63 | uint8_t reply_buf[2048]; |
[email protected] | cf3ac397 | 2010-12-22 20:02:29 | [diff] [blame] | 64 | const ssize_t r = UnixDomainSocket::SendRecvMsg(fd_, reply_buf, |
| 65 | sizeof(reply_buf), NULL, |
| 66 | request); |
| 67 | if (r == -1) |
| 68 | return false; |
| 69 | |
| 70 | Pickle reply(reinterpret_cast<char*>(reply_buf), r); |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 71 | PickleIterator iter(reply); |
[email protected] | cf3ac397 | 2010-12-22 20:02:29 | [diff] [blame] | 72 | bool result; |
| 73 | if (!reply.ReadBool(&iter, &result)) |
| 74 | return false; |
| 75 | if (!result) |
| 76 | return false; |
| 77 | |
[email protected] | 9628e0d | 2013-03-12 13:11:54 | [diff] [blame] | 78 | SkString reply_family; |
| 79 | FontIdentity reply_identity; |
| 80 | uint32_t reply_style; |
| 81 | if (!skia::ReadSkString(reply, &iter, &reply_family) || |
| 82 | !skia::ReadSkFontIdentity(reply, &iter, &reply_identity) || |
| 83 | !reply.ReadUInt32(&iter, &reply_style)) { |
[email protected] | cf3ac397 | 2010-12-22 20:02:29 | [diff] [blame] | 84 | return false; |
| 85 | } |
| 86 | |
[email protected] | 9628e0d | 2013-03-12 13:11:54 | [diff] [blame] | 87 | if (outFontIdentity) |
| 88 | *outFontIdentity = reply_identity; |
| 89 | if (outFamilyName) |
| 90 | *outFamilyName = reply_family; |
| 91 | if (outStyle) |
| 92 | *outStyle = static_cast<SkTypeface::Style>(reply_style); |
[email protected] | cf3ac397 | 2010-12-22 20:02:29 | [diff] [blame] | 93 | |
| 94 | return true; |
| 95 | } |
| 96 | |
[email protected] | 9628e0d | 2013-03-12 13:11:54 | [diff] [blame] | 97 | SkStream* FontConfigIPC::openStream(const FontIdentity& identity) { |
[email protected] | 6a1048ab | 2013-08-03 08:03:51 | [diff] [blame] | 98 | TRACE_EVENT0("sandbox_ipc", "FontConfigIPC::openStream"); |
[email protected] | cf3ac397 | 2010-12-22 20:02:29 | [diff] [blame] | 99 | Pickle request; |
| 100 | request.WriteInt(METHOD_OPEN); |
[email protected] | 9628e0d | 2013-03-12 13:11:54 | [diff] [blame] | 101 | request.WriteUInt32(identity.fID); |
[email protected] | cf3ac397 | 2010-12-22 20:02:29 | [diff] [blame] | 102 | |
| 103 | int result_fd = -1; |
| 104 | uint8_t reply_buf[256]; |
| 105 | const ssize_t r = UnixDomainSocket::SendRecvMsg(fd_, reply_buf, |
| 106 | sizeof(reply_buf), |
| 107 | &result_fd, request); |
| 108 | |
| 109 | if (r == -1) |
[email protected] | 9628e0d | 2013-03-12 13:11:54 | [diff] [blame] | 110 | return NULL; |
[email protected] | cf3ac397 | 2010-12-22 20:02:29 | [diff] [blame] | 111 | |
| 112 | Pickle reply(reinterpret_cast<char*>(reply_buf), r); |
| 113 | bool result; |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 114 | PickleIterator iter(reply); |
[email protected] | cf3ac397 | 2010-12-22 20:02:29 | [diff] [blame] | 115 | if (!reply.ReadBool(&iter, &result) || |
| 116 | !result) { |
| 117 | if (result_fd) |
[email protected] | bf408906 | 2013-03-18 21:01:33 | [diff] [blame] | 118 | CloseFD(result_fd); |
[email protected] | 9628e0d | 2013-03-12 13:11:54 | [diff] [blame] | 119 | return NULL; |
[email protected] | cf3ac397 | 2010-12-22 20:02:29 | [diff] [blame] | 120 | } |
| 121 | |
[email protected] | bf408906 | 2013-03-18 21:01:33 | [diff] [blame] | 122 | SkStream* stream = StreamFromFD(result_fd); |
| 123 | CloseFD(result_fd); |
| 124 | return stream; |
[email protected] | cf3ac397 | 2010-12-22 20:02:29 | [diff] [blame] | 125 | } |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 126 | |
| 127 | } // namespace content |
[email protected] | bf408906 | 2013-03-18 21:01:33 | [diff] [blame] | 128 | |