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 "publish/scb_dump_subscriber.h"
17 #include "window_manager_hilog.h"
18 
19 namespace OHOS::Rosen {
20 static const std::string TIME_OUT("timeout");
21 constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, HILOG_DOMAIN_WINDOW, "SceneSessionManager" };
22 
OnReceiveEvent(const EventFwk::CommonEventData & data)23 void ScbDumpSubscriber::OnReceiveEvent(const EventFwk::CommonEventData& data)
24 {
25     std::lock_guard<std::mutex> lock(mutex_);
26     std::ostringstream oss;
27     oss << data.GetData() << std::endl;
28     dumpinfo_ = oss.str();
29     valueReady_ = true;
30     cv_.notify_all();
31 }
32 
GetDebugDumpInfo(std::chrono::milliseconds const & time)33 std::string ScbDumpSubscriber::GetDebugDumpInfo(std::chrono::milliseconds const &time)
34 {
35     std::unique_lock<std::mutex> lock(mutex_);
36     if (cv_.wait_for(lock, time, [&] { return valueReady_; })) {
37         return dumpinfo_;
38     }
39     return TIME_OUT; // 超时返回
40 }
41 
Publish(std::string cmd)42 WSError ScbDumpSubscriber::Publish(std::string cmd)
43 {
44     valueReady_ = false;
45     static const std::string scbDebugEventListenerName = "com.ohos.sceneboard.debug.event.listener";
46     AAFwk::Want want;
47     want.SetAction(scbDebugEventListenerName);
48 
49     EventFwk::CommonEventData commonEventData;
50     commonEventData.SetWant(want);
51     commonEventData.SetCode(0);
52     commonEventData.SetData(cmd);
53 
54     EventFwk::CommonEventPublishInfo publishInfo;
55     publishInfo.SetSticky(false);
56     publishInfo.SetOrdered(false);
57 
58     // publish the common event
59     bool ret = EventFwk::CommonEventManager::PublishCommonEvent(commonEventData, publishInfo, nullptr);
60     if (!ret) {
61         TLOGE(WmsLogTag::WMS_FOCUS, "publish debug event to scene error.");
62         return WSError::WS_ERROR_INVALID_OPERATION;
63     }
64     return WSError::WS_OK;
65 }
66 
Subscribe(std::shared_ptr<ScbDumpSubscriber> & scbSubscriber)67 void ScbDumpSubscriber::Subscribe(std::shared_ptr<ScbDumpSubscriber>& scbSubscriber)
68 {
69     static const std::string scbDebugEventResponseName = "com.ohos.sceneboard.debug.event.response";
70 
71     EventFwk::MatchingSkills matchingSkills;
72     matchingSkills.AddEvent(scbDebugEventResponseName);
73 
74     EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
75 
76     if (scbSubscriber == nullptr) {
77         scbSubscriber = std::make_shared<ScbDumpSubscriber>(subscribeInfo);
78     }
79 
80     EventFwk::CommonEventManager::SubscribeCommonEvent(scbSubscriber);
81 }
82 
UnSubscribe(std::shared_ptr<ScbDumpSubscriber> & scbSubscriber)83 void ScbDumpSubscriber::UnSubscribe(std::shared_ptr<ScbDumpSubscriber>& scbSubscriber)
84 {
85     if (scbSubscriber) {
86         EventFwk::CommonEventManager::UnSubscribeCommonEvent(scbSubscriber);
87     }
88 }
89 
JoinCommands(const std::vector<std::string> & params,int size)90 std::string ScbDumpSubscriber::JoinCommands(const std::vector<std::string>& params, int size)
91 {
92     std::string cmd;
93     for (int i = 1; i < size; i++) { // 从1开始,0为-b
94         cmd += params[i];
95         cmd += ' ';
96     }
97     return cmd;
98 }
99 
100 } // namespace OHOS::Rosen