123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- #pragma once
- #include "modVer.h"
- #include "ResourceWatcherFSM.h"
- #include "UOSTools.hpp"
- #include "fileutil.h"
- #include "UpgradeManager_msg_g.h"
- class ResourceWatcherEntity;
- #define ENT_TIMERID_PROCESS_CHECK 66
- #define ENT_TIMERINTERVAL_PROCESS_CHECK 60 * 60 * 1000 //进程检测间隔默认一小时
- #define ENCODING (X509_ASN_ENCODING | PKCS_7_ASN_ENCODING)
- #if defined(RVC_OS_WIN)
- #define SECNAME_PROCESSDETECT "ProcDetectFlag"
- #else
- #define SECNAME_PROCESSDETECT "ProcDetectFlag4UOS"
- #endif //RVC_OS_WIN
- const int AbsolutePath = 0;
- const int AudioDefaultPath = 1;
- const int VideoDefaultPath = 2;
- class ResourceWatcherServiceSession : public ResourceWatcherService_ServerSessionBase
- {
- public:
- ResourceWatcherServiceSession(ResourceWatcherEntity* pEntity) : m_pEntity(pEntity) {}
- virtual ~ResourceWatcherServiceSession() {}
- virtual void Handle_GetDevInfo(SpReqAnsContext<ResourceWatcherService_GetDevInfo_Req,
- ResourceWatcherService_GetDevInfo_Ans>::Pointer ctx);
- virtual void Handle_OperateFile(SpReqAnsContext<ResourceWatcherService_OperateFile_Req, ResourceWatcherService_OperateFile_Ans>::Pointer ctx);
- virtual void Handle_BizLinkDetect(SpReqAnsContext<ResourceWatcherService_BizLinkDetect_Req, ResourceWatcherService_BizLinkDetect_Ans>::Pointer ctx);
- virtual void Handle_CheckNetType(SpReqAnsContext<ResourceWatcherService_CheckNetType_Req, ResourceWatcherService_CheckNetType_Ans>::Pointer ctx);
- virtual void Handle_GetBizLinks(SpReqAnsContext<ResourceWatcherService_GetBizLinks_Req, ResourceWatcherService_GetBizLinks_Ans>::Pointer ctx);
- virtual void Handle_InstallThirdPartyProgram(SpReqAnsContext<ResourceWatcherService_InstallThirdPartyProgram_Req, ResourceWatcherService_InstallThirdPartyProgram_Ans>::Pointer ctx);
- virtual void Handle_FilesClean(SpReqAnsContext<ResourceWatcherService_FilesClean_Req, ResourceWatcherService_FilesClean_Ans>::Pointer ctx);
- virtual void Handle_FetchSystemSnapshot(SpReqAnsContext<ResourceWatcherService_FetchSystemSnapshot_Req, ResourceWatcherService_FetchSystemSnapshot_Ans>::Pointer ctx);
- virtual void Handle_CheckIsFileExists(SpReqAnsContext<ResourceWatcherService_CheckIsFileExists_Req, ResourceWatcherService_CheckIsFileExists_Ans>::Pointer ctx);
- private:
- ResourceWatcherEntity* m_pEntity;
- };
- class ResourceWatcherEntity : public CEntityBase, public ITimerListener, public ISysVarListener
- {
- public:
- ResourceWatcherEntity(){}
- virtual ~ResourceWatcherEntity() {}
- virtual const char* GetEntityName() const { return "ResourceWatcher"; }
- const char* GetEntityVersion() const { return MODULE_VERSION_FULL; }
- virtual CServerSessionBase* OnNewSession(const char*, const char*)
- {
- return new ResourceWatcherServiceSession(this);
- }
- virtual void OnPreStart(CAutoArray<CSimpleStringA> strArgs,
- CSmartPointer<ITransactionContext> pTransactionContext)
- {
- DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Version %s; Complied at: %s %s", MOD_VERSION, __DATE__, __TIME__);
- ErrorCodeEnum errorCode = Error_Succeed;
- errorCode = m_fsm.Init(this);
- pTransactionContext->SendAnswer(errorCode);
- }
- void OnStarted()
- {
- m_fsm.AfterInit();
- DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("to check Sogou input install state...");
- CSmartPointer<IEntityFunction> spEntityFunction = GetFunction();
- ReportSogouInstallState(); //实体启动时检测搜狗输入法安装状态
- CSimpleStringA uiState;
- spEntityFunction->GetSysVar("UIState", uiState);
- if (uiState[0] == 'M') {
- m_fsm.TriggerAtStatusChanged(true);
- }
- spEntityFunction->RegistSysVarEvent("UIState", this);
- CSimpleStringA terminalStage;
- spEntityFunction->GetSysVar("TerminalStage", terminalStage);
- if (
- 0 == CSimpleStringA("C").Compare(terminalStage, true)
- || 0 == CSimpleStringA("S").Compare(terminalStage, true)
- || 0 == CSimpleStringA("M").Compare(terminalStage, true)
- || 0 == CSimpleStringA("A").Compare(terminalStage, true)
- )
- {
- DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Terminal has been lastStage(%s) before ResourceWatcher start.", terminalStage);
- m_fsm.TriggerProccessUpload();
- if (!(0 == CSimpleStringA("A").Compare(terminalStage, true))) //非首页,已达终态
- {
- m_fsm.TriggerFileListUpload();
- }
- }
- else
- {
- DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Terminal is starting, regist the teminalStage Event.");
- spEntityFunction->RegistSysVarEvent("TerminalStage", this);
- }
-
- CSmartPointer<IConfigInfo> spCtSettingConfig;
- GetFunction()->OpenConfig(Config_CenterSetting, spCtSettingConfig);
- int procFlag = 0;
- int closeSecCheck = 0;
- spCtSettingConfig->ReadConfigValueInt("ResourceWatcher", SECNAME_PROCESSDETECT, procFlag);
- if (procFlag > 0)
- {
- CheckProcessStatus(); //启动时检测一次进程
- spCtSettingConfig->ReadConfigValueInt("ResourceWatcher", "CloseSecProcDetect", closeSecCheck);
- if (!closeSecCheck)
- {
- SecProcCheck(); //检测一次安全软件
- }
- int tTime = 0;
- spCtSettingConfig->ReadConfigValueInt("ResourceWatcher", "ProcCheckTime", tTime);
- if (tTime >= 0)
- {
- if (tTime == 0) tTime = ENT_TIMERINTERVAL_PROCESS_CHECK; //没设置时间的话默认一小时间隔
- spEntityFunction->SetTimer(ENT_TIMERID_PROCESS_CHECK, this, tTime);
- }
- else
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Invalid proccess check time:%d", tTime);
- }
- }
- #if defined(RVC_OS_WIN)
- ReadFileContent();
- ReadFileInfo();
- #endif //RVC_OS_WIN
- }
- /*ignore*/
- virtual void OnPrePause(CSmartPointer<ITransactionContext> pTransactionContext)
- {
- pTransactionContext->SendAnswer(Error_Succeed);
- }
- /*ignore*/
- virtual void OnPreContinue(CSmartPointer<ITransactionContext> pTransactionContext)
- {
- pTransactionContext->SendAnswer(Error_Succeed);
- }
- /*invoke fsm.onExit(), invoked when entity will be closed*/
- virtual void OnPreClose(EntityCloseCauseEnum eCloseCause, CSmartPointer<ITransactionContext>pTransactionContext)
- {
- ErrorCodeEnum errorCode = m_fsm.OnExit();
- pTransactionContext->SendAnswer(errorCode);
- }
- virtual void OnSelfTest(EntityTestEnum eTestType,
- CSmartPointer<ITransactionContext>pTransactionContext)
- {
- m_fsm.SelfTest(eTestType, pTransactionContext);
- }
- virtual bool IsService() const
- {
- return true;
- }
- virtual bool IsMultiThread() const
- {
- return false;
- }
- void GetDevInfo(SpReqAnsContext<ResourceWatcherService_GetDevInfo_Req,
- ResourceWatcherService_GetDevInfo_Ans>::Pointer ctx)
- {
- SystemBasicInfo info;
- ErrorCodeEnum result = m_fsm.CatchSystemBasicInfo(info);
- if (Error_Succeed == result) {
- ctx->Ans.type = info.strProductName;
- ctx->Ans.model = info.strManufacturer;
- ctx->Ans.version = info.strSerialNumber;
- ctx->Ans.state = 0;
- }
- ctx->Answer(result);
- }
- void BizLinkDetect(SpReqAnsContext<ResourceWatcherService_BizLinkDetect_Req,
- ResourceWatcherService_BizLinkDetect_Ans>::Pointer ctx)
- {
- m_fsm.BizLinkDetect(ctx);
- }
- void CheckNetType(SpReqAnsContext<ResourceWatcherService_CheckNetType_Req,
- ResourceWatcherService_CheckNetType_Ans>::Pointer ctx)
- {
- m_fsm.CheckNetType(ctx);
- }
- void GetBizLinks(SpReqAnsContext<ResourceWatcherService_GetBizLinks_Req,
- ResourceWatcherService_GetBizLinks_Ans>::Pointer ctx)
- {
- m_fsm.GetBizLinks(ctx);
- }
- void OperateFile(
- SpReqAnsContext<ResourceWatcherService_OperateFile_Req,
- ResourceWatcherService_OperateFile_Ans>::Pointer ctx)
- {
- ctx->Answer(Error_NotSupport);
- }
- void InstallThirdPartyProgram(SpReqAnsContext<ResourceWatcherService_InstallThirdPartyProgram_Req,
- ResourceWatcherService_InstallThirdPartyProgram_Ans>::Pointer ctx);
- void FilesClean(SpReqAnsContext<ResourceWatcherService_FilesClean_Req,
- ResourceWatcherService_FilesClean_Ans>::Pointer ctx);
- void FetchSystemSnapshot(SpReqAnsContext<ResourceWatcherService_FetchSystemSnapshot_Req, ResourceWatcherService_FetchSystemSnapshot_Ans>::Pointer ctx);
- void InstallSogou(SpReqAnsContext<ResourceWatcherService_InstallThirdPartyProgram_Req,
- ResourceWatcherService_InstallThirdPartyProgram_Ans>::Pointer ctx);
- void CheckIsFileExists(SpReqAnsContext<ResourceWatcherService_CheckIsFileExists_Req,
- ResourceWatcherService_CheckIsFileExists_Ans>::Pointer ctx);
- int CheckMediaResource(int iFileType, const char* pFileName);
- void OnSysVarEvent(const char* pszKey, const char* pszValue, const char* pszOldValue, const char* pszEntityName)
- {
- if ((_strnicmp(pszKey, "UIState", strlen("UIState")) == 0)) {
- m_fsm.TriggerAtStatusChanged(_strnicmp(pszValue, "M", strlen("M")) == 0);
- }
- else if ((_strnicmp(pszKey, "TerminalStage", strlen("TerminalStage")) == 0)) {
- if (0 == CSimpleStringA("C").Compare(pszValue, true)
- || 0 == CSimpleStringA("S").Compare(pszValue, true)
- || 0 == CSimpleStringA("M").Compare(pszValue, true)
- || 0 == CSimpleStringA("A").Compare(pszValue, true)
- )
- {
- DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Status changed, teminalStage = %s.", pszValue);
- m_fsm.TriggerProccessUpload();
- if (!(0 == CSimpleStringA("A").Compare(pszValue, true))) //非通过准入的情况下,直接开始文件列表获取
- {
- m_fsm.TriggerFileListUpload();
- }
- }
- }
- }
- private:
- //report sogou input software install state and installed information
- ErrorCodeEnum ReportSogouInstallState();
- void OnTimeout(DWORD dwTimerID)
- {
- if (dwTimerID == ENT_TIMERID_PROCESS_CHECK) {
- CheckProcessStatus();
- }
- else {
- DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("Unkonwn timer id: %u", dwTimerID);
- }
- }
- std::string DoCheckCertainProcessStatus(const CAutoArray<CSimpleStringA>& pName);
- void SecProcCheck(); //检测终端安全软件应用
- ErrorCodeEnum UnzipPack(const char* unZipPackName);//解压文件
- CSimpleStringA GetFilePathWithDir(CSimpleStringA dir, CSimpleStringA fileName);
- std::vector<std::string> GetUserNameList(bool bExcludeRoot = false);
- ErrorCodeEnum RunShellScript(LPCTSTR cmdline);
- ErrorCodeEnum DeleteUnzipDir();//清理临时解压包文件
- bool is_str_utf8(const char* str);
- /// <summary>
- /// 通过集中配置检测指定进程是否存在
- /// </summary>
- void CheckProcessStatus();
- #if defined(RVC_OS_WIN)
- void ReadFileContent();
- void ReadFileInfo();
- void ChcekDiskFileSpace();
- #else
- ErrorCodeEnum GetSogouPkgDirPath(CSimpleStringA& strPkgPath);
- #endif //RVC_OS_WIN
- private:
- ResourceWatcherFSM m_fsm;
- };
- struct InstallSogouTask : public ITaskSp
- {
- SpReqAnsContext<ResourceWatcherService_InstallThirdPartyProgram_Req,
- ResourceWatcherService_InstallThirdPartyProgram_Ans>::Pointer ctx;
- ResourceWatcherEntity* m_pEntity;
- InstallSogouTask(ResourceWatcherEntity* entity) :m_pEntity(entity) {}
- void Process()
- {
- m_pEntity->InstallSogou(ctx);
- }
- };
|