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"
|
2012-08-28 03:39:12 +04:00
|
|
|
#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
|
|
|
}
|
|
|
|
|
2012-08-25 21:17:47 +04:00
|
|
|
AtomCefClient::~AtomCefClient() {
|
2012-08-10 23:32:19 +04:00
|
|
|
}
|
|
|
|
|
2012-08-28 20:08:50 +04:00
|
|
|
bool AtomCefClient::OnProcessMessageReceived(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefProcessId source_process,
|
2012-08-28 20:58:24 +04:00
|
|
|
CefRefPtr<CefProcessMessage> message) {
|
2012-08-28 20:08:50 +04:00
|
|
|
std::string name = message->GetName().ToString();
|
2012-08-30 03:31:49 +04:00
|
|
|
CefRefPtr<CefListValue> argumentList = message->GetArgumentList();
|
2012-08-29 02:20:49 +04:00
|
|
|
int messageId = argumentList->GetInt(0);
|
2012-08-30 00:33:36 +04:00
|
|
|
|
2012-08-28 20:08:50 +04:00
|
|
|
if (name == "open") {
|
2012-08-30 03:31:49 +04:00
|
|
|
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);
|
|
|
|
}
|
2012-08-30 21:24:01 +04:00
|
|
|
else if (name == "showDevTools") {
|
|
|
|
ShowDevTools(browser);
|
|
|
|
}
|
2012-08-30 19:47:21 +04:00
|
|
|
else if (name == "confirm") {
|
2012-08-30 03:31:49 +04:00
|
|
|
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);
|
|
|
|
}
|
2012-08-30 21:05:50 +04:00
|
|
|
else if (name == "showSaveDialog") {
|
2012-08-30 20:52:35 +04:00
|
|
|
ShowSaveDialog(messageId, browser);
|
2012-08-31 02:10:47 +04:00
|
|
|
}
|
|
|
|
else if (name == "focus") {
|
|
|
|
GetBrowser()->GetHost()->SetFocus(true);
|
2012-08-30 20:52:35 +04:00
|
|
|
}
|
2012-08-30 19:47:21 +04:00
|
|
|
else {
|
|
|
|
return false;
|
|
|
|
}
|
2012-08-30 20:52:35 +04:00
|
|
|
|
2012-08-30 19:47:21 +04:00
|
|
|
return true;
|
2012-08-28 20:08:50 +04:00
|
|
|
}
|
|
|
|
|
2012-08-25 21:17:47 +04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-08-25 21:17:47 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-25 21:17:47 +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
|
|
|
}
|
|
|
|
|
2012-08-28 03:39:12 +04:00
|
|
|
|
|
|
|
bool AtomCefClient::OnKeyEvent(CefRefPtr<CefBrowser> browser,
|
|
|
|
const CefKeyEvent& event,
|
|
|
|
CefEventHandle os_event) {
|
2012-08-30 03:03:13 +04:00
|
|
|
if (event.modifiers == KEY_META && event.unmodified_character == 'r') {
|
2012-08-28 03:39:12 +04:00
|
|
|
browser->SendProcessMessage(PID_RENDERER, CefProcessMessage::Create("reload"));
|
|
|
|
}
|
2012-08-30 03:03:13 +04:00
|
|
|
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
|
|
|
|
2012-08-30 03:03:13 +04:00
|
|
|
return true;
|
2012-08-28 03:39:12 +04:00
|
|
|
}
|
|
|
|
|
2012-08-25 21:17:47 +04:00
|
|
|
void AtomCefClient::OnBeforeClose(CefRefPtr<CefBrowser> browser) {
|
2012-08-10 23:32:19 +04:00
|
|
|
REQUIRE_UI_THREAD();
|
2012-08-25 03:48:27 +04:00
|
|
|
|
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?
|
2012-08-25 03:48:27 +04:00
|
|
|
// if(m_BrowserHwnd == browser->GetWindowHandle()) {
|
|
|
|
// // Free the browser pointer so that the browser can be destroyed
|
|
|
|
// m_Browser = NULL;
|
|
|
|
// }
|
2012-08-28 04:10:28 +04:00
|
|
|
|
2012-08-25 03:48:27 +04:00
|
|
|
m_Browser = NULL;
|
2012-08-10 23:32:19 +04:00
|
|
|
}
|
|
|
|
|
2012-08-25 21:17:47 +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
|
|
|
}
|
|
|
|
|
2012-08-25 21:17:47 +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
|
|
|
}
|