From 8979ec418cc67a60d9ce265a2ccc3d9e3a7268fe Mon Sep 17 00:00:00 2001
From: Sergey Linev <S.Linev@gsi.de>
Date: Fri, 21 Jul 2017 19:11:36 +0200
Subject: [PATCH] webgui: support cef messaging also in gui mode

derived SimpleHandler from BaseHandler
---
 gui/canvaspainter/v7/cef/base_handler.cxx   |  10 +-
 gui/canvaspainter/v7/cef/cef_main.cxx       |  75 ++++++-------
 gui/canvaspainter/v7/cef/osr_handler.cxx    |   6 --
 gui/canvaspainter/v7/cef/simple_app.cxx     |  21 ++--
 gui/canvaspainter/v7/cef/simple_handler.cxx | 114 +++-----------------
 gui/canvaspainter/v7/cef/simple_handler.h   |  55 ++++------
 6 files changed, 78 insertions(+), 203 deletions(-)

diff --git a/gui/canvaspainter/v7/cef/base_handler.cxx b/gui/canvaspainter/v7/cef/base_handler.cxx
index 5369ae9d530..5828f190fc8 100644
--- a/gui/canvaspainter/v7/cef/base_handler.cxx
+++ b/gui/canvaspainter/v7/cef/base_handler.cxx
@@ -64,7 +64,7 @@ public:
 
    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
    }
 
@@ -80,7 +80,7 @@ public:
 
 //////////////////////////////////////////////////////////////////////////
 //                                                                      //
-// TCefCallArg                                                          //
+// TCefWsCallArg                                                        //
 //                                                                      //
 // HTTp call argument provided to http server from CEF messaging        //
 // Allows immediately reply to JS client                                //
@@ -88,12 +88,12 @@ public:
 //////////////////////////////////////////////////////////////////////////
 
 
