[Win][MiniBrowser] Remove gMainWindow global variable
authorHironori.Fujii@sony.com <Hironori.Fujii@sony.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 7 Jun 2018 06:25:41 +0000 (06:25 +0000)
committerHironori.Fujii@sony.com <Hironori.Fujii@sony.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 7 Jun 2018 06:25:41 +0000 (06:25 +0000)
https://bugs.webkit.org/show_bug.cgi?id=186378

Reviewed by Ryosuke Niwa.

I'm going to support multiple MainWindow in Bug 186263. It should
not be assumed that MainWindow has only one instance. gMainWindow
is used only in DisplayAuthDialog.

* MiniBrowser/win/Common.cpp:
(authDialogProc): Use DWLP_USER to store the dialog data.
(displayAuthDialog): Moved and renamed from
MainWindow::displayAuthDialog. Use DialogBoxParam instead of
DialogBox to pass a data pointer. Do not return S_OK if
DialogBoxParam returns -1. Take a HWND argument as the parent
window.
(DisplayAuthDialog): Deleted.
* MiniBrowser/win/Common.h:
* MiniBrowser/win/MainWindow.cpp:
(authDialogProc): Moved to Common.cpp.
(MainWindow::displayAuthDialog): Ditto.
* MiniBrowser/win/MainWindow.h:
* MiniBrowser/win/ResourceLoadDelegate.cpp:
(ResourceLoadDelegate::didReceiveAuthenticationChallenge):
* MiniBrowser/win/WinMain.cpp:
(wWinMain): Added a local variable mainWindow instead of using
gMainWindow.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@232574 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Tools/ChangeLog
Tools/MiniBrowser/win/Common.cpp
Tools/MiniBrowser/win/Common.h
Tools/MiniBrowser/win/MainWindow.cpp
Tools/MiniBrowser/win/MainWindow.h
Tools/MiniBrowser/win/ResourceLoadDelegate.cpp
Tools/MiniBrowser/win/WinMain.cpp

index 44ef181..95329ff 100644 (file)
@@ -1,3 +1,33 @@
+2018-06-06  Fujii Hironori  <Hironori.Fujii@sony.com>
+
+        [Win][MiniBrowser] Remove gMainWindow global variable
+        https://bugs.webkit.org/show_bug.cgi?id=186378
+
+        Reviewed by Ryosuke Niwa.
+
+        I'm going to support multiple MainWindow in Bug 186263. It should
+        not be assumed that MainWindow has only one instance. gMainWindow
+        is used only in DisplayAuthDialog.
+
+        * MiniBrowser/win/Common.cpp:
+        (authDialogProc): Use DWLP_USER to store the dialog data.
+        (displayAuthDialog): Moved and renamed from
+        MainWindow::displayAuthDialog. Use DialogBoxParam instead of
+        DialogBox to pass a data pointer. Do not return S_OK if
+        DialogBoxParam returns -1. Take a HWND argument as the parent
+        window.
+        (DisplayAuthDialog): Deleted.
+        * MiniBrowser/win/Common.h:
+        * MiniBrowser/win/MainWindow.cpp:
+        (authDialogProc): Moved to Common.cpp.
+        (MainWindow::displayAuthDialog): Ditto.
+        * MiniBrowser/win/MainWindow.h:
+        * MiniBrowser/win/ResourceLoadDelegate.cpp:
+        (ResourceLoadDelegate::didReceiveAuthenticationChallenge):
+        * MiniBrowser/win/WinMain.cpp:
+        (wWinMain): Added a local variable mainWindow instead of using
+        gMainWindow.
+
 2018-06-06  Dan Bernstein  <mitz@apple.com>
 
         [Xcode] Opt out of the New Build System
index 4c27824..fa25f54 100644 (file)
 #include "stdafx.h"
 #include "Common.h"
 
+#include "MiniBrowserLibResource.h"
 #include "MiniBrowserReplace.h"
 #include <dbghelp.h>
 #include <shlobj.h>
