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.
4 
5 #ifndef SANDBOX_SRC_TARGET_SERVICES_H__
6 #define SANDBOX_SRC_TARGET_SERVICES_H__
7 
8 #include "base/macros.h"
9 #include "sandbox/win/src/sandbox.h"
10 #include "sandbox/win/src/win_utils.h"
11 
12 namespace sandbox {
13 
14 class ProcessState {
15  public:
16   ProcessState();
17   // Returns true if main has been called.
18   bool InitCalled() const;
19   // Returns true if LowerToken has been called.
20   bool RevertedToSelf() const;
21   // Returns true if Csrss is connected.
22   bool IsCsrssConnected() const;
23   // Set the current state.
24   void SetInitCalled();
25   void SetRevertedToSelf();
26   void SetCsrssConnected(bool csrss_connected);
27 
28  private:
29   enum class ProcessStateInternal { NONE = 0, INIT_CALLED, REVERTED_TO_SELF };
30 
31   ProcessStateInternal process_state_;
32   bool csrss_connected_;
33   DISALLOW_COPY_AND_ASSIGN(ProcessState);
34 };
35 
36 // This class is an implementation of the  TargetServices.
37 // Look in the documentation of sandbox::TargetServices for more info.
38 // Do NOT add a destructor to this class without changing the implementation of
39 // the factory method.
40 class TargetServicesBase : public TargetServices {
41  public:
42   TargetServicesBase();
43 
44   // Public interface of TargetServices.
45   ResultCode Init() override;
46   void LowerToken() override;
47   ProcessState* GetState() override;
48   ResultCode DuplicateHandle(HANDLE source_handle,
49                              DWORD target_process_id,
50                              HANDLE* target_handle,
51                              DWORD desired_access,
52                              DWORD options) override;
53 
54   // Factory method.
55   static TargetServicesBase* GetInstance();
56 
57   // Sends a simple IPC Message that has a well-known answer. Returns true
58   // if the IPC was successful and false otherwise. There are 2 versions of
59   // this test: 1 and 2. The first one send a simple message while the
60   // second one send a message with an in/out param.
61   bool TestIPCPing(int version);
62 
63  private:
~TargetServicesBase()64   ~TargetServicesBase() {}
65   ProcessState process_state_;
66   DISALLOW_COPY_AND_ASSIGN(TargetServicesBase);
67 };
68 
69 }  // namespace sandbox
70 
71 #endif  // SANDBOX_SRC_TARGET_SERVICES_H__
72