2011-05-20 16:42:25 +02:00
|
|
|
// Copyright (c) 2011 The Chromium Embedded Framework Authors. All rights
|
|
|
|
// reserved. Use of this source code is governed by a BSD-style license that
|
|
|
|
// can be found in the LICENSE file.
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
#include "cefclient/client_handler.h"
|
2011-05-20 16:42:25 +02:00
|
|
|
#include <stdio.h>
|
2012-01-10 00:46:23 +01:00
|
|
|
#include <sstream>
|
2011-05-20 16:42:25 +02:00
|
|
|
#include <string>
|
2012-01-10 00:46:23 +01:00
|
|
|
#include "include/cef_browser.h"
|
2012-09-07 22:35:43 +02:00
|
|
|
#include "include/cef_command_line.h"
|
2012-01-10 00:46:23 +01:00
|
|
|
#include "include/cef_frame.h"
|
|
|
|
#include "cefclient/binding_test.h"
|
|
|
|
#include "cefclient/cefclient.h"
|
2012-09-07 22:35:43 +02:00
|
|
|
#include "cefclient/cefclient_switches.h"
|
2012-01-10 00:46:23 +01:00
|
|
|
#include "cefclient/download_handler.h"
|
2012-10-26 22:39:24 +02:00
|
|
|
#include "cefclient/performance_test.h"
|
2012-01-10 00:46:23 +01:00
|
|
|
#include "cefclient/string_util.h"
|
2011-05-20 16:42:25 +02:00
|
|
|
|
|
|
|
ClientHandler::ClientHandler()
|
|
|
|
: m_MainHwnd(NULL),
|
|
|
|
m_BrowserHwnd(NULL),
|
|
|
|
m_EditHwnd(NULL),
|
|
|
|
m_BackHwnd(NULL),
|
|
|
|
m_ForwardHwnd(NULL),
|
|
|
|
m_StopHwnd(NULL),
|
2011-10-14 14:40:40 +02:00
|
|
|
m_ReloadHwnd(NULL),
|
2012-01-10 00:46:23 +01:00
|
|
|
m_bFormElementHasFocus(false) {
|
2012-09-07 22:35:43 +02:00
|
|
|
CefRefPtr<CefCommandLine> commandLine = AppGetCommandLine();
|
|
|
|
if (commandLine.get()) {
|
|
|
|
if (commandLine->HasSwitch(cefclient::kUrl))
|
|
|
|
m_StartupURL = commandLine->GetSwitchValue(cefclient::kUrl);
|
|
|
|
else
|
|
|
|
m_StartupURL = "http://www.google.com/";
|
|
|
|
}
|
2011-05-20 16:42:25 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
ClientHandler::~ClientHandler() {
|
2011-05-20 16:42:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
void ClientHandler::OnAfterCreated(CefRefPtr<CefBrowser> browser) {
|
2011-05-20 16:42:25 +02:00
|
|
|
REQUIRE_UI_THREAD();
|
|
|
|
|
|
|
|
AutoLock lock_scope(this);
|
2012-01-10 00:46:23 +01:00
|
|
|
if (!m_Browser.get()) {
|
2011-05-20 16:42:25 +02:00
|
|
|
// We need to keep the main child window, but not popup windows
|
|
|
|
m_Browser = browser;
|
|
|
|
m_BrowserHwnd = browser->GetWindowHandle();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
bool ClientHandler::DoClose(CefRefPtr<CefBrowser> browser) {
|
2011-08-03 17:35:51 +02:00
|
|
|
REQUIRE_UI_THREAD();
|
|
|
|
|
2011-10-21 21:35:19 +02:00
|
|
|
if (m_BrowserHwnd == browser->GetWindowHandle()) {
|
2011-08-03 17:35:51 +02:00
|
|
|
// Since the main window contains the browser window, we need to close
|
|
|
|
// the parent window instead of the browser window.
|
|
|
|
CloseMainWindow();
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
// Return true here so that we can skip closing the browser window
|
2011-08-03 17:35:51 +02:00
|
|
|
// in this pass. (It will be destroyed due to the call to close
|
|
|
|
// the parent above.)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// A popup browser window is not contained in another window, so we can let
|
|
|
|
// these windows close by themselves.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
void ClientHandler::OnBeforeClose(CefRefPtr<CefBrowser> browser) {
|
2011-05-20 16:42:25 +02:00
|
|
|
REQUIRE_UI_THREAD();
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
if (m_BrowserHwnd == browser->GetWindowHandle()) {
|
2011-05-20 16:42:25 +02:00
|
|
|
// Free the browser pointer so that the browser can be destroyed
|
|
|
|
m_Browser = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientHandler::OnLoadStart(CefRefPtr<CefBrowser> browser,
|
2012-01-10 00:46:23 +01:00
|
|
|
CefRefPtr<CefFrame> frame) {
|
2011-05-20 16:42:25 +02:00
|
|
|
REQUIRE_UI_THREAD();
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
if (m_BrowserHwnd == browser->GetWindowHandle() && frame->IsMain()) {
|
2011-05-20 16:42:25 +02:00
|
|
|
// We've just started loading a page
|
|
|
|
SetLoading(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientHandler::OnLoadEnd(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
2012-01-10 00:46:23 +01:00
|
|
|
int httpStatusCode) {
|
2011-05-20 16:42:25 +02:00
|
|
|
REQUIRE_UI_THREAD();
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
if (m_BrowserHwnd == browser->GetWindowHandle() && frame->IsMain()) {
|
2011-05-20 16:42:25 +02:00
|
|
|
// We've just finished loading a page
|
|
|
|
SetLoading(false);
|
|
|
|
|
|
|
|
CefRefPtr<CefDOMVisitor> visitor = GetDOMVisitor(frame->GetURL());
|
2012-01-10 00:46:23 +01:00
|
|
|
if (visitor.get())
|
2011-05-20 16:42:25 +02:00
|
|
|
frame->VisitDOM(visitor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ClientHandler::OnLoadError(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
|
|
|
ErrorCode errorCode,
|
|
|
|
const CefString& failedUrl,
|
2012-01-10 00:46:23 +01:00
|
|
|
CefString& errorText) {
|
2011-05-20 16:42:25 +02:00
|
|
|
REQUIRE_UI_THREAD();
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
if (errorCode == ERR_CACHE_MISS) {
|
2011-05-20 16:42:25 +02:00
|
|
|
// Usually caused by navigating to a page with POST data via back or
|
|
|
|
// forward buttons.
|
|
|
|
errorText = "<html><head><title>Expired Form Data</title></head>"
|
|
|
|
"<body><h1>Expired Form Data</h1>"
|
|
|
|
"<h2>Your form request has expired. "
|
|
|
|
"Click reload to re-submit the form data.</h2></body>"
|
|
|
|
"</html>";
|
|
|
|
} else {
|
|
|
|
// All other messages.
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << "<html><head><title>Load Failed</title></head>"
|
|
|
|
"<body><h1>Load Failed</h1>"
|
|
|
|
"<h2>Load of URL " << std::string(failedUrl) <<
|
|
|
|
" failed with error code " << static_cast<int>(errorCode) <<
|
|
|
|
".</h2></body>"
|
|
|
|
"</html>";
|
|
|
|
errorText = ss.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ClientHandler::GetDownloadHandler(CefRefPtr<CefBrowser> browser,
|
|
|
|
const CefString& mimeType,
|
|
|
|
const CefString& fileName,
|
|
|
|
int64 contentLength,
|
2012-01-10 00:46:23 +01:00
|
|
|
CefRefPtr<CefDownloadHandler>& handler) {
|
2011-05-20 16:42:25 +02:00
|
|
|
REQUIRE_UI_THREAD();
|
|
|
|
|
|
|
|
// Create the handler for the file download.
|
|
|
|
handler = CreateDownloadHandler(this, fileName);
|
2011-08-30 18:20:27 +02:00
|
|
|
|
|
|
|
// Close the browser window if it is a popup with no other document contents.
|
|
|
|
if (browser->IsPopup() && !browser->HasDocument())
|
|
|
|
browser->CloseBrowser();
|
|
|
|
|
2011-05-20 16:42:25 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientHandler::OnNavStateChange(CefRefPtr<CefBrowser> browser,
|
|
|
|
bool canGoBack,
|
2012-01-10 00:46:23 +01:00
|
|
|
bool canGoForward) {
|
2011-05-20 16:42:25 +02:00
|
|
|
REQUIRE_UI_THREAD();
|
|
|
|
|
|
|
|
SetNavState(canGoBack, canGoForward);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ClientHandler::OnConsoleMessage(CefRefPtr<CefBrowser> browser,
|
|
|
|
const CefString& message,
|
|
|
|
const CefString& source,
|
2012-01-10 00:46:23 +01:00
|
|
|
int line) {
|
2011-05-20 16:42:25 +02:00
|
|
|
REQUIRE_UI_THREAD();
|
|
|
|
|
|
|
|
bool first_message;
|
|
|
|
std::string logFile;
|
|
|
|
|
|
|
|
{
|
|
|
|
AutoLock lock_scope(this);
|
2012-01-10 00:46:23 +01:00
|
|
|
|
2011-05-20 16:42:25 +02:00
|
|
|
first_message = m_LogFile.empty();
|
2012-01-10 00:46:23 +01:00
|
|
|
if (first_message) {
|
2011-05-20 16:42:25 +02:00
|
|
|
std::stringstream ss;
|
|
|
|
ss << AppGetWorkingDirectory();
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
ss << "\\";
|
|
|
|
#else
|
|
|
|
ss << "/";
|
|
|
|
#endif
|
|
|
|
ss << "console.log";
|
|
|
|
m_LogFile = ss.str();
|
|
|
|
}
|
|
|
|
logFile = m_LogFile;
|
2012-01-10 00:46:23 +01:00
|
|
|
}
|
|
|
|
|
2011-05-20 16:42:25 +02:00
|
|
|
FILE* file = fopen(logFile.c_str(), "a");
|
2012-01-10 00:46:23 +01:00
|
|
|
if (file) {
|
2011-05-20 16:42:25 +02:00
|
|
|
std::stringstream ss;
|
|
|
|
ss << "Message: " << std::string(message) << "\r\nSource: " <<
|
|
|
|
std::string(source) << "\r\nLine: " << line <<
|
|
|
|
"\r\n-----------------------\r\n";
|
|
|
|
fputs(ss.str().c_str(), file);
|
|
|
|
fclose(file);
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
if (first_message)
|
2011-05-20 16:42:25 +02:00
|
|
|
SendNotification(NOTIFY_CONSOLE_MESSAGE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-10-14 14:40:40 +02:00
|
|
|
void ClientHandler::OnFocusedNodeChanged(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
2012-01-10 00:46:23 +01:00
|
|
|
CefRefPtr<CefDOMNode> node) {
|
2011-10-14 14:40:40 +02:00
|
|
|
REQUIRE_UI_THREAD();
|
|
|
|
|
|
|
|
// Set to true if a form element has focus.
|
|
|
|
m_bFormElementHasFocus = (node.get() && node->IsFormControlElement());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ClientHandler::OnKeyEvent(CefRefPtr<CefBrowser> browser,
|
|
|
|
KeyEventType type,
|
|
|
|
int code,
|
|
|
|
int modifiers,
|
2011-11-04 19:19:14 +01:00
|
|
|
bool isSystemKey,
|
2012-01-10 00:46:23 +01:00
|
|
|
bool isAfterJavaScript) {
|
2011-10-14 14:40:40 +02:00
|
|
|
REQUIRE_UI_THREAD();
|
|
|
|
|
2011-11-04 19:19:14 +01:00
|
|
|
if (isAfterJavaScript && !m_bFormElementHasFocus && code == 0x20) {
|
2011-10-14 14:40:40 +02:00
|
|
|
// Special handling for the space character if a form element does not have
|
|
|
|
// focus.
|
|
|
|
if (type == KEYEVENT_RAWKEYDOWN) {
|
|
|
|
browser->GetMainFrame()->ExecuteJavaScript(
|
|
|
|
"alert('You pressed the space bar!');", "", 0);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-05-20 16:42:25 +02:00
|
|
|
bool ClientHandler::GetPrintHeaderFooter(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
|
|
|
const CefPrintInfo& printInfo,
|
|
|
|
const CefString& url,
|
|
|
|
const CefString& title,
|
|
|
|
int currentPage,
|
|
|
|
int maxPages,
|
|
|
|
CefString& topLeft,
|
|
|
|
CefString& topCenter,
|
|
|
|
CefString& topRight,
|
|
|
|
CefString& bottomLeft,
|
|
|
|
CefString& bottomCenter,
|
2012-01-10 00:46:23 +01:00
|
|
|
CefString& bottomRight) {
|
2011-05-20 16:42:25 +02:00
|
|
|
REQUIRE_UI_THREAD();
|
|
|
|
|
|
|
|
// Place the page title at top left
|
|
|
|
topLeft = title;
|
|
|
|
// Place the page URL at top right
|
|
|
|
topRight = url;
|
2012-01-10 00:46:23 +01:00
|
|
|
|
2011-05-20 16:42:25 +02:00
|
|
|
// Place "Page X of Y" at bottom center
|
|
|
|
std::stringstream strstream;
|
|
|
|
strstream << "Page " << currentPage << " of " << maxPages;
|
|
|
|
bottomCenter = strstream.str();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-11-21 22:21:55 +01:00
|
|
|
void ClientHandler::OnContextCreated(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
2012-01-10 00:46:23 +01:00
|
|
|
CefRefPtr<CefV8Context> context) {
|
2011-05-20 16:42:25 +02:00
|
|
|
REQUIRE_UI_THREAD();
|
|
|
|
|
2012-10-26 22:39:24 +02:00
|
|
|
CefRefPtr<CefV8Value> object = context->GetGlobal();
|
|
|
|
|
2011-05-20 16:42:25 +02:00
|
|
|
// Add the V8 bindings.
|
2012-10-26 22:39:24 +02:00
|
|
|
InitBindingTest(browser, frame, object);
|
|
|
|
|
|
|
|
std::string url = frame->GetURL();
|
|
|
|
if (url == performance_test::kTestUrl)
|
|
|
|
performance_test::InitTest(browser, frame, object);
|
2011-05-20 16:42:25 +02:00
|
|
|
}
|
|
|
|
|
2011-08-22 03:31:55 +02:00
|
|
|
bool ClientHandler::OnDragStart(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefDragData> dragData,
|
2012-01-10 00:46:23 +01:00
|
|
|
DragOperationsMask mask) {
|
2011-08-22 03:31:55 +02:00
|
|
|
REQUIRE_UI_THREAD();
|
|
|
|
|
|
|
|
// Forbid dragging of image files.
|
|
|
|
if (dragData->IsFile()) {
|
2012-02-18 01:19:52 +01:00
|
|
|
std::string fileName = dragData->GetFileName();
|
|
|
|
if (fileName.find(".png") != std::string::npos ||
|
|
|
|
fileName.find(".jpg") != std::string::npos ||
|
|
|
|
fileName.find(".gif") != std::string::npos)
|
2011-08-22 03:31:55 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ClientHandler::OnDragEnter(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefDragData> dragData,
|
2012-01-10 00:46:23 +01:00
|
|
|
DragOperationsMask mask) {
|
2011-08-22 03:31:55 +02:00
|
|
|
REQUIRE_UI_THREAD();
|
|
|
|
|
|
|
|
// Forbid dragging of link URLs.
|
|
|
|
if (dragData->IsLink())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
bool ClientHandler::OnBeforeScriptExtensionLoad(
|
|
|
|
CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
|
|
|
const CefString& extensionName) {
|
2012-01-05 20:34:20 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-09-14 21:04:45 +02:00
|
|
|
void ClientHandler::OnRequestGeolocationPermission(
|
|
|
|
CefRefPtr<CefBrowser> browser,
|
|
|
|
const CefString& requesting_url,
|
|
|
|
int request_id,
|
|
|
|
CefRefPtr<CefGeolocationCallback> callback) {
|
|
|
|
// Allow geolocation access from all websites.
|
|
|
|
callback->Continue(true);
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
void ClientHandler::NotifyDownloadComplete(const CefString& fileName) {
|
2011-05-20 16:42:25 +02:00
|
|
|
SetLastDownloadFile(fileName);
|
|
|
|
SendNotification(NOTIFY_DOWNLOAD_COMPLETE);
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
void ClientHandler::NotifyDownloadError(const CefString& fileName) {
|
2011-05-20 16:42:25 +02:00
|
|
|
SetLastDownloadFile(fileName);
|
|
|
|
SendNotification(NOTIFY_DOWNLOAD_ERROR);
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
void ClientHandler::SetMainHwnd(CefWindowHandle hwnd) {
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
|
|
|
m_MainHwnd = hwnd;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
void ClientHandler::SetEditHwnd(CefWindowHandle hwnd) {
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
|
|
|
m_EditHwnd = hwnd;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientHandler::SetButtonHwnds(CefWindowHandle backHwnd,
|
|
|
|
CefWindowHandle forwardHwnd,
|
|
|
|
CefWindowHandle reloadHwnd,
|
2012-01-10 00:46:23 +01:00
|
|
|
CefWindowHandle stopHwnd) {
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
|
|
|
m_BackHwnd = backHwnd;
|
|
|
|
m_ForwardHwnd = forwardHwnd;
|
|
|
|
m_ReloadHwnd = reloadHwnd;
|
|
|
|
m_StopHwnd = stopHwnd;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
std::string ClientHandler::GetLogFile() {
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
|
|
|
return m_LogFile;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
void ClientHandler::SetLastDownloadFile(const std::string& fileName) {
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
|
|
|
m_LastDownloadFile = fileName;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
std::string ClientHandler::GetLastDownloadFile() {
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
|
|
|
return m_LastDownloadFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientHandler::AddDOMVisitor(const std::string& path,
|
2012-01-10 00:46:23 +01:00
|
|
|
CefRefPtr<CefDOMVisitor> visitor) {
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
|
|
|
DOMVisitorMap::iterator it = m_DOMVisitors.find(path);
|
|
|
|
if (it == m_DOMVisitors.end())
|
|
|
|
m_DOMVisitors.insert(std::make_pair(path, visitor));
|
|
|
|
else
|
|
|
|
it->second = visitor;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
CefRefPtr<CefDOMVisitor> ClientHandler::GetDOMVisitor(const std::string& path) {
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
|
|
|
DOMVisitorMap::iterator it = m_DOMVisitors.find(path);
|
|
|
|
if (it != m_DOMVisitors.end())
|
|
|
|
return it->second;
|
|
|
|
return NULL;
|
|
|
|
}
|