Sandbox: remove raw handles from SharedMemIPCServer::ServerControl
[chromium-blink-merge.git] / sandbox / win / src / sharedmem_ipc_server.h
blob8d776d506194ba9bf07e804442ee5c138903fccf
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef SANDBOX_SRC_SHAREDMEM_IPC_SERVER_H_
6 #define SANDBOX_SRC_SHAREDMEM_IPC_SERVER_H_
8 #include <list>
10 #include "base/basictypes.h"
11 #include "base/gtest_prod_util.h"
12 #include "base/win/scoped_handle.h"
13 #include "sandbox/win/src/crosscall_params.h"
14 #include "sandbox/win/src/crosscall_server.h"
15 #include "sandbox/win/src/sharedmem_ipc_client.h"
17 // IPC transport implementation that uses shared memory.
18 // This is the server side
20 // The server side has knowledge about the layout of the shared memory
21 // and the state transitions. Both are explained in sharedmem_ipc_client.h
23 // As opposed to SharedMemIPClient, the Server object should be one for the
24 // entire lifetime of the target process. The server is in charge of creating
25 // the events (ping, pong) both for the client and for the target that are used
26 // to signal the IPC and also in charge of setting the initial state of the
27 // channels.
29 // When an IPC is ready, the server relies on being called by on the
30 // ThreadPingEventReady callback. The IPC server then retrieves the buffer,
31 // marshals it into a CrossCallParam object and calls the Dispatcher, who is in
32 // charge of fulfilling the IPC request.
33 namespace sandbox {
35 // the shared memory implementation of the IPC server. There should be one
36 // of these objects per target (IPC client) process
37 class SharedMemIPCServer {
38 public:
39 // Creates the IPC server.
40 // target_process: handle to the target process. It must be suspended.
41 // target_process_id: process id of the target process.
42 // target_job: the job object handle associated with the target process.
43 // thread_provider: a thread provider object.
44 // dispatcher: an object that can service IPC calls.
45 SharedMemIPCServer(HANDLE target_process, DWORD target_process_id,
46 HANDLE target_job, ThreadProvider* thread_provider,
47 Dispatcher* dispatcher);
49 ~SharedMemIPCServer();
51 // Initializes the server structures, shared memory structures and
52 // creates the kernels events used to signal the IPC.
53 bool Init(void* shared_mem, uint32 shared_size, uint32 channel_size);
55 private:
56 // Allow tests to be marked DISABLED_. Note that FLAKY_ and FAILS_ prefixes
57 // do not work with sandbox tests.
58 FRIEND_TEST_ALL_PREFIXES(IPCTest, SharedMemServerTests);
59 // When an event fires (IPC request). A thread from the ThreadProvider
60 // will call this function. The context parameter should be the same as
61 // provided when ThreadProvider::RegisterWait was called.
62 static void __stdcall ThreadPingEventReady(void* context,
63 unsigned char);
65 // Makes the client and server events. This function is called once
66 // per channel.
67 bool MakeEvents(base::win::ScopedHandle* server_ping,
68 base::win::ScopedHandle* server_pong,
69 HANDLE* client_ping, HANDLE* client_pong);
71 // A copy this structure is maintained per channel.
72 // Note that a lot of the fields are just the same of what we have in the IPC
73 // object itself. It is better to have the copies since we can dispatch in the
74 // static method without worrying about converting back to a member function
75 // call or about threading issues.
76 struct ServerControl {
77 // This channel server ping event.
78 base::win::ScopedHandle ping_event;
79 // This channel server pong event.
80 base::win::ScopedHandle pong_event;
81 // The size of this channel.
82 uint32 channel_size;
83 // The pointer to the actual channel data.
84 char* channel_buffer;
85 // The pointer to the base of the shared memory.
86 char* shared_base;
87 // A pointer to this channel's client-side control structure this structure
88 // lives in the shared memory.
89 ChannelControl* channel;
90 // the IPC dispatcher associated with this channel.
91 Dispatcher* dispatcher;
92 // The target process information associated with this channel.
93 ClientInfo target_info;
96 // Looks for the appropriate handler for this IPC and invokes it.
97 static bool InvokeCallback(const ServerControl* service_context,
98 void* ipc_buffer, CrossCallReturn* call_result);
100 // Points to the shared memory channel control which lives at
101 // the start of the shared section.
102 IPCControl* client_control_;
104 // Keeps track of the server side objects that are used to answer an IPC.
105 typedef std::list<ServerControl*> ServerContexts;
106 ServerContexts server_contexts_;
108 // The thread provider provides the threads that call back into this object
109 // when the IPC events fire.
110 ThreadProvider* thread_provider_;
112 // The IPC object is associated with a target process.
113 HANDLE target_process_;
115 // The target process id associated with the IPC object.
116 DWORD target_process_id_;
118 // The target object is inside a job too.
119 HANDLE target_job_object_;
121 // The dispatcher handles 'ready' IPC calls.
122 Dispatcher* call_dispatcher_;
124 DISALLOW_COPY_AND_ASSIGN(SharedMemIPCServer);
127 } // namespace sandbox
129 #endif // SANDBOX_SRC_SHAREDMEM_IPC_SERVER_H_