• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
47 private:
48     int32_t CopyOrMoveFile(
49         const std::string& logType, const std::string& logName, const std::string& dest, bool isMove);
50 
51     template<typename T>
SendTraceRequest(HiviewServiceInterfaceCode requestCode,std::function<bool (MessageParcel &)> parcelHandler)52     CollectResultParcelable<T> SendTraceRequest(
53         HiviewServiceInterfaceCode requestCode, std::function<bool(MessageParcel&)> parcelHandler)
54     {
55         auto traceRet = CollectResultParcelable<T>::Init();
56         auto remote = Remote();
57         if (remote == nullptr) {
58             return traceRet;
59         }
60         MessageParcel data;
61         if (!data.WriteInterfaceToken(HiviewServiceAbilityProxy::GetDescriptor()) ||
62             (parcelHandler == nullptr) || !parcelHandler(data)) {
63             return traceRet;
64         }
65         MessageParcel reply;
66         MessageOption option;
67         int32_t ret = remote->SendRequest(static_cast<uint32_t>(requestCode), data, reply, option);
68         if (ret == TraceErrCode::ERR_OK) {
69             std::unique_ptr<CollectResultParcelable<T>> readParcel(reply.ReadParcelable<CollectResultParcelable<T>>());
70             if (readParcel == nullptr) {
71                 return traceRet;
72             }
73             traceRet = *readParcel;
74             return traceRet;
75         }
76         if (ret == TraceErrCode::ERR_PERMISSION_CHECK) {
77             traceRet.result_.retCode = UCollect::UcError::PERMISSION_CHECK_FAILED;
78         }
79         return traceRet;
80     }
81 };
82 } // namespace HiviewDFX
83 } // namespace OHOS
84 #endif // HIVIEW_SERVICE_ABILITY_PROXY_H
85