-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGrpcBrowser.cpp
145 lines (114 loc) · 4.15 KB
/
GrpcBrowser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "GrpcBrowser.h"
#include "SlBrowser.h"
#include "WindowsFunctions.h"
#include <filesystem>
/***
* Server
* Receiving messages from the plugin
*/
class grpc_proxy_objImpl final : public grpc_proxy_obj::Service
{
grpc::Status com_grpc_js_executeCallback(grpc::ServerContext *context, const grpc_js_api_ExecuteCallback *request, grpc_js_api_Reply *response) override
{
CefRefPtr<CefProcessMessage> msg = CefProcessMessage::Create("executeCallback");
CefRefPtr<CefListValue> execute_args = msg->GetArgumentList();
execute_args->SetInt(0, request->funcid());
execute_args->SetString(1, request->jsonstr());
if (auto ptr = SlBrowser::instance().browserClient->PopCallback(request->funcid()))
{
SendBrowserProcessMessage(ptr, PID_RENDERER, msg);
}
else
{
printf("com_grpc_js_executeCallback failed to find browser for function");
}
return grpc::Status::OK;
}
grpc::Status com_grpc_run_javascriptOnBrowser(grpc::ServerContext *context, const grpc_run_javascriptOnBrowser *request, grpc_empty_Reply *response) override
{
CefRefPtr<CefProcessMessage> msg = CefProcessMessage::Create("executeJavascript");
CefRefPtr<CefListValue> execute_args = msg->GetArgumentList();
execute_args->SetString(0, request->str());
if (auto ptr = SlBrowser::instance().browserClient->GetMostRecentRenderKnown())
{
SendBrowserProcessMessage(ptr, PID_RENDERER, msg);
}
else
{
printf("com_grpc_run_javascriptOnBrowser failed to a suitable browser for function");
}
return grpc::Status::OK;
}
grpc::Status com_grpc_window_toggleVisibility(grpc::ServerContext *context, const grpc_window_toggleVisibility *request, grpc_empty_Reply *response) override
{
// If hidden
if (SlBrowser::instance().m_widget->isHidden())
{
// Main page isn't loading and it also failed
if (!SlBrowser::instance().getMainLoadingInProgress() && !SlBrowser::instance().getMainPageSuccess())
{
// Attempt load default URL again
SlBrowser::instance().setMainLoadingInProgress(true);
if (auto browser = SlBrowser::instance().m_browser)
{
if (auto frame = SlBrowser::instance().m_browser->GetMainFrame())
frame->LoadURL(SlBrowser::getDefaultUrl());
}
}
// Swap hidden state
SlBrowser::instance().m_widget->setHidden(!SlBrowser::instance().m_widget->isHidden());
}
if (!SlBrowser::instance().m_widget->isHidden())
{
HWND hwnd = HWND(SlBrowser::instance().m_widget->winId());
WindowsFunctions::ForceForegroundWindow(hwnd);
}
SlBrowser::instance().saveHiddenState(SlBrowser::instance().m_widget->isHidden());
return grpc::Status::OK;
}
};
/***
* Client
* Sending messages to the plugin
*/
grpc_proxy_objClient::grpc_proxy_objClient(std::shared_ptr<grpc::Channel> channel) : stub_(grpc_plugin_obj::NewStub(channel))
{
m_connected = channel->WaitForConnected(std::chrono::system_clock::now() + std::chrono::seconds(3));
}
bool grpc_proxy_objClient::send_js_api(const std::string &funcName, const std::string ¶ms)
{
grpc_js_api_Request request;
request.set_funcname(funcName);
request.set_params(params);
grpc_js_api_Reply reply;
grpc::ClientContext context;
grpc::Status status = stub_->com_grpc_js_api(&context, request, &reply);
if (!status.ok())
return m_connected = false;
return true;
}
// Grpc
//
GrpcBrowser::GrpcBrowser() {}
GrpcBrowser::~GrpcBrowser() {}
bool GrpcBrowser::startServer(int32_t listenPort)
{
// Don't repeat
if (m_listenPort != 0)
return false;
m_listenPort = listenPort;
grpc::EnableDefaultHealthCheckService(true);
grpc::reflection::InitProtoReflectionServerBuilderPlugin();
m_builder = std::make_unique<grpc::ServerBuilder>();
m_builder->AddListeningPort(std::string("localhost:") + std::to_string(m_listenPort), grpc::InsecureServerCredentials());
m_serverObj = std::make_unique<grpc_proxy_objImpl>();
m_builder->RegisterService(m_serverObj.get());
m_server = m_builder->BuildAndStart();
return m_server != nullptr;
}
bool GrpcBrowser::connectToClient(int32_t portNumber)
{
m_clientObj = std::make_unique<grpc_proxy_objClient>(grpc::CreateChannel("localhost:" + std::to_string(portNumber), grpc::InsecureChannelCredentials()));
return m_clientObj != nullptr;
}
void GrpcBrowser::stop() {}