diff options
author | burnik <burnik@chromium.org> | 2014-09-07 23:58:22 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2014-09-08 07:10:25 +0000 |
commit | 3d67005036e98d91a329e5f70dbc4f1ddb27a1ab (patch) | |
tree | 6da0575c5699e1b8f8f3451c3acfa08b26f3291a /base/sync_socket.h | |
parent | 4189453237c2ef570d07e132c72b171664a19e51 (diff) | |
download | chromium_src-3d67005036e98d91a329e5f70dbc4f1ddb27a1ab.zip chromium_src-3d67005036e98d91a329e5f70dbc4f1ddb27a1ab.tar.gz chromium_src-3d67005036e98d91a329e5f70dbc4f1ddb27a1ab.tar.bz2 |
Preparing |SyncSocket|'s handle for the peer process is different for POSIX and Windows
which leads to code duplication, platform #ifdef checks on multiple levels and
general confusion on how to work with the SyncSocket.
Typical use case for |SyncSocket|:
1. Browser creates and connects the socket pair - one for browser and one for renderer.
2. Browser prepares the foreign socket (Windows duplicates, POSIX creates file descriptor).
3. Browser relays the foreign socket handle to the renderer via IPC.
4. Renderer receives the IPC and creates the socket using the handle provided.
5. Sockets are ready for send/receive on both ends.
Steps 1-4 get simplified since there is no need to check the platform in order to prepare the socket for transit.
What this CL brings:
1. Adds |SyncSocket::TransitDescriptor| type which wraps the socket handle and is cross-platform.
2. Adds |SyncSocket::PrepareTransitDescriptor| method which is implemented depending on the platform.
3. Adds |SyncSocket::UnwrapHandle| method which unwraps |SyncSocket::Handle| from |SyncSocket::TransitDescriptor|.
4. Removes #ifdefs for platform-checks in code using |SyncSocket| and simplifies preparing the SyncSocket.
Note:
- There is still some less evident duplication in the ppapi and pepper-broker code which should be addressed.
- SyncSocket unit test should also be reviewed.
- There is a similar pattern when using SharedMemory.
BUG=409656
Review URL: https://codereview.chromium.org/525313002
Cr-Commit-Position: refs/heads/master@{#293680}
Diffstat (limited to 'base/sync_socket.h')
-rw-r--r-- | base/sync_socket.h | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/base/sync_socket.h b/base/sync_socket.h index 71addd1..9923591 100644 --- a/base/sync_socket.h +++ b/base/sync_socket.h @@ -17,17 +17,24 @@ #include "base/base_export.h" #include "base/compiler_specific.h" +#include "base/process/process_handle.h" #include "base/synchronization/waitable_event.h" #include "base/time/time.h" +#if defined(OS_POSIX) +#include "base/file_descriptor_posix.h" +#endif + namespace base { class BASE_EXPORT SyncSocket { public: #if defined(OS_WIN) typedef HANDLE Handle; + typedef Handle TransitDescriptor; #else typedef int Handle; + typedef FileDescriptor TransitDescriptor; #endif static const Handle kInvalidHandle; @@ -42,6 +49,15 @@ class BASE_EXPORT SyncSocket { // return, the sockets will both be valid and connected. static bool CreatePair(SyncSocket* socket_a, SyncSocket* socket_b); + // Returns |Handle| wrapped in a |TransitDescriptor|. + static Handle UnwrapHandle(const TransitDescriptor& descriptor); + + // Prepares a |TransitDescriptor| which wraps |Handle| used for transit. + // This is used to prepare the underlying shared resource before passing back + // the handle to be used by the peer process. + bool PrepareTransitDescriptor(ProcessHandle peer_process_handle, + TransitDescriptor* descriptor); + // Closes the SyncSocket. Returns true on success, false on failure. virtual bool Close(); |