• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
18 #include <sstream>
19 
20 #include "window_manager_hilog.h"
21 
22 namespace OHOS::Rosen {
23 
OnReceiveEvent(const EventFwk::CommonEventData & data)24 void ScbDumpSubscriber::OnReceiveEvent(const EventFwk::CommonEventData& data)
25 {
26     std::ostringstream oss;
27     oss << data.GetData() << std::endl;
28     std::lock_guard<std::mutex> lock(mutex_);
29     dumpInfo_ = oss.str();
30     valueReady_ = true;
31     cv_.notify_all();
32 }
33 
GetDebugDumpInfo(const std::chrono::milliseconds & time)34 std::string ScbDumpSubscriber::GetDebugDumpInfo(const std::chrono::milliseconds& time)
35 {
36     std::unique_lock<std::mutex> lock(mutex_);
37     if (cv_.wait_for(lock, time, [&] { return valueReady_; })) {
38         return dumpInfo_;
39     }
40     return "timeout";
41 }
42 
Publish(const std::string & cmd)43 WSError ScbDumpSubscriber::Publish(const std::string& cmd)
44 {
45     {
46         std::unique_lock<std::mutex> lock(mutex_);
47         valueReady_ = false;
48     }
49 
50     AAFwk::Want want;
51     want.SetAction("com.ohos.sceneboard.debug.event.listener");
52     EventFwk::CommonEventData commonEventData;
53     commonEventData.SetWant(want);
54     commonEventData.SetCode(0);
55     commonEventData.SetData(cmd);
56     EventFwk::CommonEventPublishInfo publishInfo;
57     publishInfo.SetSticky(false);
58     publishInfo.SetOrdered(false);
59     publishInfo.SetBundleName("com.ohos.sceneboard");
60 
61     // publish the common event
62     bool ret = EventFwk::CommonEventManager::PublishCommonEvent(commonEventData, publishInfo, nullptr);
63     if (!ret) {
64         TLOGE(WmsLogTag::DEFAULT, "publish scene debug event error.");
65         return WSError::WS_ERROR_INVALID_OPERATION;
66     }
67     return WSError::WS_OK;
68 }
69 
Subscribe()70 std::shared_ptr<ScbDumpSubscriber> ScbDumpSubscriber::Subscribe()
71 {
72     EventFwk::MatchingSkills matchingSkills;
73     matchingSkills.AddEvent("com.ohos.sceneboard.debug.event.response");
74     EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
75     subscribeInfo.SetPublisherBundleName("com.ohos.sceneboard");
76     auto scbSubscriber = std::make_shared<ScbDumpSubscriber>(subscribeInfo);
77     EventFwk::CommonEventManager::SubscribeCommonEvent(scbSubscriber);
78     return scbSubscriber;
79 }
80 
UnSubscribe(const std::shared_ptr<ScbDumpSubscriber> & scbSubscriber)81 void ScbDumpSubscriber::UnSubscribe(const std::shared_ptr<ScbDumpSubscriber>& scbSubscriber)
82 {
83     if (scbSubscriber) {
84         EventFwk::CommonEventManager::UnSubscribeCommonEvent(scbSubscriber);
85     }
86 }
87 
88 } // namespace OHOS::Rosen