• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 GetFilesDir() override;
40     bool IsUpdatingConfigurations() override;
41     bool PrintDrawnCompleted() override;
42     std::string GetDatabaseDir() override;
43     std::string GetPreferencesDir() override;
44     std::string GetDistributedFilesDir() override;
45     void SwitchArea(int mode) override;
46     int GetArea() override;
47     std::string GetBundleName() const override;
48     std::shared_ptr<AppExecFwk::ApplicationInfo> GetApplicationInfo() const override;
49     std::shared_ptr<Global::Resource::ResourceManager> GetResourceManager() const override;
50     std::shared_ptr<Context> CreateBundleContext(const std::string &bundleName) override;
51     std::shared_ptr<Context> CreateModuleContext(const std::string &moduleName) override;
52     std::shared_ptr<Context> CreateModuleContext(const std::string &bundleName, const std::string &moduleName) override;
53 
54     std::string GetBundleCodePath() const override;
55     ErrCode StartAbility(const AAFwk::Want &want, int requestCode) override;
56     ErrCode StartAbilityWithAccount(const AAFwk::Want &want, int accountId, int requestCode) override;
57     ErrCode StartAbility(const AAFwk::Want &want, const AAFwk::StartOptions &startOptions, int requestCode) override;
58     ErrCode StartAbilityWithAccount(
59         const AAFwk::Want &want, int accountId, const AAFwk::StartOptions &startOptions, int requestCode) override;
60     ErrCode StartAbilityForResult(const AAFwk::Want &want, int requestCode, RuntimeTask &&task) override;
61     ErrCode StartAbilityForResultWithAccount(
62         const AAFwk::Want &want, int accountId, int requestCode, RuntimeTask &&task) override;
63     ErrCode StartAbilityForResultWithAccount(const AAFwk::Want &want, int accountId,
64         const AAFwk::StartOptions &startOptions, int requestCode, RuntimeTask &&task) override;
65     ErrCode StartAbilityForResult(const AAFwk::Want &want, const AAFwk::StartOptions &startOptions,
66         int requestCode, RuntimeTask &&task) override;
67     ErrCode StartServiceExtensionAbility(const Want &want, int32_t accountId = -1) override;
68     ErrCode StopServiceExtensionAbility(const Want& want, int32_t accountId = -1) override;
69     ErrCode TerminateAbilityWithResult(const AAFwk::Want &want, int resultCode) override;
70     void OnAbilityResult(int requestCode, int resultCode, const AAFwk::Want &resultData) override;
71     bool ConnectAbility(const AAFwk::Want &want,
72                         const sptr<AbilityConnectCallback> &connectCallback) override;
73     bool ConnectAbilityWithAccount(const AAFwk::Want &want, int accountId,
74                         const sptr<AbilityConnectCallback> &connectCallback) override;
75     void DisconnectAbility(const AAFwk::Want &want,
76                            const sptr<AbilityConnectCallback> &connectCallback) override;
77     std::shared_ptr<AppExecFwk::HapModuleInfo> GetHapModuleInfo() const override;
78     std::shared_ptr<AppExecFwk::AbilityInfo> GetAbilityInfo() const override;
79     void MinimizeAbility(bool fromUser = false) override;
80 
81     ErrCode TerminateSelf() override;
82 
83     ErrCode CloseAbility() override;
84 
85     sptr<IRemoteObject> GetToken() override;
86 
87     void RequestPermissionsFromUser(NativeEngine& engine, const std::vector<std::string> &permissions, int requestCode,
88         PermissionRequestTask &&task) override;
89 
90     ErrCode RestoreWindowStage(NativeEngine& engine, NativeValue* contentStorage) override;
91 
92     void SetStageContext(const std::shared_ptr<AbilityRuntime::Context> &stageContext);
93 
94     /**
95      * @brief Set the Ability Info object
96      *
97      * set ability info to ability context
98      */
99     void SetAbilityInfo(const std::shared_ptr<AppExecFwk::AbilityInfo> &abilityInfo);
100 
101     /**
102      * @brief Attachs ability's token.
103      *
104      * @param token The token represents ability.
105      */
SetToken(const sptr<IRemoteObject> & token)106     void SetToken(const sptr<IRemoteObject> &token) override
107     {
108         token_ = token;
109     }
110 
111     /**
112      * @brief Get ContentStorage.
113      *
114      * @return Returns the ContentStorage.
115      */
GetContentStorage()116     std::unique_ptr<NativeReference>& GetContentStorage() override
117     {
118         return contentStorage_;
119     }
120 
121     /**
122      * @brief Get LocalCallContainer.
123      *
124      * @return Returns the LocalCallContainer.
125      */
GetLocalCallContainer()126     sptr<LocalCallContainer> GetLocalCallContainer() override
127     {
128         return localCallContainer_;
129     }
130 
131     void SetConfiguration(const std::shared_ptr<AppExecFwk::Configuration> &config) override;
132 
133     std::shared_ptr<AppExecFwk::Configuration> GetConfiguration() const override;
134 
135     /**
136      * call function by callback objectS
137      *
138      * @param want Request info for ability.
139      * @param callback Indicates the callback object.
140      *
141      * @return Returns zero on success, others on failure.
142      */
143     ErrCode StartAbilityByCall(const AAFwk::Want& want, const std::shared_ptr<CallerCallBack> &callback) override;
144 
145     /**
146      * caller release by callback object
147      *
148      * @param callback Indicates the callback object.
149      *
150      * @return Returns zero on success, others on failure.
151      */
152     ErrCode ReleaseCall(const std::shared_ptr<CallerCallBack> &callback) override;
153 
154     /**
155      * register ability callback
156      *
157      * @param abilityCallback Indicates the abilityCallback object.
158      */
159     void RegisterAbilityCallback(std::weak_ptr<AppExecFwk::IAbilityCallback> abilityCallback) override;
160 
IsTerminating()161     bool IsTerminating() override
162     {
163         return isTerminating_;
164     }
165 
SetTerminating(bool state)166     void SetTerminating(bool state) override
167     {
168         isTerminating_ = state;
169     }
170 
171     ErrCode GetMissionId(int32_t &missionId) override;
172 
173     ErrCode RequestDialogService(NativeEngine &engine, AAFwk::Want &want, RequestDialogResultTask &&task) override;
174 
175 #ifdef SUPPORT_GRAPHICS
176     /**
177      * @brief Set mission label of this ability.
178      *
179      * @param label the label of this ability.
180      * @return Returns ERR_OK if success.
181      */
182     ErrCode SetMissionLabel(const std::string &label) override;
183 
184     /**
185      * @brief Set mission icon of this ability.
186      *
187      * @param icon the icon of this ability.
188      * @return Returns ERR_OK if success.
189      */
190     ErrCode SetMissionIcon(const std::shared_ptr<OHOS::Media::PixelMap> &icon) override;
191 
192     /**
193      * @brief get current window mode.
194      *
195      * @return Returns the current window mode.
196      */
197     int GetCurrentWindowMode() override;
198 #endif
199 
200 private:
201     static std::mutex mutex_;
202     static std::map<int, PermissionRequestTask> permissionRequestCallbacks;
203     sptr<IRemoteObject> token_ = nullptr;
204     std::shared_ptr<AppExecFwk::AbilityInfo> abilityInfo_ = nullptr;
205     std::shared_ptr<AbilityRuntime::Context> stageContext_ = nullptr;
206     std::map<int, RuntimeTask> resultCallbacks_;
207     std::unique_ptr<NativeReference> contentStorage_ = nullptr;
208     std::shared_ptr<AppExecFwk::Configuration> config_ = nullptr;
209     sptr<LocalCallContainer> localCallContainer_ = nullptr;
210     std::weak_ptr<AppExecFwk::IAbilityCallback> abilityCallback_;
211     bool isTerminating_ = false;
212     int32_t missionId_ = -1;
213 
214     static void ResultCallbackJSThreadWorker(uv_work_t* work, int status);
215     static void RequestDialogResultJSThreadWorker(uv_work_t* work, int status);
216     void StartGrantExtension(NativeEngine& engine, const std::vector<std::string>& permissions,
217         const std::vector<int>& permissionsState, int requestCode, PermissionRequestTask &&task);
218 
219     struct ResultCallback {
220         std::vector<std::string> permissions_;
221         std::vector<int> grantResults_;
222         int requestCode_;
223     };
224 };
225 } // namespace AbilityRuntime
226 } // namespace OHOS
227 #endif // OHOS_ABILITY_RUNTIME_ABILITY_CONTEXT_IMPL_H
228