1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "form_renderer_event_report.h"
17
18 #include <cinttypes>
19 #include <string>
20 #include <map>
21
22 #include "hisysevent.h"
23 #include "xcollie/xcollie.h"
24 #include "xcollie/xcollie_define.h"
25 #include "form_renderer_hilog.h"
26
27 namespace OHOS::Ace {
28 namespace {
29 constexpr const char *FORM_NODE_ERROR = "FORM_NODE_ERROR";
30 constexpr const char *EVENT_KEY_FORM_ID = "FORM_ID";
31 constexpr const char *EVENT_KEY_BUNDLE_NAME = "BUNDLE_NAME";
32 constexpr const char *EVENT_KEY_FORM_NAME = "FORM_NAME";
33 constexpr const char *EVENT_KEY_ERROR_NAME = "ERROR_NAME";
34 constexpr const char *EVENT_KEY_ERROR_TYPE = "ERROR_TYPE";
35 constexpr const char *EVENT_KEY_ERROR_CODE = "ERROR_CODE";
36 constexpr int32_t SURFACE_NODE_CREATE_FAILED = 2;
37 constexpr uint32_t SURFACE_NODE_CREATE_TIMEOUT = 10;
38 }
39
40 std::unordered_map<int64_t, int32_t> FormRenderEventReport::waitSurfaceNodeTimerMap_ = {};
41 std::mutex FormRenderEventReport::timerMutex_;
42
StartSurfaceNodeTimeoutReportTimer(int64_t formId,const std::string & bundleName,const std::string & formName)43 void FormRenderEventReport::StartSurfaceNodeTimeoutReportTimer(int64_t formId, const std::string &bundleName,
44 const std::string &formName)
45 {
46 auto timeoutCallback = [formId, bundleName, formName](void *) {
47 HILOG_INFO("wait surface node create timeout, formId: %{public}" PRId64, formId);
48 HiSysEventWrite(
49 OHOS::HiviewDFX::HiSysEvent::Domain::FORM_MANAGER,
50 "FORM_ERROR",
51 OHOS::HiviewDFX::HiSysEvent::EventType::FAULT,
52 EVENT_KEY_FORM_ID, formId,
53 EVENT_KEY_BUNDLE_NAME, bundleName,
54 EVENT_KEY_FORM_NAME, formName,
55 EVENT_KEY_ERROR_NAME, FORM_NODE_ERROR,
56 EVENT_KEY_ERROR_TYPE, SURFACE_NODE_CREATE_FAILED,
57 EVENT_KEY_ERROR_CODE, 0);
58 };
59 FormRenderEventReport::StopTimer(formId);
60 std::string taskName = "FRS_WaitSurfaceNodeCreate_" + std::to_string(formId);
61 int32_t timerId = OHOS::HiviewDFX::XCollie::GetInstance().SetTimer(
62 taskName, SURFACE_NODE_CREATE_TIMEOUT, timeoutCallback, nullptr, HiviewDFX::XCOLLIE_FLAG_NOOP);
63 HILOG_INFO("wait surface node create start, formId: %{public}" PRId64 "timerId: %{public}d", formId, timerId);
64 std::lock_guard<std::mutex> lock(FormRenderEventReport::timerMutex_);
65 FormRenderEventReport::waitSurfaceNodeTimerMap_.emplace(formId, timerId);
66 }
67
StopTimer(int64_t formId)68 void FormRenderEventReport::StopTimer(int64_t formId)
69 {
70 std::lock_guard<std::mutex> lock(FormRenderEventReport::timerMutex_);
71 auto iter = FormRenderEventReport::waitSurfaceNodeTimerMap_.find(formId);
72 if (iter != FormRenderEventReport::waitSurfaceNodeTimerMap_.end()) {
73 int32_t timerId = iter->second;
74 HILOG_INFO("surface node create completed, formId:%{public}" PRId64 "timerId: %{public}d", formId, timerId);
75 OHOS::HiviewDFX::XCollie::GetInstance().CancelTimer(timerId);
76 FormRenderEventReport::waitSurfaceNodeTimerMap_.erase(iter);
77 }
78 }
79 } // namespace OHOS::Ace
80