1 /*
2  * Copyright (c) 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 OHOS_HIVIEWDFX_ADAPTER_SERVICE_IDL_INCLUDE_TRACE_DELEGATE_H
17 #define OHOS_HIVIEWDFX_ADAPTER_SERVICE_IDL_INCLUDE_TRACE_DELEGATE_H
18 
19 #include <vector>
20 
21 #include "collect_result.h"
22 #include "hiview_service_ability_proxy.h"
23 #include "hiview_remote_service.h"
24 #include "parcel.h"
25 #include "client/trace_collector.h"
26 
27 namespace OHOS {
28 namespace HiviewDFX {
29 class HiViewServiceTraceDelegate {
30 public:
31     static CollectResult<int32_t> OpenSnapshot(const std::vector<std::string>& tagGroups);
32     static CollectResult<std::vector<std::string>> DumpSnapshot(int32_t caller);
33     static CollectResult<int32_t> OpenRecording(const std::string& tags);
34     static CollectResult<int32_t> RecordingOn();
35     static CollectResult<std::vector<std::string>> RecordingOff();
36     static CollectResult<int32_t> Close();
37     static CollectResult<int32_t> Recover();
38     static CollectResult<int32_t> CaptureDurationTrace(UCollectClient::AppCaller &appCaller);
39 
40 private:
41     template<typename T>
TraceCalling(std::function<CollectResultParcelable<T> (HiviewServiceAbilityProxy &)> proxyHandler)42     static CollectResult<T> TraceCalling(
43         std::function<CollectResultParcelable<T>(HiviewServiceAbilityProxy&)> proxyHandler)
44     {
45         CollectResult<T> ret;
46         if (proxyHandler == nullptr) {
47             return ret;
48         }
49         auto service = RemoteService::GetHiViewRemoteService();
50         if (service == nullptr) {
51             return ret;
52         }
53         HiviewServiceAbilityProxy proxy(service);
54         auto traceRet = proxyHandler(proxy);
55         return traceRet.result_;
56     }
57 };
58 } // namespace HiviewDFX
59 } // namespace OHOS
60 
61 #endif // OHOS_HIVIEWDFX_ADAPTER_SERVICE_IDL_INCLUDE_TRACE_DELEGATE_H
62