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_COMMON_EVENT_SERVICE_JS_STATIC_SUBSCRIBER_EXTENSION_H
17 #define OHOS_COMMON_EVENT_SERVICE_JS_STATIC_SUBSCRIBER_EXTENSION_H
18 #include <memory>
19 
20 #include "common_event_data.h"
21 #include "js_runtime.h"
22 #include "native_engine/native_reference.h"
23 #include "native_engine/native_value.h"
24 #include "runtime.h"
25 #include "static_subscriber_extension.h"
26 
27 namespace OHOS {
28 namespace EventFwk {
29 class JsStaticSubscriberExtension : public StaticSubscriberExtension {
30 public:
31     explicit JsStaticSubscriberExtension(AbilityRuntime::JsRuntime& jsRuntime);
32     virtual ~JsStaticSubscriberExtension() override;
33 
34     /**
35      * @brief Create JsStaticSubscriberExtension.
36      *
37      * @param runtime The runtime.
38      * @return The JsStaticSubscriberExtension instance.
39      */
40     static JsStaticSubscriberExtension* Create(const std::unique_ptr<AbilityRuntime::Runtime>& runtime);
41 
42     void Init(const std::shared_ptr<AppExecFwk::AbilityLocalRecord>& record,
43               const std::shared_ptr<AppExecFwk::OHOSApplication>& application,
44               std::shared_ptr<AppExecFwk::AbilityHandler>& handler,
45               const sptr<IRemoteObject>& token) override;
46 
47     void OnStart(const AAFwk::Want& want) override;
48 
49     sptr<IRemoteObject> OnConnect(const AAFwk::Want& want) override;
50 
51     void OnDisconnect(const AAFwk::Want& want) override;
52 
53     void OnStop() override;
54 
55     void OnReceiveEvent(std::shared_ptr<CommonEventData> data) override;
56 
57     void ExecNapiWrap(napi_env env, napi_value obj);
58 
59     std::weak_ptr<JsStaticSubscriberExtension> GetWeakPtr();
60 
61 private:
62     AbilityRuntime::JsRuntime& jsRuntime_;
63     std::unique_ptr<NativeReference> jsObj_;
64 };
65 } // namespace EventFwk
66 } // namespace OHOS
67 #endif // OHOS_COMMON_EVENT_SERVICE_JS_STATIC_SUBSCRIBER_EXTENSION_H
68