-class TCefCallArg : public THttpCallArg {
+class TCefWsCallArg : public THttpCallArg {
 protected:
    CefRefPtr<CefMessageRouterBrowserSide::Callback> fCallback;
 
 public:
-   TCefCallArg(CefRefPtr<CefMessageRouterBrowserSide::Callback> callback) : fCallback(callback) {}
+   TCefWsCallArg(CefRefPtr<CefMessageRouterBrowserSide::Callback> callback) : fCallback(callback) {}
 
    virtual void HttpReplied()
    {
@@ -143,7 +143,7 @@ public:
       std::string url = message.substr(0, pos);
       message.erase(0, pos + 2);
 
-      TCefCallArg *arg = new TCefCallArg(callback);
+      TCefWsCallArg *arg = new TCefWsCallArg(callback);
       arg->SetPathName(url.c_str());
       arg->SetFileName("root.ws_emulation");
 
diff --git a/gui/canvaspainter/v7/cef/cef_main.cxx b/gui/canvaspainter/v7/cef/cef_main.cxx
index a7beec616d7..20d3c983b3b 100644
--- a/gui/canvaspainter/v7/cef/cef_main.cxx
+++ b/gui/canvaspainter/v7/cef/cef_main.cxx
@@ -12,8 +12,6 @@
 
 // #include "include/cef_process_message.h"
 
-bool gBatchMode = false;
-
 class ROOTV8Handler : public CefV8Handler {
 public:
    ROOTV8Handler() {}
@@ -52,11 +50,9 @@ public:
    // CefRenderProcessHandler methods:
    void OnWebKitInitialized() OVERRIDE
    {
-      if (gBatchMode) {
-         // Create the renderer-side router for query handling.
-         CefMessageRouterConfig config;
-         message_router_ = CefMessageRouterRendererSide::Create(config);
-      }
+      // Create the renderer-side router for query handling.
+      CefMessageRouterConfig config;
+      message_router_ = CefMessageRouterRendererSide::Create(config);
    }
 
    // CefRenderProcessHandler methods
@@ -65,56 +61,53 @@ public:
    {
       printf("MyRendererProcessApp::OnContextCreated\n");
 
-      if (gBatchMode) message_router_->OnContextCreated(browser, frame, context);
-
-/*
-      // Retrieve the context's window object.
-      CefRefPtr<CefV8Value> object = context->GetGlobal();
+      message_router_->OnContextCreated(browser, frame, context);
 
-      // 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.
-      object->SetValue("ROOT_BATCH_FLAG", str, V8_PROPERTY_ATTRIBUTE_NONE);
+            // Create a new V8 string value. See the "Basic JS Types" section below.
+            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;
-      CefRefPtr<CefV8Value> func = CefV8Value::CreateFunction("ROOT_BATCH_FUNC", handler);
+            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_FUNC", func, V8_PROPERTY_ATTRIBUTE_NONE);
+            CefRefPtr<CefV8Handler> handler = new ROOTV8Handler;
+            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<CefListValue> args = msg->GetArgumentList();
+            CefRefPtr<CefProcessMessage> msg = CefProcessMessage::Create("my_message");
 
-      // Populate the argument values.
-      args->SetString(0, "my string");
-      args->SetInt(1, 10);
+            // Retrieve the argument list object.
+            CefRefPtr<CefListValue> args = msg->GetArgumentList();
 
-      // 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);
-*/
+            // Populate the argument values.
+            args->SetString(0, "my string");
+            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,
                           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,
                                  CefRefPtr<CefProcessMessage> message) OVERRIDE
    {
-      if (gBatchMode) return message_router_->OnProcessMessageReceived(browser, source_process, message);
-
-      return false;
+      return message_router_->OnProcessMessageReceived(browser, source_process, message);
    }
 
    /*   virtual bool OnProcessMessageReceived(CefRefPtr<CefBrowser> browser, CefProcessId source_process,
@@ -135,18 +128,12 @@ private:
 // Entry point function for all processes.
 int main(int argc, char *argv[])
 {
-   printf("Starting CEF_MAIN ARGC %d\n", argc);
-   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;
-   }
+   // printf("Starting CEF_MAIN ARGC %d\n", argc);
+   // for (int n = 1; n < argc; n++) printf("ARGV[%d] = %s\n", n, argv[n]);
 
    // Provide CEF with command-line arguments.
    CefMainArgs main_args(argc, argv);
 
-
    CefRefPtr<CefApp> app = new MyRendererProcessApp();
 
    // CEF applications have multiple sub-processes (render, plugin, GPU, etc)
diff --git a/gui/canvaspainter/v7/cef/osr_handler.cxx b/gui/canvaspainter/v7/cef/osr_handler.cxx
index 6f15ca2d867..e54ac0b0918 100644
--- a/gui/canvaspainter/v7/cef/osr_handler.cxx
+++ b/gui/canvaspainter/v7/cef/osr_handler.cxx
@@ -7,12 +7,6 @@
 
 #include "osr_handler.h"
 
-//#include "THttpServer.h"
-//#include "THttpEngine.h"
-//#include "THttpCallArg.h"
-//#include "TRootSniffer.h"
-//#include "TString.h"
-
 #include <sstream>
 #include <string>
 
diff --git a/gui/canvaspainter/v7/cef/simple_app.cxx b/gui/canvaspainter/v7/cef/simple_app.cxx
index 1833f789fe3..079a1c66bc6 100644
--- a/gui/canvaspainter/v7/cef/simple_app.cxx
+++ b/gui/canvaspainter/v7/cef/simple_app.cxx
@@ -25,7 +25,7 @@
 THttpServer *gHandlingServer = 0;
 
 // TODO: memory cleanup of these arguments
-class TCEFCallArg : public THttpCallArg, public CefResourceHandler {
+class TCefHttpCallArg : public THttpCallArg, public CefResourceHandler {
 protected:
    // QWebEngineUrlRequestJob *fRequest;
    int fDD;
@@ -35,9 +35,9 @@ protected:
    int offset_;
 
 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);
       fDD = -1;
@@ -107,8 +107,8 @@ public:
       return has_data;
    }
 
-   IMPLEMENT_REFCOUNTING(TCEFCallArg);
-   DISALLOW_COPY_AND_ASSIGN(TCEFCallArg);
+   IMPLEMENT_REFCOUNTING(TCefHttpCallArg);
+   DISALLOW_COPY_AND_ASSIGN(TCefHttpCallArg);
 };
 
 namespace {
@@ -191,7 +191,7 @@ public:
          return new CefStreamResourceHandler(mime, stream);
       }
 
-      TCEFCallArg *arg = new TCEFCallArg();
+      TCefHttpCallArg *arg = new TCefHttpCallArg();
       arg->SetPathAndFileName(inp_path);
       arg->SetQuery(inp_query);
       arg->SetMethod(inp_method);
@@ -238,14 +238,11 @@ void SimpleApp::OnBeforeChildProcessLaunch(CefRefPtr<CefCommandLine> command_lin
 {
 
    std::string newprog = fCefMain;
-
    command_line->SetProgram(newprog);
-
    std::string prog = command_line->GetProgram().ToString();
 
-   if (fBatch) command_line->AppendArgument("--root-batch");
-
-   printf("OnBeforeChildProcessLaunch %s\n", prog.c_str());
+   // if (fBatch) command_line->AppendArgument("--root-batch");
+   // printf("OnBeforeChildProcessLaunch %s\n", prog.c_str());
 }
 
 /*
@@ -310,7 +307,7 @@ void SimpleApp::OnContextInitialized()
 #endif
 
    // SimpleHandler implements browser-level callbacks.
-   CefRefPtr<SimpleHandler> handler(new SimpleHandler(use_views));
+   CefRefPtr<SimpleHandler> handler(new SimpleHandler(gHandlingServer, use_views));
 
    if (use_views) {
       // Create the BrowserView.
diff --git a/gui/canvaspainter/v7/cef/simple_handler.cxx b/gui/canvaspainter/v7/cef/simple_handler.cxx
index 7238124501d..96309f40fe3 100644
--- a/gui/canvaspainter/v7/cef/simple_handler.cxx
+++ b/gui/canvaspainter/v7/cef/simple_handler.cxx
@@ -1,6 +1,17 @@
-// Copyright (c) 2013 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.
+/// \file gui_handler.h
+/// \ingroup CanvasPainter ROOT7
+/// \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"
 
@@ -20,21 +31,12 @@ SimpleHandler *g_instance = NULL;
 
 } // 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()
 {
-   g_instance = NULL;
-}
-
-// static
-SimpleHandler *SimpleHandler::GetInstance()
-{
-   return g_instance;
 }
 
 void SimpleHandler::OnTitleChange(CefRefPtr<CefBrowser> browser, const CefString &title)
@@ -59,7 +61,6 @@ bool SimpleHandler::OnConsoleMessage(CefRefPtr<CefBrowser> browser, const CefStr
 {
    printf("CONSOLE: %s\n", message.ToString().c_str());
 
-
    //CefRefPtr<CefProcessMessage> msg = CefProcessMessage::Create("PING");
    // Send the process message to the render 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
 
    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;
-}
diff --git a/gui/canvaspainter/v7/cef/simple_handler.h b/gui/canvaspainter/v7/cef/simple_handler.h
index 5b3d690aa84..8f727fa38c7 100644
--- a/gui/canvaspainter/v7/cef/simple_handler.h
+++ b/gui/canvaspainter/v7/cef/simple_handler.h
@@ -1,50 +1,37 @@
-// Copyright (c) 2013 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.
+/// \file gui_handler.h
+/// \ingroup CanvasPainter ROOT7
+/// \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
 #define ROOT_cef_simple_handler
 
-#include "include/cef_client.h"
+#include "base_handler.h"
 
-#include <list>
-
-class SimpleHandler : public CefClient, public CefDisplayHandler, public CefLifeSpanHandler, public CefLoadHandler {
+class SimpleHandler : public BaseHandler, public CefDisplayHandler {
 public:
-   explicit SimpleHandler(bool use_views);
+   explicit SimpleHandler(THttpServer *serv = 0, bool use_views = false);
    ~SimpleHandler();
 
-   // Provide access to the single global instance of this object.
-   static SimpleHandler *GetInstance();
-
    // CefClient methods:
    virtual CefRefPtr<CefDisplayHandler> GetDisplayHandler() OVERRIDE { return this; }
-   virtual CefRefPtr<CefLifeSpanHandler> GetLifeSpanHandler() OVERRIDE { return this; }
-   virtual CefRefPtr<CefLoadHandler> GetLoadHandler() OVERRIDE { return this; }
 
    // CefDisplayHandler methods:
    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,
                                  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:
    // Platform-specific implementation.
    void PlatformTitleChange(CefRefPtr<CefBrowser> browser, const CefString &title);
@@ -52,15 +39,9 @@ private:
    // True if the application is using the Views framework.
    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.
    IMPLEMENT_REFCOUNTING(SimpleHandler);
    DISALLOW_COPY_AND_ASSIGN(SimpleHandler);
 };
 
-#endif // CEF_TESTS_CEFSIMPLE_SIMPLE_HANDLER_H_
+#endif // ROOT_cef_simple_handler
-- 
GitLab