1 /*
2  * Copyright (c) 2023-2024 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_ABILITY_RUNTIME_JS_AUTO_FILL_EXTENSION_H
17 #define OHOS_ABILITY_RUNTIME_JS_AUTO_FILL_EXTENSION_H
18 
19 #include "auto_fill_extension.h"
20 #include "auto_fill_extension_context.h"
21 #include "configuration.h"
22 #include "session_info.h"
23 #include "view_data.h"
24 
25 namespace OHOS {
26 namespace AbilityRuntime {
27 class AutoFillExtension;
28 class JsRuntime;
29 class JsUIExtensionBase;
30 /**
31  * @brief Basic js auto fill extension.
32  */
33 class JsAutoFillExtension : public AutoFillExtension, public IAutoFillExtensionCallback {
34 public:
35     explicit JsAutoFillExtension(JsRuntime &jsRuntime);
36     virtual ~JsAutoFillExtension() override;
37 
38     static JsAutoFillExtension *Create(const std::unique_ptr<Runtime> &runtime);
39 
40     /**
41      * @brief Init the auto fill extension.
42      *
43      * @param record the auto fill extension record.
44      * @param application the application info.
45      * @param handler the auto fill extension handler.
46      * @param token the remote token.
47      */
48     void Init(const std::shared_ptr<AppExecFwk::AbilityLocalRecord> &record,
49         const std::shared_ptr<AppExecFwk::OHOSApplication> &application,
50         std::shared_ptr<AppExecFwk::AbilityHandler> &handler, const sptr<IRemoteObject> &token) override;
51 
52     /**
53      * @brief Called when this ui extension is started. You must override this function if you want to perform some
54      *        initialization operations during ui extension startup.
55      *
56      * This function can be called only once in the entire lifecycle of an ui extension.
57      *
58      * @param Want Indicates the {@link Want} structure containing startup information about the ui extension.
59      */
60     void OnStart(const AAFwk::Want &want) override;
61 
62     /**
63      * @brief Called back when ui extension is started.
64      *
65      * This method can be called only by ui extension. You can use the StartAbility(Want) method to start
66      * ui extension. Then the system calls back the current method to use the transferred want parameter to
67      * execute its own logic.
68      *
69      * @param want Indicates the want of ui extension to start.
70      * @param restart Indicates the startup mode. The value true indicates that ui extension is restarted after being
71      * destroyed, and the value false indicates a normal startup.
72      * @param startId Indicates the number of times the ui extension has been started. The startId is incremented
73      * by 1 every time the ui extension is started. For example, if the ui extension has been started for six times, the
74      * value of startId is 6.
75      */
76     void OnCommand(const AAFwk::Want &want, bool restart, int startId) override;
77 
78     void OnCommandWindow(
79         const AAFwk::Want &want, const sptr<AAFwk::SessionInfo> &sessionInfo, AAFwk::WindowCommand winCmd) override;
80 
81     /**
82      * @brief Called when this ui extension enters the <b>STATE_STOP</b> state.
83      *
84      * The ui extension in the <b>STATE_STOP</b> is being destroyed.
85      * You can override this function to implement your own processing logic.
86      */
87     void OnStop() override;
88     void OnStop(AppExecFwk::AbilityTransactionCallbackInfo<> *callbackInfo, bool &isAsyncCallback) override;
89     /**
90      * @brief The callback of OnStop.
91      */
92     void OnStopCallBack() override;
93 
94     /**
95      * @brief Called when this extension enters the <b>STATE_FOREGROUND</b> state.
96      *
97      *
98      * The extension in the <b>STATE_FOREGROUND</b> state is visible.
99      * You can override this function to implement your own processing logic.
100      */
101     void OnForeground(const Want &want, sptr<AAFwk::SessionInfo> sessionInfo) override;
102 
103     /**
104      * @brief Called when this extension enters the <b>STATE_BACKGROUND</b> state.
105      *
106      *
107      * The extension in the <b>STATE_BACKGROUND</b> state is invisible.
108      * You can override this function to implement your own processing logic.
109      */
110     void OnBackground() override;
111 
112     /**
113      * @brief Used to create an update request.
114      *
115      * @param wantParams Indicates the view data of the update request.
116      */
117     void UpdateRequest(const AAFwk::WantParams &wantParams);
118 
119     int32_t OnReloadInModal(const sptr<AAFwk::SessionInfo> &sessionInfo, const CustomData &customData) override;
120 
121 private:
122     virtual void BindContext(napi_env env, napi_value obj);
123     napi_value CallObjectMethod(const char *name, napi_value const *argv = nullptr, size_t argc = 0,
124         bool withResult = false);
125     bool CheckPromise(napi_value result);
126     bool CallPromise(napi_value result, AppExecFwk::AbilityTransactionCallbackInfo<> *callbackInfo);
127     void ForegroundWindow(const AAFwk::Want &want, const sptr<AAFwk::SessionInfo> &sessionInfo);
128     void BackgroundWindow(const sptr<AAFwk::SessionInfo> &sessionInfo);
129     void DestroyWindow(const sptr<AAFwk::SessionInfo> &sessionInfo);
130 
131     void OnCommandWindowDone(const sptr<AAFwk::SessionInfo> &sessionInfo, AAFwk::WindowCommand winCmd) override;
132     bool HandleAutoFillCreate(const AAFwk::Want &want, const sptr<AAFwk::SessionInfo> &sessionInfo);
133     void CallJsOnRequest(const AAFwk::Want &want, const sptr<AAFwk::SessionInfo> &sessionInfo,
134         const sptr<Rosen::Window> &uiWindow);
135     void RegisterTransferComponentDataListener(const sptr<Rosen::Window> &uiWindow);
136 
137     JsRuntime& jsRuntime_;
138     std::unique_ptr<NativeReference> jsObj_;
139     std::shared_ptr<NativeReference> shellContextRef_ = nullptr;
140     std::map<sptr<IRemoteObject>, sptr<Rosen::Window>> uiWindowMap_;
141     std::set<sptr<IRemoteObject>> foregroundWindows_;
142     std::map<sptr<IRemoteObject>, std::shared_ptr<NativeReference>> contentSessions_;
143     std::map<sptr<IRemoteObject>, std::shared_ptr<NativeReference>> callbacks_;
144     bool isPopup_ = false;
145 };
146 } // namespace AbilityRuntime
147 } // namespace OHOS
148 #endif // OHOS_ABILITY_RUNTIME_JS_AUTO_FILL_EXTENSION_H
149