+#include <wtf/StdLibExtras.h>
 
 // Global Variables:
 HINSTANCE hInst;
-MainWindow* gMainWindow = nullptr;
 
 // Support moving the transparent window
 POINT s_windowPosition = { 100, 100 };
@@ -118,9 +119,50 @@ void createCrashReport(EXCEPTION_POINTERS* exceptionPointers)
     }
 }
 
-HRESULT DisplayAuthDialog(std::wstring& username, std::wstring& password)
+struct AuthDialogData {
+    std::wstring& username;
+    std::wstring& password;
+};
+
+static INT_PTR CALLBACK authDialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
+{
+    AuthDialogData& data = *reinterpret_cast<AuthDialogData*>(GetWindowLongPtr(hDlg, DWLP_USER));
+    switch (message) {
+    case WM_INITDIALOG:
+        SetWindowLongPtr(hDlg, DWLP_USER, lParam);
+        return TRUE;
+
+    case WM_COMMAND: {
+        int wmId = LOWORD(wParam);
+        switch (wmId) {
+        case IDOK: {
+            TCHAR str[256];
+            int strLen = GetWindowText(GetDlgItem(hDlg, IDC_AUTH_USER), str, WTF_ARRAY_LENGTH(str)-1);
+            str[strLen] = 0;
+            data.username = str;
+
+            strLen = GetWindowText(GetDlgItem(hDlg, IDC_AUTH_PASSWORD), str, WTF_ARRAY_LENGTH(str)-1);
+            str[strLen] = 0;
+            data.password = str;
+
+            EndDialog(hDlg, true);
+            return TRUE;
+        }
+        case IDCANCEL:
+            EndDialog(hDlg, false);
+            return TRUE;
+        }
+        break;
+    }
+    }
+    return FALSE;
+}
+
+HRESULT displayAuthDialog(HWND hwnd, std::wstring& username, std::wstring& password)
 {
-    return gMainWindow->displayAuthDialog(username, password);
+    AuthDialogData data { username, password };
+    auto result = DialogBoxParam(hInst, MAKEINTRESOURCE(IDD_AUTH), hwnd, authDialogProc, reinterpret_cast<LPARAM>(&data));
+    return result > 0 ? S_OK : E_FAIL;
 }
 
 void parseCommandLine(bool& usesLayeredWebView, bool& useFullDesktop, bool& pageLoadTesting, _bstr_t& requestedURL)
index 6663d25..4ea1273 100644 (file)
@@ -33,8 +33,8 @@ void computeFullDesktopFrame();
 bool getAppDataFolder(_bstr_t& directory);
 void parseCommandLine(bool& usesLayeredWebView, bool& useFullDesktop, bool& pageLoadTesting, _bstr_t& requestedURL);
 void createCrashReport(EXCEPTION_POINTERS*);
+HRESULT displayAuthDialog(HWND, std::wstring& username, std::wstring& password);
 
 extern HINSTANCE hInst;
-extern MainWindow* gMainWindow;
 extern POINT s_windowPosition;
 extern SIZE s_windowSize;
index 87a477c..cd8c530 100644 (file)
@@ -383,56 +383,6 @@ INT_PTR CALLBACK MainWindow::customUserAgentDialogProc(HWND hDlg, UINT message,
     return (INT_PTR)FALSE;
 }
 
