Disable InstantExtendedTest.UpdateSearchQueryOnBackNavigation, InstantExtendedTest...
[chromium-blink-merge.git] / content / common / child_process_sandbox_support_impl_linux.cc
blob6ee2f94f25604cd517ea7f1c9376ddb246f3901b
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_linux.h"
13 #include "base/safe_numerics.h"
14 #include "base/sys_byteorder.h"
15 #include "content/common/sandbox_linux.h"
16 #include "third_party/WebKit/public/platform/linux/WebFontFamily.h"
17 #include "third_party/WebKit/public/web/linux/WebFontRenderStyle.h"
19 namespace content {
21 void GetFontFamilyForCharacters(const uint16_t* utf16,
22 size_t num_utf16,
23 const char* preferred_locale,
24 WebKit::WebFontFamily* family) {
25 Pickle request;
26 request.WriteInt(LinuxSandbox::METHOD_GET_FONT_FAMILY_FOR_CHARS);
27 request.WriteInt(num_utf16);
28 for (size_t i = 0; i < num_utf16; ++i)
29 request.WriteUInt32(utf16[i]);
30 request.WriteString(preferred_locale);
32 uint8_t buf[512];
33 const ssize_t n = UnixDomainSocket::SendRecvMsg(GetSandboxFD(), buf,
34 sizeof(buf), NULL, request);
36 std::string family_name;
37 bool isBold = false;
38 bool isItalic = false;
39 if (n != -1) {
40 Pickle reply(reinterpret_cast<char*>(buf), n);
41 PickleIterator pickle_iter(reply);
42 if (reply.ReadString(&pickle_iter, &family_name) &&
43 reply.ReadBool(&pickle_iter, &isBold) &&
44 reply.ReadBool(&pickle_iter, &isItalic)) {
45 family->name = family_name;
46 family->isBold = isBold;
47 family->isItalic = isItalic;
52 void GetRenderStyleForStrike(const char* family, int sizeAndStyle,
53 WebKit::WebFontRenderStyle* out) {
54 Pickle request;
55 request.WriteInt(LinuxSandbox::METHOD_GET_STYLE_FOR_STRIKE);
56 request.WriteString(family);
57 request.WriteInt(sizeAndStyle);
59 uint8_t buf[512];
60 const ssize_t n = UnixDomainSocket::SendRecvMsg(GetSandboxFD(), buf,
61 sizeof(buf), NULL, request);
63 out->setDefaults();
64 if (n == -1) {
65 return;
68 Pickle reply(reinterpret_cast<char*>(buf), n);
69 PickleIterator pickle_iter(reply);
70 int useBitmaps, useAutoHint, useHinting, hintStyle, useAntiAlias;
71 int useSubpixelRendering, useSubpixelPositioning;
72 if (reply.ReadInt(&pickle_iter, &useBitmaps) &&
73 reply.ReadInt(&pickle_iter, &useAutoHint) &&
74 reply.ReadInt(&pickle_iter, &useHinting) &&
75 reply.ReadInt(&pickle_iter, &hintStyle) &&
76 reply.ReadInt(&pickle_iter, &useAntiAlias) &&
77 reply.ReadInt(&pickle_iter, &useSubpixelRendering) &&
78 reply.ReadInt(&pickle_iter, &useSubpixelPositioning)) {
79 out->useBitmaps = useBitmaps;
80 out->useAutoHint = useAutoHint;
81 out->useHinting = useHinting;
82 out->hintStyle = hintStyle;
83 out->useAntiAlias = useAntiAlias;
84 out->useSubpixelRendering = useSubpixelRendering;
85 out->useSubpixelPositioning = useSubpixelPositioning;
89 int MatchFontWithFallback(const std::string& face, bool bold,
90 bool italic, int charset) {
91 Pickle request;
92 request.WriteInt(LinuxSandbox::METHOD_MATCH_WITH_FALLBACK);
93 request.WriteString(face);
94 request.WriteBool(bold);
95 request.WriteBool(italic);
96 request.WriteUInt32(charset);
97 uint8_t reply_buf[64];
98 int fd = -1;
99 UnixDomainSocket::SendRecvMsg(GetSandboxFD(), reply_buf, sizeof(reply_buf),
100 &fd, request);
101 return fd;
104 bool GetFontTable(int fd, uint32_t table_tag, off_t offset,
105 uint8_t* output, size_t* output_length) {
106 if (offset < 0)
107 return false;
109 size_t data_length = 0; // the length of the file data.
110 off_t data_offset = 0; // the offset of the data in the file.
111 if (table_tag == 0) {
112 // Get the entire font file.
113 struct stat st;
114 if (fstat(fd, &st) < 0)
115 return false;
116 data_length = base::checked_numeric_cast<size_t>(st.st_size);
117 } else {
118 // Get a font table. Read the header to find its offset in the file.
119 uint16_t num_tables;
120 ssize_t n = HANDLE_EINTR(pread(fd, &num_tables, sizeof(num_tables),
121 4 /* skip the font type */));
122 if (n != sizeof(num_tables))
123 return false;
124 // Font data is stored in net (big-endian) order.
125 num_tables = base::NetToHost16(num_tables);
127 // Read the table directory.
128 static const size_t kTableEntrySize = 16;
129 const size_t directory_size = num_tables * kTableEntrySize;
130 scoped_ptr<uint8_t[]> table_entries(new uint8_t[directory_size]);
131 n = HANDLE_EINTR(pread(fd, table_entries.get(), directory_size,
132 12 /* skip the SFNT header */));
133 if (n != base::checked_numeric_cast<ssize_t>(directory_size))
134 return false;
136 for (uint16_t i = 0; i < num_tables; ++i) {
137 uint8_t* entry = table_entries.get() + i * kTableEntrySize;
138 uint32_t tag = *reinterpret_cast<uint32_t*>(entry);
139 if (tag == table_tag) {
140 // Font data is stored in net (big-endian) order.
141 data_offset =
142 base::NetToHost32(*reinterpret_cast<uint32_t*>(entry + 8));
143 data_length =
144 base::NetToHost32(*reinterpret_cast<uint32_t*>(entry + 12));
145 break;
150 if (!data_length)
151 return false;
152 // Clamp |offset| inside the allowable range. This allows the read to succeed
153 // but return 0 bytes.
154 offset = std::min(offset, base::checked_numeric_cast<off_t>(data_length));
155 // Make sure it's safe to add the data offset and the caller's logical offset.
156 // Define the maximum positive offset on 32 bit systems.
157 static const off_t kMaxPositiveOffset32 = 0x7FFFFFFF; // 2 GB - 1.
158 if ((offset > kMaxPositiveOffset32 / 2) ||
159 (data_offset > kMaxPositiveOffset32 / 2))
160 return false;
161 data_offset += offset;
162 data_length -= offset;
164 if (output) {
165 // 'output_length' holds the maximum amount of data the caller can accept.
166 data_length = std::min(data_length, *output_length);
167 ssize_t n = HANDLE_EINTR(pread(fd, output, data_length, data_offset));
168 if (n != base::checked_numeric_cast<ssize_t>(data_length))
169 return false;
171 *output_length = data_length;
173 return true;
176 } // namespace content