Skip to content
Snippets Groups Projects
Commit 8979ec41 authored by Sergey Linev's avatar Sergey Linev Committed by Bertrand Bellenot
Browse files

webgui: support cef messaging also in gui mode

derived SimpleHandler from BaseHandler
parent 203bf2ff
No related branches found
No related tags found
No related merge requests found
...@@ -64,7 +64,7 @@ public: ...@@ -64,7 +64,7 @@ public:
virtual void SendCharStar(const char *buf) virtual void SendCharStar(const char *buf)
{ {
printf("CEF sends message to client %s\n", buf); // printf("CEF sends message to client %s\n", buf);
fCallback->Success(buf); // send next message to JS fCallback->Success(buf); // send next message to JS
} }
...@@ -80,7 +80,7 @@ public: ...@@ -80,7 +80,7 @@ public:
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
// // // //
// TCefCallArg // // TCefWsCallArg //
// // // //
// HTTp call argument provided to http server from CEF messaging // // HTTp call argument provided to http server from CEF messaging //
// Allows immediately reply to JS client // // Allows immediately reply to JS client //
...@@ -88,12 +88,12 @@ public: ...@@ -88,12 +88,12 @@ public:
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
class TCefCallArg : public THttpCallArg { class TCefWsCallArg : public THttpCallArg {
protected: protected:
CefRefPtr<CefMessageRouterBrowserSide::Callback> fCallback; CefRefPtr<CefMessageRouterBrowserSide::Callback> fCallback;
public: public:
TCefCallArg(CefRefPtr<CefMessageRouterBrowserSide::Callback> callback) : fCallback(callback) {} TCefWsCallArg(CefRefPtr<CefMessageRouterBrowserSide::Callback> callback) : fCallback(callback) {}
virtual void HttpReplied() virtual void HttpReplied()
{ {
...@@ -143,7 +143,7 @@ public: ...@@ -143,7 +143,7 @@ public:
std::string url = message.substr(0, pos); std::string url = message.substr(0, pos);
message.erase(0, pos + 2); message.erase(0, pos + 2);
TCefCallArg *arg = new TCefCallArg(callback); TCefWsCallArg *arg = new TCefWsCallArg(callback);
arg->SetPathName(url.c_str()); arg->SetPathName(url.c_str());
arg->SetFileName("root.ws_emulation"); arg->SetFileName("root.ws_emulation");
......
...@@ -12,8 +12,6 @@ ...@@ -12,8 +12,6 @@
// #include "include/cef_process_message.h" // #include "include/cef_process_message.h"
bool gBatchMode = false;
class ROOTV8Handler : public CefV8Handler { class ROOTV8Handler : public CefV8Handler {
public: public:
ROOTV8Handler() {} ROOTV8Handler() {}
...@@ -52,11 +50,9 @@ public: ...@@ -52,11 +50,9 @@ public:
// CefRenderProcessHandler methods: // CefRenderProcessHandler methods:
void OnWebKitInitialized() OVERRIDE void OnWebKitInitialized() OVERRIDE
{ {
if (gBatchMode) { // Create the renderer-side router for query handling.
// Create the renderer-side router for query handling. CefMessageRouterConfig config;
CefMessageRouterConfig config; message_router_ = CefMessageRouterRendererSide::Create(config);
message_router_ = CefMessageRouterRendererSide::Create(config);
}
} }
// CefRenderProcessHandler methods // CefRenderProcessHandler methods
...@@ -65,56 +61,53 @@ public: ...@@ -65,56 +61,53 @@ public:
{ {
printf("MyRendererProcessApp::OnContextCreated\n"); printf("MyRendererProcessApp::OnContextCreated\n");
if (gBatchMode) message_router_->OnContextCreated(browser, frame, context); message_router_->OnContextCreated(browser, frame, context);
/*
// Retrieve the context's window object.
CefRefPtr<CefV8Value> object = context->GetGlobal();
// Create a new V8 string value. See the "Basic JS Types" section below. /*
CefRefPtr<CefV8Value> str = CefV8Value::CreateString("My Value!"); // Retrieve the context's window object.
CefRefPtr<CefV8Value> object = context->GetGlobal();
// Add the string to the window object as "window.myval". See the "JS Objects" section below. // Create a new V8 string value. See the "Basic JS Types" section below.
object->SetValue("ROOT_BATCH_FLAG", str, V8_PROPERTY_ATTRIBUTE_NONE); CefRefPtr<CefV8Value> str = CefV8Value::CreateString("My Value!");
printf("ADD BATCH FALG\n"); // Add the string to the window object as "window.myval". See the "JS Objects" section below.
object->SetValue("ROOT_BATCH_FLAG", str, V8_PROPERTY_ATTRIBUTE_NONE);
CefRefPtr<CefV8Handler> handler = new ROOTV8Handler; printf("ADD BATCH FALG\n");
CefRefPtr<CefV8Value> func = CefV8Value::CreateFunction("ROOT_BATCH_FUNC", handler);
// Add the string to the window object as "window.myval". See the "JS Objects" section below. CefRefPtr<CefV8Handler> handler = new ROOTV8Handler;
object->SetValue("ROOT_BATCH_FUNC", func, V8_PROPERTY_ATTRIBUTE_NONE); CefRefPtr<CefV8Value> func = CefV8Value::CreateFunction("ROOT_BATCH_FUNC", handler);
printf("ADD BATCH FUNC\n"); // Add the string to the window object as "window.myval". See the "JS Objects" section below.
object->SetValue("ROOT_BATCH_FUNC", func, V8_PROPERTY_ATTRIBUTE_NONE);
CefRefPtr<CefProcessMessage> msg = CefProcessMessage::Create("my_message"); printf("ADD BATCH FUNC\n");
// Retrieve the argument list object. CefRefPtr<CefProcessMessage> msg = CefProcessMessage::Create("my_message");
CefRefPtr<CefListValue> args = msg->GetArgumentList();
// Populate the argument values. // Retrieve the argument list object.
args->SetString(0, "my string"); CefRefPtr<CefListValue> args = msg->GetArgumentList();
args->SetInt(1, 10);
// Send the process message to the render process. // Populate the argument values.
// Use PID_BROWSER instead when sending a message to the browser process. args->SetString(0, "my string");
browser->SendProcessMessage(PID_BROWSER, msg); args->SetInt(1, 10);
*/
// Send the process message to the render process.
// Use PID_BROWSER instead when sending a message to the browser process.
browser->SendProcessMessage(PID_BROWSER, msg);
*/
} }
void OnContextReleased(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, void OnContextReleased(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame,
CefRefPtr<CefV8Context> context) OVERRIDE CefRefPtr<CefV8Context> context) OVERRIDE
{ {
if (gBatchMode) message_router_->OnContextReleased(browser, frame, context); message_router_->OnContextReleased(browser, frame, context);
} }
bool OnProcessMessageReceived(CefRefPtr<CefBrowser> browser, CefProcessId source_process, bool OnProcessMessageReceived(CefRefPtr<CefBrowser> browser, CefProcessId source_process,
CefRefPtr<CefProcessMessage> message) OVERRIDE CefRefPtr<CefProcessMessage> message) OVERRIDE
{ {
if (gBatchMode) return message_router_->OnProcessMessageReceived(browser, source_process, message); return message_router_->OnProcessMessageReceived(browser, source_process, message);
return false;
} }
/* virtual bool OnProcessMessageReceived(CefRefPtr<CefBrowser> browser, CefProcessId source_process, /* virtual bool OnProcessMessageReceived(CefRefPtr<CefBrowser> browser, CefProcessId source_process,
...@@ -135,18 +128,12 @@ private: ...@@ -135,18 +128,12 @@ private:
// Entry point function for all processes. // Entry point function for all processes.
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
printf("Starting CEF_MAIN ARGC %d\n", argc); // printf("Starting CEF_MAIN ARGC %d\n", argc);
for (int n = 1; n < argc; n++) printf("ARGV[%d] = %s\n", n, argv[n]); // for (int n = 1; n < argc; n++) printf("ARGV[%d] = %s\n", n, argv[n]);
if ((argc>1) && !strcmp(argv[argc-1],"--root-batch")) {
argc--;
gBatchMode = true;
}
// Provide CEF with command-line arguments. // Provide CEF with command-line arguments.
CefMainArgs main_args(argc, argv); CefMainArgs main_args(argc, argv);
CefRefPtr<CefApp> app = new MyRendererProcessApp(); CefRefPtr<CefApp> app = new MyRendererProcessApp();
// CEF applications have multiple sub-processes (render, plugin, GPU, etc) // CEF applications have multiple sub-processes (render, plugin, GPU, etc)
......
...@@ -7,12 +7,6 @@ ...@@ -7,12 +7,6 @@
#include "osr_handler.h" #include "osr_handler.h"
//#include "THttpServer.h"
//#include "THttpEngine.h"
//#include "THttpCallArg.h"
//#include "TRootSniffer.h"
//#include "TString.h"
#include <sstream> #include <sstream>
#include <string> #include <string>
......
...@@ -25,7 +25,7 @@ ...@@ -25,7 +25,7 @@
THttpServer *gHandlingServer = 0; THttpServer *gHandlingServer = 0;
// TODO: memory cleanup of these arguments // TODO: memory cleanup of these arguments
class TCEFCallArg : public THttpCallArg, public CefResourceHandler { class TCefHttpCallArg : public THttpCallArg, public CefResourceHandler {
protected: protected:
// QWebEngineUrlRequestJob *fRequest; // QWebEngineUrlRequestJob *fRequest;
int fDD; int fDD;
...@@ -35,9 +35,9 @@ protected: ...@@ -35,9 +35,9 @@ protected:
int offset_; int offset_;
public: public:
TCEFCallArg() : THttpCallArg(), CefResourceHandler(), fDD(0), callback_(), offset_(0) {} TCefHttpCallArg() : THttpCallArg(), CefResourceHandler(), fDD(0), callback_(), offset_(0) {}
virtual ~TCEFCallArg() virtual ~TCefHttpCallArg()
{ {
if (fDD != 1) printf("FAAAAAAAAAAAAAIL %d\n", fDD); if (fDD != 1) printf("FAAAAAAAAAAAAAIL %d\n", fDD);
fDD = -1; fDD = -1;
...@@ -107,8 +107,8 @@ public: ...@@ -107,8 +107,8 @@ public:
return has_data; return has_data;
} }
IMPLEMENT_REFCOUNTING(TCEFCallArg); IMPLEMENT_REFCOUNTING(TCefHttpCallArg);
DISALLOW_COPY_AND_ASSIGN(TCEFCallArg); DISALLOW_COPY_AND_ASSIGN(TCefHttpCallArg);
}; };
namespace { namespace {
...@@ -191,7 +191,7 @@ public: ...@@ -191,7 +191,7 @@ public:
return new CefStreamResourceHandler(mime, stream); return new CefStreamResourceHandler(mime, stream);
} }
TCEFCallArg *arg = new TCEFCallArg(); TCefHttpCallArg *arg = new TCefHttpCallArg();
arg->SetPathAndFileName(inp_path); arg->SetPathAndFileName(inp_path);
arg->SetQuery(inp_query); arg->SetQuery(inp_query);
arg->SetMethod(inp_method); arg->SetMethod(inp_method);
...@@ -238,14 +238,11 @@ void SimpleApp::OnBeforeChildProcessLaunch(CefRefPtr<CefCommandLine> command_lin ...@@ -238,14 +238,11 @@ void SimpleApp::OnBeforeChildProcessLaunch(CefRefPtr<CefCommandLine> command_lin
{ {
std::string newprog = fCefMain; std::string newprog = fCefMain;
command_line->SetProgram(newprog); command_line->SetProgram(newprog);
std::string prog = command_line->GetProgram().ToString(); std::string prog = command_line->GetProgram().ToString();
if (fBatch) command_line->AppendArgument("--root-batch"); // if (fBatch) command_line->AppendArgument("--root-batch");
// printf("OnBeforeChildProcessLaunch %s\n", prog.c_str());
printf("OnBeforeChildProcessLaunch %s\n", prog.c_str());
} }
/* /*
...@@ -310,7 +307,7 @@ void SimpleApp::OnContextInitialized() ...@@ -310,7 +307,7 @@ void SimpleApp::OnContextInitialized()
#endif #endif
// SimpleHandler implements browser-level callbacks. // SimpleHandler implements browser-level callbacks.
CefRefPtr<SimpleHandler> handler(new SimpleHandler(use_views)); CefRefPtr<SimpleHandler> handler(new SimpleHandler(gHandlingServer, use_views));
if (use_views) { if (use_views) {
// Create the BrowserView. // Create the BrowserView.
......
// Copyright (c) 2013 The Chromium Embedded Framework Authors. All rights /// \file gui_handler.h
// reserved. Use of this source code is governed by a BSD-style license that /// \ingroup CanvasPainter ROOT7
// can be found in the LICENSE file. /// \author Sergey Linev <S.Linev@gsi.de>
/// \date 2017-06-29
/// \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback
/// is welcome!
/*************************************************************************
* Copyright (C) 1995-2017, Rene Brun and Fons Rademakers. *
* All rights reserved. *
* *
* For the licensing terms see $ROOTSYS/LICENSE. *
* For the list of contributors see $ROOTSYS/README/CREDITS. *
*************************************************************************/
#include "simple_handler.h" #include "simple_handler.h"
...@@ -20,21 +31,12 @@ SimpleHandler *g_instance = NULL; ...@@ -20,21 +31,12 @@ SimpleHandler *g_instance = NULL;
} // namespace } // namespace
SimpleHandler::SimpleHandler(bool use_views) : use_views_(use_views), is_closing_(false) SimpleHandler::SimpleHandler(THttpServer *serv, bool use_views) : BaseHandler(serv), use_views_(use_views)
{ {
DCHECK(!g_instance);
g_instance = this;
} }
SimpleHandler::~SimpleHandler() SimpleHandler::~SimpleHandler()
{ {
g_instance = NULL;
}
// static
SimpleHandler *SimpleHandler::GetInstance()
{
return g_instance;
} }
void SimpleHandler::OnTitleChange(CefRefPtr<CefBrowser> browser, const CefString &title) void SimpleHandler::OnTitleChange(CefRefPtr<CefBrowser> browser, const CefString &title)
...@@ -59,7 +61,6 @@ bool SimpleHandler::OnConsoleMessage(CefRefPtr<CefBrowser> browser, const CefStr ...@@ -59,7 +61,6 @@ bool SimpleHandler::OnConsoleMessage(CefRefPtr<CefBrowser> browser, const CefStr
{ {
printf("CONSOLE: %s\n", message.ToString().c_str()); printf("CONSOLE: %s\n", message.ToString().c_str());
//CefRefPtr<CefProcessMessage> msg = CefProcessMessage::Create("PING"); //CefRefPtr<CefProcessMessage> msg = CefProcessMessage::Create("PING");
// Send the process message to the render process. // Send the process message to the render process.
// Use PID_BROWSER instead when sending a message to the browser process. // Use PID_BROWSER instead when sending a message to the browser process.
...@@ -67,88 +68,3 @@ bool SimpleHandler::OnConsoleMessage(CefRefPtr<CefBrowser> browser, const CefStr ...@@ -67,88 +68,3 @@ bool SimpleHandler::OnConsoleMessage(CefRefPtr<CefBrowser> browser, const CefStr
return true; return true;
} }
void SimpleHandler::OnAfterCreated(CefRefPtr<CefBrowser> browser)
{
CEF_REQUIRE_UI_THREAD();
// Add to the list of existing browsers.
browser_list_.push_back(browser);
}
bool SimpleHandler::DoClose(CefRefPtr<CefBrowser> browser)
{
CEF_REQUIRE_UI_THREAD();
// Closing the main window requires special handling. See the DoClose()
// documentation in the CEF header for a detailed destription of this
// process.
if (browser_list_.size() == 1) {
// Set a flag to indicate that the window close should be allowed.
is_closing_ = true;
}
// Allow the close. For windowed browsers this will result in the OS close
// event being sent.
return false;
}
void SimpleHandler::OnBeforeClose(CefRefPtr<CefBrowser> browser)
{
CEF_REQUIRE_UI_THREAD();
// Remove from the list of existing browsers.
BrowserList::iterator bit = browser_list_.begin();
for (; bit != browser_list_.end(); ++bit) {
if ((*bit)->IsSame(browser)) {
browser_list_.erase(bit);
break;
}
}
if (browser_list_.empty()) {
// All browser windows have closed. Quit the application message loop.
CefQuitMessageLoop();
}
}
void SimpleHandler::OnLoadError(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, ErrorCode errorCode,
const CefString &errorText, const CefString &failedUrl)
{
CEF_REQUIRE_UI_THREAD();
// Don't display an error for downloaded files.
if (errorCode == ERR_ABORTED) return;
// Display a load error message.
std::stringstream ss;
ss << "<html><body bgcolor=\"white\">"
"<h2>Failed to load URL "
<< std::string(failedUrl) << " with error " << std::string(errorText) << " (" << errorCode
<< ").</h2></body></html>";
frame->LoadString(ss.str(), failedUrl);
}
void SimpleHandler::CloseAllBrowsers(bool force_close)
{
if (!CefCurrentlyOn(TID_UI)) {
// Execute on the UI thread.
CefPostTask(TID_UI, base::Bind(&SimpleHandler::CloseAllBrowsers, this, force_close));
return;
}
if (browser_list_.empty()) return;
BrowserList::const_iterator it = browser_list_.begin();
for (; it != browser_list_.end(); ++it) (*it)->GetHost()->CloseBrowser(force_close);
}
bool SimpleHandler::OnProcessMessageReceived(CefRefPtr<CefBrowser> browser, CefProcessId source_process,
CefRefPtr<CefProcessMessage> message)
{
const std::string& message_name = message->GetName();
printf("SimpleHandler::OnProcessMessageReceived %s\n", message_name.c_str());
return true;
}
// Copyright (c) 2013 The Chromium Embedded Framework Authors. All rights /// \file gui_handler.h
// reserved. Use of this source code is governed by a BSD-style license that /// \ingroup CanvasPainter ROOT7
// can be found in the LICENSE file. /// \author Sergey Linev <S.Linev@gsi.de>
/// \date 2017-06-29
/// \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback
/// is welcome!
/*************************************************************************
* Copyright (C) 1995-2017, Rene Brun and Fons Rademakers. *
* All rights reserved. *
* *
* For the licensing terms see $ROOTSYS/LICENSE. *
* For the list of contributors see $ROOTSYS/README/CREDITS. *
*************************************************************************/
#ifndef ROOT_cef_simple_handler #ifndef ROOT_cef_simple_handler
#define ROOT_cef_simple_handler #define ROOT_cef_simple_handler
#include "include/cef_client.h" #include "base_handler.h"
#include <list> class SimpleHandler : public BaseHandler, public CefDisplayHandler {
class SimpleHandler : public CefClient, public CefDisplayHandler, public CefLifeSpanHandler, public CefLoadHandler {
public: public:
explicit SimpleHandler(bool use_views); explicit SimpleHandler(THttpServer *serv = 0, bool use_views = false);
~SimpleHandler(); ~SimpleHandler();
// Provide access to the single global instance of this object.
static SimpleHandler *GetInstance();
// CefClient methods: // CefClient methods:
virtual CefRefPtr<CefDisplayHandler> GetDisplayHandler() OVERRIDE { return this; } virtual CefRefPtr<CefDisplayHandler> GetDisplayHandler() OVERRIDE { return this; }
virtual CefRefPtr<CefLifeSpanHandler> GetLifeSpanHandler() OVERRIDE { return this; }
virtual CefRefPtr<CefLoadHandler> GetLoadHandler() OVERRIDE { return this; }
// CefDisplayHandler methods: // CefDisplayHandler methods:
virtual void OnTitleChange(CefRefPtr<CefBrowser> browser, const CefString &title) OVERRIDE; virtual void OnTitleChange(CefRefPtr<CefBrowser> browser, const CefString &title) OVERRIDE;
// CefLifeSpanHandler methods:
virtual void OnAfterCreated(CefRefPtr<CefBrowser> browser) OVERRIDE;
virtual bool DoClose(CefRefPtr<CefBrowser> browser) OVERRIDE;
virtual void OnBeforeClose(CefRefPtr<CefBrowser> browser) OVERRIDE;
// CefLoadHandler methods:
virtual void OnLoadError(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, ErrorCode errorCode,
const CefString &errorText, const CefString &failedUrl) OVERRIDE;
virtual bool OnConsoleMessage(CefRefPtr<CefBrowser> browser, const CefString &message, const CefString &source, virtual bool OnConsoleMessage(CefRefPtr<CefBrowser> browser, const CefString &message, const CefString &source,
int line) OVERRIDE; int line) OVERRIDE;
virtual bool OnProcessMessageReceived(CefRefPtr<CefBrowser> browser, CefProcessId source_process,
CefRefPtr<CefProcessMessage> message) OVERRIDE;
// Request that all existing browser windows close.
void CloseAllBrowsers(bool force_close);
bool IsClosing() const { return is_closing_; }
private: private:
// Platform-specific implementation. // Platform-specific implementation.
void PlatformTitleChange(CefRefPtr<CefBrowser> browser, const CefString &title); void PlatformTitleChange(CefRefPtr<CefBrowser> browser, const CefString &title);
...@@ -52,15 +39,9 @@ private: ...@@ -52,15 +39,9 @@ private:
// True if the application is using the Views framework. // True if the application is using the Views framework.
const bool use_views_; const bool use_views_;
// List of existing browser windows. Only accessed on the CEF UI thread.
typedef std::list<CefRefPtr<CefBrowser>> BrowserList;
BrowserList browser_list_;
bool is_closing_;
// Include the default reference counting implementation. // Include the default reference counting implementation.
IMPLEMENT_REFCOUNTING(SimpleHandler); IMPLEMENT_REFCOUNTING(SimpleHandler);
DISALLOW_COPY_AND_ASSIGN(SimpleHandler); DISALLOW_COPY_AND_ASSIGN(SimpleHandler);
}; };
#endif // CEF_TESTS_CEFSIMPLE_SIMPLE_HANDLER_H_ #endif // ROOT_cef_simple_handler
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment