1 /* 2 * Copyright (c) 2021-2022 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_INTERFACE_NOTIFICATION_MANAGER_H 17 #define ECMASCRIPT_TOOLING_INTERFACE_NOTIFICATION_MANAGER_H 18 19 #include <string_view> 20 21 #include "ecmascript/js_thread.h" 22 23 namespace panda::ecmascript::tooling { 24 class RuntimeListener { 25 public: 26 RuntimeListener() = default; 27 virtual ~RuntimeListener() = default; 28 DEFAULT_COPY_SEMANTIC(RuntimeListener); 29 DEFAULT_MOVE_SEMANTIC(RuntimeListener); 30 31 virtual void LoadModule(std::string_view name) = 0; 32 33 virtual void BytecodePcChanged(JSThread *thread, JSMethod *method, 34 uint32_t bc_offset) = 0; 35 36 virtual void VmStart() = 0; 37 virtual void VmDeath() = 0; 38 virtual void PendingJobEntry() = 0; 39 }; 40 41 class NotificationManager { 42 public: 43 NotificationManager() = default; 44 ~NotificationManager() = default; 45 NO_COPY_SEMANTIC(NotificationManager); 46 NO_MOVE_SEMANTIC(NotificationManager); 47 AddListener(RuntimeListener * listener)48 void AddListener(RuntimeListener *listener) 49 { 50 listener_ = listener; 51 } RemoveListener()52 void RemoveListener() 53 { 54 listener_ = nullptr; 55 } 56 LoadModuleEvent(std::string_view name)57 void LoadModuleEvent(std::string_view name) const 58 { 59 if (UNLIKELY(listener_ != nullptr)) { 60 listener_->LoadModule(name); 61 } 62 } 63 BytecodePcChangedEvent(JSThread * thread,JSMethod * method,uint32_t bcOffset)64 void BytecodePcChangedEvent(JSThread *thread, JSMethod *method, uint32_t bcOffset) const 65 { 66 if (UNLIKELY(listener_ != nullptr)) { 67 listener_->BytecodePcChanged(thread, method, bcOffset); 68 } 69 } 70 PendingJobEntryEvent()71 void PendingJobEntryEvent() const 72 { 73 if (UNLIKELY(listener_ != nullptr)) { 74 listener_->PendingJobEntry(); 75 } 76 } 77 VmStartEvent()78 void VmStartEvent() const 79 { 80 if (UNLIKELY(listener_ != nullptr)) { 81 listener_->VmStart(); 82 } 83 } VmDeathEvent()84 void VmDeathEvent() const 85 { 86 if (UNLIKELY(listener_ != nullptr)) { 87 listener_->VmDeath(); 88 } 89 } 90 91 private: 92 RuntimeListener *listener_ {nullptr}; 93 }; 94 } // panda::ecmascript::tooling 95 96 #endif // ECMASCRIPT_TOOLING_INTERFACE_NOTIFICATION_MANAGER_H