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