async_js_callback_handler.cpp
12.2 KB
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#include "stdafx.h"
#include "etradeclient/browser/async_js_callback_handler.h"
#include <string>
#include <boost/property_tree/ptree.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/ptr_container/ptr_map.hpp>
#include <boost/property_tree/json_parser.hpp>
#include "etradeclient/hardware/hardware_cmd.h"
#include "etradeclient/utility/url_regex.h"
#include "etradeclient/utility/url_config.h"
#include "etradeclient/utility/logging.h"
#include "etradeclient/utility/string_converter.h"
#include "ETradeClient/utility/win_msg_define.h"
#include "ETradeClient/browser/popup_browser_handler.h"
namespace HW
{
namespace PT = boost::property_tree;
// Tag & value of the hardware function request & response string in JSON format.
static const std::string JSON_TAG_CMD = "command";
static const std::string JSON_TAG_FILEID = "fileId";
static const std::string JSON_TAG_DATA = "data";
static const std::string JSON_TAG_SEQ = "seq";
static const std::string JSON_TAG_ANS = "answer";
static const std::string JSON_TAG_ERROR_CODE = "errorCode";
static const std::string JSON_VAL_CMD_ACTIVATE = "activate";
static const std::string JSON_VAL_CMD_RESET = "reset";
static const std::string JSON_VAL_CMD_READ = "read";
static const std::string JSON_VAL_CMD_WRITE = "write";
static const std::string JSON_VAL_CMD_PRINT = "print";
static const std::string JSON_VAL_ANS_ACTIVATE_ACK = "activate_ack";
static const std::string JSON_VAL_ANS_ACTIVATE_FAILED = "activate_failed";
static const std::string JSON_VAL_ANS_RESET_ACK = "reset_ack";
static const std::string JSON_VAL_ANS_RESET_FAILED = "reset_failed";
static const std::string JSON_VAL_ANS_READ_ACK = "read_ack";
static const std::string JSON_VAL_ANS_READ_FAILED = "read_failed";
static const std::string JSON_VAL_ANS_WRITE_ACK = "write_ack";
static const std::string JSON_VAL_ANS_WRITE_FAILED = "write_failed";
static const std::string JSON_VAL_ANS_PRINT_ACK = "print_ack";
static const std::string JSON_VAL_ANS_PRINT_FAILED = "print_failed";
static const std::string JSON_VAL_FILEID_DILI_CARD = "00";
static const std::string JSON_VAL_FILEID_DILI_CARD_BASIC_INFO = "01";
static const std::string JSON_VAL_FILEID_DILI_CARD_SERVICE_INFO = "02"; // Service info.
static const std::string JSON_VAL_FILEID_ID_CARD = "10";
static const std::string JSON_VAL_FILEID_PIN_PAD = "11";
static const std::string JSON_VAL_FILEID_BANK_CARD_NUM = "20";
static const std::string JSON_VAL_FILEID_DILI_CARD_AND_BANK = "30";
static const std::string JSON_VAL_FILEID_ORDER = "40";
static const std::string JSON_VAL_FILEID_DISTRIBUTE = "41";
struct HWRequest
{
std::string cmd;
std::string file_id;
std::string data;
std::string seq; // The sequence number of the request.
};
HWRequest ParseRequest(const std::string& request_json)
{
PT::ptree ptree;
std::stringstream ss;
ss << request_json;
PT::read_json(ss, ptree);
std::string cmd = ptree.get<std::string>(JSON_TAG_CMD);
std::string file_id = ptree.get<std::string>(JSON_TAG_FILEID);
std::string data;
if (cmd.compare(JSON_VAL_CMD_PRINT) == 0)
{
PT::ptree data_child = ptree.get_child(JSON_TAG_DATA);
std::ostringstream os;
PT::write_json(os, data_child);
data = std::string(os.str());
}
else
{
data = ptree.get<std::string>(JSON_TAG_DATA);
}
std::string seq = ptree.get<std::string>(JSON_TAG_SEQ);
return{ cmd, file_id, data, seq };
}
std::string MakeResultJSON(const std::string& answer,
const std::string& file_id,
const std::string& error_code,
const std::string& seq,
const PT::ptree& data)
{
PT::ptree root;
root.put(JSON_TAG_ANS, answer);
root.put(JSON_TAG_FILEID, file_id);
root.put_child(JSON_TAG_DATA, data);
root.put(JSON_TAG_ERROR_CODE, error_code);
root.put(JSON_TAG_SEQ, seq);
std::stringstream ss;
PT::write_json(ss, root, false);
return ss.str();
}
class HWRequestHandler
{
typedef boost::ptr_map<std::string, HardwareCmd> HardwareCmdMap; // Key: cmd+file_id.
public:
HWRequestHandler()
{
m_hw_cmd_map.insert(JSON_VAL_CMD_READ + JSON_VAL_FILEID_ID_CARD, new ReadIDCardCmd());
m_hw_cmd_map.insert(JSON_VAL_CMD_READ + JSON_VAL_FILEID_PIN_PAD, new ReadPINPadCmd());
m_hw_cmd_map.insert(JSON_VAL_CMD_ACTIVATE + JSON_VAL_FILEID_DILI_CARD, new ActivateDILICardCmd());
m_hw_cmd_map.insert(JSON_VAL_CMD_RESET + JSON_VAL_FILEID_DILI_CARD, new ResetDILICardCmd());
m_hw_cmd_map.insert(JSON_VAL_CMD_READ + JSON_VAL_FILEID_DILI_CARD_BASIC_INFO, new ReadDILICardBasicInfoCmd());
m_hw_cmd_map.insert(JSON_VAL_CMD_READ + JSON_VAL_FILEID_DILI_CARD_SERVICE_INFO, new ReadDILICardServiceInfoCmd());
m_hw_cmd_map.insert(JSON_VAL_CMD_WRITE + JSON_VAL_FILEID_DILI_CARD_SERVICE_INFO, new WriteDILICardServiceInfoCmd());
m_hw_cmd_map.insert(JSON_VAL_CMD_READ + JSON_VAL_FILEID_BANK_CARD_NUM, new ReadBankCardNumCmd());
m_hw_cmd_map.insert(JSON_VAL_CMD_READ + JSON_VAL_FILEID_DILI_CARD_AND_BANK, new ReadDILIAndBankCardNumCmd());
m_hw_cmd_map.insert(JSON_VAL_CMD_PRINT + JSON_VAL_FILEID_ORDER, new StylusPrinterCmd());
m_hw_cmd_map.insert(JSON_VAL_CMD_PRINT + JSON_VAL_FILEID_DISTRIBUTE, new DistributePrinterCmd());
}
std::string HandleRequest(const std::string& request_json)
{
try
{
static const std::string OK = ""; // TODO maybe improve this use the same variable defined by "HardwareCmd".
HWRequest hw_req = ParseRequest(request_json);
LOG_TRACE(L"执行硬件调用请求:" + str_2_wstr("[" + hw_req.cmd + "," + hw_req.file_id + "," + hw_req.seq + "]"));
HardwareCmd::Reply reply;
std::string answer = "";
reply = m_hw_cmd_map.at(hw_req.cmd + hw_req.file_id).Execute(hw_req.data);
if (boost::iequals(OK, reply.error_code)) // Succeed.
answer = hw_req.cmd + "_ack";
else
answer = hw_req.cmd + "_failed";
std::string reply_json = MakeResultJSON(answer, hw_req.file_id, reply.error_code, hw_req.seq, reply.data);
LOG_TRACE(L"执行硬件调用结果:" + str_2_wstr("[" + hw_req.cmd + "," + hw_req.file_id + "," + hw_req.seq + "] [" + reply.error_code + "]") + +L"\n");
return reply_json;
}
catch (std::exception& ex)
{
LOG_FATAL(L"执行硬件调用请求发生异常,异常信息: " + str_2_wstr(ex.what()) + L"\n");
return "";
}
}
private:
HardwareCmdMap m_hw_cmd_map;
};
};
namespace
{
namespace PT = boost::property_tree;
// Tag & value of the hardware function request & response string in JSON format.
static const std::string JSON_TAG_CMD = "command";
static const std::string JSON_TAG_FILEID = "fileId";
static const std::string JSON_TAG_DATA = "data";
static const std::string JSON_TAG_SEQ = "seq";
static const std::string JSON_TAG_ANS = "answer";
static const std::string JSON_TAG_ERROR_CODE = "errorCode";
struct JSRequest
{
std::string cmd;
std::string file_id;
std::string data;
std::string seq; // The sequence number of the request.
};
JSRequest ParseRequest(const std::string& request_json)
{
PT::ptree ptree;
std::stringstream ss;
ss << request_json;
PT::read_json(ss, ptree);
std::string cmd = ptree.get<std::string>(JSON_TAG_CMD);
std::string file_id = ptree.get<std::string>(JSON_TAG_FILEID);
std::string data = ptree.get<std::string>(JSON_TAG_DATA);
std::string seq = ptree.get<std::string>(JSON_TAG_SEQ);
return{ cmd, file_id, data, seq };
}
std::string MakeResultJSON(const std::string& answer,
const std::string& file_id,
const std::string& error_code,
const std::string& seq,
const PT::ptree& data = PT::ptree())
{
PT::ptree root;
root.put(JSON_TAG_ANS, answer);
root.put(JSON_TAG_FILEID, file_id);
root.put_child(JSON_TAG_DATA, data);
root.put(JSON_TAG_ERROR_CODE, error_code);
root.put(JSON_TAG_SEQ, seq);
std::stringstream ss;
PT::write_json(ss, root, false);
return ss.str();
}
bool IsEqual(const std::string& str1, const std::string& str2)
{
return 0 == str1.compare(str2);
}
}
namespace UI{
// Tag & value of the hardware function request & response string in JSON format.
static const std::string JSON_VAL_CMD_CLOSE_WND = "close_wnd";
static const std::string JSON_VAL_CMD_USER_RELOGIN = "user_relogin";
static const std::string JSON_VAL_CMD_APP_EXIT = "app_exit";
static const std::string JSON_VAL_CMD_APP_RELAUNCH = "app_relauch";
static const std::string JSON_VAL_ANS_ACK = "_ack";
static const std::string JSON_VAL_ANS_FAILED = "_failed";
static const std::string OK = ""; // 操作成功.
static const std::string ERR = "601"; // 操作失败.
class UIRequestHandler
{
typedef std::map<std::string, int> UserInterfaceCmdMap; // Key: cmd+file_id.
public:
UIRequestHandler()
{
m_ui_cmd_map.insert(std::pair<std::string, int>(JSON_VAL_CMD_CLOSE_WND, WM_CEF_JS_CAMMAND_CLOSE_WND));
m_ui_cmd_map.insert(std::pair<std::string, int>(JSON_VAL_CMD_USER_RELOGIN, WM_CEF_JS_CAMMAND_USER_RELOGIN));
m_ui_cmd_map.insert(std::pair<std::string, int>(JSON_VAL_CMD_APP_EXIT, WM_CEF_JS_CAMMAND_APP_EXIT));
m_ui_cmd_map.insert(std::pair<std::string, int>(JSON_VAL_CMD_APP_RELAUNCH, WM_CEF_JS_CAMMAND_APP_RELAUNCH));
}
std::string HandleRequest(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, const CefString& request_json)
{
JSRequest js_rq = ParseRequest(request_json);
try
{
auto w_hand = ::GetParent(browser->GetHost()->GetWindowHandle());
auto w_message = m_ui_cmd_map.at(js_rq.cmd);
::PostMessage(w_hand, w_message, NULL, NULL);
return MakeResultJSON(js_rq.cmd + JSON_VAL_ANS_ACK, js_rq.file_id, OK, js_rq.seq);
}
catch (std::exception& ex)
{
LOG_FATAL(L"执行UI调用请求发生异常,异常信息: " + str_2_wstr(ex.what()) + L"\n");
return MakeResultJSON(js_rq.cmd + JSON_VAL_ANS_FAILED, js_rq.file_id, ERR, js_rq.seq);
}
}
private:
UserInterfaceCmdMap m_ui_cmd_map;
};
}
namespace
{
/*Callback handler for call the hardware function.*/
class Handler : public CefMessageRouterBrowserSide::Handler
{
public:
// This handler function will be called due to 'window.cefQuery' call in web page's JavaScript.
virtual bool OnQuery(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
int64 query_id,
const CefString& request_json,
bool persistent,
CefRefPtr<Callback> callback) OVERRIDE
{
// Only handle the message from our own host. This is an important security check!
CefString url_ = frame->GetURL();
if (!CheckHost(url_.ToString()))
return false;
//callback->Success(m_hw_req_handler.HandleRequest(request_json));
Handing(browser, frame, request_json, callback);
return true;
}
private:
bool CheckHost(const std::string& url_)
{
std::smatch match_res;
if (!URLRegex::Parse(url_, match_res))
return false;
return 0 == URLConfig::Instance().Host().compare(match_res[3].str());
}
private:
//HWRequestHandler m_hw_req_handler;
virtual void Handing(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
const CefString& request_json,
CefRefPtr<Callback> callback) = 0;
};
class HWHandle :public Handler
{
private:
void Handing(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
const CefString& request_json,
CefRefPtr<Callback> callback)
{
callback->Success(m_hw_req_handler.HandleRequest(request_json));
}
private:
HW::HWRequestHandler m_hw_req_handler;
};
class UIHandle :public Handler
{
private:
void Handing(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
const CefString& request_json,
CefRefPtr<Callback> callback)
{
callback->Success(m_ui_req_handler.HandleRequest(browser, frame, request_json));
}
private:
UI::UIRequestHandler m_ui_req_handler;
};
}
namespace AsyncJSCallbackHandler
{
// Handler creation. Called from MainViewBrowserHandler.
//void Create(MainViewBrowserHandler::MessageHandlerSet& handlers)
//{
// //TODO: need exception handling here? return true/false indicator?
// handlers.emplace(new HW::Handler());
//}
// Handler creation. Called from MainViewBrowserHandler.
void HW_Create(MainViewBrowserHandler::MessageHandlerSet& handlers)
{
//TODO: need exception handling here? return true/false indicator?
handlers.emplace(new HWHandle());
}
// Handler creation. Called from PopupBrowserHandler(CreateMerchantView).
void UI_Create(PopupBrowserHandler::MessageHandlerSet& handlers)
{
//TODO: need exception handling here? return true/false indicator?
handlers.emplace(new UIHandle());
}
} // AsyncJSCallbackHandler