• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 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_ABILITY_CONTEXT_IMPL_H
17 #define OHOS_ABILITY_RUNTIME_ABILITY_CONTEXT_IMPL_H
18 
19 #include "ability_context.h"
20 
21 #include <uv.h>
22 #include <vector>
23 #include <unordered_map>
24 
25 #include "context_impl.h"
26 #include "configuration.h"
27 #include "local_call_container.h"
28 
29 namespace OHOS {
30 namespace AbilityRuntime {
31 class AbilityContextImpl : public AbilityContext {
32 public:
33     AbilityContextImpl();
34     virtual ~AbilityContextImpl() = default;
35 
36     Global::Resource::DeviceType GetDeviceType() const override;
37     std::string GetBaseDir() const override;
38     std::string GetBundleCodeDir() override;
39     std::string GetCacheDir() override;
40     std::string GetTempDir() override;
41     std::string GetResourceDir(const std::string &moduleName = "") override;
42     std::string GetFilesDir() override;
43     bool IsUpdatingConfigurations() override;
44     bool PrintDrawnCompleted() override;
45     std::string GetDatabaseDir() override;
46     std::string GetGroupDir(std::string groupId) override;
47     std::string GetPreferencesDir() override;
48     std::string GetDistributedFilesDir() override;
49     std::string GetCloudFileDir() override;
50     int32_t GetSystemDatabaseDir(const std::string &groupId, bool checkExist, std::string &databaseDir) override;
51     int32_t GetSystemPreferencesDir(const std::string &groupId, bool checkExist, std::string &preferencesDir) override;
52     void SwitchArea(int mode) override;
53     int GetArea() override;
54     std::string GetProcessName() override;
55     std::string GetBundleName() const override;
56     std::shared_ptr<AppExecFwk::ApplicationInfo> GetApplicationInfo() const override;
57     std::shared_ptr<Global::Resource::ResourceManager> GetResourceManager() const override;
58     void SetAbilityResourceManager(std::shared_ptr<Global::Resource::ResourceManager> abilityResourceMgr) override;
59     void RegisterAbilityConfigUpdateCallback(AbilityConfigUpdateCallback abilityConfigUpdateCallback) override;
60     std::shared_ptr<AppExecFwk::Configuration> GetAbilityConfiguration() const override;
61     void SetAbilityConfiguration(const AppExecFwk::Configuration &config) override;
62     void SetAbilityColorMode(int32_t colorMode) override;
63     std::shared_ptr<Context> CreateBundleContext(const std::string &bundleName) override;
64     std::shared_ptr<Context> CreateModuleContext(const std::string &moduleName) override;
65     std::shared_ptr<Context> CreateModuleContext(const std::string &bundleName, const std::string &moduleName) override;
66     std::shared_ptr<Global::Resource::ResourceManager> CreateModuleResourceManager(
67         const std::string &bundleName, const std::string &moduleName) override;
68     int32_t CreateSystemHspModuleResourceManager(const std::string &bundleName,
69         const std::string &moduleName, std::shared_ptr<Global::Resource::ResourceManager> &resourceManager) override;
70     std::shared_ptr<Context> CreateAreaModeContext(int areaMode) override;
71 #ifdef SUPPORT_GRAPHICS
72     std::shared_ptr<Context> CreateDisplayContext(uint64_t displayId) override;
73 #endif
74 
75     std::string GetBundleCodePath() const override;
76     ErrCode StartAbility(const AAFwk::Want &want, int requestCode) override;
77     ErrCode StartAbilityWithAccount(const AAFwk::Want &want, int accountId, int requestCode) override;
78     ErrCode StartAbility(const AAFwk::Want &want, const AAFwk::StartOptions &startOptions, int requestCode) override;
79     ErrCode StartAbilityAsCaller(const AAFwk::Want &want, int requestCode) override;
80     ErrCode StartAbilityAsCaller(const AAFwk::Want &want, const AAFwk::StartOptions &startOptions,
81         int requestCode) override;
82     ErrCode StartAbilityWithAccount(
83         const AAFwk::Want &want, int accountId, const AAFwk::StartOptions &startOptions, int requestCode) override;
84     ErrCode StartAbilityForResult(const AAFwk::Want &want, int requestCode, RuntimeTask &&task) override;
85     ErrCode StartAbilityForResultWithAccount(
86         const AAFwk::Want &want, int accountId, int requestCode, RuntimeTask &&task) override;
87     ErrCode StartAbilityForResultWithAccount(const AAFwk::Want &want, int accountId,
88         const AAFwk::StartOptions &startOptions, int requestCode, RuntimeTask &&task) override;
89     ErrCode StartAbilityForResult(const AAFwk::Want &want, const AAFwk::StartOptions &startOptions,
90         int requestCode, RuntimeTask &&task) override;
91     ErrCode StartUIServiceExtensionAbility(const AAFwk::Want &want, int32_t accountId = -1) override;
92     ErrCode StartServiceExtensionAbility(const Want &want, int32_t accountId = -1) override;
93     ErrCode StopServiceExtensionAbility(const Want& want, int32_t accountId = -1) override;
94     ErrCode TerminateAbilityWithResult(const AAFwk::Want &want, int resultCode) override;
95     ErrCode BackToCallerAbilityWithResult(const AAFwk::Want &want, int resultCode, int64_t requestCode) override;
96     void OnAbilityResult(int requestCode, int resultCode, const AAFwk::Want &resultData) override;
97     ErrCode ConnectAbility(const AAFwk::Want &want,
98                         const sptr<AbilityConnectCallback> &connectCallback) override;
99     ErrCode ConnectAbilityWithAccount(const AAFwk::Want &want, int accountId,
100                         const sptr<AbilityConnectCallback> &connectCallback) override;
101     ErrCode ConnectUIServiceExtensionAbility(const AAFwk::Want& want,
102         const sptr<AbilityConnectCallback>& connectCallback) override;
103     void DisconnectAbility(const AAFwk::Want &want, const sptr<AbilityConnectCallback> &connectCallback,
104         int32_t accountId = -1) override;
105     std::shared_ptr<AppExecFwk::HapModuleInfo> GetHapModuleInfo() const override;
106     std::shared_ptr<AppExecFwk::AbilityInfo> GetAbilityInfo() const override;
107     void MinimizeAbility(bool fromUser = false) override;
108 
109     ErrCode OnBackPressedCallBack(bool &needMoveToBackground) override;
110 
111     ErrCode MoveAbilityToBackground() override;
112 
113     ErrCode MoveUIAbilityToBackground() override;
114 
115     ErrCode TerminateSelf() override;
116 
117     ErrCode CloseAbility() override;
118 
119     sptr<IRemoteObject> GetToken() override;
120 
121     ErrCode RestoreWindowStage(napi_env env, napi_value contentStorage) override;
122 
123     void SetStageContext(const std::shared_ptr<AbilityRuntime::Context> &stageContext);
124 
125     /**
126      * @brief Set the Ability Info object
127      *
128      * set ability info to ability context
129      */
130     void SetAbilityInfo(const std::shared_ptr<AppExecFwk::AbilityInfo> &abilityInfo);
131 
132     /**
133      * @brief Attachs ability's token.
134      *
135      * @param token The token represents ability.
136      */
SetToken(const sptr<IRemoteObject> & token)137     void SetToken(const sptr<IRemoteObject> &token) override
138     {
139         token_ = token;
140     }
141 
142     /**
143      * @brief Get ContentStorage.
144      *
145      * @return Returns the ContentStorage.
146      */
GetContentStorage()147     std::unique_ptr<NativeReference>& GetContentStorage() override
148     {
149         return contentStorage_;
150     }
151 
152     /**
153      * @brief Get LocalCallContainer.
154      *
155      * @return Returns the LocalCallContainer.
156      */
GetLocalCallContainer()157     std::shared_ptr<LocalCallContainer> GetLocalCallContainer() override
158     {
159         return localCallContainer_;
160     }
161 
162     void SetConfiguration(const std::shared_ptr<AppExecFwk::Configuration> &config) override;
163 
164     std::shared_ptr<AppExecFwk::Configuration> GetConfiguration() const override;
165 
166     /**
167      * call function by callback objectS
168      *
169      * @param want Request info for ability.
170      * @param callback Indicates the callback object.
171      * @param accountId Indicates the account to start.
172      *
173      * @return Returns zero on success, others on failure.
174      */
175     ErrCode StartAbilityByCall(const AAFwk::Want& want, const std::shared_ptr<CallerCallBack> &callback,
176         int32_t accountId = DEFAULT_INVAL_VALUE) override;
177 
178     /**
179      * caller release by callback object
180      *
181      * @param callback Indicates the callback object.
182      *
183      * @return Returns zero on success, others on failure.
184      */
185     ErrCode ReleaseCall(const std::shared_ptr<CallerCallBack> &callback) override;
186 
187     /**
188      * clear failed call connection by callback object
189      *
190      * @param callback Indicates the callback object.
191      *
192      * @return void.
193      */
194     void ClearFailedCallConnection(const std::shared_ptr<CallerCallBack> &callback) override;
195 
196     /**
197      * register ability callback
198      *
199      * @param abilityCallback Indicates the abilityCallback object.
200      */
201     void RegisterAbilityCallback(std::weak_ptr<AppExecFwk::IAbilityCallback> abilityCallback) override;
202 
IsTerminating()203     bool IsTerminating() override
204     {
205         return isTerminating_.load();
206     }
207 
208     void SetWeakSessionToken(const wptr<IRemoteObject>& sessionToken) override;
209     void SetAbilityRecordId(int32_t abilityRecordId) override;
210     int32_t GetAbilityRecordId() override;
211 
SetTerminating(bool state)212     void SetTerminating(bool state) override
213     {
214         isTerminating_.store(state);
215     }
216 
217     ErrCode RequestDialogService(napi_env env, AAFwk::Want &want, RequestDialogResultTask &&task) override;
218 
219     ErrCode RequestDialogService(AAFwk::Want &want, RequestDialogResultTask &&task) override;
220 
221     ErrCode ReportDrawnCompleted() override;
222 
223     ErrCode GetMissionId(int32_t &missionId) override;
224 
225     /**
226      * @brief Set mission continue state of this ability.
227      *
228      * @param state the mission continuation state of this ability.
229      * @return Returns ERR_OK if success.
230      */
231     ErrCode SetMissionContinueState(const AAFwk::ContinueState &state) override;
232 #ifdef SUPPORT_SCREEN
233     ErrCode StartAbilityByType(const std::string &type,
234         AAFwk::WantParams &wantParam, const std::shared_ptr<JsUIExtensionCallback> &uiExtensionCallbacks) override;
235 #endif
236     ErrCode RequestModalUIExtension(const Want &want) override;
237 
238     ErrCode ChangeAbilityVisibility(bool isShow) override;
239 
240     ErrCode AddFreeInstallObserver(const sptr<AbilityRuntime::IFreeInstallObserver> &observer) override;
241 
242     ErrCode OpenLink(const AAFwk::Want& want, int requestCode) override;
243 
244     ErrCode OpenAtomicService(AAFwk::Want& want, const AAFwk::StartOptions &options, int requestCode,
245         RuntimeTask &&task) override;
246 
247     void RegisterAbilityLifecycleObserver(const std::shared_ptr<AppExecFwk::ILifecycleObserver> &observer) override;
248 
249     void UnregisterAbilityLifecycleObserver(const std::shared_ptr<AppExecFwk::ILifecycleObserver> &observer) override;
250 
251     void InsertResultCallbackTask(int requestCode, RuntimeTask&& task) override;
252 
253     void RemoveResultCallbackTask(int requestCode) override;
254 
255     void SetRestoreEnabled(bool enabled) override;
256     bool GetRestoreEnabled() override;
257 
258     std::shared_ptr<AAFwk::Want> GetWant() override;
259 
260 #ifdef SUPPORT_SCREEN
261     /**
262      * @brief Set mission label of this ability.
263      *
264      * @param label the label of this ability.
265      * @return Returns ERR_OK if success.
266      */
267     ErrCode SetMissionLabel(const std::string &label) override;
268 
269     /**
270      * @brief Set mission icon of this ability.
271      *
272      * @param icon the icon of this ability.
273      * @return Returns ERR_OK if success.
274      */
275     ErrCode SetMissionIcon(const std::shared_ptr<OHOS::Media::PixelMap> &icon) override;
276 
277      /**
278      * @brief Set ability label and icon of this ability.
279      *
280      * @param label the label of this ability.
281      * @param icon the icon of this ability.
282      * @return Returns ERR_OK if success.
283      */
284     ErrCode SetAbilityInstanceInfo(const std::string& label, std::shared_ptr<OHOS::Media::PixelMap> icon) override;
285 
286     /**
287      * @brief get current window mode.
288      *
289      * @return Returns the current window mode.
290      */
291     int GetCurrentWindowMode() override;
292 
293     /**
294      * @brief Get window rectangle of this ability.
295      *
296      * @param the left position of window rectangle.
297      * @param the top position of window rectangle.
298      * @param the width position of window rectangle.
299      * @param the height position of window rectangle.
300      */
301     void GetWindowRect(int32_t &left, int32_t &top, int32_t &width, int32_t &height) override;
302 
303     /**
304      * @brief Get ui content object.
305      *
306      * @return UIContent object of ACE.
307      */
308     Ace::UIContent* GetUIContent() override;
309 
310     /**
311      * @brief create modal UIExtension.
312      * @param want Create modal UIExtension with want object.
313      */
314     ErrCode CreateModalUIExtensionWithApp(const Want &want) override;
315     void EraseUIExtension(int32_t sessionId) override;
316     bool IsUIExtensionExist(const AAFwk::Want &want);
317     ErrCode RevokeDelegator() override;
GetHookOff()318     inline bool GetHookOff() override
319     {
320         return hookOff_;
321     }
SetHookOff(bool hookOff)322     inline void SetHookOff(bool hookOff) override
323     {
324         hookOff_ = hookOff;
325     }
IsHook()326     bool IsHook() override
327     {
328         return isHook_;
329     }
SetHook(bool isHook)330     void SetHook(bool isHook) override
331     {
332         isHook_ = isHook;
333     }
334 #endif
335 
336     /**
337      * @brief Add CompletioHandler.
338      *
339      * @param requestId, the requestId.
340      * @param onRequestSucc, the callback ot be called upon request success.
341      * @param onRequestFail, the callback ot be called upon request failure.
342      * @return ERR_OK on success, otherwise failure.
343      */
344     ErrCode AddCompletionHandler(const std::string &requestId, OnRequestResult onRequestSucc,
345         OnRequestResult onRequestFail) override;
346 
347     /**
348      * @brief Callback on request success.
349      *
350      * @param requestId, the requestId.
351      * @param element, the want element of startAbility.
352      * @param message, the message returned to the callback.
353      */
354     void OnRequestSuccess(const std::string &requestId, const AppExecFwk::ElementName &element,
355         const std::string &message) override;
356 
357     /**
358      * @brief Callback on request failure.
359      *
360      * @param requestId, the requestId.
361      * @param element, the want element of startAbility.
362      * @param message, the message returned to the callback.
363      */
364     void OnRequestFailure(const std::string &requestId, const AppExecFwk::ElementName &element,
365         const std::string &message, int32_t resultCode = 0) override;
366 
367     ErrCode StartExtensionAbilityWithExtensionType(const AAFwk::Want &want,
368         AppExecFwk::ExtensionAbilityType extensionType) override;
369     ErrCode StopExtensionAbilityWithExtensionType(const AAFwk::Want& want,
370         AppExecFwk::ExtensionAbilityType extensionType) override;
371     ErrCode ConnectExtensionAbilityWithExtensionType(const AAFwk::Want& want,
372         const sptr<AbilityConnectCallback>& connectCallback, AppExecFwk::ExtensionAbilityType extensionType) override;
373     ErrCode SetOnNewWantSkipScenarios(int32_t scenarios) override;
374 
375     ErrCode AddCompletionHandlerForAtomicService(const std::string &requestId, OnAtomicRequestSuccess onRequestSucc,
376         OnAtomicRequestFailure onRequestFail, const std::string &appId) override;
377 
378 private:
379     sptr<IRemoteObject> token_ = nullptr;
380     std::shared_ptr<AppExecFwk::AbilityInfo> abilityInfo_ = nullptr;
381     std::shared_ptr<AbilityRuntime::Context> stageContext_ = nullptr;
382     std::map<int, RuntimeTask> resultCallbacks_;
383     std::unique_ptr<NativeReference> contentStorage_ = nullptr;
384     std::shared_ptr<AppExecFwk::Configuration> config_ = nullptr;
385     std::shared_ptr<LocalCallContainer> localCallContainer_ = nullptr;
386     std::weak_ptr<AppExecFwk::IAbilityCallback> abilityCallback_;
387     std::atomic<bool> isTerminating_ = false;
388     int32_t missionId_ = -1;
389     int32_t abilityRecordId_ = 0;
390     std::mutex sessionTokenMutex_;
391     wptr<IRemoteObject> sessionToken_;
392     std::mutex uiExtensionMutex_;
393     std::map<int32_t, Want> uiExtensionMap_;
394     std::atomic<bool> restoreEnabled_ = false;
395     std::shared_ptr<Global::Resource::ResourceManager> abilityResourceMgr_ = nullptr;
396     AbilityConfigUpdateCallback abilityConfigUpdateCallback_ = nullptr;
397     std::shared_ptr<AppExecFwk::Configuration> abilityConfiguration_ = nullptr;
398     bool isHook_ = false;
399     bool hookOff_ = false;
400 
401     static void RequestDialogResultJSThreadWorker(uv_work_t* work, int status);
402     void OnAbilityResultInner(int requestCode, int resultCode, const AAFwk::Want &resultData);
403     sptr<IRemoteObject> GetSessionToken();
404     void SetWindowRectangleParams(AAFwk::Want &want);
405     void GetFailureInfoByMessage(const std::string &message, int32_t &failureCode,
406         std::string &failureMessage, int32_t resultCode);
407 
408     std::mutex onRequestResultMutex_;
409     std::mutex onAtomicRequestResultMutex_;
410     std::vector<std::shared_ptr<OnRequestResultElement>> onRequestResults_;
411     std::vector<std::shared_ptr<OnAtomicRequestResult>> onAtomicRequestResults_;
412 };
413 } // namespace AbilityRuntime
414 } // namespace OHOS
415 #endif // OHOS_ABILITY_RUNTIME_ABILITY_CONTEXT_IMPL_H
416