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