Thu, 15 Jan 2015 21:03:48 +0100
Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)
michael@0 | 1 | // Copyright (c) 2006-2009 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 | #ifndef CHROME_COMMON_TRANSPORT_DIB_H_ |
michael@0 | 6 | #define CHROME_COMMON_TRANSPORT_DIB_H_ |
michael@0 | 7 | |
michael@0 | 8 | #include "base/basictypes.h" |
michael@0 | 9 | |
michael@0 | 10 | #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_BSD) |
michael@0 | 11 | #include "base/shared_memory.h" |
michael@0 | 12 | #endif |
michael@0 | 13 | |
michael@0 | 14 | #if defined(OS_WIN) |
michael@0 | 15 | #include <windows.h> |
michael@0 | 16 | #elif defined(OS_LINUX) |
michael@0 | 17 | #include "chrome/common/x11_util.h" |
michael@0 | 18 | #endif |
michael@0 | 19 | |
michael@0 | 20 | // ----------------------------------------------------------------------------- |
michael@0 | 21 | // A TransportDIB is a block of memory that is used to transport pixels |
michael@0 | 22 | // between processes: from the renderer process to the browser, and |
michael@0 | 23 | // between renderer and plugin processes. |
michael@0 | 24 | // ----------------------------------------------------------------------------- |
michael@0 | 25 | class TransportDIB { |
michael@0 | 26 | public: |
michael@0 | 27 | ~TransportDIB(); |
michael@0 | 28 | |
michael@0 | 29 | // Two typedefs are defined. A Handle is the type which can be sent over |
michael@0 | 30 | // the wire so that the remote side can map the transport DIB. The Id typedef |
michael@0 | 31 | // is sufficient to identify the transport DIB when you know that the remote |
michael@0 | 32 | // side already may have it mapped. |
michael@0 | 33 | #if defined(OS_WIN) |
michael@0 | 34 | typedef HANDLE Handle; |
michael@0 | 35 | // On Windows, the Id type includes a sequence number (epoch) to solve an ABA |
michael@0 | 36 | // issue: |
michael@0 | 37 | // 1) Process A creates a transport DIB with HANDLE=1 and sends to B. |
michael@0 | 38 | // 2) Process B maps the transport DIB and caches 1 -> DIB. |
michael@0 | 39 | // 3) Process A closes the transport DIB and creates a new one. The new DIB |
michael@0 | 40 | // is also assigned HANDLE=1. |
michael@0 | 41 | // 4) Process A sends the Handle to B, but B incorrectly believes that it |
michael@0 | 42 | // already has it cached. |
michael@0 | 43 | struct HandleAndSequenceNum { |
michael@0 | 44 | HandleAndSequenceNum() |
michael@0 | 45 | : handle(NULL), |
michael@0 | 46 | sequence_num(0) { |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | HandleAndSequenceNum(HANDLE h, uint32_t seq_num) |
michael@0 | 50 | : handle(h), |
michael@0 | 51 | sequence_num(seq_num) { |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | bool operator< (const HandleAndSequenceNum& other) const { |
michael@0 | 55 | // Use the lexicographic order on the tuple <handle, sequence_num>. |
michael@0 | 56 | if (other.handle != handle) |
michael@0 | 57 | return other.handle < handle; |
michael@0 | 58 | return other.sequence_num < sequence_num; |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | HANDLE handle; |
michael@0 | 62 | uint32_t sequence_num; |
michael@0 | 63 | }; |
michael@0 | 64 | typedef HandleAndSequenceNum Id; |
michael@0 | 65 | #elif defined(OS_MACOSX) || defined(OS_BSD) |
michael@0 | 66 | typedef base::SharedMemoryHandle Handle; |
michael@0 | 67 | // On Mac, the inode number of the backing file is used as an id. |
michael@0 | 68 | typedef base::SharedMemoryId Id; |
michael@0 | 69 | #elif defined(OS_LINUX) |
michael@0 | 70 | typedef int Handle; // These two ints are SysV IPC shared memory keys |
michael@0 | 71 | typedef int Id; |
michael@0 | 72 | #endif |
michael@0 | 73 | |
michael@0 | 74 | // Create a new TransportDIB |
michael@0 | 75 | // size: the minimum size, in bytes |
michael@0 | 76 | // epoch: Windows only: a global counter. See comment above. |
michael@0 | 77 | // returns: NULL on failure |
michael@0 | 78 | static TransportDIB* Create(size_t size, uint32_t sequence_num); |
michael@0 | 79 | |
michael@0 | 80 | // Map the referenced transport DIB. Returns NULL on failure. |
michael@0 | 81 | static TransportDIB* Map(Handle transport_dib); |
michael@0 | 82 | |
michael@0 | 83 | // Return a pointer to the shared memory |
michael@0 | 84 | void* memory() const; |
michael@0 | 85 | |
michael@0 | 86 | // Return the maximum size of the shared memory. This is not the amount of |
michael@0 | 87 | // data which is valid, you have to know that via other means, this is simply |
michael@0 | 88 | // the maximum amount that /could/ be valid. |
michael@0 | 89 | size_t size() const { return size_; } |
michael@0 | 90 | |
michael@0 | 91 | // Return the identifier which can be used to refer to this shared memory |
michael@0 | 92 | // on the wire. |
michael@0 | 93 | Id id() const; |
michael@0 | 94 | |
michael@0 | 95 | // Return a handle to the underlying shared memory. This can be sent over the |
michael@0 | 96 | // wire to give this transport DIB to another process. |
michael@0 | 97 | Handle handle() const; |
michael@0 | 98 | |
michael@0 | 99 | #if defined(OS_LINUX) |
michael@0 | 100 | // Map the shared memory into the X server and return an id for the shared |
michael@0 | 101 | // segment. |
michael@0 | 102 | XID MapToX(Display* connection); |
michael@0 | 103 | #endif |
michael@0 | 104 | |
michael@0 | 105 | private: |
michael@0 | 106 | TransportDIB(); |
michael@0 | 107 | #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_BSD) |
michael@0 | 108 | explicit TransportDIB(base::SharedMemoryHandle dib); |
michael@0 | 109 | base::SharedMemory shared_memory_; |
michael@0 | 110 | #elif defined(OS_LINUX) |
michael@0 | 111 | int key_; // SysV shared memory id |
michael@0 | 112 | void* address_; // mapped address |
michael@0 | 113 | XID x_shm_; // X id for the shared segment |
michael@0 | 114 | Display* display_; // connection to the X server |
michael@0 | 115 | #endif |
michael@0 | 116 | #ifdef OS_WIN |
michael@0 | 117 | uint32_t sequence_num_; |
michael@0 | 118 | #endif |
michael@0 | 119 | size_t size_; // length, in bytes |
michael@0 | 120 | }; |
michael@0 | 121 | |
michael@0 | 122 | class MessageLoop; |
michael@0 | 123 | |
michael@0 | 124 | #endif // CHROME_COMMON_TRANSPORT_DIB_H_ |