1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/ipc/chromium/src/chrome/common/transport_dib.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,124 @@ 1.4 +// Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. 1.5 +// Use of this source code is governed by a BSD-style license that can be 1.6 +// found in the LICENSE file. 1.7 + 1.8 +#ifndef CHROME_COMMON_TRANSPORT_DIB_H_ 1.9 +#define CHROME_COMMON_TRANSPORT_DIB_H_ 1.10 + 1.11 +#include "base/basictypes.h" 1.12 + 1.13 +#if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_BSD) 1.14 +#include "base/shared_memory.h" 1.15 +#endif 1.16 + 1.17 +#if defined(OS_WIN) 1.18 +#include <windows.h> 1.19 +#elif defined(OS_LINUX) 1.20 +#include "chrome/common/x11_util.h" 1.21 +#endif 1.22 + 1.23 +// ----------------------------------------------------------------------------- 1.24 +// A TransportDIB is a block of memory that is used to transport pixels 1.25 +// between processes: from the renderer process to the browser, and 1.26 +// between renderer and plugin processes. 1.27 +// ----------------------------------------------------------------------------- 1.28 +class TransportDIB { 1.29 + public: 1.30 + ~TransportDIB(); 1.31 + 1.32 + // Two typedefs are defined. A Handle is the type which can be sent over 1.33 + // the wire so that the remote side can map the transport DIB. The Id typedef 1.34 + // is sufficient to identify the transport DIB when you know that the remote 1.35 + // side already may have it mapped. 1.36 +#if defined(OS_WIN) 1.37 + typedef HANDLE Handle; 1.38 + // On Windows, the Id type includes a sequence number (epoch) to solve an ABA 1.39 + // issue: 1.40 + // 1) Process A creates a transport DIB with HANDLE=1 and sends to B. 1.41 + // 2) Process B maps the transport DIB and caches 1 -> DIB. 1.42 + // 3) Process A closes the transport DIB and creates a new one. The new DIB 1.43 + // is also assigned HANDLE=1. 1.44 + // 4) Process A sends the Handle to B, but B incorrectly believes that it 1.45 + // already has it cached. 1.46 + struct HandleAndSequenceNum { 1.47 + HandleAndSequenceNum() 1.48 + : handle(NULL), 1.49 + sequence_num(0) { 1.50 + } 1.51 + 1.52 + HandleAndSequenceNum(HANDLE h, uint32_t seq_num) 1.53 + : handle(h), 1.54 + sequence_num(seq_num) { 1.55 + } 1.56 + 1.57 + bool operator< (const HandleAndSequenceNum& other) const { 1.58 + // Use the lexicographic order on the tuple <handle, sequence_num>. 1.59 + if (other.handle != handle) 1.60 + return other.handle < handle; 1.61 + return other.sequence_num < sequence_num; 1.62 + } 1.63 + 1.64 + HANDLE handle; 1.65 + uint32_t sequence_num; 1.66 + }; 1.67 + typedef HandleAndSequenceNum Id; 1.68 +#elif defined(OS_MACOSX) || defined(OS_BSD) 1.69 + typedef base::SharedMemoryHandle Handle; 1.70 + // On Mac, the inode number of the backing file is used as an id. 1.71 + typedef base::SharedMemoryId Id; 1.72 +#elif defined(OS_LINUX) 1.73 + typedef int Handle; // These two ints are SysV IPC shared memory keys 1.74 + typedef int Id; 1.75 +#endif 1.76 + 1.77 + // Create a new TransportDIB 1.78 + // size: the minimum size, in bytes 1.79 + // epoch: Windows only: a global counter. See comment above. 1.80 + // returns: NULL on failure 1.81 + static TransportDIB* Create(size_t size, uint32_t sequence_num); 1.82 + 1.83 + // Map the referenced transport DIB. Returns NULL on failure. 1.84 + static TransportDIB* Map(Handle transport_dib); 1.85 + 1.86 + // Return a pointer to the shared memory 1.87 + void* memory() const; 1.88 + 1.89 + // Return the maximum size of the shared memory. This is not the amount of 1.90 + // data which is valid, you have to know that via other means, this is simply 1.91 + // the maximum amount that /could/ be valid. 1.92 + size_t size() const { return size_; } 1.93 + 1.94 + // Return the identifier which can be used to refer to this shared memory 1.95 + // on the wire. 1.96 + Id id() const; 1.97 + 1.98 + // Return a handle to the underlying shared memory. This can be sent over the 1.99 + // wire to give this transport DIB to another process. 1.100 + Handle handle() const; 1.101 + 1.102 +#if defined(OS_LINUX) 1.103 + // Map the shared memory into the X server and return an id for the shared 1.104 + // segment. 1.105 + XID MapToX(Display* connection); 1.106 +#endif 1.107 + 1.108 + private: 1.109 + TransportDIB(); 1.110 +#if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_BSD) 1.111 + explicit TransportDIB(base::SharedMemoryHandle dib); 1.112 + base::SharedMemory shared_memory_; 1.113 +#elif defined(OS_LINUX) 1.114 + int key_; // SysV shared memory id 1.115 + void* address_; // mapped address 1.116 + XID x_shm_; // X id for the shared segment 1.117 + Display* display_; // connection to the X server 1.118 +#endif 1.119 +#ifdef OS_WIN 1.120 + uint32_t sequence_num_; 1.121 +#endif 1.122 + size_t size_; // length, in bytes 1.123 +}; 1.124 + 1.125 +class MessageLoop; 1.126 + 1.127 +#endif // CHROME_COMMON_TRANSPORT_DIB_H_