Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | // Copyright (c) 2010 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 SANDBOX_SRC_SHARED_HANDLES_H__ |
michael@0 | 6 | #define SANDBOX_SRC_SHARED_HANDLES_H__ |
michael@0 | 7 | |
michael@0 | 8 | #include "base/basictypes.h" |
michael@0 | 9 | |
michael@0 | 10 | #ifndef HANDLE |
michael@0 | 11 | // We can provide our own windows compatilble handle definition, but |
michael@0 | 12 | // in general we want to rely on the client of this api to include |
michael@0 | 13 | // the proper windows headers. Note that we don't want to bring the |
michael@0 | 14 | // whole <windows.h> into scope if we don't have to. |
michael@0 | 15 | typedef void* HANDLE; |
michael@0 | 16 | #endif |
michael@0 | 17 | |
michael@0 | 18 | namespace sandbox { |
michael@0 | 19 | |
michael@0 | 20 | // SharedHandles is a simple class to stash and find windows object handles |
michael@0 | 21 | // given a raw block of memory which is shared between two processes. |
michael@0 | 22 | // It addresses the need to communicate a handle value between two windows |
michael@0 | 23 | // processes given that they are already sharing some memory. |
michael@0 | 24 | // |
michael@0 | 25 | // This class is not exposed directly to users of the sanbox API, instead |
michael@0 | 26 | // we expose the wrapper methods TargetProcess::TransferHandle( ) and |
michael@0 | 27 | // TargetServices::GetTransferHandle() |
michael@0 | 28 | // |
michael@0 | 29 | // Use it for a small number of items, since internaly uses linear seach |
michael@0 | 30 | // |
michael@0 | 31 | // The use is very simple. Given a shared memory between proces A and B: |
michael@0 | 32 | // process A: |
michael@0 | 33 | // HANDLE handle = SomeFunction(..); |
michael@0 | 34 | // SharedHandles shared_handes; |
michael@0 | 35 | // shared_handles.Init(memory) |
michael@0 | 36 | // shared_handles.SetHandle(3, handle); |
michael@0 | 37 | // |
michael@0 | 38 | // process B: |
michael@0 | 39 | // SharedHandles shared_handes; |
michael@0 | 40 | // shared_handles.Init(memory) |
michael@0 | 41 | // HANDLE handle = shared_handles.GetHandle(3); |
michael@0 | 42 | // |
michael@0 | 43 | // Note that '3' in this example is a unique id, that must be agreed before |
michael@0 | 44 | // transfer |
michael@0 | 45 | // |
michael@0 | 46 | // Note2: While this class can be used in a single process, there are |
michael@0 | 47 | // better alternatives such as STL |
michael@0 | 48 | // |
michael@0 | 49 | // Note3: Under windows a kernel object handle in one process does not |
michael@0 | 50 | // make sense for another process unless there is a DuplicateHandle( ) |
michael@0 | 51 | // call involved which this class DOES NOT do that for you. |
michael@0 | 52 | // |
michael@0 | 53 | // Note4: Under windows, shared memory when created is initialized to |
michael@0 | 54 | // zeros always. If you are not using shared memory it is your responsability |
michael@0 | 55 | // to zero it for the setter process and to copy it to the getter process. |
michael@0 | 56 | class SharedHandles { |
michael@0 | 57 | public: |
michael@0 | 58 | SharedHandles(); |
michael@0 | 59 | |
michael@0 | 60 | // Initializes the shared memory for use. |
michael@0 | 61 | // Pass the shared memory base and size. It will internally compute |
michael@0 | 62 | // how many handles can it store. If initialization fails the return value |
michael@0 | 63 | // is false. |
michael@0 | 64 | bool Init(void* raw_mem, size_t size_bytes); |
michael@0 | 65 | |
michael@0 | 66 | // Sets a handle in the shared memory for transfer. |
michael@0 | 67 | // Parameters: |
michael@0 | 68 | // tag : an integer, different from zero that uniquely identfies the |
michael@0 | 69 | // handle to transfer. |
michael@0 | 70 | // handle: the handle value associated with 'tag' to tranfer |
michael@0 | 71 | // Returns false if there is not enough space in the shared memory for |
michael@0 | 72 | // this handle. |
michael@0 | 73 | bool SetHandle(uint32 tag, HANDLE handle); |
michael@0 | 74 | |
michael@0 | 75 | // Gets a handle previously stored by SetHandle. |
michael@0 | 76 | // Parameters: |
michael@0 | 77 | // tag: an integer different from zero that uniquely identfies the handle |
michael@0 | 78 | // to retrieve. |
michael@0 | 79 | // *handle: output handle value if the call was succesful. |
michael@0 | 80 | // If a handle with the provided tag is not found the return value is false. |
michael@0 | 81 | // If the tag is found the return value is true. |
michael@0 | 82 | bool GetHandle(uint32 tag, HANDLE* handle); |
michael@0 | 83 | |
michael@0 | 84 | private: |
michael@0 | 85 | // A single item is the tuple handle/tag |
michael@0 | 86 | struct SharedItem { |
michael@0 | 87 | uint32 tag; |
michael@0 | 88 | void* item; |
michael@0 | 89 | }; |
michael@0 | 90 | |
michael@0 | 91 | // SharedMem is used to layout the memory as an array of SharedItems |
michael@0 | 92 | struct SharedMem { |
michael@0 | 93 | size_t max_items; |
michael@0 | 94 | SharedItem* items; |
michael@0 | 95 | }; |
michael@0 | 96 | |
michael@0 | 97 | // Finds an Item tuple provided the handle tag. |
michael@0 | 98 | // Uses linear search because we expect the number of handles to be |
michael@0 | 99 | // small (say less than ~100). |
michael@0 | 100 | SharedItem* FindByTag(uint32 tag); |
michael@0 | 101 | |
michael@0 | 102 | SharedMem shared_; |
michael@0 | 103 | DISALLOW_COPY_AND_ASSIGN(SharedHandles); |
michael@0 | 104 | }; |
michael@0 | 105 | |
michael@0 | 106 | } // namespace sandbox |
michael@0 | 107 | |
michael@0 | 108 | #endif // SANDBOX_SRC_SHARED_HANDLES_H__ |