Revert 114539 - Move content::MakeSharedMemorySegmentViaIPC into its own file
[chromium-blink-merge.git] / content / common / child_process_sandbox_support_impl_linux.cc
blob4e7b1f0f5d3c1f6843512e7e373d03460657efcd
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"
7 #include <sys/stat.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;
23 namespace content {
25 void GetFontFamilyForCharacters(const uint16_t* utf16,
26 size_t num_utf16,
27 const char* preferred_locale,
28 WebKit::WebFontFamily* family) {
29 Pickle request;
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);
36 uint8_t buf[512];
37 const ssize_t n = UnixDomainSocket::SendRecvMsg(GetSandboxFD(), buf,
38 sizeof(buf), NULL, request);
40 std::string family_name;
41 bool isBold = false;
42 bool isItalic = false;
43 if (n != -1) {
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) {
58 Pickle request;
59 request.WriteInt(LinuxSandbox::METHOD_GET_STYLE_FOR_STRIKE);
60 request.WriteString(family);
61 request.WriteInt(sizeAndStyle);
63 uint8_t buf[512];
64 const ssize_t n = UnixDomainSocket::SendRecvMsg(GetSandboxFD(), buf,
65 sizeof(buf), NULL, request);
67 out->setDefaults();
68 if (n == -1) {
69 return;
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) {
91 Pickle request;
92 request.WriteInt(LinuxSandbox::METHOD_MAKE_SHARED_MEMORY_SEGMENT);
93 request.WriteUInt32(length);
94 request.WriteBool(executable);
95 uint8_t reply_buf[10];
96 int result_fd;
97 ssize_t result = UnixDomainSocket::SendRecvMsg(GetSandboxFD(),
98 reply_buf, sizeof(reply_buf),
99 &result_fd, request);
100 if (result == -1)
101 return -1;
102 return result_fd;
105 int MatchFontWithFallback(const std::string& face, bool bold,
106 bool italic, int charset) {
107 Pickle request;
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];
114 int fd = -1;
115 UnixDomainSocket::SendRecvMsg(GetSandboxFD(), reply_buf, sizeof(reply_buf),
116 &fd, request);
117 return fd;
120 bool GetFontTable(int fd, uint32_t table, uint8_t* output,
121 size_t* output_length) {
122 if (table == 0) {
123 struct stat st;
124 if (fstat(fd, &st) < 0)
125 return false;
126 size_t length = st.st_size;
127 if (!output) {
128 *output_length = length;
129 return true;
131 if (*output_length < length)
132 return false;
133 *output_length = length;
134 ssize_t n = HANDLE_EINTR(pread(fd, output, length, 0));
135 if (n != static_cast<ssize_t>(length))
136 return false;
137 return true;
140 unsigned num_tables;
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))
146 return false;
148 num_tables = static_cast<unsigned>(num_tables_buf[0]) << 8 |
149 num_tables_buf[1];
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))
158 return false;
160 size_t offset;
161 size_t length = 0;
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]);
174 break;
178 if (!length)
179 return false;
181 if (!output) {
182 *output_length = length;
183 return true;
186 if (*output_length < length)
187 return false;
189 *output_length = length;
190 n = HANDLE_EINTR(pread(fd, output, length, offset));
191 if (n != static_cast<ssize_t>(length))
192 return false;
194 return true;
197 } // namespace content