Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 // Takes "ownership" of the file descriptor, and will close() it. | |
| 24 class SkFileDescriptorStream : public SkStream { | |
| 25 public: | |
| 26 SkFileDescriptorStream(int fd) : memory_(NULL), offset_(0), length_(0) { | |
| 27 struct stat st; | |
| 28 if (fstat(fd, &st)) { | |
| 29 DCHECK(!HANDLE_EINTR(close(fd))); | |
|
piman
2013/03/18 19:49:14
no side effects inside of DCHECK please.
| |
| 30 return; | |
| 31 } | |
| 32 | |
| 33 void* memory = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0); | |
| 34 DCHECK(!HANDLE_EINTR(close(fd))); | |
|
piman
2013/03/18 19:49:14
no side effects inside of DCHECK please.
| |
| 35 if (memory == MAP_FAILED) | |
| 36 return; | |
| 37 | |
| 38 memory_ = reinterpret_cast<uint8_t*>(memory); | |
| 39 length_ = st.st_size; | |
| 40 } | |
| 41 | |
| 42 virtual ~SkFileDescriptorStream() { | |
| 43 if (memory_) | |
| 44 munmap(const_cast<uint8_t*>(memory_), length_); | |
| 45 } | |
| 46 | |
| 47 virtual bool rewind() OVERRIDE { | |
| 48 offset_ = 0; | |
| 49 return true; | |
| 50 } | |
| 51 | |
| 52 virtual size_t read(void* buffer, size_t size) OVERRIDE { | |
| 53 if (!buffer && !size) { | |
| 54 // This is request for the length of the stream. | |
| 55 return length_; | |
| 56 } | |
| 57 | |
| 58 size_t remaining = length_ - offset_; | |
| 59 if (size > remaining) | |
| 60 size = remaining; | |
| 61 if (buffer) | |
| 62 memcpy(buffer, memory_ + offset_, size); | |
| 63 | |
| 64 offset_ += size; | |
| 65 return size; | |
| 66 } | |
| 67 | |
| 68 virtual const void* getMemoryBase() OVERRIDE { | |
| 69 return memory_; | |
| 70 } | |
| 71 | |
| 72 private: | |
| 73 const uint8_t* memory_; | |
| 74 size_t offset_, length_; | |
| 75 }; | |
| 76 | |
| 20 FontConfigIPC::FontConfigIPC(int fd) | 77 FontConfigIPC::FontConfigIPC(int fd) |
| 21 : fd_(fd) { | 78 : fd_(fd) { |
| 22 } | 79 } |
| 23 | 80 |
| 24 FontConfigIPC::~FontConfigIPC() { | 81 FontConfigIPC::~FontConfigIPC() { |
| 25 close(fd_); | 82 close(fd_); |
| 26 } | 83 } |
| 27 | 84 |
| 28 bool FontConfigIPC::matchFamilyName(const char familyName[], | 85 bool FontConfigIPC::matchFamilyName(const char familyName[], |
| 29 SkTypeface::Style requestedStyle, | 86 SkTypeface::Style requestedStyle, |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 90 Pickle reply(reinterpret_cast<char*>(reply_buf), r); | 147 Pickle reply(reinterpret_cast<char*>(reply_buf), r); |
| 91 bool result; | 148 bool result; |
| 92 PickleIterator iter(reply); | 149 PickleIterator iter(reply); |
| 93 if (!reply.ReadBool(&iter, &result) || | 150 if (!reply.ReadBool(&iter, &result) || |
| 94 !result) { | 151 !result) { |
| 95 if (result_fd) | 152 if (result_fd) |
| 96 close(result_fd); | 153 close(result_fd); |
| 97 return NULL; | 154 return NULL; |
| 98 } | 155 } |
| 99 | 156 |
| 100 return new SkFDStream(result_fd, true); | 157 return new SkFileDescriptorStream(result_fd); |
|
piman
2013/03/18 19:49:14
If the map failed inside of the constructor, shoul
reed1
2013/03/18 20:15:41
Done.
| |
| 101 } | 158 } |
| 102 | 159 |
| 103 } // namespace content | 160 } // namespace content |
| OLD | NEW |