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