- Add CefHandler::HandleConsoleMessage callback for handling console messages (issue #90).

- Normalize newlines in browser_webkit_init.h.

git-svn-id: https://chromiumembedded.googlecode.com/svn/trunk@84 5089003a-bbd8-11dd-ad1f-f1f9622dbc98
This commit is contained in:
Marshall Greenblatt 2010-06-22 14:49:03 +00:00
parent 2799fccb7d
commit de4cee4415
10 changed files with 129 additions and 6 deletions

View File

@ -630,6 +630,13 @@ public:
int code,
int modifiers,
bool isSystemKey) =0;
// Called to display a console message. Return RV_HANDLED to stop the message
// from being output to the console.
/*--cef()--*/
virtual RetVal HandleConsoleMessage(CefRefPtr<CefBrowser> browser,
const std::wstring& message,
const std::wstring& source, int line) =0;
};

View File

@ -489,6 +489,12 @@ typedef struct _cef_handler_t
enum cef_handler_keyevent_type_t type, int code, int modifiers,
int isSystemKey);
// Called to display a console message. Return RV_HANDLED to stop the message
// from being output to the console.
enum cef_retval_t (CEF_CALLBACK *handle_console_message)(
struct _cef_handler_t* self, struct _cef_browser_t* browser,
const wchar_t* message, const wchar_t* source, int line);
} cef_handler_t;

View File

