• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_UI_EXTENSION_BASE_H
17 #define OHOS_ABILITY_RUNTIME_JS_UI_EXTENSION_BASE_H
18 
19 #include "ability_handler.h"
20 #include "ability_local_record.h"
21 #include "configuration.h"
22 #include "insight_intent_executor_info.h"
23 #include "js_extension_common.h"
24 #include "js_ui_extension_content_session.h"
25 #include "native_engine/native_engine.h"
26 #include "ohos_application.h"
27 #include "session_info.h"
28 #include "ui_extension_base.h"
29 #include "ui_extension_context.h"
30 #include "ui_extension_window_command.h"
31 #include "want.h"
32 #include "window.h"
33 #ifdef SUPPORT_GRAPHICS
34 #include "display_manager.h"
35 #include "window_manager.h"
36 #endif // SUPPORT_GRAPHICS
37 
38 class NativeReference;
39 
40 namespace OHOS {
41 namespace AbilityRuntime {
42 using Want = OHOS::AAFwk::Want;
43 class JsRuntime;
44 /**
45  * @brief Js ui extension base.
46  */
47 class JsUIExtensionBase : public UIExtensionBaseImpl,
48                           public std::enable_shared_from_this<JsUIExtensionBase> {
49 public:
50     explicit JsUIExtensionBase(const std::unique_ptr<Runtime> &runtime);
51     virtual ~JsUIExtensionBase();
52 
53     /**
54      * @brief Init the ui extension.
55      *
56      * @param record the ui extension record.
57      * @param application the application info.
58      * @param handler the ui extension handler.
59      * @param token the remote token.
60      * @return js extension common object.
61      */
62     std::shared_ptr<ExtensionCommon> Init(const std::shared_ptr<AppExecFwk::AbilityLocalRecord> &record,
63         const std::shared_ptr<AppExecFwk::OHOSApplication> &application,
64         std::shared_ptr<AppExecFwk::AbilityHandler> &handler, const sptr<IRemoteObject> &token) override;
65 
66     /**
67      * @brief Called when this ui extension is started. You must override this function if you want to perform some
68      *        initialization operations during ui extension startup.
69      *
70      * This function can be called only once in the entire lifecycle of an ui extension.
71      *
72      * @param Want Indicates the {@link Want} structure containing startup information about the ui extension.
73      * @param launchParam The launch param.
74      * @param sessionInfo The session info of the ability.
75      */
76     void OnStart(
77         const AAFwk::Want &want, AAFwk::LaunchParam &launchParam, sptr<AAFwk::SessionInfo> sessionInfo) override;
78 
79     /**
80      * @brief Called back when ui extension is started.
81      *
82      * This method can be called only by ui extension. You can use the StartAbility(Want) method to start
83      * ui extension. Then the system calls back the current method to use the transferred want parameter to
84      * execute its own logic.
85      *
86      * @param want Indicates the want of ui extension to start.
87      * @param restart Indicates the startup mode. The value true indicates that ui extension is restarted after
88      * being destroyed, and the value false indicates a normal startup.
89      * @param startId Indicates the number of times the ui extension has been started. The startId is incremented
90      * by 1 every time the ui extension is started. For example, if the ui extension has been started for six
91      * times, the value of startId is 6.
92      */
93     void OnCommand(const AAFwk::Want &want, bool restart, int32_t startId) override;
94 
95     void OnCommandWindow(
96         const AAFwk::Want &want, const sptr<AAFwk::SessionInfo> &sessionInfo, AAFwk::WindowCommand winCmd) override;
97 
98     /**
99      * @brief Called when this ui extension enters the <b>STATE_STOP</b> state.
100      *
101      * The ui extension in the <b>STATE_STOP</b> is being destroyed.
102      * You can override this function to implement your own processing logic.
103      */
104     void OnStop() override;
105     void OnStop(AppExecFwk::AbilityTransactionCallbackInfo<> *callbackInfo, bool &isAsyncCallback) override;
106     /**
107      * @brief The callback of OnStop.
108      */
109     void OnStopCallBack() override;
110 
111     /**
112      * @brief Called when the system configuration is updated.
113      *
114      * @param configuration Indicates the updated configuration information.
115      */
116     void OnConfigurationUpdated(const AppExecFwk::Configuration &configuration) override;
117 
118     /**
119      * @brief Called when this extension enters the <b>STATE_FOREGROUND</b> state.
120      *
121      *
122      * The extension in the <b>STATE_FOREGROUND</b> state is visible.
123      * You can override this function to implement your own processing logic.
124      */
125     void OnForeground(const Want &want, sptr<AAFwk::SessionInfo> sessionInfo) override;
126 
127     /**
128      * @brief Called when this extension enters the <b>STATE_BACKGROUND</b> state.
129      *
130      *
131      * The extension in the <b>STATE_BACKGROUND</b> state is invisible.
132      * You can override this function to implement your own processing logic.
133      */
134     void OnBackground() override;
135 
136     /**
137      * @brief Called when ui extension need dump info.
138      *
139      * @param params The params from ui extension.
140      * @param info The dump info to show.
141      */
142     void Dump(const std::vector<std::string> &params, std::vector<std::string> &info) override;
143 
144     /**
145      * @brief Called when startAbilityForResult(ohos.aafwk.content.Want,int32_t) is called to start an extension ability
146      * and the result is returned.
147      * @param requestCode Indicates the request code returned after the ability is started. You can define the request
148      * code to identify the results returned by abilities. The value ranges from 0 to 65535.
149      * @param resultCode Indicates the result code returned after the ability is started. You can define the result
150      * code to identify an error.
151      * @param resultData Indicates the data returned after the ability is started. You can define the data returned. The
152      * value can be null.
153      */
154     void OnAbilityResult(int32_t requestCode, int32_t resultCode, const Want &resultData) override;
155 
156     /**
157      * @brief Set ability info.
158      */
159     void SetAbilityInfo(const std::shared_ptr<AppExecFwk::AbilityInfo> &abilityInfo) override;
160 
161     /**
162      * @brief Set ui extension context.
163      */
164     void SetContext(const std::shared_ptr<UIExtensionContext> &context) override;
165 
166     void BindContext() override;
167 
168     /**
169      * @brief Called when configuration changed, including system configuration and window configuration.
170      */
171     void ConfigurationUpdated();
172 
173     void OnAbilityConfigurationUpdated(const AppExecFwk::Configuration& configuration);
174 
175     void RegisterAbilityConfigUpdateCallback();
176 
177 protected:
178     napi_value CallObjectMethod(const char *name, napi_value const *argv = nullptr, size_t argc = 0,
179         bool withResult = false);
180     bool CheckPromise(napi_value result);
181     bool CallPromise(napi_value result, AppExecFwk::AbilityTransactionCallbackInfo<> *callbackInfo);
182     void ForegroundWindow(const AAFwk::Want &want, const sptr<AAFwk::SessionInfo> &sessionInfo);
183     void BackgroundWindow(const sptr<AAFwk::SessionInfo> &sessionInfo);
184     void DestroyWindow(const sptr<AAFwk::SessionInfo> &sessionInfo);
185     bool CallJsOnSessionCreate(const AAFwk::Want &want, const sptr<AAFwk::SessionInfo> &sessionInfo,
186         const sptr<Rosen::Window> &uiWindow, const uint64_t &uiExtensionComponentId);
187     void OnCommandWindowDone(const sptr<AAFwk::SessionInfo> &sessionInfo, AAFwk::WindowCommand winCmd);
188     void ForegroundWindowInitInsightIntentExecutorInfo(const AAFwk::Want &want,
189         const sptr<AAFwk::SessionInfo> &sessionInfo, InsightIntentExecutorInfo &executorInfo);
190     bool ForegroundWindowWithInsightIntent(const AAFwk::Want &want, const sptr<AAFwk::SessionInfo> &sessionInfo,
191         bool needForeground);
192     bool HandleSessionCreate(const AAFwk::Want &want, const sptr<AAFwk::SessionInfo> &sessionInfo);
193     void OnInsightIntentExecuteDone(const sptr<AAFwk::SessionInfo> &sessionInfo,
194         const AppExecFwk::InsightIntentExecuteResult &result);
195     void PostInsightIntentExecuted(const sptr<AAFwk::SessionInfo> &sessionInfo,
196         const AppExecFwk::InsightIntentExecuteResult &result, bool needForeground);
197     void ExecuteInsightIntentDone(uint64_t intentId, const InsightIntentExecuteResult &result);
198 
199 protected:
200     JsRuntime &jsRuntime_;
201     std::shared_ptr<NativeReference> shellContextRef_;
202     std::shared_ptr<NativeReference> jsObj_;
203     std::shared_ptr<UIExtensionContext> context_;
204     std::map<uint64_t, sptr<Rosen::Window>> uiWindowMap_;
205     std::set<uint64_t> foregroundWindows_;
206     std::map<uint64_t, std::shared_ptr<NativeReference>> contentSessions_;
207     std::shared_ptr<AbilityResultListeners> abilityResultListeners_ = nullptr;
208     std::shared_ptr<AppExecFwk::AbilityInfo> abilityInfo_;
209     sptr<IRemoteObject> token_ = nullptr;
210     std::shared_ptr<AbilityHandler> handler_ = nullptr;
211 
212 private:
213     sptr<Rosen::WindowOption> CreateWindowOption(const sptr<AAFwk::SessionInfo> &sessionInfo);
214 
215 #ifdef SUPPORT_GRAPHICS
216 private:
217     class JsUIExtensionBaseDisplayListener : public OHOS::Rosen::IDisplayInfoChangedListener {
218     public:
JsUIExtensionBaseDisplayListener(const std::weak_ptr<JsUIExtensionBase> & jsUiExtensionBase)219         explicit JsUIExtensionBaseDisplayListener(const std::weak_ptr<JsUIExtensionBase> &jsUiExtensionBase)
220         {
221             jsUiExtensionBase_ = jsUiExtensionBase;
222         }
223 
OnDisplayInfoChange(const sptr<IRemoteObject> & token,Rosen::DisplayId displayId,float density,Rosen::DisplayOrientation orientation)224         void OnDisplayInfoChange(const sptr<IRemoteObject> &token, Rosen::DisplayId displayId, float density,
225             Rosen::DisplayOrientation orientation) override
226         {
227             auto sptr = jsUiExtensionBase_.lock();
228             if (sptr != nullptr) {
229                 sptr->OnDisplayInfoChange(token, displayId, density, orientation);
230             }
231         }
232 
233     private:
234         std::weak_ptr<JsUIExtensionBase> jsUiExtensionBase_;
235     };
236 
237     void RegisterDisplayInfoChangedListener();
238     void UnregisterDisplayInfoChangedListener();
239     void OnDisplayInfoChange(const sptr<IRemoteObject> &token, Rosen::DisplayId displayId, float density,
240         Rosen::DisplayOrientation orientation);
241 
242     sptr<JsUIExtensionBaseDisplayListener> jsUIExtensionBaseDisplayListener_ = nullptr;
243 #endif
244 };
245 } // namespace AbilityRuntime
246 } // namespace OHOS
247 #endif // OHOS_ABILITY_RUNTIME_JS_UI_EXTENSION_BASE_H
248