-static INT_PTR CALLBACK authDialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
-{
-    switch (message) {
-    case WM_INITDIALOG: {
-        HWND edit = ::GetDlgItem(hDlg, IDC_AUTH_USER);
-        ::SetWindowText(edit, static_cast<LPCTSTR>(L""));
-
-        edit = ::GetDlgItem(hDlg, IDC_AUTH_PASSWORD);
-        ::SetWindowText(edit, static_cast<LPCTSTR>(L""));
-        return (INT_PTR)TRUE;
-    }
-
-    case WM_COMMAND:
-        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) {
-            INT_PTR result { };
-
-            if (LOWORD(wParam) == IDOK) {
-                TCHAR user[256];
-                int strLen = ::GetWindowText(::GetDlgItem(hDlg, IDC_AUTH_USER), user, 256);
-                user[strLen] = 0;
-
-                TCHAR pass[256];
-                strLen = ::GetWindowText(::GetDlgItem(hDlg, IDC_AUTH_PASSWORD), pass, 256);
-                pass[strLen] = 0;
-
-                result = reinterpret_cast<INT_PTR>(new std::pair<std::wstring, std::wstring>(user, pass));
-            }
-
-            ::EndDialog(hDlg, result);
-            return (INT_PTR)TRUE;
-        }
-        break;
-    }
-    return (INT_PTR)FALSE;
-}
-
-HRESULT MainWindow::displayAuthDialog(std::wstring& username, std::wstring& password)
-{
-    auto result = DialogBox(hInst, MAKEINTRESOURCE(IDD_AUTH), hwnd(), authDialogProc);
-    if (!result)
-        return E_FAIL;
-
-    auto pair = reinterpret_cast<std::pair<std::wstring, std::wstring>*>(result);
-    username = pair->first;
-    password = pair->second;
-    delete pair;
-
-    return S_OK;
-}
-
 void MainWindow::loadURL(BSTR url)
 {
     if (FAILED(m_browserWindow->loadURL(url)))
index 1c88ede..0ec4eee 100644 (file)
@@ -39,7 +39,6 @@ public:
     MiniBrowser* browserWindow() const { return m_browserWindow.get(); }
 
     void loadURL(BSTR url);
-    HRESULT displayAuthDialog(std::wstring& username, std::wstring& password);
     
 private:
     static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
index 9153bc8..13bae3d 100644 (file)
@@ -25,6 +25,7 @@
 #include "stdafx.h"
 #include "ResourceLoadDelegate.h"
 
+#include "Common.h"
 #include "MiniBrowser.h"
 #include "PageLoadTestClient.h"
 #include <WebCore/COMPtr.h>
@@ -37,8 +38,6 @@
 #include <string>
 #include <wininet.h>
 
-extern HRESULT DisplayAuthDialog(std::wstring& username, std::wstring& password);
-
 HRESULT ResourceLoadDelegate::QueryInterface(_In_ REFIID riid, _COM_Outptr_ void** ppvObject)
 {
     if (!ppvObject)
@@ -94,7 +93,7 @@ HRESULT ResourceLoadDelegate::didReceiveAuthenticationChallenge(_In_opt_ IWebVie
         return E_FAIL;
 
     std::wstring username, password;
-    if (DisplayAuthDialog(username, password) != S_OK)
+    if (displayAuthDialog(m_client->hwnd(), username, password) != S_OK)
         return E_FAIL;
 
     COMPtr<IWebURLCredential> credential;
index fb93a5b..337f3ba 100644 (file)
@@ -65,19 +65,19 @@ int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance,
 
     ::SetProcessDPIAware();
 
-    gMainWindow = new MainWindow();
-    HRESULT hr = gMainWindow->init(hInst, usesLayeredWebView, pageLoadTesting);
+    auto mainWindow = new MainWindow();
+    HRESULT hr = mainWindow->init(hInst, usesLayeredWebView, pageLoadTesting);
     if (FAILED(hr))
         goto exit;
 
-    ShowWindow(gMainWindow->hwnd(), nCmdShow);
+    ShowWindow(mainWindow->hwnd(), nCmdShow);
 
     hAccelTable = LoadAccelerators(hInst, MAKEINTRESOURCE(IDC_MINIBROWSER));
 
     if (requestedURL.length())
-        gMainWindow->loadURL(requestedURL.GetBSTR());
+        mainWindow->loadURL(requestedURL.GetBSTR());
     else
-        gMainWindow->browserWindow()->loadHTMLString(_bstr_t(defaultHTML).GetBSTR());
+        mainWindow->browserWindow()->loadHTMLString(_bstr_t(defaultHTML).GetBSTR());
 
 #pragma warning(disable:4509)