Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | #include "base/shared_memory.h" |
michael@0 | 6 | |
michael@0 | 7 | #include "base/logging.h" |
michael@0 | 8 | #include "base/win_util.h" |
michael@0 | 9 | #include "base/string_util.h" |
michael@0 | 10 | |
michael@0 | 11 | namespace base { |
michael@0 | 12 | |
michael@0 | 13 | SharedMemory::SharedMemory() |
michael@0 | 14 | : mapped_file_(NULL), |
michael@0 | 15 | memory_(NULL), |
michael@0 | 16 | read_only_(false), |
michael@0 | 17 | max_size_(0), |
michael@0 | 18 | lock_(NULL) { |
michael@0 | 19 | } |
michael@0 | 20 | |
michael@0 | 21 | SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only) |
michael@0 | 22 | : mapped_file_(handle), |
michael@0 | 23 | memory_(NULL), |
michael@0 | 24 | read_only_(read_only), |
michael@0 | 25 | max_size_(0), |
michael@0 | 26 | lock_(NULL) { |
michael@0 | 27 | } |
michael@0 | 28 | |
michael@0 | 29 | SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only, |
michael@0 | 30 | ProcessHandle process) |
michael@0 | 31 | : mapped_file_(NULL), |
michael@0 | 32 | memory_(NULL), |
michael@0 | 33 | read_only_(read_only), |
michael@0 | 34 | max_size_(0), |
michael@0 | 35 | lock_(NULL) { |
michael@0 | 36 | ::DuplicateHandle(process, handle, |
michael@0 | 37 | GetCurrentProcess(), &mapped_file_, |
michael@0 | 38 | STANDARD_RIGHTS_REQUIRED | |
michael@0 | 39 | (read_only_ ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS), |
michael@0 | 40 | FALSE, 0); |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | SharedMemory::~SharedMemory() { |
michael@0 | 44 | Close(); |
michael@0 | 45 | if (lock_ != NULL) |
michael@0 | 46 | CloseHandle(lock_); |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | // static |
michael@0 | 50 | bool SharedMemory::IsHandleValid(const SharedMemoryHandle& handle) { |
michael@0 | 51 | return handle != NULL; |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | // static |
michael@0 | 55 | SharedMemoryHandle SharedMemory::NULLHandle() { |
michael@0 | 56 | return NULL; |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | bool SharedMemory::Create(const std::string &cname, bool read_only, |
michael@0 | 60 | bool open_existing, size_t size) { |
michael@0 | 61 | DCHECK(mapped_file_ == NULL); |
michael@0 | 62 | std::wstring name = UTF8ToWide(cname); |
michael@0 | 63 | name_ = name; |
michael@0 | 64 | read_only_ = read_only; |
michael@0 | 65 | mapped_file_ = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, |
michael@0 | 66 | read_only_ ? PAGE_READONLY : PAGE_READWRITE, 0, static_cast<DWORD>(size), |
michael@0 | 67 | name.empty() ? NULL : name.c_str()); |
michael@0 | 68 | if (!mapped_file_) |
michael@0 | 69 | return false; |
michael@0 | 70 | |
michael@0 | 71 | // Check if the shared memory pre-exists. |
michael@0 | 72 | if (GetLastError() == ERROR_ALREADY_EXISTS && !open_existing) { |
michael@0 | 73 | Close(); |
michael@0 | 74 | return false; |
michael@0 | 75 | } |
michael@0 | 76 | max_size_ = size; |
michael@0 | 77 | return true; |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | bool SharedMemory::Delete(const std::wstring& name) { |
michael@0 | 81 | // intentionally empty -- there is nothing for us to do on Windows. |
michael@0 | 82 | return true; |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | bool SharedMemory::Open(const std::wstring &name, bool read_only) { |
michael@0 | 86 | DCHECK(mapped_file_ == NULL); |
michael@0 | 87 | |
michael@0 | 88 | name_ = name; |
michael@0 | 89 | read_only_ = read_only; |
michael@0 | 90 | mapped_file_ = OpenFileMapping( |
michael@0 | 91 | read_only_ ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS, false, |
michael@0 | 92 | name.empty() ? NULL : name.c_str()); |
michael@0 | 93 | if (mapped_file_ != NULL) { |
michael@0 | 94 | // Note: size_ is not set in this case. |
michael@0 | 95 | return true; |
michael@0 | 96 | } |
michael@0 | 97 | return false; |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | bool SharedMemory::Map(size_t bytes) { |
michael@0 | 101 | if (mapped_file_ == NULL) |
michael@0 | 102 | return false; |
michael@0 | 103 | |
michael@0 | 104 | memory_ = MapViewOfFile(mapped_file_, |
michael@0 | 105 | read_only_ ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS, 0, 0, bytes); |
michael@0 | 106 | if (memory_ != NULL) { |
michael@0 | 107 | return true; |
michael@0 | 108 | } |
michael@0 | 109 | return false; |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | bool SharedMemory::Unmap() { |
michael@0 | 113 | if (memory_ == NULL) |
michael@0 | 114 | return false; |
michael@0 | 115 | |
michael@0 | 116 | UnmapViewOfFile(memory_); |
michael@0 | 117 | memory_ = NULL; |
michael@0 | 118 | return true; |
michael@0 | 119 | } |
michael@0 | 120 | |
michael@0 | 121 | bool SharedMemory::ShareToProcessCommon(ProcessHandle process, |
michael@0 | 122 | SharedMemoryHandle *new_handle, |
michael@0 | 123 | bool close_self) { |
michael@0 | 124 | *new_handle = 0; |
michael@0 | 125 | DWORD access = STANDARD_RIGHTS_REQUIRED | FILE_MAP_READ; |
michael@0 | 126 | DWORD options = 0; |
michael@0 | 127 | HANDLE mapped_file = mapped_file_; |
michael@0 | 128 | HANDLE result; |
michael@0 | 129 | if (!read_only_) |
michael@0 | 130 | access |= FILE_MAP_WRITE; |
michael@0 | 131 | if (close_self) { |
michael@0 | 132 | // DUPLICATE_CLOSE_SOURCE causes DuplicateHandle to close mapped_file. |
michael@0 | 133 | options = DUPLICATE_CLOSE_SOURCE; |
michael@0 | 134 | mapped_file_ = NULL; |
michael@0 | 135 | Unmap(); |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | if (process == GetCurrentProcess() && close_self) { |
michael@0 | 139 | *new_handle = mapped_file; |
michael@0 | 140 | return true; |
michael@0 | 141 | } |
michael@0 | 142 | |
michael@0 | 143 | if (!DuplicateHandle(GetCurrentProcess(), mapped_file, process, |
michael@0 | 144 | &result, access, FALSE, options)) |
michael@0 | 145 | return false; |
michael@0 | 146 | *new_handle = result; |
michael@0 | 147 | return true; |
michael@0 | 148 | } |
michael@0 | 149 | |
michael@0 | 150 | |
michael@0 | 151 | void SharedMemory::Close() { |
michael@0 | 152 | if (memory_ != NULL) { |
michael@0 | 153 | UnmapViewOfFile(memory_); |
michael@0 | 154 | memory_ = NULL; |
michael@0 | 155 | } |
michael@0 | 156 | |
michael@0 | 157 | if (mapped_file_ != NULL) { |
michael@0 | 158 | CloseHandle(mapped_file_); |
michael@0 | 159 | mapped_file_ = NULL; |
michael@0 | 160 | } |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | void SharedMemory::Lock() { |
michael@0 | 164 | if (lock_ == NULL) { |
michael@0 | 165 | std::wstring name = name_; |
michael@0 | 166 | name.append(L"lock"); |
michael@0 | 167 | lock_ = CreateMutex(NULL, FALSE, name.c_str()); |
michael@0 | 168 | DCHECK(lock_ != NULL); |
michael@0 | 169 | if (lock_ == NULL) { |
michael@0 | 170 | DLOG(ERROR) << "Could not create mutex" << GetLastError(); |
michael@0 | 171 | return; // there is nothing good we can do here. |
michael@0 | 172 | } |
michael@0 | 173 | } |
michael@0 | 174 | WaitForSingleObject(lock_, INFINITE); |
michael@0 | 175 | } |
michael@0 | 176 | |
michael@0 | 177 | void SharedMemory::Unlock() { |
michael@0 | 178 | DCHECK(lock_ != NULL); |
michael@0 | 179 | ReleaseMutex(lock_); |
michael@0 | 180 | } |
michael@0 | 181 | |
michael@0 | 182 | SharedMemoryHandle SharedMemory::handle() const { |
michael@0 | 183 | return mapped_file_; |
michael@0 | 184 | } |
michael@0 | 185 | |
michael@0 | 186 | } // namespace base |