1 // Copyright (c) 2011 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/eintr_wrapper.h"
10 #include "base/global_descriptors_posix.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/pickle.h"
13 #include "content/common/chrome_descriptors.h"
14 #include "content/common/sandbox_methods_linux.h"
15 #include "content/common/unix_domain_socket_posix.h"
16 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/linux/WebFontFamily.h"
17 #include "third_party/WebKit/Source/WebKit/chromium/public/linux/WebFontRenderStyle.h"
19 static int GetSandboxFD() {
20 return kSandboxIPCChannel
+ base::GlobalDescriptors::kBaseDescriptor
;
25 void GetFontFamilyForCharacters(const uint16_t* utf16
,
27 const char* preferred_locale
,
28 WebKit::WebFontFamily
* family
) {
30 request
.WriteInt(LinuxSandbox::METHOD_GET_FONT_FAMILY_FOR_CHARS
);
31 request
.WriteInt(num_utf16
);
32 for (size_t i
= 0; i
< num_utf16
; ++i
)
33 request
.WriteUInt32(utf16
[i
]);
34 request
.WriteString(preferred_locale
);
37 const ssize_t n
= UnixDomainSocket::SendRecvMsg(GetSandboxFD(), buf
,
38 sizeof(buf
), NULL
, request
);
40 std::string family_name
;
42 bool isItalic
= false;
44 Pickle
reply(reinterpret_cast<char*>(buf
), n
);
45 void* pickle_iter
= NULL
;
46 if (reply
.ReadString(&pickle_iter
, &family_name
) &&
47 reply
.ReadBool(&pickle_iter
, &isBold
) &&
48 reply
.ReadBool(&pickle_iter
, &isItalic
)) {
49 family
->name
= family_name
;
50 family
->isBold
= isBold
;
51 family
->isItalic
= isItalic
;
56 void GetRenderStyleForStrike(const char* family
, int sizeAndStyle
,
57 WebKit::WebFontRenderStyle
* out
) {
59 request
.WriteInt(LinuxSandbox::METHOD_GET_STYLE_FOR_STRIKE
);
60 request
.WriteString(family
);
61 request
.WriteInt(sizeAndStyle
);
64 const ssize_t n
= UnixDomainSocket::SendRecvMsg(GetSandboxFD(), buf
,
65 sizeof(buf
), NULL
, request
);
72 Pickle
reply(reinterpret_cast<char*>(buf
), n
);
73 void* pickle_iter
= NULL
;
74 int useBitmaps
, useAutoHint
, useHinting
, hintStyle
, useAntiAlias
, useSubpixel
;
75 if (reply
.ReadInt(&pickle_iter
, &useBitmaps
) &&
76 reply
.ReadInt(&pickle_iter
, &useAutoHint
) &&
77 reply
.ReadInt(&pickle_iter
, &useHinting
) &&
78 reply
.ReadInt(&pickle_iter
, &hintStyle
) &&
79 reply
.ReadInt(&pickle_iter
, &useAntiAlias
) &&
80 reply
.ReadInt(&pickle_iter
, &useSubpixel
)) {
81 out
->useBitmaps
= useBitmaps
;
82 out
->useAutoHint
= useAutoHint
;
83 out
->useHinting
= useHinting
;
84 out
->hintStyle
= hintStyle
;
85 out
->useAntiAlias
= useAntiAlias
;
86 out
->useSubpixel
= useSubpixel
;
90 int MakeSharedMemorySegmentViaIPC(size_t length
, bool executable
) {
92 request
.WriteInt(LinuxSandbox::METHOD_MAKE_SHARED_MEMORY_SEGMENT
);
93 request
.WriteUInt32(length
);
94 request
.WriteBool(executable
);
95 uint8_t reply_buf
[10];
97 ssize_t result
= UnixDomainSocket::SendRecvMsg(GetSandboxFD(),
98 reply_buf
, sizeof(reply_buf
),
105 int MatchFontWithFallback(const std::string
& face
, bool bold
,
106 bool italic
, int charset
) {
108 request
.WriteInt(LinuxSandbox::METHOD_MATCH_WITH_FALLBACK
);
109 request
.WriteString(face
);
110 request
.WriteBool(bold
);
111 request
.WriteBool(italic
);
112 request
.WriteUInt32(charset
);
113 uint8_t reply_buf
[64];
115 UnixDomainSocket::SendRecvMsg(GetSandboxFD(), reply_buf
, sizeof(reply_buf
),
120 bool GetFontTable(int fd
, uint32_t table
, uint8_t* output
,
121 size_t* output_length
) {
124 if (fstat(fd
, &st
) < 0)
126 size_t length
= st
.st_size
;
128 *output_length
= length
;
131 if (*output_length
< length
)
133 *output_length
= length
;
134 ssize_t n
= HANDLE_EINTR(pread(fd
, output
, length
, 0));
135 if (n
!= static_cast<ssize_t
>(length
))
141 uint8_t num_tables_buf
[2];
143 ssize_t n
= HANDLE_EINTR(pread(fd
, &num_tables_buf
, sizeof(num_tables_buf
),
144 4 /* skip the font type */));
145 if (n
!= sizeof(num_tables_buf
))
148 num_tables
= static_cast<unsigned>(num_tables_buf
[0]) << 8 |
151 // The size in bytes of an entry in the table directory.
152 static const unsigned kTableEntrySize
= 16;
153 scoped_array
<uint8_t> table_entries(
154 new uint8_t[num_tables
* kTableEntrySize
]);
155 n
= HANDLE_EINTR(pread(fd
, table_entries
.get(), num_tables
* kTableEntrySize
,
156 12 /* skip the SFNT header */));
157 if (n
!= static_cast<ssize_t
>(num_tables
* kTableEntrySize
))
162 for (unsigned i
= 0; i
< num_tables
; i
++) {
163 const uint8_t* entry
= table_entries
.get() + i
* kTableEntrySize
;
164 if (memcmp(entry
, &table
, sizeof(table
)) == 0) {
165 offset
= static_cast<size_t>(entry
[8]) << 24 |
166 static_cast<size_t>(entry
[9]) << 16 |
167 static_cast<size_t>(entry
[10]) << 8 |
168 static_cast<size_t>(entry
[11]);
169 length
= static_cast<size_t>(entry
[12]) << 24 |
170 static_cast<size_t>(entry
[13]) << 16 |
171 static_cast<size_t>(entry
[14]) << 8 |
172 static_cast<size_t>(entry
[15]);
182 *output_length
= length
;
186 if (*output_length
< length
)
189 *output_length
= length
;
190 n
= HANDLE_EINTR(pread(fd
, output
, length
, offset
));
191 if (n
!= static_cast<ssize_t
>(length
))
197 } // namespace content