blob: 0594bcbe5408a2934d33159ce6a2f5bddbc70f42 [file] [log] [blame]
/*
* Copyright (C) 2006, 2008, 2013-2015 Apple Inc. All rights reserved.
* Copyright (C) 2009, 2011 Brent Fulgham. All rights reserved.
* Copyright (C) 2009, 2010, 2011 Appcelerator, Inc. All rights reserved.
* Copyright (C) 2013 Alex Christensen. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma warning(disable: 4091)
#include "stdafx.h"
#include "MiniBrowserLibResource.h"
#include "MiniBrowserWebHost.h"
#include "Common.cpp"
namespace WebCore {
float deviceScaleFactorForWindow(HWND);
}
int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpstrCmdLine, _In_ int nCmdShow)
{
#ifdef _CRTDBG_MAP_ALLOC
_CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
_CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
#endif
MSG msg = {0};
HACCEL hAccelTable;
INITCOMMONCONTROLSEX InitCtrlEx;
InitCtrlEx.dwSize = sizeof(INITCOMMONCONTROLSEX);
InitCtrlEx.dwICC = 0x00004000; // ICC_STANDARD_CLASSES;
InitCommonControlsEx(&InitCtrlEx);
bool usesLayeredWebView = false;
bool useFullDesktop = false;
bool pageLoadTesting = false;
_bstr_t requestedURL;
parseCommandLine(usesLayeredWebView, useFullDesktop, pageLoadTesting, requestedURL);
// Initialize global strings
LoadString(hInst, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInst, IDC_MINIBROWSER, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInst);
if (useFullDesktop)
computeFullDesktopFrame();
// Init COM
OleInitialize(nullptr);
::SetProcessDPIAware();
float scaleFactor = WebCore::deviceScaleFactorForWindow(nullptr);
if (usesLayeredWebView) {
hURLBarWnd = CreateWindow(L"EDIT", L"Type URL Here",
WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_BORDER | ES_LEFT | ES_AUTOVSCROLL,
scaleFactor * s_windowPosition.x, scaleFactor * (s_windowPosition.y + s_windowSize.cy), scaleFactor * s_windowSize.cx, scaleFactor * URLBAR_HEIGHT,
0, 0, hInst, 0);
} else {
hMainWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, 0, 0, hInst, 0);
if (!hMainWnd)
return FALSE;
hBackButtonWnd = CreateWindow(L"BUTTON", L"<", WS_CHILD | WS_VISIBLE | BS_TEXT, 0, 0, 0, 0, hMainWnd, 0, hInst, 0);
hForwardButtonWnd = CreateWindow(L"BUTTON", L">", WS_CHILD | WS_VISIBLE | BS_TEXT, scaleFactor * CONTROLBUTTON_WIDTH, 0, 0, 0, hMainWnd, 0, hInst, 0);
hURLBarWnd = CreateWindow(L"EDIT", 0, WS_CHILD | WS_VISIBLE | WS_BORDER | ES_LEFT | ES_AUTOVSCROLL, scaleFactor * CONTROLBUTTON_WIDTH * 2, 0, 0, 0, hMainWnd, 0, hInst, 0);
ShowWindow(hMainWnd, nCmdShow);
UpdateWindow(hMainWnd);
}
DefEditProc = reinterpret_cast<WNDPROC>(GetWindowLongPtr(hURLBarWnd, GWLP_WNDPROC));
DefButtonProc = reinterpret_cast<WNDPROC>(GetWindowLongPtr(hBackButtonWnd, GWLP_WNDPROC));
SetWindowLongPtr(hURLBarWnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(EditProc));
SetWindowLongPtr(hBackButtonWnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(BackButtonProc));
SetWindowLongPtr(hForwardButtonWnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(ForwardButtonProc));
SetFocus(hURLBarWnd);
RECT clientRect;
::GetClientRect(hMainWnd, &clientRect);
if (usesLayeredWebView)
clientRect = { s_windowPosition.x, s_windowPosition.y, s_windowPosition.x + s_windowSize.cx, s_windowPosition.y + s_windowSize.cy };
MiniBrowserWebHost* webHost = nullptr;
IWebDownloadDelegatePtr downloadDelegate;
downloadDelegate.Attach(new WebDownloadDelegate());
gMiniBrowser = new MiniBrowser(hMainWnd, hURLBarWnd, usesLayeredWebView, pageLoadTesting);
if (!gMiniBrowser)
goto exit;
if (!gMiniBrowser->seedInitialDefaultPreferences())
goto exit;
if (!gMiniBrowser->setToDefaultPreferences())
goto exit;
HRESULT hr = gMiniBrowser->init();
if (FAILED(hr))
goto exit;
if (!setCacheFolder())
goto exit;
webHost = new MiniBrowserWebHost(gMiniBrowser, hURLBarWnd);
hr = gMiniBrowser->setFrameLoadDelegate(webHost);
if (FAILED(hr))
goto exit;
hr = gMiniBrowser->setFrameLoadDelegatePrivate(webHost);
if (FAILED(hr))
goto exit;
hr = gMiniBrowser->setUIDelegate(new PrintWebUIDelegate());
if (FAILED (hr))
goto exit;
hr = gMiniBrowser->setAccessibilityDelegate(new AccessibilityDelegate());
if (FAILED (hr))
goto exit;
hr = gMiniBrowser->setResourceLoadDelegate(new ResourceLoadDelegate(gMiniBrowser));
if (FAILED(hr))
goto exit;
hr = gMiniBrowser->setDownloadDelegate(downloadDelegate);
if (FAILED(hr))
goto exit;
hr = gMiniBrowser->prepareViews(hMainWnd, clientRect, requestedURL.GetBSTR(), gViewWindow);
if (FAILED(hr) || !gViewWindow)
goto exit;
if (gMiniBrowser->usesLayeredWebView())
subclassForLayeredWindow();
resizeSubViews();
ShowWindow(gViewWindow, nCmdShow);
UpdateWindow(gViewWindow);
hAccelTable = LoadAccelerators(hInst, MAKEINTRESOURCE(IDC_MINIBROWSER));
if (requestedURL.length())
loadURL(requestedURL.GetBSTR());
#pragma warning(disable:4509)
// Main message loop:
__try {
_com_ptr_t<_com_IIID<IWebKitMessageLoop, &__uuidof(IWebKitMessageLoop)>> messageLoop;
hr = WebKitCreateInstance(CLSID_WebKitMessageLoop, 0, IID_IWebKitMessageLoop, reinterpret_cast<void**>(&messageLoop.GetInterfacePtr()));
if (FAILED(hr))
goto exit;
messageLoop->run(hAccelTable);
} __except(createCrashReport(GetExceptionInformation()), EXCEPTION_EXECUTE_HANDLER) { }
exit:
shutDownWebKit();
#ifdef _CRTDBG_MAP_ALLOC
_CrtDumpMemoryLeaks();
#endif
// Shut down COM.
OleUninitialize();
delete gMiniBrowser;
return static_cast<int>(msg.wParam);
}
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MINIBROWSER));
wcex.hCursor = LoadCursor(0, IDC_ARROW);
wcex.hbrBackground = 0;
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_MINIBROWSER);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassEx(&wcex);
}