summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorerikchen <erikchen@chromium.org>2015-06-02 17:26:59 -0700
committerCommit bot <commit-bot@chromium.org>2015-06-03 00:27:22 +0000
commit2096f624841f5c4c3a54163178ee0e6e2caacdcf (patch)
tree3afa552c621547b7cefae559e8d7bffefda6c556
parent6bb8de49e503e4b53e7822418e682ade926c070f (diff)
downloadchromium_src-2096f624841f5c4c3a54163178ee0e6e2caacdcf.zip
chromium_src-2096f624841f5c4c3a54163178ee0e6e2caacdcf.tar.gz
chromium_src-2096f624841f5c4c3a54163178ee0e6e2caacdcf.tar.bz2
Several small changes to base::SharedMemory.
- Removed ShallowCopyHandle(). - Renamed DeepCopyHandle() -> DuplicateHandle(). - Changed the signature of DuplicateHandle() to not accept |clean_up_resources_on_destruction|. (The flag has no effect on base::SharedMemory). - Defined DuplicateHandle() on Win, POSIX, and Nacl. BUG=466437 Review URL: https://codereview.chromium.org/1160503004 Cr-Commit-Position: refs/heads/master@{#332506}
-rw-r--r--base/memory/shared_memory.h14
-rw-r--r--base/memory/shared_memory_nacl.cc9
-rw-r--r--base/memory/shared_memory_posix.cc13
-rw-r--r--base/memory/shared_memory_win.cc16
-rw-r--r--content/renderer/npapi/webplugin_delegate_proxy.cc2
-rw-r--r--content/renderer/pepper/pepper_compositor_host.cc14
-rw-r--r--gpu/command_buffer/service/in_process_command_buffer.cc18
7 files changed, 32 insertions, 54 deletions
diff --git a/base/memory/shared_memory.h b/base/memory/shared_memory.h
index 0ea75b9..258a926 100644
--- a/base/memory/shared_memory.h
+++ b/base/memory/shared_memory.h
@@ -116,19 +116,11 @@ class BASE_EXPORT SharedMemory {
// Returns the maximum number of handles that can be open at once per process.
static size_t GetHandleLimit();
- // The copy shares the same underlying OS primitives. The new
- // SharedMemoryHandle will not clean up the OS primitives when destroyed. The
- // original must outlive the copy.
- static SharedMemoryHandle ShallowCopyHandle(const SharedMemoryHandle& handle);
+ // Duplicates The underlying OS primitive. Returns NULLHandle() on failure.
+ // The caller is responsible for destroying the duplicated OS primitive.
+ static SharedMemoryHandle DuplicateHandle(const SharedMemoryHandle& handle);
#if defined(OS_POSIX)
- // The underlying OS primitives are duplicated.
- // |clean_up_resources_on_destruction| indicates whether the underlying OS
- // primitives are cleaned up on destruction.
- static SharedMemoryHandle DeepCopyHandle(
- const SharedMemoryHandle& handle,
- bool clean_up_resources_on_destruction);
-
// This method requires that the SharedMemoryHandle is backed by a POSIX fd.
static int GetFdFromSharedMemoryHandle(const SharedMemoryHandle& handle);
#endif
diff --git a/base/memory/shared_memory_nacl.cc b/base/memory/shared_memory_nacl.cc
index 8435b2b..2b7e9f7 100644
--- a/base/memory/shared_memory_nacl.cc
+++ b/base/memory/shared_memory_nacl.cc
@@ -67,6 +67,15 @@ void SharedMemory::CloseHandle(const SharedMemoryHandle& handle) {
DPLOG(ERROR) << "close";
}
+// static
+SharedMemoryHandle SharedMemory::DuplicateHandle(
+ const SharedMemoryHandle& handle) {
+ int duped_handle = HANDLE_EINTR(dup(handle.fd));
+ if (duped_handle < 0)
+ return base::SharedMemory::NULLHandle();
+ return base::FileDescriptor(duped_handle, true);
+}
+
bool SharedMemory::CreateAndMapAnonymous(size_t size) {
// Untrusted code can't create descriptors or handles.
return false;
diff --git a/base/memory/shared_memory_posix.cc b/base/memory/shared_memory_posix.cc
index 613c26f..430ae0e 100644
--- a/base/memory/shared_memory_posix.cc
+++ b/base/memory/shared_memory_posix.cc
@@ -247,21 +247,12 @@ size_t SharedMemory::GetHandleLimit() {
}
// static
-SharedMemoryHandle SharedMemory::ShallowCopyHandle(
+SharedMemoryHandle SharedMemory::DuplicateHandle(
const SharedMemoryHandle& handle) {
- SharedMemoryHandle new_handle = handle;
- new_handle.auto_close = false;
- return new_handle;
-}
-
-// static
-SharedMemoryHandle SharedMemory::DeepCopyHandle(
- const SharedMemoryHandle& handle,
- bool clean_up_resources_on_destruction) {
int duped_handle = HANDLE_EINTR(dup(handle.fd));
if (duped_handle < 0)
return base::SharedMemory::NULLHandle();
- return base::FileDescriptor(duped_handle, clean_up_resources_on_destruction);
+ return base::FileDescriptor(duped_handle, true);
}
// static
diff --git a/base/memory/shared_memory_win.cc b/base/memory/shared_memory_win.cc
index 2b59fab..eacf0d6 100644
--- a/base/memory/shared_memory_win.cc
+++ b/base/memory/shared_memory_win.cc
@@ -100,9 +100,16 @@ size_t SharedMemory::GetHandleLimit() {
return static_cast<size_t>(1 << 23);
}
-SharedMemoryHandle SharedMemory::ShallowCopyHandle(
+// static
+SharedMemoryHandle SharedMemory::DuplicateHandle(
const SharedMemoryHandle& handle) {
- return handle;
+ ProcessHandle process = GetCurrentProcess();
+ SharedMemoryHandle duped_handle;
+ BOOL success = ::DuplicateHandle(process, handle, process, &duped_handle, 0,
+ FALSE, DUPLICATE_SAME_ACCESS);
+ if (success)
+ return duped_handle;
+ return NULLHandle();
}
bool SharedMemory::CreateAndMapAnonymous(size_t size) {
@@ -247,9 +254,10 @@ bool SharedMemory::ShareToProcessCommon(ProcessHandle process,
return true;
}
- if (!DuplicateHandle(GetCurrentProcess(), mapped_file, process,
- &result, access, FALSE, options))
+ if (!::DuplicateHandle(GetCurrentProcess(), mapped_file, process, &result,
+ access, FALSE, options)) {
return false;
+ }
*new_handle = result;
return true;
}
diff --git a/content/renderer/npapi/webplugin_delegate_proxy.cc b/content/renderer/npapi/webplugin_delegate_proxy.cc
index 800ce12..a563d55 100644
--- a/content/renderer/npapi/webplugin_delegate_proxy.cc
+++ b/content/renderer/npapi/webplugin_delegate_proxy.cc
@@ -498,7 +498,7 @@ static void CopySharedMemoryHandleForMessage(
base::SharedMemoryHandle* handle_out,
base::ProcessId peer_pid) {
#if defined(OS_POSIX)
- *handle_out = base::SharedMemory::DeepCopyHandle(handle_in, true);
+ *handle_out = base::SharedMemory::DuplicateHandle(handle_in);
#elif defined(OS_WIN)
// On Windows we need to duplicate the handle for the plugin process.
*handle_out = NULL;
diff --git a/content/renderer/pepper/pepper_compositor_host.cc b/content/renderer/pepper/pepper_compositor_host.cc
index 80b2ef6..c2d4685 100644
--- a/content/renderer/pepper/pepper_compositor_host.cc
+++ b/content/renderer/pepper/pepper_compositor_host.cc
@@ -121,17 +121,11 @@ int32_t VerifyCommittedLayer(
if (enter.object()->GetSharedMemory(&shm, &byte_count) != PP_OK)
return PP_ERROR_FAILED;
-#if defined(OS_WIN)
- base::SharedMemoryHandle shm_handle;
- if (!::DuplicateHandle(::GetCurrentProcess(), shm->handle(),
- ::GetCurrentProcess(), &shm_handle, 0, FALSE,
- DUPLICATE_SAME_ACCESS)) {
- return PP_ERROR_FAILED;
- }
-#else
base::SharedMemoryHandle shm_handle =
- base::SharedMemory::DeepCopyHandle(shm->handle(), false);
-#endif
+ base::SharedMemory::DuplicateHandle(shm->handle());
+ if (!base::SharedMemory::IsHandleValid(shm_handle))
+ return PP_ERROR_FAILED;
+
image_shm->reset(new base::SharedMemory(shm_handle, true));
if (!(*image_shm)->Map(desc.stride * desc.size.height)) {
image_shm->reset();
diff --git a/gpu/command_buffer/service/in_process_command_buffer.cc b/gpu/command_buffer/service/in_process_command_buffer.cc
index 9e1f3e5..5b325ab 100644
--- a/gpu/command_buffer/service/in_process_command_buffer.cc
+++ b/gpu/command_buffer/service/in_process_command_buffer.cc
@@ -196,23 +196,7 @@ base::LazyInstance<SyncPointManagerWrapper> g_sync_point_manager =
base::SharedMemoryHandle ShareToGpuThread(
base::SharedMemoryHandle source_handle) {
-#if defined(OS_WIN)
- // Windows needs to explicitly duplicate the handle to current process.
- base::SharedMemoryHandle target_handle;
- if (!DuplicateHandle(GetCurrentProcess(),
- source_handle,
- GetCurrentProcess(),
- &target_handle,
- FILE_GENERIC_READ | FILE_GENERIC_WRITE,
- FALSE,
- 0)) {
- return base::SharedMemory::NULLHandle();
- }
-
- return target_handle;
-#else
- return base::SharedMemory::DeepCopyHandle(source_handle, true);
-#endif
+ return base::SharedMemory::DuplicateHandle(source_handle);
}
gfx::GpuMemoryBufferHandle ShareGpuMemoryBufferToGpuThread(