diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4045fd6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea +.vscode/ diff --git a/services/BUILD.gn b/services/BUILD.gn index 4ef0508..5268c4b 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -25,6 +25,7 @@ config("print_service_config") { ohos_shared_library("print_service") { sources = [ + "src/hisys_event_util.cpp", "src/print_bms_death_recipient.cpp", "src/print_bms_helper.cpp", "src/print_callback_proxy.cpp", diff --git a/services/include/hisys_event_util.h b/services/include/hisys_event_util.h new file mode 100644 index 0000000..254aa41 --- /dev/null +++ b/services/include/hisys_event_util.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef HISYS_EVENT_UTIL_H +#define HISYS_EVENT_UTIL_H + +#include + +namespace OHOS::Print { + class HisysEventUtil { + public: + static void reportPrintSuccess(const std::string &msg); + + static void faultPrint(std::string scene, std::string msg); + + private: + static constexpr char HW_PRINT_SPOOLER[] = "HW_PRINT_SPOOLER"; + static const int PRINT_SUCCESS = 1003; + static const int FAULT_PRINT = 3002; + + static void reportBehaviorEvent(std::string name, int behaviorCode, const std::string &msg); + + static void reportFaultEvent(std::string name, int faultCode, std::string scene, std::string msg); + }; +} // namespace OHOS::Print +#endif // HISYS_EVENT_UTIL_H \ No newline at end of file diff --git a/services/include/print_service_ability.h b/services/include/print_service_ability.h index f23f415..d2acc38 100644 --- a/services/include/print_service_ability.h +++ b/services/include/print_service_ability.h @@ -96,6 +96,9 @@ private: void DestroyExtension(const std::string &printerId); void NotifyAppJobQueueChanged(const std::string &applyResult); std::shared_ptr getPrinterInfo(const std::string printerId); + bool isEprint(const std::string &printerId); + void ReportHisysEvent(const std::shared_ptr &jobInfo, const std::string &printerId, uint32_t subState); + void ReportCompletedPrint(const std::string & printerId); #ifndef TDD_ENABLE private: @@ -105,6 +108,8 @@ private: static std::mutex instanceLock_; static sptr instance_; static std::shared_ptr serviceHandler_; + static std::chrono::time_point startPrintTime_; + static std::string ingressPackage; std::recursive_mutex apiMutex_; std::map> registeredListeners_; diff --git a/services/src/hisys_event_util.cpp b/services/src/hisys_event_util.cpp new file mode 100644 index 0000000..4879575 --- /dev/null +++ b/services/src/hisys_event_util.cpp @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "hisysevent.h" +#include "print_log.h" +#include "hisys_event_util.h" + +namespace OHOS::Print { + using namespace OHOS::HiviewDFX; + + void HisysEventUtil::reportBehaviorEvent(std::string name, int behaviorCode, const std::string &msg) + { + HiSysEventWrite( + HW_PRINT_SPOOLER, + name, + HiSysEvent::EventType::BEHAVIOR, + "PACKAGE_NAME", "com.huawei.hmos.hwprintspooler", + "PROCESS_NAME", "hwPrintSpooler", + "BEHAVIOR_CODE", behaviorCode, + "MSG", msg); + } + + void HisysEventUtil::reportFaultEvent(std::string name, int faultCode, std::string scene, std::string msg) + { + HiSysEventWrite( + HW_PRINT_SPOOLER, + name, + HiSysEvent::EventType::FAULT, + "PACKAGE_NAME", "com.huawei.hmos.hwprintspooler", + "PROCESS_NAME", "hwPrintSpooler", + "FAULT_CODE", faultCode, + "SCENE", scene, + "MSG", msg); + } + + void HisysEventUtil::reportPrintSuccess(const std::string &msg) + { + reportBehaviorEvent("PRINT_SUCCESS", PRINT_SUCCESS, msg); + } + + void HisysEventUtil::faultPrint(std::string scene, std::string msg) + { + reportFaultEvent("PRINT_EXCEPTION", FAULT_PRINT, scene, msg); + } +} \ No newline at end of file diff --git a/services/src/print_service_ability.cpp b/services/src/print_service_ability.cpp index 61006ea..df05347 100644 --- a/services/src/print_service_ability.cpp +++ b/services/src/print_service_ability.cpp @@ -40,11 +40,14 @@ #include "common_event_manager.h" #include "common_event_support.h" #include "print_security_guard_manager.h" +#include "hisys_event_util.h" +#include "nlohmann/json.hpp" namespace OHOS::Print { using namespace std; using namespace OHOS::HiviewDFX; using namespace Security::AccessToken; +using json = nlohmann::json; const uint32_t MAX_RETRY_TIMES = 10; const uint32_t START_ABILITY_INTERVAL = 6; @@ -81,6 +84,8 @@ REGISTER_SYSTEM_ABILITY_BY_ID(PrintServiceAbility, PRINT_SERVICE_ID, true); std::mutex PrintServiceAbility::instanceLock_; sptr PrintServiceAbility::instance_; std::shared_ptr PrintServiceAbility::serviceHandler_; +std::chrono::time_point PrintServiceAbility::startPrintTime_; +std::string PrintServiceAbility::ingressPackage; PrintServiceAbility::PrintServiceAbility(int32_t systemAbilityId, bool runOnCreate) : SystemAbility(systemAbilityId, runOnCreate), state_(ServiceRunningState::STATE_NOT_START), @@ -227,6 +232,7 @@ int32_t PrintServiceAbility::StartPrint(const std::vector &fileList BuildFDParam(fdList, want); int32_t callerTokenId = static_cast(IPCSkeleton::GetCallingTokenID()); std::string callerPkg = DelayedSingleton::GetInstance()->QueryCallerBundleName(); + ingressPackage = callerPkg; int32_t callerUid = IPCSkeleton::GetCallingUid(); int32_t callerPid = IPCSkeleton::GetCallingPid(); want.SetParam(AAFwk::Want::PARAM_RESV_CALLER_TOKEN, callerTokenId); @@ -504,6 +510,7 @@ int32_t PrintServiceAbility::QueryPrintJobById(std::string &printJobId, PrintJob int32_t PrintServiceAbility::StartPrintJob(const PrintJob &jobInfo) { + startPrintTime_ = std::chrono::high_resolution_clock::now(); ManualStart(); if (!CheckPermission(PERMISSION_NAME_PRINT_JOB)) { PRINT_HILOGE("no permission to access print service"); @@ -802,15 +809,16 @@ int32_t PrintServiceAbility::UpdatePrintJobState(const std::string &jobId, uint3 SendPrintJobEvent(*jobIt->second); auto printerId = jobIt->second->GetPrinterId(); + if (state == PRINT_JOB_BLOCKED) { + ReportHisysEvent(jobIt->second, printerId, subState); + } if (state == PRINT_JOB_COMPLETED) { if (jobInQueue) { printerJobMap_[printerId].erase(jobId); queuedJobList_.erase(jobIt); } if (printerJobMap_[printerId].empty()) { - NotifyAppJobQueueChanged(QUEUE_JOB_LIST_COMPLETED); - PRINT_HILOGD("no print job exists, destroy extension"); - DestroyExtension(printerId); + ReportCompletedPrint(printerId); } SendQueuePrintJob(printerId); } @@ -819,6 +827,55 @@ int32_t PrintServiceAbility::UpdatePrintJobState(const std::string &jobId, uint3 return E_PRINT_NONE; } +void PrintServiceAbility::ReportCompletedPrint(const std::string &printerId) +{ + NotifyAppJobQueueChanged(QUEUE_JOB_LIST_COMPLETED); + PRINT_HILOGD("no print job exists, destroy extension"); + DestroyExtension(printerId); + json msg; + auto endPrintTime = std::chrono::high_resolution_clock::now(); + auto printTime = std::chrono::duration_cast(endPrintTime - startPrintTime_); + msg["PRINT_TIME"] = printTime.count(); + msg["INGRESS_PACKAGE"] = ingressPackage; + msg["STATUS"] = 0; + HisysEventUtil::reportPrintSuccess(msg.dump()); +} + +void PrintServiceAbility::ReportHisysEvent(const std::shared_ptr &jobInfo, + const std::string &printerId, uint32_t subState) +{ + json msg; + auto endPrintTime = std::chrono::high_resolution_clock::now(); + auto printTime = std::chrono::duration_cast(endPrintTime - startPrintTime_); + msg["PRINT_TIME"] = printTime.count(); + msg["INGRESS_PACKAGE"] = ingressPackage; + if (isEprint(printerId)) { + msg["PRINT_TYPE"] = 1; + } else { + msg["PRINT_TYPE"] = 0; + } + auto printInfo = printerInfoList_.find(printerId); + std::vector fdList; + jobInfo->GetFdList(fdList); + msg["FILE_NUM"] = fdList.size(); + msg["PAGE_NUM"] = fdList.size(); + if (printInfo == printerInfoList_.end()) { + msg["MODEL"] = ""; + } else { + msg["MODEL"] = printInfo->second->GetPrinterName(); + } + msg["COPIES_SETTING"] = jobInfo->GetCopyNumber(); + std::string option = jobInfo->GetOption(); + PRINT_HILOGI("test option:%{public}s", option.c_str()); + json optionJson = json::parse(option); + PRINT_HILOGI("test optionJson: %{public}s", optionJson.dump().c_str()); + PRINT_HILOGI("test jobDescription: %{public}s", optionJson["jobDescription"].get().c_str()); + msg["JOB_DESCRIPTION"] = optionJson["jobDescription"].get(); + msg["PRINT_STYLE_SETTING"] = jobInfo->GetDuplexMode(); + msg["FAIL_REASON_CODE"] = subState; + HisysEventUtil::faultPrint("PRINT_JOB_BLOCKED", msg.dump()); +} + void PrintServiceAbility::NotifyAppJobQueueChanged(const std::string &applyResult) { PRINT_HILOGD("NotifyAppJobQueueChanged started. %{public}s ", applyResult.c_str()); @@ -830,6 +887,18 @@ void PrintServiceAbility::NotifyAppJobQueueChanged(const std::string &applyResul PRINT_HILOGD("NotifyAppJobQueueChanged end."); } +bool PrintServiceAbility::isEprint(const std::string &printerId) +{ + if (printerId.length() <= 0) { + return false; + } + std::string ePrintID = "ePrintID"; + if (printerId.length() < ePrintID.length()) { + return false; + } + return std::equal(ePrintID.rbegin(), ePrintID.rend(), printerId.rbegin()); +} + void PrintServiceAbility::DestroyExtension(const std::string &printerId) { std::lock_guard lock(apiMutex_);