1 /*
2  * Copyright (c) 2021-2023 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 #ifndef HIVIEW_SERVICE_ABILITY_PROXY_H
17 #define HIVIEW_SERVICE_ABILITY_PROXY_H
18 
19 #include <string>
20 
21 #include "hiview_err_code.h"
22 #include "hiview_file_info.h"
23 #include "ihiview_service_ability.h"
24 #include "iremote_proxy.h"
25 
26 namespace OHOS {
27 namespace HiviewDFX {
28 class HiviewServiceAbilityProxy : public IRemoteProxy<IHiviewServiceAbility> {
29 public:
HiviewServiceAbilityProxy(const sptr<IRemoteObject> & remote)30     explicit HiviewServiceAbilityProxy(const sptr<IRemoteObject> &remote)
31         : IRemoteProxy<IHiviewServiceAbility>(remote) {}
32     virtual ~HiviewServiceAbilityProxy() = default;
33 
34     int32_t List(const std::string& logType, std::vector<HiviewFileInfo>& fileInfos) override;
35     int32_t Copy(const std::string& logType, const std::string& logName, const std::string& dest) override;
36     int32_t Move(const std::string& logType, const std::string& logName, const std::string& dest) override;
37     int32_t Remove(const std::string& logType, const std::string& logName) override;
38 
39     CollectResultParcelable<int32_t> OpenSnapshotTrace(const std::vector<std::string>& tagGroups) override;
40     CollectResultParcelable<std::vector<std::string>> DumpSnapshotTrace(int32_t caller) override;
41     CollectResultParcelable<int32_t> OpenRecordingTrace(const std::string& tags) override;
42     CollectResultParcelable<int32_t> RecordingTraceOn() override;
43     CollectResultParcelable<std::vector<std::string>> RecordingTraceOff() override;
44     CollectResultParcelable<int32_t> CloseTrace() override;
45     CollectResultParcelable<int32_t> RecoverTrace() override;
46     CollectResultParcelable<int32_t> CaptureDurationTrace(UCollectClient::AppCaller &appCaller) override;
47     CollectResultParcelable<double> GetSysCpuUsage() override;
48     CollectResultParcelable<int32_t> SetAppResourceLimit(UCollectClient::MemoryCaller& memoryCaller) override;
49     CollectResultParcelable<int32_t> GetGraphicUsage() override;
50 
51 private:
52     int32_t CopyOrMoveFile(
53         const std::string& logType, const std::string& logName, const std::string& dest, bool isMove);
54 
55     template<typename T>
SendTraceRequest(HiviewServiceInterfaceCode requestCode,std::function<bool (MessageParcel &)> parcelHandler)56     CollectResultParcelable<T> SendTraceRequest(
57         HiviewServiceInterfaceCode requestCode, std::function<bool(MessageParcel&)> parcelHandler)
58     {
59         auto traceRet = CollectResultParcelable<T>::Init();
60         auto remote = Remote();
61         if (remote == nullptr) {
62             return traceRet;
63         }
64         MessageParcel data;
65         if (!data.WriteInterfaceToken(HiviewServiceAbilityProxy::GetDescriptor()) ||
66             (parcelHandler == nullptr) || !parcelHandler(data)) {
67             return traceRet;
68         }
69         MessageParcel reply;
70         MessageOption option;
71         int32_t ret = remote->SendRequest(static_cast<uint32_t>(requestCode), data, reply, option);
72         if (ret == TraceErrCode::ERR_OK) {
73             std::unique_ptr<CollectResultParcelable<T>> readParcel(reply.ReadParcelable<CollectResultParcelable<T>>());
74             if (readParcel == nullptr) {
75                 return traceRet;
76             }
77             traceRet = *readParcel;
78             return traceRet;
79         }
80         if (ret == TraceErrCode::ERR_PERMISSION_CHECK) {
81             traceRet.result_.retCode = UCollect::UcError::PERMISSION_CHECK_FAILED;
82         }
83         return traceRet;
84     }
85 };
86 } // namespace HiviewDFX
87 } // namespace OHOS
88 #endif // HIVIEW_SERVICE_ABILITY_PROXY_H
89