Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(12)

Side by Side Diff: content/common/font_config_ipc_linux.cc

Issue 12700027: Merge 188813 "mmap the opened file descriptor in the renderer pr..." (Closed) Base URL: svn://svn.chromium.org/chrome/branches/1444/src/
Patch Set: Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « content/browser/renderer_host/render_sandbox_host_linux.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/common/font_config_ipc_linux.h" 5 #include "content/common/font_config_ipc_linux.h"
6 6
7 #include <errno.h> 7 #include <errno.h>
8 #include <fcntl.h> 8 #include <fcntl.h>
9 #include <sys/mman.h>
9 #include <sys/socket.h> 10 #include <sys/socket.h>
11 #include <sys/stat.h>
10 #include <sys/uio.h> 12 #include <sys/uio.h>
11 #include <unistd.h> 13 #include <unistd.h>
12 14
15 #include "base/file_util.h"
13 #include "base/pickle.h" 16 #include "base/pickle.h"
14 #include "base/posix/unix_domain_socket_linux.h" 17 #include "base/posix/unix_domain_socket_linux.h"
15 #include "skia/ext/skia_utils_base.h" 18 #include "skia/ext/skia_utils_base.h"
16 #include "third_party/skia/include/core/SkStream.h" 19 #include "third_party/skia/include/core/SkStream.h"
17 20
18 namespace content { 21 namespace content {
19 22
23 // Is given a mmap'd address, and will unmap it in its destructor
24 class MMapStream : public SkStream {
25 public:
26 MMapStream(const void* addr, size_t length)
27 : memory_(reinterpret_cast<const uint8_t*>(addr))
28 , offset_(0)
29 , length_(length)
30 {}
31
32 virtual ~MMapStream() {
33 munmap(const_cast<uint8_t*>(memory_), length_);
34 }
35
36 virtual bool rewind() OVERRIDE {
37 offset_ = 0;
38 return true;
39 }
40
41 virtual size_t read(void* buffer, size_t size) OVERRIDE {
42 if (!buffer && !size) {
43 // This is request for the length of the stream.
44 return length_;
45 }
46
47 size_t remaining = length_ - offset_;
48 if (size > remaining)
49 size = remaining;
50 if (buffer)
51 memcpy(buffer, memory_ + offset_, size);
52
53 offset_ += size;
54 return size;
55 }
56
57 virtual const void* getMemoryBase() OVERRIDE {
58 return memory_;
59 }
60
61 private:
62 const uint8_t* memory_;
63 size_t offset_, length_;
64 };
65
66 // Return a stream from the file descriptor, or NULL on failure.
67 SkStream* StreamFromFD(int fd) {
68 struct stat st;
69 if (fstat(fd, &st))
70 return NULL;
71
72 void* memory = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
73 if (memory == MAP_FAILED)
74 return NULL;
75
76 return new MMapStream(memory, st.st_size);
77 }
78
79 void CloseFD(int fd) {
80 int err = HANDLE_EINTR(close(fd));
81 DCHECK(!err);
82 }
83
20 FontConfigIPC::FontConfigIPC(int fd) 84 FontConfigIPC::FontConfigIPC(int fd)
21 : fd_(fd) { 85 : fd_(fd) {
22 } 86 }
23 87
24 FontConfigIPC::~FontConfigIPC() { 88 FontConfigIPC::~FontConfigIPC() {
25 close(fd_); 89 CloseFD(fd_);
26 } 90 }
27 91
28 bool FontConfigIPC::matchFamilyName(const char familyName[], 92 bool FontConfigIPC::matchFamilyName(const char familyName[],
29 SkTypeface::Style requestedStyle, 93 SkTypeface::Style requestedStyle,
30 FontIdentity* outFontIdentity, 94 FontIdentity* outFontIdentity,
31 SkString* outFamilyName, 95 SkString* outFamilyName,
32 SkTypeface::Style* outStyle) { 96 SkTypeface::Style* outStyle) {
33 size_t familyNameLen = familyName ? strlen(familyName) : 0; 97 size_t familyNameLen = familyName ? strlen(familyName) : 0;
34 if (familyNameLen > kMaxFontFamilyLength) 98 if (familyNameLen > kMaxFontFamilyLength)
35 return false; 99 return false;
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 150
87 if (r == -1) 151 if (r == -1)
88 return NULL; 152 return NULL;
89 153
90 Pickle reply(reinterpret_cast<char*>(reply_buf), r); 154 Pickle reply(reinterpret_cast<char*>(reply_buf), r);
91 bool result; 155 bool result;
92 PickleIterator iter(reply); 156 PickleIterator iter(reply);
93 if (!reply.ReadBool(&iter, &result) || 157 if (!reply.ReadBool(&iter, &result) ||
94 !result) { 158 !result) {
95 if (result_fd) 159 if (result_fd)
96 close(result_fd); 160 CloseFD(result_fd);
97 return NULL; 161 return NULL;
98 } 162 }
99 163
100 return new SkFDStream(result_fd, true); 164 SkStream* stream = StreamFromFD(result_fd);
165 CloseFD(result_fd);
166 return stream;
101 } 167 }
102 168
103 } // namespace content 169 } // namespace content
170
OLDNEW
« no previous file with comments | « content/browser/renderer_host/render_sandbox_host_linux.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698