Skip to content

Commit 107e293

Browse files
committed
Chrome 47.3 RC. Issue #178.
Tray icon loaded image resource for each of Windows Message and these objects were never freed. This caused GDI objects peak at 10001 and FATAL in Chrome to inform about this resource leak. Call NotifyMoveOrResizeStarted() when moving/resizing window. This is required to notify <select> or other controls. Fixed WM_ERASEBKGND - should return 0. Added WM_PAINT with BeginPaint and EndPaint - because it exists in cefclient. Not sure why these calls are there, there is no actual painting being done. But let's mimic cefclient behavior just to be safe.
1 parent 0b2a014 commit 107e293

File tree

5 files changed

+72
-35
lines changed

5 files changed

+72
-35
lines changed

phpdesktop-chrome47/cef/browser_window.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ BrowserWindow* GetBrowserWindow(HWND hwnd) {
3333
// This would return parent browser for popup browsers,
3434
// which would not be an expected behavior. Commenting
3535
// out as it causes app hang when closing popup and then
36-
// main window. See RemoveBrowserWindow in OnBeforeClose
36+
// main window. See RemoveBrowserWindow in OnBeforeClose
3737
// and WM_DESTROY.
3838
HWND owner = GetWindow(hwnd, GW_OWNER);
3939
if (owner) {
@@ -87,6 +87,11 @@ void RemoveBrowserWindow(HWND hwnd) {
8787
}
8888
}
8989

90+
int CountBrowserWindows()
91+
{
92+
return g_browserWindows.size();
93+
}
94+
9095
BrowserWindow::BrowserWindow(HWND inWindowHandle, bool isPopup)
9196
: windowHandle_(inWindowHandle),
9297
isPopup_(isPopup),
@@ -124,6 +129,7 @@ void BrowserWindow::SetCefBrowser(CefRefPtr<CefBrowser> cefBrowser) {
124129
return;
125130
}
126131
cefBrowser_ = cefBrowser;
132+
browserHandle_ = cefBrowser->GetHost()->GetWindowHandle();
127133
fullscreen_.reset(new Fullscreen(cefBrowser));
128134
json_value* appSettings = GetApplicationSettings();
129135
if (!IsPopup()) {
@@ -136,7 +142,7 @@ void BrowserWindow::SetCefBrowser(CefRefPtr<CefBrowser> cefBrowser) {
136142
cefBrowser->SendProcessMessage(PID_RENDERER, message);
137143
}
138144
}
139-
145+
140146
// OnSize was called from WM_SIZE, but cefBrowser_ was not yet
141147
// set, so the window wasn't yet positioned correctly.
142148
this->OnSize();
@@ -162,7 +168,7 @@ bool BrowserWindow::CreateBrowserControl(const wchar_t* navigateUrl) {
162168
CefBrowserSettings browser_settings;
163169
// Create the first browser window.
164170
CefBrowserHost::CreateBrowser(
165-
window_info, handler.get(),
171+
window_info, handler.get(),
166172
GetWebServerUrl(), browser_settings, NULL);
167173

168174
return true;
@@ -209,12 +215,11 @@ void BrowserWindow::OnGetMinMaxInfo(UINT uMsg, WPARAM wParam, LPARAM lParam) {
209215
}
210216
}
211217
void BrowserWindow::OnSize() {
212-
if (cefBrowser_) {
218+
if (browserHandle_) {
213219
RECT rect;
214220
GetClientRect(windowHandle_, &rect);
215-
HDWP hdwp = BeginDeferWindowPos(1);
216-
CefWindowHandle cefHwnd = cefBrowser_->GetHost()->GetWindowHandle();
217-
hdwp = DeferWindowPos(hdwp, cefHwnd, NULL,
221+
HDWP hdwp = BeginDeferWindowPos(2);
222+
hdwp = DeferWindowPos(hdwp, browserHandle_, NULL,
218223
rect.left, rect.top,
219224
rect.right - rect.left,
220225
rect.bottom - rect.top,

phpdesktop-chrome47/cef/browser_window.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ class BrowserWindow;
1717
BrowserWindow* GetBrowserWindow(HWND hwnd);
1818
void StoreBrowserWindow(HWND hwnd, BrowserWindow* browser);
1919
void RemoveBrowserWindow(HWND hwnd);
20+
int CountBrowserWindows();
2021

2122
class BrowserWindow {
2223
private:
2324
HWND windowHandle_;
25+
HWND browserHandle_;
2426
bool isPopup_;
2527
CefRefPtr<CefBrowser> cefBrowser_; // may be empty, always check using .get()
2628
std::tr1::shared_ptr<Fullscreen> fullscreen_; // may be empty

phpdesktop-chrome47/main.cpp

Lines changed: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,29 @@ HCURSOR g_appStartingCursor = 0;
4242
extern std::map<HWND, BrowserWindow*> g_browserWindows; // browser_window.cpp
4343
std::string g_cgiEnvironmentFromArgv = "";
4444

45+
NOTIFYICONDATA GetTrayData(HWND hwnd)
46+
{
47+
static NOTIFYICONDATA tray = {0};
48+
tray.hWnd = hwnd;
49+
if (tray.cbSize) {
50+
return tray;
51+
}
52+
json_value* appSettings = GetApplicationSettings();
53+
std::string main_window_title = (*appSettings)["main_window"]["title"];
54+
std::string minimize_to_tray_message = (*appSettings)["main_window"]["minimize_to_tray_message"];
55+
tray.cbSize = sizeof(tray);
56+
tray.uID = 1;
57+
tray.uCallbackMessage = WM_TRAY_MESSAGE;
58+
tray.hIcon = (HICON)LoadImage(g_hInstance, MAKEINTRESOURCE(IDR_MAINWINDOW), IMAGE_ICON,
59+
GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON),
60+
LR_DEFAULTCOLOR);
61+
wcscpy_s(tray.szInfo, 256, Utf8ToWide(minimize_to_tray_message).c_str());
62+
wcscpy_s(tray.szInfoTitle, 64, Utf8ToWide(main_window_title).c_str());
63+
tray.uFlags = NIF_ICON | NIF_INFO | NIF_MESSAGE;
64+
tray.dwInfoFlags = NIIF_NONE;
65+
return tray;
66+
}
67+
4568
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam,
4669
LPARAM lParam) {
4770
BrowserWindow* browser = 0;
@@ -55,19 +78,9 @@ LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam,
5578

5679
// Minimize to system tray
5780
bool minimize_to_tray = (*appSettings)["main_window"]["minimize_to_tray"];
58-
std::string minimize_to_tray_message = (*appSettings)["main_window"]["minimize_to_tray_message"];
59-
NOTIFYICONDATA tray = {0};
60-
tray.cbSize = sizeof(tray);
61-
tray.hWnd = hwnd;
62-
tray.uID = 1;
63-
tray.uCallbackMessage = WM_TRAY_MESSAGE;
64-
tray.hIcon = (HICON) LoadImage(g_hInstance, MAKEINTRESOURCE(IDR_MAINWINDOW), IMAGE_ICON,
65-
GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON),
66-
LR_DEFAULTCOLOR);
67-
wcscpy_s(tray.szInfo, 256, Utf8ToWide(minimize_to_tray_message).c_str());
68-
wcscpy_s(tray.szInfoTitle, 64, Utf8ToWide(main_window_title).c_str());
69-
tray.uFlags = NIF_ICON | NIF_INFO | NIF_MESSAGE;
70-
tray.dwInfoFlags = NIIF_NONE;
81+
if (CountBrowserWindows() > 1) {
82+
minimize_to_tray = false;
83+
}
7184

7285
switch (uMsg) {
7386
case WM_SIZE:
@@ -79,6 +92,17 @@ LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam,
7992
"could not fetch BrowserWindow";
8093
}
8194
break;
95+
case WM_MOVE:
96+
case WM_MOVING:
97+
case WM_SIZING:
98+
browser = GetBrowserWindow(hwnd);
99+
if (browser) {
100+
browser->GetCefBrowser()->GetHost()->NotifyMoveOrResizeStarted();
101+
} else {
102+
LOG_WARNING << "WindowProc(): event WM_MOVING/WM_MOVE/WM_SIZING: "
103+
"could not fetch BrowserWindow";
104+
}
105+
return 0;
82106
case WM_CREATE:
83107
if (GetWindow(hwnd, GW_OWNER)) {
84108
browser = new BrowserWindow(hwnd, true);
@@ -92,13 +116,8 @@ LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam,
92116
RemoveBrowserWindow(hwnd);
93117
if (g_browserWindows.empty()) {
94118
StopWebServer();
95-
#ifdef DEBUG
96-
// Debugging mongoose, see InitializeLogging().
97-
printf("----------------------------------------");
98-
printf("----------------------------------------\n");
119+
Shell_NotifyIcon(NIM_DELETE, &GetTrayData(hwnd));
99120

100-
Shell_NotifyIcon(NIM_DELETE, &tray);
101-
#endif
102121
// Cannot call PostQuitMessage as cookies won't be flushed to disk
103122
// if application is closed immediately. See comment #2:
104123
// https://code.google.com/p/phpdesktop/issues/detail?id=146
@@ -108,6 +127,12 @@ LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam,
108127
// -------------------
109128
// PostQuitMessage(0);
110129
// -------------------
130+
131+
#ifdef DEBUG
132+
// Debugging mongoose, see InitializeLogging().
133+
printf("----------------------------------------");
134+
printf("----------------------------------------\n");
135+
#endif
111136
}
112137
return 0;
113138
case WM_GETMINMAXINFO:
@@ -133,24 +158,30 @@ LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam,
133158
}
134159
break;
135160
case WM_ERASEBKGND:
161+
// Erase the background when the browser does not exist.
136162
browser = GetBrowserWindow(hwnd);
137163
if (browser && browser->GetCefBrowser().get()) {
138164
CefWindowHandle hwnd = \
139165
browser->GetCefBrowser()->GetHost()->GetWindowHandle();
140166
if (hwnd) {
141167
// Dont erase the background if the browser window has been loaded
142168
// (this avoids flashing)
143-
return 1;
169+
return 0;
144170
}
145171
}
146172
break;
173+
case WM_PAINT:
174+
PAINTSTRUCT ps;
175+
BeginPaint(hwnd, &ps);
176+
EndPaint(hwnd, &ps);
177+
return 0;
147178
case WM_SYSCOMMAND:
148-
if (wParam == SC_MINIMIZE && minimize_to_tray && !GetBrowserWindow(hwnd)->IsPopup()) {
179+
if (wParam == SC_MINIMIZE && minimize_to_tray) {
149180
LOG_DEBUG << "Minimize to tray";
150181
ShowWindow(hwnd, SW_MINIMIZE);
151182
Sleep(200);
152183
ShowWindow(hwnd, SW_HIDE);
153-
Shell_NotifyIcon(NIM_ADD, &tray);
184+
Shell_NotifyIcon(NIM_ADD, &GetTrayData(hwnd));
154185
break;
155186
}
156187
break;
@@ -160,7 +191,7 @@ LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam,
160191
ShowWindow(hwnd, SW_SHOW);
161192
ShowWindow(hwnd, SW_RESTORE);
162193
SetForegroundWindow(hwnd);
163-
Shell_NotifyIcon(NIM_DELETE, &tray);
194+
Shell_NotifyIcon(NIM_DELETE, &GetTrayData(hwnd));
164195
break;
165196
}
166197
break;

phpdesktop-chrome47/phpdesktop-chrome.vcxproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,6 @@
337337
</ItemGroup>
338338
<ItemGroup>
339339
<None Include="AUTHORS.txt" />
340-
<None Include="before-release.txt" />
341340
<None Include="cef\README.txt" />
342341
<None Include="icon.ico" />
343342
<None Include=".gitignore" />

phpdesktop-chrome47/resource.rc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ IDR_MAINWINDOW ICON "icon.ico"
4747
//
4848

4949
VS_VERSION_INFO VERSIONINFO
50-
FILEVERSION 47,2,0,0
51-
PRODUCTVERSION 47,2,0,0
50+
FILEVERSION 47,3,0,0
51+
PRODUCTVERSION 47,3,0,0
5252
FILEFLAGSMASK 0x3fL
5353
#ifdef _DEBUG
5454
FILEFLAGS 0x1L
@@ -65,12 +65,12 @@ BEGIN
6565
BEGIN
6666
VALUE "CompanyName", "PHP Desktop"
6767
VALUE "FileDescription", "PHP Desktop Chrome"
68-
VALUE "FileVersion", "47.2.0.0"
68+
VALUE "FileVersion", "47.3.0.0"
6969
VALUE "InternalName", "phpdesktop"
7070
VALUE "LegalCopyright", "(c) Czarek Tomczak 2012"
7171
VALUE "OriginalFilename", "phpdesktop-chrome.exe"
7272
VALUE "ProductName", "PHP Desktop Chrome"
73-
VALUE "ProductVersion", "47.2.0.0"
73+
VALUE "ProductVersion", "47.3.0.0"
7474
END
7575
END
7676
BLOCK "VarFileInfo"

0 commit comments

Comments
 (0)