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