Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / sandbox / win / src / sharedmem_ipc_server.h
blobcb40bca7b626f7ed6f3bcacd811aec9c8cd09eb4
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. It is
41 // unfortunate to receive a raw handle (and store it inside this object) as
42 // that dilutes ownership of the process, but in practice a SharedMemIPCServer
43 // is owned by TargetProcess, which calls this method, and owns the handle, so
44 // everything is safe. If that changes, we should break this dependency and
45 // duplicate the handle instead.
46 // target_process_id: process id of the target process.
47 // thread_provider: a thread provider object.
48 // dispatcher: an object that can service IPC calls.
49 SharedMemIPCServer(HANDLE target_process, DWORD target_process_id,
50 ThreadProvider* thread_provider, Dispatcher* dispatcher);
52 ~SharedMemIPCServer();
54 // Initializes the server structures, shared memory structures and
55 // creates the kernels events used to signal the IPC.
56 bool Init(void* shared_mem, uint32 shared_size, uint32 channel_size);
58 private:
59 // Allow tests to be marked DISABLED_. Note that FLAKY_ and FAILS_ prefixes
60 // do not work with sandbox tests.
61 FRIEND_TEST_ALL_PREFIXES(IPCTest, SharedMemServerTests);
62 // When an event fires (IPC request). A thread from the ThreadProvider
63 // will call this function. The context parameter should be the same as
64 // provided when ThreadProvider::RegisterWait was called.
65 static void __stdcall ThreadPingEventReady(void* context,
66 unsigned char);
68 // Makes the client and server events. This function is called once
69 // per channel.
70 bool MakeEvents(base::win::ScopedHandle* server_ping,
71 base::win::ScopedHandle* server_pong,
72 HANDLE* client_ping, HANDLE* client_pong);
74 // A copy this structure is maintained per channel.
75 // Note that a lot of the fields are just the same of what we have in the IPC
76 // object itself. It is better to have the copies since we can dispatch in the
77 // static method without worrying about converting back to a member function
78 // call or about threading issues.
79 struct ServerControl {
80 ServerControl();
81 ~ServerControl();
83 // This channel server ping event.
84 base::win::ScopedHandle ping_event;
85 // This channel server pong event.
86 base::win::ScopedHandle pong_event;
87 // The size of this channel.
88 uint32 channel_size;
89 // The pointer to the actual channel data.
90 char* channel_buffer;
91 // The pointer to the base of the shared memory.
92 char* shared_base;
93 // A pointer to this channel's client-side control structure this structure
94 // lives in the shared memory.
95 ChannelControl* channel;
96 // the IPC dispatcher associated with this channel.
97 Dispatcher* dispatcher;
98 // The target process information associated with this channel.
99 ClientInfo target_info;
102 // Looks for the appropriate handler for this IPC and invokes it.
103 static bool InvokeCallback(const ServerControl* service_context,
104 void* ipc_buffer, CrossCallReturn* call_result);
106 // Points to the shared memory channel control which lives at
107 // the start of the shared section.
108 IPCControl* client_control_;
110 // Keeps track of the server side objects that are used to answer an IPC.
111 typedef std::list<ServerControl*> ServerContexts;
112 ServerContexts server_contexts_;
114 // The thread provider provides the threads that call back into this object
115 // when the IPC events fire.
116 ThreadProvider* thread_provider_;
118 // The IPC object is associated with a target process.
119 HANDLE target_process_;
121 // The target process id associated with the IPC object.
122 DWORD target_process_id_;
124 // The dispatcher handles 'ready' IPC calls.
125 Dispatcher* call_dispatcher_;
127 DISALLOW_COPY_AND_ASSIGN(SharedMemIPCServer);
130 } // namespace sandbox
132 #endif // SANDBOX_SRC_SHAREDMEM_IPC_SERVER_H_