pulsar/native/atom_cef_client.cpp

133 lines
3.8 KiB
C++
Raw Normal View History

2012-08-10 23:32:19 +04: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.
#include <sstream>
2012-08-22 04:22:43 +04:00
#include <iostream>
2012-08-10 23:32:19 +04:00
#include "include/cef_path_util.h"
#include "include/cef_process_util.h"
#include "include/cef_runnable.h"
2012-08-28 00:21:59 +04:00
#include "native/atom_cef_client.h"
#include "cef_v8.h"
2012-08-10 23:32:19 +04:00
AtomCefClient::AtomCefClient(){
2012-08-28 20:58:24 +04:00
2012-08-10 23:32:19 +04:00
}
AtomCefClient::~AtomCefClient() {
2012-08-10 23:32:19 +04:00
}
bool AtomCefClient::OnProcessMessageReceived(CefRefPtr<CefBrowser> browser,
CefProcessId source_process,
2012-08-28 20:58:24 +04:00
CefRefPtr<CefProcessMessage> message) {
std::string name = message->GetName().ToString();
if (name == "open") {
2012-08-29 04:01:02 +04:00
bool hasArguments = message->GetArgumentList()->GetSize() == 1;
hasArguments ? Open(message->GetArgumentList()->GetString(0)) : Open();
return true;
}
2012-08-29 01:05:00 +04:00
return false;
}
void AtomCefClient::OnBeforeContextMenu(
2012-08-10 23:32:19 +04:00
CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefContextMenuParams> params,
CefRefPtr<CefMenuModel> model) {
2012-08-21 01:09:57 +04:00
model->AddItem(MENU_ID_USER_FIRST, "&Show DevTools");
2012-08-10 23:32:19 +04:00
}
bool AtomCefClient::OnContextMenuCommand(
2012-08-10 23:32:19 +04:00
CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefContextMenuParams> params,
int command_id,
EventFlags event_flags) {
2012-08-21 01:09:57 +04:00
if (command_id == MENU_ID_USER_FIRST) {
2012-08-23 02:29:39 +04:00
ShowDevTools(browser);
2012-08-21 01:09:57 +04:00
return true;
}
else {
return false;
2012-08-10 23:32:19 +04:00
}
}
bool AtomCefClient::OnConsoleMessage(CefRefPtr<CefBrowser> browser,
2012-08-10 23:32:19 +04:00
const CefString& message,
const CefString& source,
int line) {
REQUIRE_UI_THREAD();
2012-08-28 20:58:24 +04:00
std::cout << std::string(message) << "\n";
2012-08-22 04:22:43 +04:00
return true;
2012-08-10 23:32:19 +04:00
}
bool AtomCefClient::OnKeyEvent(CefRefPtr<CefBrowser> browser,
const CefKeyEvent& event,
CefEventHandle os_event) {
2012-08-28 20:58:24 +04:00
if (event.modifiers == KEY_META && event.character == 'r') {
browser->SendProcessMessage(PID_RENDERER, CefProcessMessage::Create("reload"));
}
}
void AtomCefClient::OnBeforeClose(CefRefPtr<CefBrowser> browser) {
2012-08-10 23:32:19 +04:00
REQUIRE_UI_THREAD();
2012-08-28 20:58:24 +04:00
// this was in cefclient... was there a good reason?
// if(m_BrowserHwnd == browser->GetWindowHandle()) {
// // Free the browser pointer so that the browser can be destroyed
// m_Browser = NULL;
// }
m_Browser = NULL;
2012-08-10 23:32:19 +04:00
}
void AtomCefClient::OnAfterCreated(CefRefPtr<CefBrowser> browser) {
2012-08-10 23:32:19 +04:00
REQUIRE_UI_THREAD();
2012-08-28 20:58:24 +04:00
2012-08-22 04:22:43 +04:00
AutoLock lock_scope(this);
if (!m_Browser.get()) {
m_Browser = browser;
2012-08-28 20:58:24 +04:00
}
2012-08-29 01:04:44 +04:00
GetBrowser()->GetHost()->SetFocus(true);
2012-08-10 23:32:19 +04:00
}
void AtomCefClient::OnLoadError(CefRefPtr<CefBrowser> browser,
2012-08-10 23:32:19 +04:00
CefRefPtr<CefFrame> frame,
ErrorCode errorCode,
const CefString& errorText,
const CefString& failedUrl) {
REQUIRE_UI_THREAD();
2012-08-22 04:22:43 +04:00
if (errorCode == ERR_ABORTED) { // Don't display an error for downloaded files.
2012-08-21 01:09:57 +04:00
return;
2012-08-28 20:58:24 +04:00
}
2012-08-21 01:09:57 +04:00
else if (errorCode == ERR_UNKNOWN_URL_SCHEME) { // Don't display an error for external protocols that we allow the OS to handle. See OnProtocolExecution().
2012-08-10 23:32:19 +04:00
return;
}
2012-08-28 20:58:24 +04:00
else {
2012-08-21 01:09:57 +04:00
std::stringstream ss;
ss << "<html><body><h2>Failed to load URL " << std::string(failedUrl) <<
" with error " << std::string(errorText) << " (" << errorCode <<
").</h2></body></html>";
frame->LoadString(ss.str(), failedUrl);
}
2012-08-10 23:32:19 +04:00
}
void AtomCefClient::ShowDevTools(CefRefPtr<CefBrowser> browser) {
2012-08-23 23:38:52 +04:00
std::string devtools_url = browser->GetHost()->GetDevToolsURL(true);
2012-08-10 23:32:19 +04:00
if (!devtools_url.empty()) {
2012-08-23 02:29:39 +04:00
browser->GetMainFrame()->ExecuteJavaScript("window.open('" + devtools_url + "');", "about:blank", 0);
2012-08-10 23:32:19 +04:00
}
}