OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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_SANDBOX_POC_MAIN_UI_WINDOW_H__ | |
6 #define SANDBOX_SANDBOX_POC_MAIN_UI_WINDOW_H__ | |
7 | |
8 #include <windows.h> | |
9 | |
10 #include <string> | |
11 | |
12 #include "base/macros.h" | |
13 #include "base/strings/string16.h" | |
14 | |
15 namespace sandbox { | |
16 class BrokerServices; | |
17 enum ResultCode; | |
18 } | |
19 | |
20 // Header file for the MainUIWindow, a simple window with a menu bar that | |
21 // can pop up a dialog (accessible through the menu bar), to specify a path and | |
22 // filename of any DLL to load and choose an entry point of his/her choice | |
23 // (note: only entry points with no parameters are expected to work). | |
24 // | |
25 // The purpose of this is to be able to spawn an EXE inside a SandBox, have it | |
26 // load a DLL and call the entry point on it to test how it behaves inside the | |
27 // sandbox. This is useful for developer debugging and for security testing. | |
28 // | |
29 // The MainUIWindow also has a listview that displays debugging information to | |
30 // the user. | |
31 // | |
32 // Sample usage: | |
33 // | |
34 // MainUIWindow window; | |
35 // unsigned int ret = window.CreateMainWindowAndLoop( | |
36 // handle_to_current_instance, | |
37 // ::GetCommandLineW(), | |
38 // show_command, | |
39 // broker); | |
40 // | |
41 // The CreateMainWindowAndLoop() contains a message loop that ends when the | |
42 // user closes the MainUIWindow. | |
43 | |
44 // This class encapsulates the Main UI window for the broker application. | |
45 // It simply shows a menu that gives the user the ability (through a dialog) to | |
46 // specify a DLL and what entry point to call in the DLL. | |
47 class MainUIWindow { | |
48 public: | |
49 MainUIWindow(); | |
50 ~MainUIWindow(); | |
51 | |
52 // Creates the main window, displays it and starts the message pump. This | |
53 // call will not return until user closes the main UI window that appears | |
54 // as a result. Arguments 'instance', 'command_line' and 'show_cmd' can be | |
55 // passed in directly from winmain. The 'broker' argument is a pointer to a | |
56 // BrokerService that will launch a new EXE inside the sandbox and load the | |
57 // DLL of the user's choice. | |
58 unsigned int CreateMainWindowAndLoop(HINSTANCE instance, | |
59 wchar_t* command_line, | |
60 int show_command, | |
61 sandbox::BrokerServices* broker); | |
62 | |
63 private: | |
64 // The default value DLL name to add to the edit box. | |
65 static const wchar_t kDefaultDll_[]; | |
66 | |
67 // The default value to show in the entry point. | |
68 static const wchar_t kDefaultEntryPoint_[]; | |
69 | |
70 // The default value to show in the log file. | |
71 static const wchar_t kDefaultLogFile_[]; | |
72 | |
73 // Handles the messages sent to the main UI window. The return value is the | |
74 // result of the message processing and depends on the message. | |
75 static LRESULT CALLBACK WndProc(HWND window, | |
76 UINT message_id, | |
77 WPARAM wparam, | |
78 LPARAM lparam); | |
79 | |
80 // Handles the messages sent to the SpawnTarget dialog. The return value is | |
81 // the result of the message processing and depends on the message. | |
82 static INT_PTR CALLBACK SpawnTargetWndProc(HWND dialog, | |
83 UINT message_id, | |
84 WPARAM wparam, | |
85 LPARAM lparam); | |
86 | |
87 // Retrieves a pointer to the MainWindow from a value stored along with the | |
88 // window handle (passed in as hwnd). Return value is a pointer to the | |
89 // MainUIWindow previously stored with SetWindowLong() during WM_CREATE. | |
90 static MainUIWindow* FromWindow(HWND main_window); | |
91 | |
92 // Handles the WM_CREATE message for the main UI window. Returns TRUE on | |
93 // success. | |
94 static BOOL OnCreate(HWND parent_window, LPCREATESTRUCT); | |
95 | |
96 // Handles the WM_DESTROY message for the main UI window. | |
97 void OnDestroy(HWND window); | |
98 | |
99 // Handles the WM_SIZE message for the main UI window. | |
100 void OnSize(HWND window, UINT state, int cx, int cy); | |
101 | |
102 // Handles the WM_PAINT message for the main UI window. | |
103 void OnPaint(HWND window); | |
104 | |
105 // Handles the menu command File \ Exit for the main UI window. | |
106 void OnFileExit(); | |
107 | |
108 // Handles the menu command Commands \ Launch for the main UI window. | |
109 void OnCommandsLaunch(HWND window); | |
110 | |
111 // Handles the Launch button in the SpawnTarget dialog (normally clicked | |
112 // after selecting DLL and entry point). OnLaunchDll will retrieve the | |
113 // values entered by the user and store it in the members of the class. | |
114 // Returns true if user selected a non-zero values for DLL filename | |
115 // (possibly including path also) and entry point. | |
116 bool OnLaunchDll(HWND dialog); | |
117 | |
118 // Spawns a target EXE inside the sandbox (with the help of the | |
119 // BrokerServices passed in to CreateMainWindowAndLoop), and passes to it | |
120 // (as command line arguments) the DLL path and the entry point function | |
121 // name. The EXE is expected to parse the command line and load the DLL. | |
122 // NOTE: The broker does not know if the target EXE successfully loaded the | |
123 // DLL, for that you have to rely on the EXE providing a log. | |
124 // Returns true if the broker reports that it was successful in creating | |
125 // the target and false if not. | |
126 bool SpawnTarget(); | |
127 | |
128 // Shows a standard File Open dialog and returns the DLL filename selected or | |
129 // blank string if the user cancelled (or an error occurred). | |
130 base::string16 OnShowBrowseForDllDlg(HWND owner); | |
131 | |
132 // Shows a standard Save As dialog and returns the log filename selected or | |
133 // blank string if the user cancelled (or an error occurred). | |
134 base::string16 OnShowBrowseForLogFileDlg(HWND owner); | |
135 | |
136 // Formats a message using the supplied format string and prints it in the | |
137 // listview in the main UI window. Passing a NULL param in 'fmt' results in | |
138 // no action being performed. Maximum message length is 1K. | |
139 void AddDebugMessage(const wchar_t* format, ...); | |
140 | |
141 // Assists AddDebugMessage in displaying a message in the ListView. It | |
142 // simply wraps ListView_InsertItem to insert a debugging message to the | |
143 // top of the list view. Passing a NULL param in 'fmt' results in no action | |
144 // being performed. | |
145 void InsertLineInListView(wchar_t* debug_message); | |
146 | |
147 // Calls ListenPipe using the class instance received in parameter. This is | |
148 // used to create new threads executing ListenPipe | |
149 static DWORD WINAPI ListenPipeThunk(void *param); | |
150 | |
151 // Calls WaitForTargetThunk using the class instance received in parameter | |
152 // This is used to create new threads executing WaitForTarget. | |
153 static DWORD WINAPI WaitForTargetThunk(void *param); | |
154 | |
155 // Listens on a pipe and output the data received to a file and to the UI. | |
156 DWORD ListenPipe(); | |
157 | |
158 // Waits for the target to dies and display a message in the UI. | |
159 DWORD WaitForTarget(); | |
160 | |
161 // The BrokerServices will be used to spawn an EXE in a sandbox and ask | |
162 // it to load a DLL. | |
163 sandbox::BrokerServices* broker_; | |
164 | |
165 // Contains the information about the running target. | |
166 PROCESS_INFORMATION target_; | |
167 | |
168 // This is essentially a command line to a target executable that the | |
169 // broker will spawn and ask to load the DLL. | |
170 base::string16 spawn_target_; | |
171 | |
172 // A handle to the current instance of the app. Passed in to this class | |
173 // through CreateMainWindowAndLoop. | |
174 HINSTANCE instance_handle_; | |
175 | |
176 // A path to the DLL that the target should load once it executes. | |
177 base::string16 dll_path_; | |
178 | |
179 // The name of the entry point the target should call after it loads the DLL. | |
180 base::string16 entry_point_; | |
181 | |
182 // The name of the log file to use. | |
183 base::string16 log_file_; | |
184 | |
185 // This is a static handle to the list view that fills up the entire main | |
186 // UI window. The list view is used to display debugging information to the | |
187 // user. | |
188 static HWND list_view_; | |
189 | |
190 // Pipe used to communicate the logs between the target and the broker. | |
191 HANDLE pipe_handle_; | |
192 | |
193 DISALLOW_COPY_AND_ASSIGN(MainUIWindow); | |
194 }; | |
195 | |
196 #endif // SANDBOX_SANDBOX_POC_MAIN_UI_WINDOW_H__ | |
OLD | NEW |