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