1 /*
2  * Copyright (c) 2024 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 "core/components_ng/manager/drag_drop/drag_drop_behavior_reporter/drag_drop_behavior_reporter.h"
17 
18 #include "base/log/event_report.h"
19 #include "core/pipeline_ng/pipeline_context.h"
20 
21 namespace OHOS::Ace::NG {
22 namespace {
23 const std::string PNAME_ID {"ARKUI"};
24 const std::string PVERSION_ID {"1.0.0"};
25 } // namespace
26 
GetInstance()27 DragDropBehaviorReporter& DragDropBehaviorReporter::GetInstance()
28 {
29     static DragDropBehaviorReporter behaviorReporter;
30     return behaviorReporter;
31 }
32 
UpdateDragStartResult(DragStartResult result)33 void DragDropBehaviorReporter::UpdateDragStartResult(DragStartResult result)
34 {
35     if (startResult_ != DragStartResult::UNKNOW && result == DragStartResult::DRAG_START_SUCCESS) {
36         return;
37     }
38     startResult_ = result;
39 }
40 
UpdateDragStopResult(DragStopResult result)41 void DragDropBehaviorReporter::UpdateDragStopResult(DragStopResult result)
42 {
43     if (stopResult_ != DragStopResult::UNKNOW && result == DragStopResult::DRAG_SOTP_SUCCESS) {
44         return;
45     }
46     stopResult_ = result;
47 }
48 
UpdateRecordSize(int32_t recordSize)49 void DragDropBehaviorReporter::UpdateRecordSize(int32_t recordSize)
50 {
51     recordSize_ = recordSize;
52 }
53 
UpdateSummaryType(const std::string & summaryType)54 void DragDropBehaviorReporter::UpdateSummaryType(const std::string& summaryType)
55 {
56     summaryType_ = summaryType;
57 }
58 
UpdateAllowDropType(const std::set<std::string> & allowDropType)59 void DragDropBehaviorReporter::UpdateAllowDropType(const std::set<std::string>& allowDropType)
60 {
61     allowDropType_ = allowDropType;
62 }
63 
UpdateIsCrossing(CrossingEnd isCrossing)64 void DragDropBehaviorReporter::UpdateIsCrossing(CrossingEnd isCrossing)
65 {
66     isCrossing_ = isCrossing;
67 }
68 
UpdateContainerId(int32_t containerId)69 void DragDropBehaviorReporter::UpdateContainerId(int32_t containerId)
70 {
71     containerId_ = containerId;
72 }
73 
Reset()74 void DragDropBehaviorReporter::Reset()
75 {
76     isCrossing_ = CrossingEnd::NOT_CROSSING;
77     startResult_ = DragStartResult::UNKNOW;
78     stopResult_ = DragStopResult::UNKNOW;
79     recordSize_ = 0;
80     summaryType_ = "";
81     allowDropType_ = {};
82 }
83 
Submit(DragReporterPharse pharse,int32_t containerId)84 void DragDropBehaviorReporter::Submit(DragReporterPharse pharse, int32_t containerId)
85 {
86     bool isStart = pharse == DragReporterPharse::DRAG_START;
87     std::string dragBehavior = isStart ? "DRAG_START" : "DRAG_STOP";
88     int32_t result = isStart ? static_cast<int32_t>(startResult_) : static_cast<int32_t>(stopResult_);
89     std::string allowDropTypes;
90     for (const auto& type: allowDropType_) {
91         std::string str = type + ";";
92         allowDropTypes += str;
93     }
94     containerId_ = containerId_ == INSTANCE_ID_UNDEFINED ? containerId : containerId_;
95     auto container = Container::GetContainer(containerId_);
96     CHECK_NULL_VOID(container);
97     std::string hostName = container->GetBundleName();
98 
99     DragInfo dragInfo { static_cast<int32_t>(isCrossing_), result, recordSize_, dragBehavior, PNAME_ID,
100         PVERSION_ID, hostName, summaryType_, allowDropTypes };
101 
102     auto pipeline = container->GetPipelineContext();
103     CHECK_NULL_VOID(pipeline);
104     auto taskScheduler = pipeline->GetTaskExecutor();
105     CHECK_NULL_VOID(taskScheduler);
106     taskScheduler->PostTask(
107         [dragInfo]() {
108             EventReport::ReportDragInfo(dragInfo);
109         },
110         TaskExecutor::TaskType::BACKGROUND, "ArkUIDragDropBehaviorReporter");
111     Reset();
112 }
113 } // namespace OHOS::Ace::NG