@ -93,8 +93,8 @@ class BrowserWebKitInit : public webkit_glue::WebKitClientImpl {
return &clipboard_;
}
virtual WebKit::WebFileSystem* fileSystem() {
return &file_system_;
virtual WebKit::WebFileSystem* fileSystem() {
return &file_system_;
}
virtual WebKit::WebSandboxSupport* sandboxSupport() {
@ -178,8 +178,8 @@ class BrowserWebKitInit : public webkit_glue::WebKitClientImpl {
WebKit::WebStorageNamespace::m_localStorageQuota);
}
virtual WebKit::WebIndexedDatabase* indexedDatabase() {
return WebKit::WebIndexedDatabase::create();
virtual WebKit::WebIndexedDatabase* indexedDatabase() {
return WebKit::WebIndexedDatabase::create();
}
private:

View File

@ -164,6 +164,17 @@ WebStorageNamespace* BrowserWebViewDelegate::createSessionStorageNamespace(
void BrowserWebViewDelegate::didAddMessageToConsole(
const WebConsoleMessage& message, const WebString& source_name,
unsigned source_line) {
std::wstring wmessage = UTF16ToWideHack(message.text);
std::wstring wsource = UTF16ToWideHack(source_name);
CefHandler::RetVal rv = RV_CONTINUE;
CefRefPtr<CefHandler> handler = browser_->GetHandler();
if(handler.get()) {
rv = handler->HandleConsoleMessage(browser_, wmessage, wsource,
source_line);
}
if(rv == RV_CONTINUE) {
logging::LogMessage("CONSOLE", 0).stream() << "\""
<< message.text.utf8().data()
<< ",\" source: "
@ -171,6 +182,7 @@ void BrowserWebViewDelegate::didAddMessageToConsole(
<< "("
<< source_line
<< ")";
}
}
void BrowserWebViewDelegate::didStartLoading() {

View File

@ -15,7 +15,6 @@
#include "libcef_dll/ctocpp/frame_ctocpp.h"
#include "libcef_dll/ctocpp/request_ctocpp.h"
#include "libcef_dll/ctocpp/stream_reader_ctocpp.h"
#include "libcef_dll/ctocpp/v8value_ctocpp.h"
#include "libcef_dll/transfer_util.h"
@ -458,6 +457,25 @@ enum cef_retval_t CEF_CALLBACK handler_handle_key_event(
modifiers, isSystemKey?true:false);
}
enum cef_retval_t CEF_CALLBACK handler_handle_console_message(
struct _cef_handler_t* self, cef_browser_t* browser, const wchar_t* message,
const wchar_t* source, int line)
{
DCHECK(self);
DCHECK(browser);
if(!self || !browser)
return RV_CONTINUE;
std::wstring messageStr, sourceStr;
if(message)
messageStr = message;
if(source)
sourceStr = source;
return CefHandlerCppToC::Get(self)->HandleConsoleMessage(
CefBrowserCToCpp::Wrap(browser), messageStr, sourceStr, line);
}
// CONSTRUCTOR - Do not edit by hand.
@ -487,6 +505,7 @@ CefHandlerCppToC::CefHandlerCppToC(CefHandler* cls)
struct_.struct_.handle_take_focus = handler_handle_take_focus;
struct_.struct_.handle_set_focus = handler_handle_set_focus;
struct_.struct_.handle_key_event = handler_handle_key_event;
struct_.struct_.handle_console_message = handler_handle_console_message;
}
#ifdef _DEBUG

View File

@ -14,7 +14,6 @@
#include "libcef_dll/cpptoc/frame_cpptoc.h"
#include "libcef_dll/cpptoc/request_cpptoc.h"
#include "libcef_dll/cpptoc/stream_reader_cpptoc.h"
#include "libcef_dll/cpptoc/v8value_cpptoc.h"
#include "libcef_dll/ctocpp/handler_ctocpp.h"
#include "libcef_dll/transfer_util.h"
@ -349,6 +348,17 @@ CefHandler::RetVal CefHandlerCToCpp::HandleKeyEvent(
type, code, modifiers, isSystemKey);
}
CefHandler::RetVal CefHandlerCToCpp::HandleConsoleMessage(
CefRefPtr<CefBrowser> browser, const std::wstring& message,
const std::wstring& source, int line)
{
if(CEF_MEMBER_MISSING(struct_, handle_console_message))
return RV_CONTINUE;
return struct_->handle_console_message(struct_,
CefBrowserCppToC::Wrap(browser), message.c_str(), source.c_str(), line);
}
#ifdef _DEBUG
long CefCToCpp<CefHandlerCToCpp, CefHandler, cef_handler_t>::DebugObjCt = 0;

View File

@ -76,6 +76,8 @@ public:
virtual RetVal HandleSetFocus(CefRefPtr<CefBrowser> browser, bool isWidget);
virtual RetVal HandleKeyEvent(CefRefPtr<CefBrowser> browser,
KeyEventType type, int code, int modifiers, bool isSystemKey);
virtual RetVal HandleConsoleMessage(CefRefPtr<CefBrowser> browser,
const std::wstring& message, const std::wstring& source, int line);
};
#endif // BUILDING_CEF_SHARED

View File

@ -27,6 +27,7 @@
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
TCHAR szWorkingDir[MAX_PATH]; // The current working directory
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
@ -44,6 +45,10 @@ int APIENTRY wWinMain(HINSTANCE hInstance,
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// Retrieve the current working directory.
if(_wgetcwd(szWorkingDir, MAX_PATH) == NULL)
szWorkingDir[0] = 0;
#ifdef TEST_SINGLE_THREADED_MESSAGE_LOOP
// Initialize the CEF with messages processed using the current application's
// message loop.
@ -522,6 +527,41 @@ public:
return RV_CONTINUE;
}
// Called to display a console message. Return RV_HANDLED to stop the message
// from being output to the console.
RetVal HandleConsoleMessage(CefRefPtr<CefBrowser> browser,
const std::wstring& message,
const std::wstring& source, int line)
{
Lock();
bool first_message = m_LogFile.empty();
if(first_message) {
std::wstringstream ss;
ss << szWorkingDir << L"\\console.log";
m_LogFile = ss.str();
}
std::wstring logFile = m_LogFile;
Unlock();
FILE* file = NULL;
_wfopen_s(&file, logFile.c_str(), L"a, ccs=UTF-8");
if(file) {
std::wstringstream ss;
ss << L"Message: " << message << L"\r\nSource: " << source <<
L"\r\nLine: " << line << L"\r\n-----------------------\r\n";
fputws(ss.str().c_str(), file);
fclose(file);
if(first_message) {
// Show the message box on the UI thread.
PostMessage(m_MainHwnd, WM_COMMAND, ID_WARN_CONSOLEMESSAGE, 0);
}
}
return RV_HANDLED;
}
// Retrieve the current navigation state flags
void GetNavState(bool &isLoading, bool &canGoBack, bool &canGoForward)
{
@ -557,6 +597,14 @@ public:
return m_BrowserHwnd;
}
std::wstring GetLogFile()
{
Lock();
std::wstring str = m_LogFile;
Unlock();
return str;
}
protected:
// The child browser window
CefRefPtr<CefBrowser> m_Browser;
@ -576,6 +624,8 @@ protected:
bool m_bCanGoBack;
// True if the user can navigate forwards
bool m_bCanGoForward;
std::wstring m_LogFile;
};
// global handler instance
@ -765,6 +815,15 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
case IDM_EXIT:
DestroyWindow(hWnd);
return 0;
case ID_WARN_CONSOLEMESSAGE:
if(g_handler.get()) {
std::wstringstream ss;
ss << L"Console messages will be written to "
<< g_handler->GetLogFile();
MessageBoxW(hWnd, ss.str().c_str(), L"Console Messages",
MB_OK | MB_ICONINFORMATION);
}
return 0;
case IDC_NAV_BACK: // Back button
if(browser.get())
browser->GoBack();

View File

@ -21,6 +21,7 @@
#define IDC_NAV_FORWARD 201
#define IDC_NAV_RELOAD 202
#define IDC_NAV_STOP 203
#define ID_WARN_CONSOLEMESSAGE 32000
#define ID_TESTS_GETSOURCE 32769
#define ID_TESTS_GETTEXT 32770
#define ID_TESTS_JAVASCRIPT_HANDLER 32771

View File

@ -202,6 +202,13 @@ public:
return RV_CONTINUE;
}
virtual RetVal HandleConsoleMessage(CefRefPtr<CefBrowser> browser,
const std::wstring& message,
const std::wstring& source, int line)
{
return RV_CONTINUE;
}
CefRefPtr<CefBrowser> GetBrowser()
{
return browser_;