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