pulsar/native/atom_cef_client.cpp

148 lines
4.2 KiB
C++
Raw Normal View History

2012-08-10 23:32:19 +04:00
#include <sstream>
2012-08-22 04:22:43 +04:00
#include <iostream>
2012-08-30 20:07:39 +04:00
#include <assert.h>
2012-08-10 23:32:19 +04:00
#include "include/cef_path_util.h"
#include "include/cef_process_util.h"
2012-08-30 20:07:39 +04:00
#include "include/cef_task.h"
2012-08-10 23:32:19 +04:00
#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
2012-08-30 20:07:39 +04:00
#define REQUIRE_UI_THREAD() assert(CefCurrentlyOn(TID_UI));
#define REQUIRE_IO_THREAD() assert(CefCurrentlyOn(TID_IO));
#define REQUIRE_FILE_THREAD() assert(CefCurrentlyOn(TID_FILE));
2012-08-28 20:58:24 +04:00
2012-08-30 20:07:39 +04:00
AtomCefClient::AtomCefClient(){
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 04:04:56 +04:00
}
2012-08-30 19:47:21 +04:00
else if (name == "newWindow") {
2012-08-29 04:04:56 +04:00
NewWindow();
2012-08-30 02:31:06 +04:00
}
2012-08-30 19:47:21 +04:00
else if (name == "toggleDevTools") {
2012-08-30 02:31:06 +04:00
ToggleDevTools(browser);
}
else if (name == "showDevTools") {
ShowDevTools(browser);
}
2012-08-30 19:47:21 +04:00
else 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);
}
else if (name == "showSaveDialog") {
ShowSaveDialog(messageId, browser);
}
else if (name == "focus") {
GetBrowser()->GetHost()->SetFocus(true);
}
2012-08-30 19:47:21 +04:00
else {
return false;
}
2012-08-30 19:47:21 +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;
}
2012-08-30 09:27:30 +04:00
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
2012-08-30 19:47:21 +04:00
// TODO: Ask Marshal. 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-30 19:47:21 +04:00
frame->LoadString(std::string(errorText) + "<br />" + std::string(failedUrl), failedUrl);
2012-08-10 23:32:19 +04:00
}