-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathClient.cpp
242 lines (197 loc) · 5.98 KB
/
Client.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include "Client.h"
#include "Common.h"
#include "SchemeHandlerFactory.h"
#include "PrintHandler.h"
#include "RenderHandler.h"
#include "RequestHandler.h"
#include "include/base/cef_logging.h"
#include "include/wrapper/cef_helpers.h"
#include "include/base/cef_bind.h"
#include "include/wrapper/cef_closure_task.h"
namespace cefpdf {
Client::Client() :
m_jobsManager(new job::Manager()),
m_processCount(0),
m_initialized(false),
m_running(false),
m_stopAfterLastJob(false),
m_printHandler(new PrintHandler),
m_renderHandler(new RenderHandler),
m_requestHandler(new RequestHandler)
{
m_settings.no_sandbox = true;
m_settings.windowless_rendering_enabled = true;
m_settings.command_line_args_disabled = true;
m_windowInfo.windowless_rendering_enabled = true;
m_browserSettings.windowless_frame_rate = 1;
CefString(&m_browserSettings.default_encoding).FromString(constants::encoding);
m_browserSettings.plugins = STATE_DISABLED;
m_browserSettings.javascript_open_windows = STATE_DISABLED;
m_browserSettings.javascript_close_windows = STATE_DISABLED;
}
int Client::ExecuteSubProcess(const CefMainArgs& mainArgs)
{
return CefExecuteProcess(mainArgs, this, NULL);
}
void Client::Initialize(const CefMainArgs& mainArgs)
{
DCHECK(!m_initialized);
CefInitialize(mainArgs, m_settings, this, NULL);
m_initialized = true;
}
void Client::Shutdown()
{
DCHECK(m_initialized);
CefShutdown();
m_initialized = false;
}
void Client::Run()
{
DCHECK(m_initialized);
DCHECK(!m_running);
m_running = true;
CefRunMessageLoop();
m_running = false;
Shutdown();
}
void Client::Stop()
{
if (m_running) {
CefQuitMessageLoop();
} else if (m_initialized) {
Shutdown();
}
}
void Client::PostJob(CefRefPtr<job::Job> job)
{
m_jobsQueue.push(job);
if (m_initialized) {
ProcessJobsQueue();
}
}
void Client::ProcessJobsQueue()
{
CEF_REQUIRE_UI_THREAD();
while (!m_jobsQueue.empty() && m_processCount < constants::maxProcesses) {
auto job = m_jobsQueue.front();
m_jobsManager->Queue(job);
m_jobsQueue.pop();
++m_processCount;
// Create the browser window.
CefBrowserHost::CreateBrowser(m_windowInfo, this, "", m_browserSettings, NULL);
}
}
// CefApp methods:
// -----------------------------------------------------------------------------
CefRefPtr<CefBrowserProcessHandler> Client::GetBrowserProcessHandler()
{
return this;
}
void Client::OnRegisterCustomSchemes(CefRawPtr<CefSchemeRegistrar> registrar)
{
registrar->AddCustomScheme(constants::scheme, true, false, false, false, true, false);
}
void Client::OnBeforeCommandLineProcessing(const CefString& process_type, CefRefPtr<CefCommandLine> command_line)
{
command_line->AppendSwitch("disable-gpu");
command_line->AppendSwitch("disable-gpu-compositing");
};
// CefBrowserProcessHandler methods:
// -----------------------------------------------------------------------------
CefRefPtr<CefPrintHandler> Client::GetPrintHandler()
{
return m_printHandler;
}
void Client::OnBeforeChildProcessLaunch(CefRefPtr<CefCommandLine> command_line)
{
DLOG(INFO) << "OnBeforeChildProcessLaunch: " << command_line->GetCommandLineString().ToString() ;
}
void Client::OnContextInitialized()
{
DLOG(INFO) << "OnContextInitialized";
CEF_REQUIRE_UI_THREAD();
CefRegisterSchemeHandlerFactory(constants::scheme, "", new SchemeHandlerFactory(m_jobsManager));
m_initialized = true;
ProcessJobsQueue();
}
// CefClient methods:
// -----------------------------------------------------------------------------
CefRefPtr<CefLifeSpanHandler> Client::GetLifeSpanHandler()
{
return this;
}
CefRefPtr<CefLoadHandler> Client::GetLoadHandler()
{
return this;
}
CefRefPtr<CefRenderHandler> Client::GetRenderHandler()
{
return m_renderHandler;
}
CefRefPtr<CefRequestHandler> Client::GetRequestHandler()
{
return m_requestHandler;
}
// CefLifeSpanHandler methods:
// -----------------------------------------------------------------------------
void Client::OnAfterCreated(CefRefPtr<CefBrowser> browser)
{
DLOG(INFO) << "OnAfterCreated";
CEF_REQUIRE_UI_THREAD();
// Assign this browser to the next job. JobsManager will
// check if there is any queued job
m_jobsManager->Assign(browser);
}
bool Client::DoClose(CefRefPtr<CefBrowser> browser)
{
DLOG(INFO) << "DoClose";
CEF_REQUIRE_UI_THREAD();
// Allow the close. For windowed browsers this will result in the OS close
// event being sent.
return false;
}
void Client::OnBeforeClose(CefRefPtr<CefBrowser> browser)
{
DLOG(INFO) << "OnBeforeClose";
CEF_REQUIRE_UI_THREAD();
--m_processCount;
ProcessJobsQueue();
if (0 == m_processCount && m_stopAfterLastJob) {
CefPostDelayedTask(TID_UI, base::Bind(&cefpdf::Client::Stop, this), 50);
}
}
// CefLoadHandler methods:
// -----------------------------------------------------------------------------
void Client::OnLoadStart(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, TransitionType transition_type)
{
DLOG(INFO) << "OnLoadStart" << ", url: " << frame->GetURL().ToString();
CEF_REQUIRE_UI_THREAD();
}
void Client::OnLoadEnd(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, int httpStatusCode)
{
DLOG(INFO)
<< "OnLoadEnd"
<< ", url: " << frame->GetURL().ToString()
<< ", httpStatusCode: " << httpStatusCode;
CEF_REQUIRE_UI_THREAD();
if (frame->IsMain()) {
m_jobsManager->Process(browser, httpStatusCode);
}
}
void Client::OnLoadError(
CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
ErrorCode errorCode,
const CefString& errorText,
const CefString& failedUrl
) {
DLOG(INFO)
<< "OnLoadError"
<< ", errorCode: " << errorCode
<< ", failedUrl: " << failedUrl.ToString();
CEF_REQUIRE_UI_THREAD();
if (frame->IsMain()) {
m_jobsManager->Abort(browser, errorCode);
}
}
} // namespace cefpdf