• 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     int32_t GetSystemDatabaseDir(const std::string &groupId, bool checkExist, std::string &databaseDir) override;
48     int32_t GetSystemPreferencesDir(const std::string &groupId, bool checkExist, std::string &preferencesDir) override;
49     void SwitchArea(int mode) override;
50     int GetArea() override;
51     std::string GetBundleName() const override;
52     std::shared_ptr<AppExecFwk::ApplicationInfo> GetApplicationInfo() const override;
53     std::shared_ptr<Global::Resource::ResourceManager> GetResourceManager() const override;
54     std::shared_ptr<Context> CreateBundleContext(const std::string &bundleName) override;
55     std::shared_ptr<Context> CreateModuleContext(const std::string &moduleName) override;
56     std::shared_ptr<Context> CreateModuleContext(const std::string &bundleName, const std::string &moduleName) override;
57     std::shared_ptr<Global::Resource::ResourceManager> CreateModuleResourceManager(
58         const std::string &bundleName, const std::string &moduleName) override;
59 
60     std::string GetBundleCodePath() const override;
61     ErrCode StartAbility(const AAFwk::Want &want, int requestCode) override;
62     ErrCode StartAbilityWithAccount(const AAFwk::Want &want, int accountId, int requestCode) override;
63     ErrCode StartAbility(const AAFwk::Want &want, const AAFwk::StartOptions &startOptions, int requestCode) override;
64     ErrCode StartAbilityAsCaller(const AAFwk::Want &want, int requestCode) override;
65     ErrCode StartAbilityAsCaller(const AAFwk::Want &want, const AAFwk::StartOptions &startOptions,
66         int requestCode) override;
67     ErrCode StartAbilityWithAccount(
68         const AAFwk::Want &want, int accountId, const AAFwk::StartOptions &startOptions, int requestCode) override;
69     ErrCode StartAbilityForResult(const AAFwk::Want &want, int requestCode, RuntimeTask &&task) override;
70     ErrCode StartAbilityForResultWithAccount(
71         const AAFwk::Want &want, int accountId, int requestCode, RuntimeTask &&task) override;
72     ErrCode StartAbilityForResultWithAccount(const AAFwk::Want &want, int accountId,
73         const AAFwk::StartOptions &startOptions, int requestCode, RuntimeTask &&task) override;
74     ErrCode StartAbilityForResult(const AAFwk::Want &want, const AAFwk::StartOptions &startOptions,
75         int requestCode, RuntimeTask &&task) override;
76     ErrCode StartServiceExtensionAbility(const Want &want, int32_t accountId = -1) override;
77     ErrCode StopServiceExtensionAbility(const Want& want, int32_t accountId = -1) override;
78     ErrCode TerminateAbilityWithResult(const AAFwk::Want &want, int resultCode) override;
79     void OnAbilityResult(int requestCode, int resultCode, const AAFwk::Want &resultData) override;
80     ErrCode ConnectAbility(const AAFwk::Want &want,
81                         const sptr<AbilityConnectCallback> &connectCallback) override;
82     ErrCode ConnectAbilityWithAccount(const AAFwk::Want &want, int accountId,
83                         const sptr<AbilityConnectCallback> &connectCallback) override;
84     void DisconnectAbility(const AAFwk::Want &want,
85                            const sptr<AbilityConnectCallback> &connectCallback) override;
86     std::shared_ptr<AppExecFwk::HapModuleInfo> GetHapModuleInfo() const override;
87     std::shared_ptr<AppExecFwk::AbilityInfo> GetAbilityInfo() const override;
88     void MinimizeAbility(bool fromUser = false) override;
89 
90     ErrCode OnBackPressedCallBack(bool &needMoveToBackground) override;
91 
92     ErrCode MoveAbilityToBackground() override;
93 
94     ErrCode TerminateSelf() override;
95 
96     ErrCode CloseAbility() override;
97 
98     sptr<IRemoteObject> GetToken() override;
99 
100     ErrCode RestoreWindowStage(napi_env env, napi_value contentStorage) override;
101 
102     void SetStageContext(const std::shared_ptr<AbilityRuntime::Context> &stageContext);
103 
104     /**
105      * @brief Set the Ability Info object
106      *
107      * set ability info to ability context
108      */
109     void SetAbilityInfo(const std::shared_ptr<AppExecFwk::AbilityInfo> &abilityInfo);
110 
111     /**
112      * @brief Attachs ability's token.
113      *
114      * @param token The token represents ability.
115      */
SetToken(const sptr<IRemoteObject> & token)116     void SetToken(const sptr<IRemoteObject> &token) override
117     {
118         token_ = token;
119     }
120 
121     /**
122      * @brief Get ContentStorage.
123      *
124      * @return Returns the ContentStorage.
125      */
GetContentStorage()126     std::unique_ptr<NativeReference>& GetContentStorage() override
127     {
128         return contentStorage_;
129     }
130 
131     /**
132      * @brief Get LocalCallContainer.
133      *
134      * @return Returns the LocalCallContainer.
135      */
GetLocalCallContainer()136     std::shared_ptr<LocalCallContainer> GetLocalCallContainer() override
137     {
138         return localCallContainer_;
139     }
140 
141     void SetConfiguration(const std::shared_ptr<AppExecFwk::Configuration> &config) override;
142 
143     std::shared_ptr<AppExecFwk::Configuration> GetConfiguration() const override;
144 
145     /**
146      * call function by callback objectS
147      *
148      * @param want Request info for ability.
149      * @param callback Indicates the callback object.
150      * @param accountId Indicates the account to start.
151      *
152      * @return Returns zero on success, others on failure.
153      */
154     ErrCode StartAbilityByCall(const AAFwk::Want& want, const std::shared_ptr<CallerCallBack> &callback,
155         int32_t accountId = DEFAULT_INVAL_VALUE) override;
156 
157     /**
158      * caller release by callback object
159      *
160      * @param callback Indicates the callback object.
161      *
162      * @return Returns zero on success, others on failure.
163      */
164     ErrCode ReleaseCall(const std::shared_ptr<CallerCallBack> &callback) override;
165 
166     /**
167      * clear failed call connection by callback object
168      *
169      * @param callback Indicates the callback object.
170      *
171      * @return void.
172      */
173     void ClearFailedCallConnection(const std::shared_ptr<CallerCallBack> &callback) override;
174 
175     /**
176      * register ability callback
177      *
178      * @param abilityCallback Indicates the abilityCallback object.
179      */
180     void RegisterAbilityCallback(std::weak_ptr<AppExecFwk::IAbilityCallback> abilityCallback) override;
181 
IsTerminating()182     bool IsTerminating() override
183     {
184         return isTerminating_;
185     }
186 
187     void SetWeakSessionToken(const wptr<IRemoteObject>& sessionToken) override;
188 
SetTerminating(bool state)189     void SetTerminating(bool state) override
190     {
191         isTerminating_ = state;
192     }
193 
194     ErrCode RequestDialogService(napi_env env, AAFwk::Want &want, RequestDialogResultTask &&task) override;
195 
196     ErrCode ReportDrawnCompleted() override;
197 
198     ErrCode GetMissionId(int32_t &missionId) override;
199 
200     /**
201      * @brief Set mission continue state of this ability.
202      *
203      * @param state the mission continuation state of this ability.
204      * @return Returns ERR_OK if success.
205      */
206     ErrCode SetMissionContinueState(const AAFwk::ContinueState &state) override;
207 
208     ErrCode StartAbilityByType(const std::string &type,
209         AAFwk::WantParams &wantParam, const std::shared_ptr<JsUIExtensionCallback> &uiExtensionCallbacks) override;
210 
211     ErrCode RequestModalUIExtension(const Want &want) override;
212 
213 #ifdef SUPPORT_GRAPHICS
214     /**
215      * @brief Set mission label of this ability.
216      *
217      * @param label the label of this ability.
218      * @return Returns ERR_OK if success.
219      */
220     ErrCode SetMissionLabel(const std::string &label) override;
221 
222     /**
223      * @brief Set mission icon of this ability.
224      *
225      * @param icon the icon of this ability.
226      * @return Returns ERR_OK if success.
227      */
228     ErrCode SetMissionIcon(const std::shared_ptr<OHOS::Media::PixelMap> &icon) override;
229 
230     /**
231      * @brief get current window mode.
232      *
233      * @return Returns the current window mode.
234      */
235     int GetCurrentWindowMode() override;
236 
237     /**
238      * @brief Get window rectangle of this ability.
239      *
240      * @param the left position of window rectangle.
241      * @param the top position of window rectangle.
242      * @param the width position of window rectangle.
243      * @param the height position of window rectangle.
244      */
245     void GetWindowRect(int32_t &left, int32_t &top, int32_t &width, int32_t &height) override;
246 
247     /**
248      * @brief Get ui content object.
249      *
250      * @return UIContent object of ACE.
251      */
252     Ace::UIContent* GetUIContent() override;
253 
254     /**
255      * @brief create modal UIExtension.
256      * @param want Create modal UIExtension with want object.
257      */
258     ErrCode CreateModalUIExtensionWithApp(const Want &want) override;
259     void EraseUIExtension(int32_t sessionId) override;
260     bool IsUIExtensionExist(const AAFwk::Want &want);
261 #endif
262 
263 private:
264     sptr<IRemoteObject> token_ = nullptr;
265     std::shared_ptr<AppExecFwk::AbilityInfo> abilityInfo_ = nullptr;
266     std::shared_ptr<AbilityRuntime::Context> stageContext_ = nullptr;
267     std::map<int, RuntimeTask> resultCallbacks_;
268     std::unique_ptr<NativeReference> contentStorage_ = nullptr;
269     std::shared_ptr<AppExecFwk::Configuration> config_ = nullptr;
270     std::shared_ptr<LocalCallContainer> localCallContainer_ = nullptr;
271     std::weak_ptr<AppExecFwk::IAbilityCallback> abilityCallback_;
272     bool isTerminating_ = false;
273     int32_t missionId_ = -1;
274     std::mutex sessionTokenMutex_;
275     wptr<IRemoteObject> sessionToken_;
276     std::mutex uiExtensionMutex_;
277     std::map<int32_t, Want> uiExtensionMap_;
278 
279     static void RequestDialogResultJSThreadWorker(uv_work_t* work, int status);
280     void OnAbilityResultInner(int requestCode, int resultCode, const AAFwk::Want &resultData);
281     sptr<IRemoteObject> GetSessionToken();
282 };
283 } // namespace AbilityRuntime
284 } // namespace OHOS
285 #endif // OHOS_ABILITY_RUNTIME_ABILITY_CONTEXT_IMPL_H
286