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