1 /*
2  * Copyright (c) 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 JS_WINDOW_EXTENSION_H
17 #define JS_WINDOW_EXTENSION_H
18 
19 #include <js_runtime.h>
20 #include <js_runtime_utils.h>
21 
22 #include "js_window_extension_context.h"
23 #include "window.h"
24 #include "window_extension.h"
25 #include "window_extension_stub.h"
26 #include "window_extension_stub_impl.h"
27 
28 namespace OHOS {
29 namespace Rosen {
30 napi_valuetype GetType(napi_env env, napi_value value);
31 class JsWindowExtension : public WindowExtension {
32 public:
33     explicit JsWindowExtension(AbilityRuntime::JsRuntime& jsRuntime);
34     virtual ~JsWindowExtension() override;
35 
36     /**
37      * @brief Create JsAccessibilityExtension.
38      *
39      * @param runtime The runtime.
40      * @return The JsAccessibilityExtension instance.
41      */
42     static JsWindowExtension* Create(const std::unique_ptr<AbilityRuntime::Runtime>& runtime);
43 
44     /**
45      * @brief Init the extension.
46      *
47      * @param record the extension record.
48      * @param application the application info.
49      * @param handler the extension handler.
50      * @param token the remote token.
51      */
52     void Init(const std::shared_ptr<AppExecFwk::AbilityLocalRecord>& record,
53         const std::shared_ptr<AppExecFwk::OHOSApplication>& application,
54         std::shared_ptr<AppExecFwk::AbilityHandler>& handler,
55         const sptr<IRemoteObject>& token) override;
56 
57     /**
58      * @brief Called when this Accessibility extension is connected for the first time.
59      *
60      * You can override this function to implement your own processing logic.
61      *
62      * @param want Indicates the {@link Want} structure containing connection information
63      * about the Accessibility extension.
64      * @return Returns a pointer to the <b>sid</b> of the connected Accessibility extension.
65      */
66     sptr<IRemoteObject> OnConnect(const AAFwk::Want& want) override;
67 
68     /**
69      * @brief Called when all abilities connected to this Service extension are disconnected.
70      *
71      * You can override this function to implement your own processing logic.
72      *
73      */
74     void OnDisconnect(const AAFwk::Want& want) override;
75 
76     /**
77      * @brief Called when this extension is started. You must override this function if you want to perform some
78      *        initialization operations during extension startup.
79      *
80      * This function can be called only once in the entire lifecycle of an extension.
81      * @param Want Indicates the {@link Want} structure containing startup information about the extension.
82      * @param sessionInfo Indicates the {@link SessionInfo} structure containing session info about the extension.
83      */
84     virtual void OnStart(const AAFwk::Want& want, sptr<AAFwk::SessionInfo> sessionInfo) override;
85 private:
86     void GetSrcPath(std::string& srcPath) const;
87     napi_value CallJsMethod(const char* name, napi_value const * argv = nullptr, size_t argc = 0) const;
88     void OnWindowCreated() const;
89     void BindContext(napi_env env, napi_value obj);
90 
91     AbilityRuntime::JsRuntime& jsRuntime_;
92     std::unique_ptr<NativeReference> jsObj_;
93     sptr<WindowExtensionStubImpl> stub_;
94     static int extensionCnt_;
95     sptr<AAFwk::SessionInfo> sessionInfo_;
96 };
97 } // namespace Rosen
98 } // namespace OHOS
99 #endif // JS_WINDOW_EXTENSION_H
100