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/child_process_sandbox_support_impl_linux.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/pickle.h"
11 #include "base/posix/eintr_wrapper.h"
12 #include "base/posix/unix_domain_socket.h"
13 #include "content/common/sandbox_linux.h"
14 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/linux/WebFontFamily.h"
15 #include "third_party/WebKit/Source/WebKit/chromium/public/linux/WebFontRenderStyle.h"
19 void GetFontFamilyForCharacters(const uint16_t* utf16
,
21 const char* preferred_locale
,
22 WebKit::WebFontFamily
* family
) {
24 request
.WriteInt(LinuxSandbox::METHOD_GET_FONT_FAMILY_FOR_CHARS
);
25 request
.WriteInt(num_utf16
);
26 for (size_t i
= 0; i
< num_utf16
; ++i
)
27 request
.WriteUInt32(utf16
[i
]);
28 request
.WriteString(preferred_locale
);
31 const ssize_t n
= UnixDomainSocket::SendRecvMsg(GetSandboxFD(), buf
,
32 sizeof(buf
), NULL
, request
);
34 std::string family_name
;
36 bool isItalic
= false;
38 Pickle
reply(reinterpret_cast<char*>(buf
), n
);
39 PickleIterator
pickle_iter(reply
);
40 if (reply
.ReadString(&pickle_iter
, &family_name
) &&
41 reply
.ReadBool(&pickle_iter
, &isBold
) &&
42 reply
.ReadBool(&pickle_iter
, &isItalic
)) {
43 family
->name
= family_name
;
44 family
->isBold
= isBold
;
45 family
->isItalic
= isItalic
;
50 void GetRenderStyleForStrike(const char* family
, int sizeAndStyle
,
51 WebKit::WebFontRenderStyle
* out
) {
53 request
.WriteInt(LinuxSandbox::METHOD_GET_STYLE_FOR_STRIKE
);
54 request
.WriteString(family
);
55 request
.WriteInt(sizeAndStyle
);
58 const ssize_t n
= UnixDomainSocket::SendRecvMsg(GetSandboxFD(), buf
,
59 sizeof(buf
), NULL
, request
);
66 Pickle
reply(reinterpret_cast<char*>(buf
), n
);
67 PickleIterator
pickle_iter(reply
);
68 int useBitmaps
, useAutoHint
, useHinting
, hintStyle
, useAntiAlias
;
69 int useSubpixelRendering
, useSubpixelPositioning
;
70 if (reply
.ReadInt(&pickle_iter
, &useBitmaps
) &&
71 reply
.ReadInt(&pickle_iter
, &useAutoHint
) &&
72 reply
.ReadInt(&pickle_iter
, &useHinting
) &&
73 reply
.ReadInt(&pickle_iter
, &hintStyle
) &&
74 reply
.ReadInt(&pickle_iter
, &useAntiAlias
) &&
75 reply
.ReadInt(&pickle_iter
, &useSubpixelRendering
) &&
76 reply
.ReadInt(&pickle_iter
, &useSubpixelPositioning
)) {
77 out
->useBitmaps
= useBitmaps
;
78 out
->useAutoHint
= useAutoHint
;
79 out
->useHinting
= useHinting
;
80 out
->hintStyle
= hintStyle
;
81 out
->useAntiAlias
= useAntiAlias
;
82 out
->useSubpixelRendering
= useSubpixelRendering
;
83 out
->useSubpixelPositioning
= useSubpixelPositioning
;
87 int MatchFontWithFallback(const std::string
& face
, bool bold
,
88 bool italic
, int charset
) {
90 request
.WriteInt(LinuxSandbox::METHOD_MATCH_WITH_FALLBACK
);
91 request
.WriteString(face
);
92 request
.WriteBool(bold
);
93 request
.WriteBool(italic
);
94 request
.WriteUInt32(charset
);
95 uint8_t reply_buf
[64];
97 UnixDomainSocket::SendRecvMsg(GetSandboxFD(), reply_buf
, sizeof(reply_buf
),
102 bool GetFontTable(int fd
, uint32_t table
, uint8_t* output
,
103 size_t* output_length
) {
106 if (fstat(fd
, &st
) < 0)
108 size_t length
= st
.st_size
;
110 *output_length
= length
;
113 if (*output_length
< length
)
115 *output_length
= length
;
116 ssize_t n
= HANDLE_EINTR(pread(fd
, output
, length
, 0));
117 if (n
!= static_cast<ssize_t
>(length
))
123 uint8_t num_tables_buf
[2];
125 ssize_t n
= HANDLE_EINTR(pread(fd
, &num_tables_buf
, sizeof(num_tables_buf
),
126 4 /* skip the font type */));
127 if (n
!= sizeof(num_tables_buf
))
130 num_tables
= static_cast<unsigned>(num_tables_buf
[0]) << 8 |
133 // The size in bytes of an entry in the table directory.
134 static const unsigned kTableEntrySize
= 16;
135 scoped_array
<uint8_t> table_entries(
136 new uint8_t[num_tables
* kTableEntrySize
]);
137 n
= HANDLE_EINTR(pread(fd
, table_entries
.get(), num_tables
* kTableEntrySize
,
138 12 /* skip the SFNT header */));
139 if (n
!= static_cast<ssize_t
>(num_tables
* kTableEntrySize
))
144 for (unsigned i
= 0; i
< num_tables
; i
++) {
145 const uint8_t* entry
= table_entries
.get() + i
* kTableEntrySize
;
146 if (memcmp(entry
, &table
, sizeof(table
)) == 0) {
147 offset
= static_cast<size_t>(entry
[8]) << 24 |
148 static_cast<size_t>(entry
[9]) << 16 |
149 static_cast<size_t>(entry
[10]) << 8 |
150 static_cast<size_t>(entry
[11]);
151 length
= static_cast<size_t>(entry
[12]) << 24 |
152 static_cast<size_t>(entry
[13]) << 16 |
153 static_cast<size_t>(entry
[14]) << 8 |
154 static_cast<size_t>(entry
[15]);
164 *output_length
= length
;
168 if (*output_length
< length
)
171 *output_length
= length
;
172 n
= HANDLE_EINTR(pread(fd
, output
, length
, offset
));
173 if (n
!= static_cast<ssize_t
>(length
))
179 } // namespace content