1 /*
2 * Copyright (c) 2022 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 "frame_msg_intf.h"
17 #include "intellisense_server.h"
18 #include "event_handler.h"
19 #include "rme_log_domain.h"
20
21 namespace OHOS {
22 namespace RME {
23 DEFINE_RMELOG_INTELLISENSE("ueaServer-FrameMsgIntf");
24
GetInstance()25 FrameMsgIntf& FrameMsgIntf::GetInstance()
26 {
27 static FrameMsgIntf instance;
28 return instance;
29 }
30
Init()31 bool FrameMsgIntf::Init()
32 {
33 std::lock_guard<std::mutex> autoLock(frameMsgIntfMutex_);
34 RME_LOGI("init begin!");
35 if (!GetThreadHandler()) {
36 return false;
37 }
38 threadHandler_->PostTask([] {
39 IntelliSenseServer::GetInstance().Init();
40 });
41 return true;
42 }
43
GetThreadHandler()44 bool FrameMsgIntf::GetThreadHandler()
45 {
46 if (threadHandler_ == nullptr) {
47 runner_ = AppExecFwk::EventRunner::Create("frame_aware_sched_msg");
48 if (runner_ == nullptr) {
49 RME_LOGE("failed to create eventRunner!");
50 return false;
51 }
52 threadHandler_ = std::make_shared<AppExecFwk::EventHandler>(runner_);
53 if (threadHandler_ == nullptr) {
54 RME_LOGE("failed to create thread handler!");
55 return false;
56 }
57 }
58 RME_LOGI("Init process success!");
59 return true;
60 }
61
62
ReportAppInfo(std::string appName,std::string processName,int pid,AppStateUpdateReason reason)63 void FrameMsgIntf::ReportAppInfo(std::string appName, std::string processName, int pid, AppStateUpdateReason reason)
64 {
65 std::lock_guard<std::mutex> autoLock(frameMsgIntfMutex_);
66 if (threadHandler_ == nullptr) {
67 RME_LOGI("[ReportAppInfo]:threandHandler none!");
68 return;
69 }
70 threadHandler_->PostTask([appName, processName, pid, reason] {
71 IntelliSenseServer::GetInstance().ReportMessage(appName, processName, pid, reason);
72 });
73 }
74
ReportWindowFocus(const int pid,const int isFocus)75 void FrameMsgIntf::ReportWindowFocus(const int pid, const int isFocus)
76 {
77 std::lock_guard<std::mutex> autoLock(frameMsgIntfMutex_);
78 if (threadHandler_ == nullptr) {
79 RME_LOGE("[ReportWindowFocus]:threandHandler none!");
80 return;
81 }
82 threadHandler_->PostTask([pid, isFocus] {
83 IntelliSenseServer::GetInstance().ReportWindowFocus(pid, isFocus);
84 });
85 }
86
ReportProcessInfo(const int pid,const int tid,ThreadState state)87 void FrameMsgIntf::ReportProcessInfo(const int pid, const int tid, ThreadState state)
88 {
89 std::lock_guard<std::mutex> autoLock(frameMsgIntfMutex_);
90 if (threadHandler_ == nullptr) {
91 RME_LOGI("[ReportProcessInfo]:threandHandler none!");
92 return;
93 }
94 threadHandler_->PostTask([pid, tid, state] {
95 IntelliSenseServer::GetInstance().ReportProcessInfo(pid, tid, state);
96 });
97 }
98
Stop()99 void FrameMsgIntf::Stop()
100 {
101 std::lock_guard<std::mutex> autoLock(frameMsgIntfMutex_);
102 if (threadHandler_ != nullptr) {
103 threadHandler_->RemoveAllEvents();
104 threadHandler_ = nullptr;
105 }
106 if (runner_ != nullptr) {
107 runner_->Stop();
108 runner_ = nullptr;
109 }
110 RME_LOGI("stop eventRunner success!");
111 }
112 } // namespace RME
113 } // namespace OHOS
114
115