1
0

CEF Changes

This commit is contained in:
Scott E. Graves
2017-03-18 14:07:45 -05:00
parent 2d1c098d92
commit 7bed11a276
10 changed files with 390 additions and 8 deletions

View File

@@ -5,13 +5,17 @@
#include <siacommon.h>
#include <siadriveapp.h>
#include <include/cef_app.h>
#include <include/cef_sandbox_win.h>
using namespace Sia;
using namespace Sia::Api;
// Entry point function for all processes.
int APIENTRY wWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow) {
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
@@ -35,10 +39,10 @@ int APIENTRY wWinMain(HINSTANCE hInstance,
// SimpleApp implements application-level callbacks for the browser process.
// It will create the first browser instance in OnContextInitialized() after
// CEF has initialized.
// CefRefPtr<CSiaDriveApp> app(new CSiaDriveApp);
CefRefPtr<CSiaDriveApp> app(new CSiaDriveApp);
// Initialize CEF.
//CefInitialize(mainArgs, settings, app.get(), sandboxInfo);
CefInitialize(mainArgs, settings, app.get(), nullptr);
// Run the CEF message loop. This will block until CefQuitMessageLoop() is
// called.

View File

@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<!--The ID below indicates application support for Windows 8.1 -->
<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}"/>
<!--The ID below indicates application support for Windows 10 -->
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"/>
</application>
</compatibility>
<dependency>
<dependentAssembly>
<assemblyIdentity type="Win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="*" publicKeyToken="6595b64144ccf1df" language="*"></assemblyIdentity>
</dependentAssembly>
</dependency>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel level="asInvoker" />
</requestedPrivileges>
</security>
</trustInfo>
</assembly>

View File

@@ -1,3 +1,98 @@
#include <siadriveapp.h>
#include "include/views/cef_browser_view.h"
#include "include/views/cef_window.h"
#include "include/wrapper/cef_helpers.h"
#include "siadrivehandler.h"
using namespace Sia::Api;
using namespace Sia;
// When using the Views framework this object provides the delegate
// implementation for the CefWindow that hosts the Views-based browser.
class SimpleWindowDelegate :
public CefWindowDelegate
{
public:
explicit SimpleWindowDelegate(CefRefPtr<CefBrowserView> browserView)
: _browserView(browserView)
{
}
void OnWindowCreated(CefRefPtr<CefWindow> window) OVERRIDE
{
// Add the browser view and show the window.
window->AddChildView(_browserView);
window->Show();
// Give keyboard focus to the browser view.
_browserView->RequestFocus();
}
void OnWindowDestroyed(CefRefPtr<CefWindow> window) OVERRIDE
{
_browserView = nullptr;
}
bool CanClose(CefRefPtr<CefWindow> window) OVERRIDE
{
// Allow the window to close if the browser says it's OK.
CefRefPtr<CefBrowser> browser = _browserView->GetBrowser();
return (browser) ? browser->GetHost()->TryCloseBrowser() : true;
}
private:
CefRefPtr<CefBrowserView> _browserView;
IMPLEMENT_REFCOUNTING(SimpleWindowDelegate);
DISALLOW_COPY_AND_ASSIGN(SimpleWindowDelegate);
};
CSiaDriveApp::CSiaDriveApp()
{
}
void CSiaDriveApp::OnContextInitialized()
{
CEF_REQUIRE_UI_THREAD();
CefRefPtr<CefCommandLine> commandLine = CefCommandLine::GetGlobalCommandLine();
#if defined(OS_WIN) || defined(OS_LINUX)
// Create the browser using the Views framework if "--use-views" is specified
// via the command-line. Otherwise, create the browser using the native
// platform framework. The Views framework is currently only supported on
// Windows and Linux.
const bool useViews = commandLine->HasSwitch("use-views");
#else
const bool useViews = false;
#endif
// SimpleHandler implements browser-level callbacks.
CefRefPtr<CSiaDriveHandler> handler(new CSiaDriveHandler(useViews));
// Specify CEF browser settings here.
CefBrowserSettings browserSettings;
std::string url = "https://www.google.com";
if (useViews)
{
// Create the BrowserView.
CefRefPtr<CefBrowserView> browserView = CefBrowserView::CreateBrowserView(handler, url, browserSettings, nullptr, nullptr);
// Create the Window. It will show itself after creation.
CefWindow::CreateTopLevelWindow(new SimpleWindowDelegate(browserView));
}
else
{
// Information used when creating the native window.
CefWindowInfo windowInfo;
#ifdef _WIN32
// On Windows we need to specify certain flags that will be passed to
// CreateWindowEx().
windowInfo.SetAsPopup(nullptr, "SiaDrive");
#endif
// Create the first browser window.
CefBrowserHost::CreateBrowser(windowInfo, handler, url, browserSettings, nullptr);
}
}

View File

@@ -0,0 +1,146 @@
#include <siadrivehandler.h>
#include <include/wrapper/cef_helpers.h>
#include <include/views/cef_browser_view.h>
#include <include/views/cef_window.h>
#include <include/cef_app.h>
#include <include/base/cef_bind.h>
#include <include/wrapper/cef_closure_task.h>
using namespace Sia;
CSiaDriveHandler* g_instance = nullptr;
CSiaDriveHandler::CSiaDriveHandler(const bool& useViews) :
_useViews(useViews),
_isClosing(false)
{
DCHECK(!g_instance);
g_instance = this;
}
CSiaDriveHandler::~CSiaDriveHandler()
{
g_instance = nullptr;
}
// static
CSiaDriveHandler* CSiaDriveHandler::GetInstance()
{
return g_instance;
}
void CSiaDriveHandler::OnTitleChange(CefRefPtr<CefBrowser> browser, const CefString& title)
{
CEF_REQUIRE_UI_THREAD();
if (_useViews)
{
// Set the title of the window using the Views framework.
CefRefPtr<CefBrowserView> browserView = CefBrowserView::GetForBrowser(browser);
if (browserView)
{
CefRefPtr<CefWindow> window = browserView->GetWindow();
if (window)
window->SetTitle(title);
}
}
else
{
// Set the title of the window using platform APIs.
PlatformTitleChange(browser, title);
}
}
void CSiaDriveHandler::OnAfterCreated(CefRefPtr<CefBrowser> browser)
{
CEF_REQUIRE_UI_THREAD();
// Add to the list of existing browsers.
_browserList.push_back(browser);
}
bool CSiaDriveHandler::DoClose(CefRefPtr<CefBrowser> browser)
{
CEF_REQUIRE_UI_THREAD();
// Closing the main window requires special handling. See the DoClose()
// documentation in the CEF header for a detailed destription of this
// process.
if (_browserList.size() == 1)
{
// Set a flag to indicate that the window close should be allowed.
_isClosing = true;
}
// Allow the close. For windowed browsers this will result in the OS close
// event being sent.
return false;
}
void CSiaDriveHandler::OnBeforeClose(CefRefPtr<CefBrowser> browser)
{
CEF_REQUIRE_UI_THREAD();
// Remove from the list of existing browsers.
BrowserList::iterator bit = _browserList.begin();
for (; bit != _browserList.end(); ++bit)
{
if ((*bit)->IsSame(browser))
{
_browserList.erase(bit);
break;
}
}
if (_browserList.empty())
{
// All browser windows have closed. Quit the application message loop.
CefQuitMessageLoop();
}
}
void CSiaDriveHandler::OnLoadError(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
ErrorCode errorCode,
const CefString& errorText,
const CefString& failedUrl)
{
CEF_REQUIRE_UI_THREAD();
// Don't display an error for downloaded files.
if (errorCode == ERR_ABORTED)
return;
// Display a load error message.
std::stringstream ss;
ss << "<html><body bgcolor=\"white\">"
"<h2>Failed to load URL " << std::string(failedUrl) <<
" with error " << std::string(errorText) << " (" << errorCode <<
").</h2></body></html>";
frame->LoadString(ss.str(), failedUrl);
}
void CSiaDriveHandler::CloseAllBrowsers(bool forceClose)
{
if (!CefCurrentlyOn(TID_UI))
{
// Execute on the UI thread.
CefPostTask(TID_UI, base::Bind(&CSiaDriveHandler::CloseAllBrowsers, this, forceClose));
return;
}
if (_browserList.empty())
return;
BrowserList::const_iterator it = _browserList.begin();
for (; it != _browserList.end(); ++it)
(*it)->GetHost()->CloseBrowser(forceClose);
}
#ifdef _WIN32
void CSiaDriveHandler::PlatformTitleChange(CefRefPtr<CefBrowser> browser, const CefString& title)
{
CefWindowHandle hwnd = browser->GetHost()->GetWindowHandle();
SetWindowText(hwnd, std::wstring(title).c_str());
}
#endif