Fixed last bookmark button hiding behavior.
[chromium-blink-merge.git] / base / shared_memory_nacl.cc
blob63e56e5df9ae5e877df7d957814730f5826f52aa
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 "base/shared_memory.h"
7 #include <errno.h>
8 #include <fcntl.h>
9 #include <sys/mman.h>
10 #include <sys/stat.h>
11 #include <unistd.h>
13 #include <limits>
15 #include "base/logging.h"
17 namespace base {
19 SharedMemory::SharedMemory()
20 : mapped_file_(-1),
21 mapped_size_(0),
22 inode_(0),
23 memory_(NULL),
24 read_only_(false),
25 created_size_(0) {
28 SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only)
29 : mapped_file_(handle.fd),
30 mapped_size_(0),
31 inode_(0),
32 memory_(NULL),
33 read_only_(read_only),
34 created_size_(0) {
37 SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only,
38 ProcessHandle process)
39 : mapped_file_(handle.fd),
40 mapped_size_(0),
41 inode_(0),
42 memory_(NULL),
43 read_only_(read_only),
44 created_size_(0) {
45 NOTREACHED();
48 SharedMemory::~SharedMemory() {
49 Close();
52 // static
53 bool SharedMemory::IsHandleValid(const SharedMemoryHandle& handle) {
54 return handle.fd >= 0;
57 // static
58 SharedMemoryHandle SharedMemory::NULLHandle() {
59 return SharedMemoryHandle();
62 // static
63 void SharedMemory::CloseHandle(const SharedMemoryHandle& handle) {
64 DCHECK_GE(handle.fd, 0);
65 if (close(handle.fd) < 0)
66 DPLOG(ERROR) << "close";
69 bool SharedMemory::CreateAndMapAnonymous(size_t size) {
70 // Untrusted code can't create descriptors or handles.
71 return false;
74 bool SharedMemory::Create(const SharedMemoryCreateOptions& options) {
75 // Untrusted code can't create descriptors or handles.
76 return false;
79 bool SharedMemory::Delete(const std::string& name) {
80 return false;
83 bool SharedMemory::Open(const std::string& name, bool read_only) {
84 return false;
87 bool SharedMemory::MapAt(off_t offset, size_t bytes) {
88 if (mapped_file_ == -1)
89 return false;
91 if (bytes > static_cast<size_t>(std::numeric_limits<int>::max()))
92 return false;
94 memory_ = mmap(NULL, bytes, PROT_READ | (read_only_ ? 0 : PROT_WRITE),
95 MAP_SHARED, mapped_file_, offset);
97 bool mmap_succeeded = memory_ != MAP_FAILED && memory_ != NULL;
98 if (mmap_succeeded) {
99 mapped_size_ = bytes;
100 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) &
101 (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1));
102 } else {
103 memory_ = NULL;
106 return mmap_succeeded;
109 bool SharedMemory::Unmap() {
110 if (memory_ == NULL)
111 return false;
113 if (munmap(memory_, mapped_size_) < 0)
114 DPLOG(ERROR) << "munmap";
115 memory_ = NULL;
116 mapped_size_ = 0;
117 return true;
120 SharedMemoryHandle SharedMemory::handle() const {
121 return FileDescriptor(mapped_file_, false);
124 void SharedMemory::Close() {
125 Unmap();
127 if (mapped_file_ > 0) {
128 if (close(mapped_file_) < 0)
129 DPLOG(ERROR) << "close";
130 mapped_file_ = -1;
134 void SharedMemory::Lock() {
135 NOTIMPLEMENTED();
138 void SharedMemory::Unlock() {
139 NOTIMPLEMENTED();
142 bool SharedMemory::ShareToProcessCommon(ProcessHandle process,
143 SharedMemoryHandle *new_handle,
144 bool close_self) {
145 const int new_fd = dup(mapped_file_);
146 if (new_fd < 0) {
147 DPLOG(ERROR) << "dup() failed.";
148 return false;
151 new_handle->fd = new_fd;
152 new_handle->auto_close = true;
154 if (close_self)
155 Close();
156 return true;
159 } // namespace base