Skip to content

Commit

Permalink
Chrome 47.3 RC. Issue #178.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cztomczak committed Feb 12, 2016
1 parent 0b2a014 commit 107e293
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 35 deletions.
19 changes: 12 additions & 7 deletions phpdesktop-chrome47/cef/browser_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ BrowserWindow* GetBrowserWindow(HWND hwnd) {
// This would return parent browser for popup browsers,
// which would not be an expected behavior. Commenting
// out as it causes app hang when closing popup and then
// main window. See RemoveBrowserWindow in OnBeforeClose
// main window. See RemoveBrowserWindow in OnBeforeClose
// and WM_DESTROY.
HWND owner = GetWindow(hwnd, GW_OWNER);
if (owner) {
Expand Down Expand Up @@ -87,6 +87,11 @@ void RemoveBrowserWindow(HWND hwnd) {
}
}

int CountBrowserWindows()
{
return g_browserWindows.size();
}

BrowserWindow::BrowserWindow(HWND inWindowHandle, bool isPopup)
: windowHandle_(inWindowHandle),
isPopup_(isPopup),
Expand Down Expand Up @@ -124,6 +129,7 @@ void BrowserWindow::SetCefBrowser(CefRefPtr<CefBrowser> cefBrowser) {
return;
}
cefBrowser_ = cefBrowser;
browserHandle_ = cefBrowser->GetHost()->GetWindowHandle();
fullscreen_.reset(new Fullscreen(cefBrowser));
json_value* appSettings = GetApplicationSettings();
if (!IsPopup()) {
Expand All @@ -136,7 +142,7 @@ void BrowserWindow::SetCefBrowser(CefRefPtr<CefBrowser> cefBrowser) {
cefBrowser->SendProcessMessage(PID_RENDERER, message);
}
}

// OnSize was called from WM_SIZE, but cefBrowser_ was not yet
// set, so the window wasn't yet positioned correctly.
this->OnSize();
Expand All @@ -162,7 +168,7 @@ bool BrowserWindow::CreateBrowserControl(const wchar_t* navigateUrl) {
CefBrowserSettings browser_settings;
// Create the first browser window.
CefBrowserHost::CreateBrowser(
window_info, handler.get(),
window_info, handler.get(),
GetWebServerUrl(), browser_settings, NULL);

return true;
Expand Down Expand Up @@ -209,12 +215,11 @@ void BrowserWindow::OnGetMinMaxInfo(UINT uMsg, WPARAM wParam, LPARAM lParam) {
}
}
void BrowserWindow::OnSize() {
if (cefBrowser_) {
if (browserHandle_) {
RECT rect;
GetClientRect(windowHandle_, &rect);
HDWP hdwp = BeginDeferWindowPos(1);
CefWindowHandle cefHwnd = cefBrowser_->GetHost()->GetWindowHandle();
hdwp = DeferWindowPos(hdwp, cefHwnd, NULL,
HDWP hdwp = BeginDeferWindowPos(2);
hdwp = DeferWindowPos(hdwp, browserHandle_, NULL,
rect.left, rect.top,
rect.right - rect.left,
rect.bottom - rect.top,
Expand Down
2 changes: 2 additions & 0 deletions phpdesktop-chrome47/cef/browser_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ class BrowserWindow;
BrowserWindow* GetBrowserWindow(HWND hwnd);
void StoreBrowserWindow(HWND hwnd, BrowserWindow* browser);
void RemoveBrowserWindow(HWND hwnd);
int CountBrowserWindows();

class BrowserWindow {
private:
HWND windowHandle_;
HWND browserHandle_;
bool isPopup_;
CefRefPtr<CefBrowser> cefBrowser_; // may be empty, always check using .get()
std::tr1::shared_ptr<Fullscreen> fullscreen_; // may be empty
Expand Down
77 changes: 54 additions & 23 deletions phpdesktop-chrome47/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,29 @@ HCURSOR g_appStartingCursor = 0;
extern std::map<HWND, BrowserWindow*> g_browserWindows; // browser_window.cpp
std::string g_cgiEnvironmentFromArgv = "";

NOTIFYICONDATA GetTrayData(HWND hwnd)
{
static NOTIFYICONDATA tray = {0};
tray.hWnd = hwnd;
if (tray.cbSize) {
return tray;
}
json_value* appSettings = GetApplicationSettings();
std::string main_window_title = (*appSettings)["main_window"]["title"];
std::string minimize_to_tray_message = (*appSettings)["main_window"]["minimize_to_tray_message"];
tray.cbSize = sizeof(tray);
tray.uID = 1;
tray.uCallbackMessage = WM_TRAY_MESSAGE;
tray.hIcon = (HICON)LoadImage(g_hInstance, MAKEINTRESOURCE(IDR_MAINWINDOW), IMAGE_ICON,
GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON),
LR_DEFAULTCOLOR);
wcscpy_s(tray.szInfo, 256, Utf8ToWide(minimize_to_tray_message).c_str());
wcscpy_s(tray.szInfoTitle, 64, Utf8ToWide(main_window_title).c_str());
tray.uFlags = NIF_ICON | NIF_INFO | NIF_MESSAGE;
tray.dwInfoFlags = NIIF_NONE;
return tray;
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam,
LPARAM lParam) {
BrowserWindow* browser = 0;
Expand All @@ -55,19 +78,9 @@ LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam,

// Minimize to system tray
bool minimize_to_tray = (*appSettings)["main_window"]["minimize_to_tray"];
std::string minimize_to_tray_message = (*appSettings)["main_window"]["minimize_to_tray_message"];
NOTIFYICONDATA tray = {0};
tray.cbSize = sizeof(tray);
tray.hWnd = hwnd;
tray.uID = 1;
tray.uCallbackMessage = WM_TRAY_MESSAGE;
tray.hIcon = (HICON) LoadImage(g_hInstance, MAKEINTRESOURCE(IDR_MAINWINDOW), IMAGE_ICON,
GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON),
LR_DEFAULTCOLOR);
wcscpy_s(tray.szInfo, 256, Utf8ToWide(minimize_to_tray_message).c_str());
wcscpy_s(tray.szInfoTitle, 64, Utf8ToWide(main_window_title).c_str());
tray.uFlags = NIF_ICON | NIF_INFO | NIF_MESSAGE;
tray.dwInfoFlags = NIIF_NONE;
if (CountBrowserWindows() > 1) {
minimize_to_tray = false;
}

switch (uMsg) {
case WM_SIZE:
Expand All @@ -79,6 +92,17 @@ LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam,
"could not fetch BrowserWindow";
}
break;
case WM_MOVE:
case WM_MOVING:
case WM_SIZING:
browser = GetBrowserWindow(hwnd);
if (browser) {
browser->GetCefBrowser()->GetHost()->NotifyMoveOrResizeStarted();
} else {
LOG_WARNING << "WindowProc(): event WM_MOVING/WM_MOVE/WM_SIZING: "
"could not fetch BrowserWindow";
}
return 0;
case WM_CREATE:
if (GetWindow(hwnd, GW_OWNER)) {
browser = new BrowserWindow(hwnd, true);
Expand All @@ -92,13 +116,8 @@ LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam,
RemoveBrowserWindow(hwnd);
if (g_browserWindows.empty()) {
StopWebServer();
#ifdef DEBUG
// Debugging mongoose, see InitializeLogging().
printf("----------------------------------------");
printf("----------------------------------------\n");
Shell_NotifyIcon(NIM_DELETE, &GetTrayData(hwnd));

Shell_NotifyIcon(NIM_DELETE, &tray);
#endif
// Cannot call PostQuitMessage as cookies won't be flushed to disk
// if application is closed immediately. See comment #2:
// https://code.google.com/p/phpdesktop/issues/detail?id=146
Expand All @@ -108,6 +127,12 @@ LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam,
// -------------------
// PostQuitMessage(0);
// -------------------

#ifdef DEBUG
// Debugging mongoose, see InitializeLogging().
printf("----------------------------------------");
printf("----------------------------------------\n");
#endif
}
return 0;
case WM_GETMINMAXINFO:
Expand All @@ -133,24 +158,30 @@ LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam,
}
break;
case WM_ERASEBKGND:
// Erase the background when the browser does not exist.
browser = GetBrowserWindow(hwnd);
if (browser && browser->GetCefBrowser().get()) {
CefWindowHandle hwnd = \
browser->GetCefBrowser()->GetHost()->GetWindowHandle();
if (hwnd) {
// Dont erase the background if the browser window has been loaded
// (this avoids flashing)
return 1;
return 0;
}
}
break;
case WM_PAINT:
PAINTSTRUCT ps;
BeginPaint(hwnd, &ps);
EndPaint(hwnd, &ps);
return 0;
case WM_SYSCOMMAND:
if (wParam == SC_MINIMIZE && minimize_to_tray && !GetBrowserWindow(hwnd)->IsPopup()) {
if (wParam == SC_MINIMIZE && minimize_to_tray) {
LOG_DEBUG << "Minimize to tray";
ShowWindow(hwnd, SW_MINIMIZE);
Sleep(200);
ShowWindow(hwnd, SW_HIDE);
Shell_NotifyIcon(NIM_ADD, &tray);
Shell_NotifyIcon(NIM_ADD, &GetTrayData(hwnd));
break;
}
break;
Expand All @@ -160,7 +191,7 @@ LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam,
ShowWindow(hwnd, SW_SHOW);
ShowWindow(hwnd, SW_RESTORE);
SetForegroundWindow(hwnd);
Shell_NotifyIcon(NIM_DELETE, &tray);
Shell_NotifyIcon(NIM_DELETE, &GetTrayData(hwnd));
break;
}
break;
Expand Down
1 change: 0 additions & 1 deletion phpdesktop-chrome47/phpdesktop-chrome.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,6 @@
</ItemGroup>
<ItemGroup>
<None Include="AUTHORS.txt" />
<None Include="before-release.txt" />
<None Include="cef\README.txt" />
<None Include="icon.ico" />
<None Include=".gitignore" />
Expand Down
8 changes: 4 additions & 4 deletions phpdesktop-chrome47/resource.rc
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ IDR_MAINWINDOW ICON "icon.ico"
//

VS_VERSION_INFO VERSIONINFO
FILEVERSION 47,2,0,0
PRODUCTVERSION 47,2,0,0
FILEVERSION 47,3,0,0
PRODUCTVERSION 47,3,0,0
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
FILEFLAGS 0x1L
Expand All @@ -65,12 +65,12 @@ BEGIN
BEGIN
VALUE "CompanyName", "PHP Desktop"
VALUE "FileDescription", "PHP Desktop Chrome"
VALUE "FileVersion", "47.2.0.0"
VALUE "FileVersion", "47.3.0.0"
VALUE "InternalName", "phpdesktop"
VALUE "LegalCopyright", "(c) Czarek Tomczak 2012"
VALUE "OriginalFilename", "phpdesktop-chrome.exe"
VALUE "ProductName", "PHP Desktop Chrome"
VALUE "ProductVersion", "47.2.0.0"
VALUE "ProductVersion", "47.3.0.0"
END
END
BLOCK "VarFileInfo"
Expand Down

0 comments on commit 107e293

Please sign in to comment.