1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved.
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 "monitor_service_stub.h"
17 #include "monitor_server.h"
18 #include "media_log.h"
19 #include "media_errors.h"
20 #include "ipc_skeleton.h"
21 
22 namespace {
23 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_PLAYER, "MonitorServiceStub"};
24 }
25 
26 namespace OHOS {
27 namespace Media {
MonitorServiceStub()28 MonitorServiceStub::MonitorServiceStub()
29 {
30     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
31 }
32 
~MonitorServiceStub()33 MonitorServiceStub::~MonitorServiceStub()
34 {
35     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
36 }
37 
GetInstance()38 sptr<MonitorServiceStub> MonitorServiceStub::GetInstance()
39 {
40     static sptr<MonitorServiceStub> monitor = nullptr;
41     if (monitor == nullptr) {
42         monitor = new(std::nothrow) MonitorServiceStub();
43         (void)monitor->Init();
44     }
45     return monitor;
46 }
47 
Init()48 int32_t MonitorServiceStub::Init()
49 {
50     monitorFuncs_ = {
51         { MONITOR_CLICK, [this](MessageParcel &data, MessageParcel &reply) { return Click(); } },
52         { MONITOR_ENABLE, [this](MessageParcel &data, MessageParcel &reply) { return EnableMonitor(); } },
53         { MONITOR_DISABLE, [this](MessageParcel &data, MessageParcel &reply) { return DisableMonitor(); } },
54     };
55     return MSERR_OK;
56 }
57 
DumpInfo(int32_t fd,bool needDetail)58 int32_t MonitorServiceStub::DumpInfo(int32_t fd, bool needDetail)
59 {
60     return MonitorServer::GetInstance().Dump(fd, needDetail);
61 }
62 
OnClientDie(int32_t pid)63 int32_t MonitorServiceStub::OnClientDie(int32_t pid)
64 {
65     return MonitorServer::GetInstance().OnClientDie(pid);
66 }
67 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)68 int MonitorServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
69     MessageOption &option)
70 {
71     MEDIA_LOGD("Stub: OnRemoteRequest of code: %{public}d is received", code);
72 
73     auto remoteDescriptor = data.ReadInterfaceToken();
74     CHECK_AND_RETURN_RET_LOG(MonitorServiceStub::GetDescriptor() == remoteDescriptor,
75         MSERR_INVALID_OPERATION, "Invalid descriptor");
76 
77     auto itFunc = monitorFuncs_.find(code);
78     CHECK_AND_RETURN_RET_LOG(itFunc != monitorFuncs_.end(), IPCObjectStub::OnRemoteRequest(code, data, reply, option),
79         "MonitorServiceStub: no member func supporting, applying default process");
80 
81     auto memberFunc = itFunc->second;
82     CHECK_AND_RETURN_RET_LOG(memberFunc != nullptr, IPCObjectStub::OnRemoteRequest(code, data, reply, option),
83         "MonitorServiceStub: no member func supporting, applying default process");
84 
85     int32_t ret = memberFunc(data, reply);
86     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_OK, "calling memberFunc is failed.");
87     return MSERR_OK;
88 }
89 
Click()90 int32_t MonitorServiceStub::Click()
91 {
92     int32_t pid = IPCSkeleton::GetCallingPid();
93     return MonitorServer::GetInstance().Click(pid);
94 }
95 
EnableMonitor()96 int32_t MonitorServiceStub::EnableMonitor()
97 {
98     int32_t pid = IPCSkeleton::GetCallingPid();
99     return MonitorServer::GetInstance().EnableMonitor(pid);
100 }
101 
DisableMonitor()102 int32_t MonitorServiceStub::DisableMonitor()
103 {
104     int32_t pid = IPCSkeleton::GetCallingPid();
105     return MonitorServer::GetInstance().DisableMonitor(pid);
106 }
107 
Click(MessageParcel & data,MessageParcel & reply)108 int32_t MonitorServiceStub::Click(MessageParcel &data, MessageParcel &reply)
109 {
110     (void)data;
111     reply.WriteInt32(Click());
112     return MSERR_OK;
113 }
114 
EnableMonitor(MessageParcel & data,MessageParcel & reply)115 int32_t MonitorServiceStub::EnableMonitor(MessageParcel &data, MessageParcel &reply)
116 {
117     (void)data;
118     reply.WriteInt32(EnableMonitor());
119     return MSERR_OK;
120 }
121 
DisableMonitor(MessageParcel & data,MessageParcel & reply)122 int32_t MonitorServiceStub::DisableMonitor(MessageParcel &data, MessageParcel &reply)
123 {
124     (void)data;
125     reply.WriteInt32(DisableMonitor());
126     return MSERR_OK;
127 }
128 } // namespace Media
129 } // namespace OHOS
130