}std::stringHandleRequest(conststd::string&request_json){try{staticconststd::stringOK="";//TODOmaybeimprovethisusethesamevariabledefinedby"HardwareCmd".HWRequesthw_req=ParseRequest(request_json);LOG_TRACE(L"执行硬件调用请求:"+str_2_wstr("["+hw_req.cmd+","+hw_req.file_id+","+hw_req.seq+"]"));HardwareCmd::Replyreply;std::stringanswer="";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";elseanswer=hw_req.cmd+"_failed";std::stringreply_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");returnreply_json;}catch(std::exception&ex){LOG_FATAL(L"执行硬件调用请求发生异常,异常信息: "+str_2_wstr(ex.what())+L"\n");return"";}}private:HardwareCmdMapm_hw_cmd_map;};};namespace{namespacePT=boost::property_tree;//Tag&valueofthehardwarefunctionrequest&responsestringinJSONformat.staticconststd::stringJSON_TAG_CMD="command";staticconststd::stringJSON_TAG_FILEID="fileId";staticconststd::stringJSON_TAG_DATA="data";staticconststd::stringJSON_TAG_SEQ="seq";staticconststd::stringJSON_TAG_ANS="answer";staticconststd::stringJSON_TAG_ERROR_CODE="errorCode";structJSRequest{std::stringcmd;std::stringfile_id;std::stringdata;std::stringseq;//Thesequencenumberoftherequest.};JSRequestParseRequest(conststd::string&request_json){PT::ptreeptree;std::stringstreamss;ss<<request_json;PT::read_json(ss,ptree);std::stringcmd=ptree.get<std::string>(JSON_TAG_CMD);std::stringfile_id=ptree.get<std::string>(JSON_TAG_FILEID);std::stringdata=ptree.get<std::string>(JSON_TAG_DATA);std::stringseq=ptree.get<std::string>(JSON_TAG_SEQ);return{cmd,file_id,data,seq};}std::stringMakeResultJSON(conststd::string&answer,conststd::string&file_id,conststd::string&error_code,conststd::string&seq,constPT::ptree&data=PT::ptree()){PT::ptreeroot;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::stringstreamss;PT::write_json(ss,root,false);returnss.str();}boolIsEqual(conststd::string&str1,conststd::string&str2){return0==str1.compare(str2);}}namespaceUI{//Tag&valueofthehardwarefunctionrequest&responsestringinJSONformat.staticconststd::stringJSON_VAL_CMD_CLOSE_WND="close_wnd";staticconststd::stringJSON_VAL_CMD_USER_RELOGIN="user_relogin";staticconststd::stringJSON_VAL_CMD_APP_EXIT="app_exit";staticconststd::stringJSON_VAL_CMD_APP_RELAUNCH="app_relauch";staticconststd::stringJSON_VAL_ANS_ACK="_ack";staticconststd::stringJSON_VAL_ANS_FAILED="_failed";staticconststd::stringOK="";//操作成功.staticconststd::stringERR="601";//操作失败.classUIRequestHandler{typedefstd::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::stringHandleRequest(CefRefPtr<CefBrowser>browser,CefRefPtr<CefFrame>frame,constCefString&request_json){JSRequestjs_rq=ParseRequest(request_json);try{autow_hand=::GetParent(browser->GetHost()->GetWindowHandle());autow_message=m_ui_cmd_map.at(js_rq.cmd);::PostMessage(w_hand,w_message,NULL,NULL);returnMakeResultJSON(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");returnMakeResultJSON(js_rq.cmd+JSON_VAL_ANS_FAILED,js_rq.file_id,ERR,js_rq.seq);}}private:UserInterfaceCmdMapm_ui_cmd_map;};}namespace{/*Callback handler for call the hardware function.*/classHandler:publicCefMessageRouterBrowserSide::Handler{public://Thishandlerfunctionwillbecalleddueto'window.cefQuery'callinwebpage'sJavaScript.virtualboolOnQuery(CefRefPtr<CefBrowser>browser,CefRefPtr<CefFrame>frame,int64query_id,constCefString&request_json,boolpersistent,CefRefPtr<Callback>callback)OVERRIDE{//Onlyhandlethemessagefromourownhost.Thisisanimportantsecuritycheck!CefStringurl_=frame->GetURL();if(!CheckHost(url_.ToString()))returnfalse;//callback->Success(m_hw_req_handler.HandleRequest(request_json));Handing(browser,frame,request_json,callback);returntrue;}private:boolCheckHost(conststd::string&url_){std::smatchmatch_res;if(!URLRegex::Parse(url_,match_res))returnfalse;return0==URLConfig::Instance().Host().compare(match_res[3].str());}private://HWRequestHandlerm_hw_req_handler;virtualvoidHanding(CefRefPtr<CefBrowser>browser,CefRefPtr<CefFrame>frame,constCefString&request_json,CefRefPtr<Callback>callback)=0;};classHWHandle:publicHandler{private:voidHanding(CefRefPtr<CefBrowser>browser,CefRefPtr<CefFrame>frame,constCefString&request_json,CefRefPtr<Callback>callback){callback->Success(m_hw_req_handler.HandleRequest(request_json));}private:HW::HWRequestHandlerm_hw_req_handler;};classUIHandle:publicHandler{private:voidHanding(CefRefPtr<CefBrowser>browser,CefRefPtr<CefFrame>frame,constCefString&request_json,CefRefPtr<Callback>callback){callback->Success(m_ui_req_handler.HandleRequest(browser,frame,request_json));}private:UI::UIRequestHandlerm_ui_req_handler;};}namespaceAsyncJSCallbackHandler{//Handlercreation.CalledfromMainViewBrowserHandler.//voidCreate(MainViewBrowserHandler::MessageHandlerSet&handlers)//{////TODO:needexceptionhandlinghere?returntrue/falseindicator?//handlers.emplace(newHW::Handler());//}//Handlercreation.CalledfromMainViewBrowserHandler.voidHW_Create(MainViewBrowserHandler::MessageHandlerSet&handlers){//TODO:needexceptionhandlinghere?returntrue/falseindicator?handlers.emplace(newHWHandle());}//Handlercreation.CalledfromPopupBrowserHandler(CreateMerchantView).voidUI_Create(PopupBrowserHandler::MessageHandlerSet&handlers){//TODO:needexceptionhandlinghere?returntrue/falseindicator?handlers.emplace(newUIHandle());}}//AsyncJSCallbackHandler