-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainBrowserDlg.cpp
400 lines (344 loc) · 11.7 KB
/
MainBrowserDlg.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#include "stdafx.h"
#include "MainBrowserDlg.hpp"
#include "BrowserPopupDlg.hpp"
#include "DlgResizeHelper.h"
#include "JSHook.hpp"
#include "LeakDlg.hpp"
#include "resource.h"
#define TIMER_AUTO_REFRESH 0
#define TIMER_MONITOR_MEMORY 1
#define TIMER_CHECK_LEAKS 2
void GetMemoryUsage(wchar_t *pszUsage)
{
PROCESS_MEMORY_COUNTERS procMem;
memset(&procMem, 0, sizeof(PROCESS_MEMORY_COUNTERS));
procMem.cb = sizeof(PROCESS_MEMORY_COUNTERS);
GetProcessMemoryInfo(GetCurrentProcess(), &procMem, procMem.cb);
_itow((int)procMem.WorkingSetSize, pszUsage, 10);
}
BEGIN_MESSAGE_MAP(CMainBrowserDlg, CBrowserHostDlg)
//{{AFX_MSG_MAP(CMainBrowserDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_SIZE()
ON_WM_GETMINMAXINFO()
ON_WM_CLOSE()
ON_WM_DESTROY()
ON_WM_TIMER()
ON_BN_CLICKED(IDOK, doNothing)
ON_BN_CLICKED(IDCANCEL, doNothing)
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDC_GO, OnBnClickedGo)
ON_BN_CLICKED(IDC_BACK, OnBnClickedBack)
ON_BN_CLICKED(IDC_FORWARD, OnBnClickedForward)
ON_BN_CLICKED(IDC_CHECKLEAKS, OnBnClickedCheckLeaks)
ON_BN_CLICKED(IDC_AUTOREFRESH, OnBnClickedAutoRefresh)
END_MESSAGE_MAP()
CMainBrowserDlg::CMainBrowserDlg(CWnd* pParent) : CBrowserHostDlg(IDC_EXPLORER, CMainBrowserDlg::IDD, pParent),
m_waitingForDoc(false), m_waitingForBlankDoc(false), m_autoRefreshMode(false), m_checkLeakDoc(NULL)
{
//{{AFX_DATA_INIT(CMainBrowserDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
m_hIcon = AfxGetApp()->LoadIcon(IDI_ICON1);
// Create the browser hook, which will be shared across all HTML documents.
//
CComObject<JSHook>::CreateInstance(&m_hook);
m_hook->AddRef();
}
CMainBrowserDlg::~CMainBrowserDlg() {
// Release the browser hook.
//
m_hook->Release();
}
void CMainBrowserDlg::DoDataExchange(CDataExchange* pDX) {
CBrowserHostDlg::DoDataExchange(pDX);
}
BOOL CMainBrowserDlg::OnInitDialog() {
CBrowserHostDlg::OnInitDialog();
// Set the application icon.
//
SetIcon(m_hIcon, TRUE);
SetIcon(m_hIcon, FALSE);
// Disable the leak check button.
//
GetDlgItem(IDC_CHECKLEAKS)->EnableWindow(FALSE);
// Focus the URL edit control.
//
GetDlgItem(IDC_EDITURL)->SetFocus();
// Enable memory monitoring
//
SetTimer(TIMER_MONITOR_MEMORY, 100, NULL);
// Set up resizing
m_resizeHelper.Init(m_hWnd);
m_resizeHelper.Fix(IDC_AUTOREFRESH, DlgResizeHelper::kWidthRight, DlgResizeHelper::kHeightTop);
m_resizeHelper.Fix(IDC_CHECKLEAKS, DlgResizeHelper::kWidthRight, DlgResizeHelper::kHeightTop);
m_resizeHelper.Fix(IDC_GO, DlgResizeHelper::kWidthRight, DlgResizeHelper::kHeightTop);
m_resizeHelper.Fix(IDC_EDITURL, DlgResizeHelper::kLeftRight, DlgResizeHelper::kHeightTop);
m_resizeHelper.Fix(IDC_ADDRESS_STATIC, DlgResizeHelper::kWidthLeft, DlgResizeHelper::kHeightTop);
m_resizeHelper.Fix(IDC_FORWARD, DlgResizeHelper::kWidthLeft, DlgResizeHelper::kHeightTop);
m_resizeHelper.Fix(IDC_BACK, DlgResizeHelper::kWidthLeft, DlgResizeHelper::kHeightTop);
m_resizeHelper.Fix(IDC_TOTALMEMLABEL, DlgResizeHelper::kWidthRight, DlgResizeHelper::kHeightTop);
m_resizeHelper.Fix(IDC_CURRENT_MEMORY_EDIT, DlgResizeHelper::kWidthRight, DlgResizeHelper::kHeightTop);
m_resizeHelper.Fix(IDC_MEMLABEL, DlgResizeHelper::kWidthRight, DlgResizeHelper::kHeightTop);
m_resizeHelper.Fix(IDC_MEMLIST, DlgResizeHelper::kWidthRight, DlgResizeHelper::kTopBottom);
m_resizeHelper.Fix(IDC_EXPLORER, DlgResizeHelper::kLeftRight, DlgResizeHelper::kTopBottom);
m_resizeHelper.Fix(getExplorerHwnd(), DlgResizeHelper::kLeftRight, DlgResizeHelper::kTopBottom);
// Navigate to a page so that the border appears in the browser
//
go();
// Must return false when focusing a control.
//
return FALSE;
}
void CMainBrowserDlg::OnPaint() {
if (IsIconic()) {
CPaintDC dc(this);
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle.
//
CRect rect;
GetClientRect(&rect);
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon.
//
dc.DrawIcon(x, y, m_hIcon);
}
else {
CBrowserHostDlg::OnPaint();
}
}
HCURSOR CMainBrowserDlg::OnQueryDragIcon() {
return (HCURSOR) m_hIcon;
}
void CMainBrowserDlg::OnSize(UINT nType, int cx, int cy) {
m_resizeHelper.OnSize();
}
void CMainBrowserDlg::OnGetMinMaxInfo(MINMAXINFO FAR* lpMMI)
{
// Using arbitrary figures here (dialog units would be better)
//
lpMMI->ptMinTrackSize.x = 500;
lpMMI->ptMinTrackSize.y = 300;
CWnd::OnGetMinMaxInfo(lpMMI);
}
void CMainBrowserDlg::OnClose() {
DestroyWindow();
}
void CMainBrowserDlg::OnDestroy() {
// Destroy the popups without navigating to blank page
//
for (std::vector<CBrowserPopupDlg*>::const_iterator it = m_popups.begin(); it != m_popups.end(); ++it) {
CBrowserPopupDlg *dlg = *it;
dlg->DestroyWindow();
delete dlg;
}
m_popups.clear();
// When the main dialog is destroyed, quit the application.
//
PostQuitMessage(0);
}
void CMainBrowserDlg::doNothing() {
}
LRESULT CMainBrowserDlg::WindowProc(UINT message, WPARAM wparam, LPARAM lparam) {
return CBrowserHostDlg::WindowProc(message, wparam, lparam);
}
void CMainBrowserDlg::OnTimer(UINT_PTR nIDEvent) {
switch (nIDEvent) {
case TIMER_AUTO_REFRESH:
KillTimer(nIDEvent);
go();
break;
case TIMER_MONITOR_MEMORY:
wchar_t memText[32];
GetMemoryUsage(memText);
GetDlgItem(IDC_CURRENT_MEMORY_EDIT)->SendMessage(WM_SETTEXT, 0, (LPARAM)memText);
break;
case TIMER_CHECK_LEAKS:
if (m_checkLeakDoc == NULL) {
KillTimer(nIDEvent);
ASSERT(false);
break;
}
// Display the leak dialog if all the popups are finished
//
destroyFinishedPopups();
if (m_popups.begin() == m_popups.end()) {
KillTimer(nIDEvent);
CLeakDlg leakDlg(this);
m_hook->showLeaks(m_checkLeakDoc->parentWindow, &leakDlg);
GetDlgItem(IDC_CHECKLEAKS)->EnableWindow(FALSE);
leakDlg.DoModal();
}
break;
}
}
// Gets the text in the url edit control. The caller must free it.
//
wchar_t* CMainBrowserDlg::getUrlText() {
CWnd* item = GetDlgItem(IDC_EDITURL);
int len = (int)item->SendMessage(WM_GETTEXTLENGTH, 0, 0);
// Navigating to a blank URL will cause the browser not to show a document at all.
// Either use about:blank or bulletproof this dialog against NULL document pointers.
//
if (!len) {
wchar_t about_blank[] = L"about:blank";
wchar_t* text = new wchar_t[sizeof(about_blank)];
memcpy(text, about_blank, sizeof(about_blank));
return text;
}
wchar_t* text = new wchar_t[len + 1];
item->SendMessage(WM_GETTEXT, (WPARAM)len + 1, (LPARAM)text);
return text;
}
// Loads the document specified by the url edit control.
//
void CMainBrowserDlg::go() {
wchar_t* url = getUrlText();
Navigate(url);
delete[] url;
}
void CMainBrowserDlg::OnBnClickedBack() {
GoBack();
}
void CMainBrowserDlg::OnBnClickedForward() {
GoForward();
}
void CMainBrowserDlg::OnBnClickedGo() {
if (m_waitingForDoc) {
// (The button says 'stop')
// If waiting for a document, cancel it, set the button back to
// 'go', and enable the leak test and auto-refresh buttons.
//
m_waitingForDoc = false;
GetDlgItem(IDC_GO)->SendMessage(WM_SETTEXT, 0, (LPARAM)L"Go");
GetDlgItem(IDC_DRIP)->EnableWindow(TRUE);
GetDlgItem(IDC_AUTOREFRESH)->EnableWindow(TRUE);
Stop();
}
else {
// (The button says 'go')
// Load the specified document, set the button to 'stop', and
// disable the leak test and auto-refresh buttons.
//
m_waitingForDoc = true;
GetDlgItem(IDC_GO)->SendMessage(WM_SETTEXT, 0, (LPARAM)L"Stop");
GetDlgItem(IDC_DRIP)->EnableWindow(FALSE);
GetDlgItem(IDC_AUTOREFRESH)->EnableWindow(FALSE);
go();
}
}
void CMainBrowserDlg::OnBnClickedCheckLeaks() {
// When the leak test button is pressed, navigate to the blank document
// so that the browser will release all of its elements. Set
// m_waitingForBlankDoc to true so that the DocumentComplete event
// handler will know to check for leaks when the blank document
// finishes loading.
//
requestClosePopups();
Navigate(L"about:blank");
m_waitingForBlankDoc = true;
}
void CMainBrowserDlg::OnBnClickedAutoRefresh() {
if (!m_autoRefreshMode) {
// (The button says 'auto refresh')
// Start automatically refreshing ("blowing memory"), change the button to 'stop', and disable
// the go and leak test buttons.
//
m_autoRefreshMode = true;
GetDlgItem(IDC_AUTOREFRESH)->SendMessage(WM_SETTEXT, 0, (LPARAM)L"Stop");
GetDlgItem(IDC_GO)->EnableWindow(FALSE);
GetDlgItem(IDC_DRIP)->EnableWindow(FALSE);
GetDlgItem(IDC_MEMLIST)->SendMessage(LB_RESETCONTENT, 0, 0);
// Load the specified document, which will start the auto-refresh cycle.
//
go();
}
else {
// (The button says 'stop')
// Stop auto-refresh, change the button back to 'auto refresh', and
// re-enable the go and leak test buttons.
//
m_autoRefreshMode = false;
GetDlgItem(IDC_AUTOREFRESH)->SendMessage(WM_SETTEXT, 0, (LPARAM)L"Auto-Refresh");
GetDlgItem(IDC_GO)->EnableWindow(TRUE);
GetDlgItem(IDC_DRIP)->EnableWindow(TRUE);
}
}
void CMainBrowserDlg::requestClosePopups()
{
for (std::vector<CBrowserPopupDlg*>::const_iterator it = m_popups.begin(); it != m_popups.end(); ++it) {
CBrowserPopupDlg *dlg = *it;
dlg->requestClose();
}
}
void CMainBrowserDlg::destroyFinishedPopups()
{
for (std::vector<CBrowserPopupDlg*>::iterator it = m_popups.begin(); it != m_popups.end(); ++it) {
CBrowserPopupDlg *dlg = *it;
if (dlg->isFinished()) {
dlg->DestroyWindow();
delete dlg;
m_popups.erase(it);
it--;
}
}
}
void CMainBrowserDlg::onTitleChange(LPCTSTR lpszText) {
CStringW title(lpszText);
if (title.IsEmpty())
SetWindowText(CStringW(L"Drip"));
else
SetWindowText(CStringW(L"Drip - ") + CStringW(lpszText));
}
void CMainBrowserDlg::onOuterDocumentLoad(MSHTML::IHTMLDocument2Ptr doc)
{
CBrowserHostDlg::onOuterDocumentLoad(doc);
// If we're in 'auto-refresh' mode, display the current memory usage
// and refresh the document.
//
if (m_autoRefreshMode) {
// Make sure that the browser's GC pass is done so that we don't report
// as-yet-uncollected memory.
//
doc->parentWindow->execScript(L"window.CollectGarbage()", L"javascript");
// Get the process' memory usage and add it to the list.
//
wchar_t memText[32];
GetMemoryUsage(memText);
GetDlgItem(IDC_MEMLIST)->SendMessage(LB_INSERTSTRING, 0, (LPARAM)memText);
// Reload the document in 500 ms.
//
SetTimer(0, 500, NULL);
}
// If we're waiting for a blank document (that is, we're waiting for the
// current document to fully unload so that we can check for leaks),
// then check for leaks and display the leak dialog.
//
else if (m_waitingForBlankDoc) {
m_waitingForBlankDoc = false;
SetTimer(TIMER_CHECK_LEAKS, 100, NULL);
m_checkLeakDoc = doc;
}
// If we're simply waiting on the page to load, re-enable the drip, leak
// test, and auto-refresh buttons, and set the 'stop' button back to 'go'.
//
else {
GetDlgItem(IDC_DRIP)->EnableWindow(TRUE);
GetDlgItem(IDC_AUTOREFRESH)->EnableWindow(TRUE);
GetDlgItem(IDC_GO)->SendMessage(WM_SETTEXT, 0, (LPARAM)L"Go");
GetDlgItem(IDC_CHECKLEAKS)->EnableWindow(TRUE);
m_waitingForDoc = false;
}
}
void CMainBrowserDlg::onNewWindow(CBrowserHostDlg** ppDlg)
{
CBrowserPopupDlg *dlg = new CBrowserPopupDlg(m_hook,&m_popups,this);
dlg->Create(CBrowserPopupDlg::IDD);
dlg->ShowWindow(SW_SHOW);
m_popups.push_back(dlg);
*ppDlg = dlg;
}