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
3 // found in the LICENSE file.
5 #include "content/common/font_config_ipc_linux.h"
10 #include <sys/socket.h>
15 #include "base/debug/trace_event.h"
16 #include "base/files/file_util.h"
17 #include "base/pickle.h"
18 #include "base/posix/unix_domain_socket_linux.h"
19 #include "skia/ext/refptr.h"
20 #include "skia/ext/skia_utils_base.h"
21 #include "third_party/skia/include/core/SkData.h"
22 #include "third_party/skia/include/core/SkStream.h"
26 // Return a stream from the file descriptor, or NULL on failure.
27 SkStream
* StreamFromFD(int fd
) {
28 skia::RefPtr
<SkData
> data
= skia::AdoptRef(SkData::NewFromFD(fd
));
32 return new SkMemoryStream(data
.get());
35 void CloseFD(int fd
) {
36 int err
= IGNORE_EINTR(close(fd
));
40 FontConfigIPC::FontConfigIPC(int fd
)
44 FontConfigIPC::~FontConfigIPC() {
48 bool FontConfigIPC::matchFamilyName(const char familyName
[],
49 SkTypeface::Style requestedStyle
,
50 FontIdentity
* outFontIdentity
,
51 SkString
* outFamilyName
,
52 SkTypeface::Style
* outStyle
) {
53 TRACE_EVENT0("sandbox_ipc", "FontConfigIPC::matchFamilyName");
54 size_t familyNameLen
= familyName
? strlen(familyName
) : 0;
55 if (familyNameLen
> kMaxFontFamilyLength
)
59 request
.WriteInt(METHOD_MATCH
);
60 request
.WriteData(familyName
, familyNameLen
);
61 request
.WriteUInt32(requestedStyle
);
63 uint8_t reply_buf
[2048];
64 const ssize_t r
= UnixDomainSocket::SendRecvMsg(fd_
, reply_buf
,
65 sizeof(reply_buf
), NULL
,
70 Pickle
reply(reinterpret_cast<char*>(reply_buf
), r
);
71 PickleIterator
iter(reply
);
73 if (!iter
.ReadBool(&result
))
78 SkString reply_family
;
79 FontIdentity reply_identity
;
81 if (!skia::ReadSkString(&iter
, &reply_family
) ||
82 !skia::ReadSkFontIdentity(&iter
, &reply_identity
) ||
83 !iter
.ReadUInt32(&reply_style
)) {
88 *outFontIdentity
= reply_identity
;
90 *outFamilyName
= reply_family
;
92 *outStyle
= static_cast<SkTypeface::Style
>(reply_style
);
97 SkStream
* FontConfigIPC::openStream(const FontIdentity
& identity
) {
98 TRACE_EVENT0("sandbox_ipc", "FontConfigIPC::openStream");
100 request
.WriteInt(METHOD_OPEN
);
101 request
.WriteUInt32(identity
.fID
);
104 uint8_t reply_buf
[256];
105 const ssize_t r
= UnixDomainSocket::SendRecvMsg(fd_
, reply_buf
,
107 &result_fd
, request
);
112 Pickle
reply(reinterpret_cast<char*>(reply_buf
), r
);
114 PickleIterator
iter(reply
);
115 if (!iter
.ReadBool(&result
) || !result
) {
121 SkStream
* stream
= StreamFromFD(result_fd
);
126 } // namespace content