• 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 ABILITY_RUNTIME_ABILITY_CONTEXT_IMPL_H
17 #define ABILITY_RUNTIME_ABILITY_CONTEXT_IMPL_H
18 
19 #include "ability_context.h"
20 
21 #include "context_impl.h"
22 #include "configuration.h"
23 #include "local_call_container.h"
24 
25 namespace OHOS {
26 namespace AbilityRuntime {
27 class AbilityContextImpl : public AbilityContext {
28 public:
29     AbilityContextImpl() = default;
30     virtual ~AbilityContextImpl() = default;
31 
32     std::string GetBaseDir() const override;
33     std::string GetBundleCodeDir() override;
34     std::string GetCacheDir() override;
35     std::string GetTempDir() override;
36     std::string GetFilesDir() override;
37     bool IsUpdatingConfigurations() override;
38     bool PrintDrawnCompleted() override;
39     std::string GetDatabaseDir() override;
40     std::string GetStorageDir() override;
41     std::string GetDistributedFilesDir() override;
42     void SwitchArea(int mode) override;
43     std::string GetBundleName() const override;
44     std::shared_ptr<AppExecFwk::ApplicationInfo> GetApplicationInfo() const override;
45     std::shared_ptr<Global::Resource::ResourceManager> GetResourceManager() const override;
46     std::shared_ptr<Context> CreateBundleContext(const std::string &bundleName) override;
47 
48     std::string GetBundleCodePath() const override;
49     ErrCode StartAbility(const AAFwk::Want &want, int requestCode) override;
50     ErrCode StartAbilityWithAccount(const AAFwk::Want &want, int accountId, int requestCode) override;
51     ErrCode StartAbility(const AAFwk::Want &want, const AAFwk::StartOptions &startOptions, int requestCode) override;
52     ErrCode StartAbilityWithAccount(
53         const AAFwk::Want &want, int accountId, const AAFwk::StartOptions &startOptions, int requestCode) override;
54     ErrCode StartAbilityForResult(const AAFwk::Want &want, int requestCode, RuntimeTask &&task) override;
55     ErrCode StartAbilityForResultWithAccount(
56         const AAFwk::Want &want, int accountId, int requestCode, RuntimeTask &&task) override;
57     ErrCode StartAbilityForResultWithAccount(const AAFwk::Want &want, int accountId,
58         const AAFwk::StartOptions &startOptions, int requestCode, RuntimeTask &&task) override;
59     ErrCode StartAbilityForResult(const AAFwk::Want &want, const AAFwk::StartOptions &startOptions,
60         int requestCode, RuntimeTask &&task) override;
61     ErrCode TerminateAbilityWithResult(const AAFwk::Want &want, int resultCode) override;
62     void OnAbilityResult(int requestCode, int resultCode, const AAFwk::Want &resultData) override;
63     bool ConnectAbility(const AAFwk::Want &want,
64                         const sptr<AbilityConnectCallback> &connectCallback) override;
65     bool ConnectAbilityWithAccount(const AAFwk::Want &want, int accountId,
66                         const sptr<AbilityConnectCallback> &connectCallback) override;
67     void DisconnectAbility(const AAFwk::Want &want,
68                            const sptr<AbilityConnectCallback> &connectCallback) override;
69     std::shared_ptr<AppExecFwk::HapModuleInfo> GetHapModuleInfo() const override;
70     std::shared_ptr<AppExecFwk::AbilityInfo> GetAbilityInfo() const override;
71     void MinimizeAbility(bool fromUser = false) override;
72 
73     ErrCode TerminateSelf() override;
74 
75     ErrCode CloseAbility() override;
76 
77     sptr<IRemoteObject> GetToken() override;
78 
79     void RequestPermissionsFromUser(const std::vector<std::string> &permissions,
80         int requestCode, PermissionRequestTask &&task) override;
81     void OnRequestPermissionsFromUserResult(
82         int requestCode, const std::vector<std::string> &permissions, const std::vector<int> &grantResults) override;
83 
84     ErrCode RestoreWindowStage(NativeEngine& engine, NativeValue* contentStorage) override;
85 
86     /**
87      * @brief Set mission label of this ability.
88      *
89      * @param label the label of this ability.
90      * @return Returns ERR_OK if success.
91      */
92     ErrCode SetMissionLabel(const std::string &label) override;
93 
94 #ifdef SUPPORT_GRAPHICS
95     /**
96      * @brief Set mission icon of this ability.
97      *
98      * @param icon the icon of this ability.
99      * @return Returns ERR_OK if success.
100      */
101     ErrCode SetMissionIcon(const std::shared_ptr<OHOS::Media::PixelMap> &icon) override;
102 #endif
103 
104     void SetStageContext(const std::shared_ptr<AbilityRuntime::Context> &stageContext);
105 
106     /**
107      * @brief Set the Ability Info object
108      *
109      * set ability info to ability context
110      */
111     void SetAbilityInfo(const std::shared_ptr<AppExecFwk::AbilityInfo> &abilityInfo);
112 
113     /**
114      * @brief Attachs ability's token.
115      *
116      * @param token The token represents ability.
117      */
SetToken(const sptr<IRemoteObject> & token)118     void SetToken(const sptr<IRemoteObject> &token) override
119     {
120         token_ = token;
121     }
122 
123     /**
124      * @brief Get ContentStorage.
125      *
126      * @return Returns the ContentStorage.
127      */
GetContentStorage()128     std::unique_ptr<NativeReference>& GetContentStorage() override
129     {
130         return contentStorage_;
131     }
132 
133     /**
134      * @brief Get LocalCallContainer.
135      *
136      * @return Returns the LocalCallContainer.
137      */
GetLocalCallContainer()138     sptr<LocalCallContainer> GetLocalCallContainer() override
139     {
140         return localCallContainer_;
141     }
142 
143     void SetConfiguration(const std::shared_ptr<AppExecFwk::Configuration> &config) override;
144 
145     std::shared_ptr<AppExecFwk::Configuration> GetConfiguration() const override;
146     /**
147      * call function by callback object
148      *
149      * @param want Request info for ability.
150      * @param callback Indicates the callback object.
151      *
152      * @return Returns zero on success, others on failure.
153      */
154     ErrCode StartAbility(const AAFwk::Want& want, const std::shared_ptr<CallerCallBack> &callback) override;
155 
156     /**
157      * caller release by callback object
158      *
159      * @param callback Indicates the callback object.
160      *
161      * @return Returns zero on success, others on failure.
162      */
163     ErrCode ReleaseAbility(const std::shared_ptr<CallerCallBack> &callback) override;
164 
165     /**
166      * register ability callback
167      *
168      * @param abilityCallback Indicates the abilityCallback object.
169      */
170     void RegisterAbilityCallback(std::weak_ptr<AppExecFwk::IAbilityCallback> abilityCallback) override;
171 
172 #ifdef SUPPORT_GRAPHICS
173     /**
174      * get current window mode
175      */
176     int GetCurrentWindowMode() override;
177 #endif
178 
179 private:
180     sptr<IRemoteObject> token_;
181     std::shared_ptr<AppExecFwk::AbilityInfo> abilityInfo_ = nullptr;
182     std::shared_ptr<AbilityRuntime::Context> stageContext_ = nullptr;
183     std::map<int, RuntimeTask> resultCallbacks_;
184     std::map<int, PermissionRequestTask> permissionRequestCallbacks_;
185     std::unique_ptr<NativeReference> contentStorage_ = nullptr;
186     std::shared_ptr<AppExecFwk::Configuration> config_;
187     sptr<LocalCallContainer> localCallContainer_;
188     std::weak_ptr<AppExecFwk::IAbilityCallback> abilityCallback_;
189 };
190 }  // namespace AbilityRuntime
191 }  // namespace OHOS
192 #endif  // ABILITY_RUNTIME_ABILITY_CONTEXT_IMPL_H