#include #include #include #include #include #include #include #include class AyaCEFApp : public CefApp { public: AyaCEFApp () {}; private: IMPLEMENT_REFCOUNTING(AyaCEFApp); }; class AyaSubProcess : public AyaCEFApp , public CefRenderProcessHandler , public CefV8Handler { public: virtual CefRefPtr GetRenderProcessHandler() override { return this; } void OnContextCreated(CefRefPtr browser, CefRefPtr frame, CefRefPtr context) override { CefRefPtr global = context->GetGlobal(); CefRefPtr func = CefV8Value::CreateFunction("sendResultToCpp", this); global->SetValue("sendResultToCpp", func, V8_PROPERTY_ATTRIBUTE_NONE); } bool Execute(const CefString& name, CefRefPtr object, const CefV8ValueList& arguments, CefRefPtr& retval, CefString& exception) override { // this should start to become a tuple of some kind because this is // shit if we want to let users have more control over js w/ lua // but this is good enough as a poc & can be used for replication // across different clients connected to a server if (name == "sendResultToCpp" && arguments.size() == 1) { CefRefPtr msg = CefProcessMessage::Create("resultMessage"); CefRefPtr args = msg->GetArgumentList(); args->SetString(0, arguments[0]->GetStringValue()); CefRefPtr browser = CefV8Context::GetCurrentContext()->GetBrowser(); CefRefPtr frame = browser->GetMainFrame(); frame->SendProcessMessage(PID_BROWSER, msg); return true; } return false; } private: IMPLEMENT_REFCOUNTING(AyaSubProcess); }; #ifdef WIN32 int CALLBACK WinMain(_In_ HINSTANCE hInstance, _In_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nCmdShow) { CefMainArgs args(hInstance); return CefExecuteProcess(args, new AyaSubProcess(), nullptr); } #else int main(int argc, char* argv[]) { CefMainArgs args(argc, argv); return CefExecuteProcess(args, new AyaSubProcess(), nullptr); } #endif