1/*
2 * Copyright (c) 2021 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 ECMASCRIPT_TOOLING_AGENT_PROFILER_IMPL_H
17#define ECMASCRIPT_TOOLING_AGENT_PROFILER_IMPL_H
18
19#include "tooling/base/pt_params.h"
20#include "tooling/base/pt_returns.h"
21#include "dispatcher.h"
22
23#include "ecmascript/dfx/cpu_profiler/samples_record.h"
24#include "libpandabase/macros.h"
25
26namespace panda::ecmascript::tooling {
27class ProfilerImpl final {
28public:
29    ProfilerImpl(const EcmaVM *vm, ProtocolChannel *channel) : vm_(vm), frontend_(channel) {}
30    ~ProfilerImpl() = default;
31
32    DispatchResponse Disable();
33    DispatchResponse Enable();
34    DispatchResponse Start();
35    DispatchResponse Stop(std::unique_ptr<Profile> *profile);
36    DispatchResponse SetSamplingInterval(const SetSamplingIntervalParams &params);
37    DispatchResponse GetBestEffortCoverage();
38    DispatchResponse StopPreciseCoverage();
39    DispatchResponse TakePreciseCoverage();
40    DispatchResponse StartPreciseCoverage(const StartPreciseCoverageParams &params);
41    DispatchResponse StartTypeProfile();
42    DispatchResponse StopTypeProfile();
43    DispatchResponse TakeTypeProfile();
44    DispatchResponse EnableSerializationTimeoutCheck(const SeriliazationTimeoutCheckEnableParams &params);
45    DispatchResponse DisableSerializationTimeoutCheck();
46
47    class DispatcherImpl final : public DispatcherBase {
48    public:
49        DispatcherImpl(ProtocolChannel *channel, std::unique_ptr<ProfilerImpl> profiler)
50            : DispatcherBase(channel), profiler_(std::move(profiler)) {}
51        ~DispatcherImpl() override = default;
52
53        void Dispatch(const DispatchRequest &request) override;
54        void Enable(const DispatchRequest &request);
55        void Disable(const DispatchRequest &request);
56        void Start(const DispatchRequest &request);
57        void Stop(const DispatchRequest &request);
58        void SetSamplingInterval(const DispatchRequest &request);
59        void GetBestEffortCoverage(const DispatchRequest &request);
60        void StopPreciseCoverage(const DispatchRequest &request);
61        void TakePreciseCoverage(const DispatchRequest &request);
62        void StartPreciseCoverage(const DispatchRequest &request);
63        void StartTypeProfile(const DispatchRequest &request);
64        void StopTypeProfile(const DispatchRequest &request);
65        void TakeTypeProfile(const DispatchRequest &request);
66        void EnableSerializationTimeoutCheck(const DispatchRequest &request);
67        void DisableSerializationTimeoutCheck(const DispatchRequest &request);
68
69        enum class Method {
70            DISABLE,
71            ENABLE,
72            START,
73            STOP,
74            SET_SAMPLING_INTERVAL,
75            GET_BEST_EFFORT_COVERAGE,
76            STOP_PRECISE_COVERAGE,
77            TAKE_PRECISE_COVERAGE,
78            START_PRECISE_COVERAGE,
79            START_TYPE_PROFILE,
80            STOP_TYPE_PROFILE,
81            TAKE_TYPE_PROFILE,
82            ENABLE_SERIALIZATION_TIMEOUT_CHECK,
83            DISABLE_SERIALIZATION_TIMEOUT_CHECK,
84            UNKNOWN
85        };
86        Method GetMethodEnum(const std::string& method);
87
88    private:
89        NO_COPY_SEMANTIC(DispatcherImpl);
90        NO_MOVE_SEMANTIC(DispatcherImpl);
91
92        std::unique_ptr<ProfilerImpl> profiler_ {};
93    };
94
95    class Frontend {
96    public:
97        explicit Frontend(ProtocolChannel *channel) : channel_(channel) {}
98        ~Frontend() = default;
99
100        void PreciseCoverageDeltaUpdate();
101
102    private:
103        bool AllowNotify() const;
104
105        ProtocolChannel *channel_ {nullptr};
106    };
107
108private:
109    NO_COPY_SEMANTIC(ProfilerImpl);
110    NO_MOVE_SEMANTIC(ProfilerImpl);
111
112    void InitializeExtendedProtocolsList();
113
114    const EcmaVM *vm_ {nullptr};
115    [[maybe_unused]] Frontend frontend_;
116    std::vector<std::string> profilerExtendedProtocols_ {};
117};
118}  // namespace panda::ecmascript::tooling
119#endif
120