[content shell] hook up testRunner.dumpEditingCallbacks
[chromium-blink-merge.git] / content / common / child_process_sandbox_support_impl_linux.cc
blob7d251804ce4bc76c91ea4b706fa43751380aa25b
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"
7 #include <sys/stat.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"
17 namespace content {
19 void GetFontFamilyForCharacters(const uint16_t* utf16,
20 size_t num_utf16,
21 const char* preferred_locale,
22 WebKit::WebFontFamily* family) {
23 Pickle request;
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);
30 uint8_t buf[512];
31 const ssize_t n = UnixDomainSocket::SendRecvMsg(GetSandboxFD(), buf,
32 sizeof(buf), NULL, request);
34 std::string family_name;
35 bool isBold = false;
36 bool isItalic = false;
37 if (n != -1) {
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) {
52 Pickle request;
53 request.WriteInt(LinuxSandbox::METHOD_GET_STYLE_FOR_STRIKE);
54 request.WriteString(family);
55 request.WriteInt(sizeAndStyle);
57 uint8_t buf[512];
58 const ssize_t n = UnixDomainSocket::SendRecvMsg(GetSandboxFD(), buf,
59 sizeof(buf), NULL, request);
61 out->setDefaults();
62 if (n == -1) {
63 return;
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) {
89 Pickle request;
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];
96 int fd = -1;
97 UnixDomainSocket::SendRecvMsg(GetSandboxFD(), reply_buf, sizeof(reply_buf),
98 &fd, request);
99 return fd;
102 bool GetFontTable(int fd, uint32_t table, uint8_t* output,
103 size_t* output_length) {
104 if (table == 0) {
105 struct stat st;
106 if (fstat(fd, &st) < 0)
107 return false;
108 size_t length = st.st_size;
109 if (!output) {
110 *output_length = length;
111 return true;
113 if (*output_length < length)
114 return false;
115 *output_length = length;
116 ssize_t n = HANDLE_EINTR(pread(fd, output, length, 0));
117 if (n != static_cast<ssize_t>(length))
118 return false;
119 return true;
122 unsigned num_tables;
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))
128 return false;
130 num_tables = static_cast<unsigned>(num_tables_buf[0]) << 8 |
131 num_tables_buf[1];
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))
140 return false;
142 size_t offset;
143 size_t length = 0;
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]);
156 break;
160 if (!length)
161 return false;
163 if (!output) {
164 *output_length = length;
165 return true;
168 if (*output_length < length)
169 return false;
171 *output_length = length;
172 n = HANDLE_EINTR(pread(fd, output, length, offset));
173 if (n != static_cast<ssize_t>(length))
174 return false;
176 return true;
179 } // namespace content