pulsar/native/atom_cef_client.cpp

155 lines
4.4 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();
CefRefPtr<CefListValue> argumentList = message->GetArgumentList();
int messageId = argumentList->GetInt(0);
if (name == "open") {
bool hasArguments = argumentList->GetSize() > 1;
hasArguments ? Open(argumentList->GetString(1)) : Open();
2012-08-29 22:36:52 +04:00
return true;
2012-08-29 04:04:56 +04:00
}
2012-08-30 02:31:06 +04:00
else if (name == "newWindow") {
2012-08-29 04:04:56 +04:00
NewWindow();
2012-08-30 02:31:06 +04:00
}
else if (name == "toggleDevTools") {
ToggleDevTools(browser);
}
else {
return false;
}
if (name == "confirm") {
std::string message = argumentList->GetString(1).ToString();
std::string detailedMessage = argumentList->GetString(2).ToString();
std::vector<std::string> buttonLabels(argumentList->GetSize() - 3);
for (int i = 3; i < argumentList->GetSize(); i++) {
buttonLabels[i - 3] = argumentList->GetString(i).ToString();
}
Confirm(messageId, message, detailedMessage, buttonLabels, browser);
return true;
}
2012-08-29 01:05:00 +04:00
2012-08-30 02:31:06 +04:00
return true;
}
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-29 23:27:24 +04:00
model->Clear();
2012-08-30 02:31:06 +04:00
model->AddItem(MENU_ID_USER_FIRST, "&Toggle 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-30 02:31:06 +04:00
ToggleDevTools(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) {
if (event.modifiers == KEY_META && event.unmodified_character == 'r') {
browser->SendProcessMessage(PID_RENDERER, CefProcessMessage::Create("reload"));
}
else if (event.modifiers == (KEY_META | KEY_ALT) && event.unmodified_character == 'i') {
ToggleDevTools(browser);
}
else {
return false;
}
return true;
}
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
}