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