1 /*
2 * Copyright (c) 2021 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 #include "application_context.h"
17 #include "hilog_wrapper.h"
18 #include "task_dispatcher_context.h"
19
20 namespace OHOS {
21 namespace AppExecFwk {
ApplicationContext()22 ApplicationContext::ApplicationContext()
23 {}
~ApplicationContext()24 ApplicationContext::~ApplicationContext()
25 {}
26
27 /**
28 * @brief Obtains information about the current ability.
29 * The returned information includes the class name, bundle name, and other information about the current ability.
30 *
31 * @return Returns the AbilityInfo object for the current ability.
32 */
GetAbilityInfo()33 const std::shared_ptr<AbilityInfo> ApplicationContext::GetAbilityInfo()
34 {
35 return nullptr;
36 }
37
38 /**
39 * @brief Starts a new ability.
40 * An ability using the AbilityInfo.AbilityType.SERVICE or AbilityInfo.AbilityType.PAGE template uses this method
41 * to start a specific ability. The system locates the target ability from installed abilities based on the value
42 * of the want parameter and then starts it. You can specify the ability to start using the want parameter.
43 *
44 * @param want Indicates the Want containing information about the target ability to start.
45 *
46 * @param requestCode Indicates the request code returned after the ability using the AbilityInfo.AbilityType.PAGE
47 * template is started. You can define the request code to identify the results returned by abilities. The value
48 * ranges from 0 to 65535. This parameter takes effect only on abilities using the AbilityInfo.AbilityType.PAGE
49 * template.
50 *
51 * @return errCode ERR_OK on success, others on failure.
52 */
StartAbility(const AAFwk::Want & want,int requestCode)53 ErrCode ApplicationContext::StartAbility(const AAFwk::Want &want, int requestCode)
54 {
55 return ERR_INVALID_VALUE;
56 }
57
58 /**
59 * @brief Starts a new ability with special ability start setting.
60 *
61 * @param want Indicates the Want containing information about the target ability to start.
62 * @param requestCode Indicates the request code returned after the ability is started. You can define the request code
63 * to identify the results returned by abilities. The value ranges from 0 to 65535.
64 * @param abilityStartSetting Indicates the special start setting used in starting ability.
65 *
66 * @return errCode ERR_OK on success, others on failure.
67 */
StartAbility(const Want & want,int requestCode,const AbilityStartSetting & abilityStartSetting)68 ErrCode ApplicationContext::StartAbility(const Want &want, int requestCode,
69 const AbilityStartSetting &abilityStartSetting)
70 {
71 return ERR_INVALID_VALUE;
72 }
73
74 /**
75 * @brief Destroys another ability you had previously started by calling Ability.startAbilityForResult
76 * (ohos.aafwk.content.Want, int, ohos.aafwk.ability.startsetting.AbilityStartSetting) with the same requestCode passed.
77 *
78 * @param requestCode Indicates the request code passed for starting the ability.
79 *
80 * @return errCode ERR_OK on success, others on failure.
81 */
TerminateAbility(int requestCode)82 ErrCode ApplicationContext::TerminateAbility(int requestCode)
83 {
84 return ERR_INVALID_VALUE;
85 }
86
87 /**
88 * @brief Destroys the current ability.
89 *
90 * @return errCode ERR_OK on success, others on failure.
91 */
TerminateAbility()92 ErrCode ApplicationContext::TerminateAbility()
93 {
94 return ERR_INVALID_VALUE;
95 }
96
97 /**
98 * @brief
99 * Destroys this Service ability if the number of times it has been started equals the number represented by the
100 * given {@code startId}. This method is the same as calling {@link #terminateAbility} to destroy this Service
101 * ability, except that this method helps you avoid destroying it if a client has requested a Service
102 * ability startup in {@link ohos.aafwk.ability.Ability#onCommand} but you are unaware of it.
103 *
104 * @param startId Indicates the number of startup times of this Service ability passed to
105 * {@link ohos.aafwk.ability.Ability#onCommand}. The {@code startId} is
106 * incremented by 1 every time this ability is started. For example,
107 * if this ability has been started for six times, the value of {@code startId} is {@code 6}.
108 *
109 * @return Returns {@code true} if the {@code startId} matches the number of startup times
110 * and this Service ability will be destroyed; returns {@code false} otherwise.
111 */
TerminateAbilityResult(int startId)112 bool ApplicationContext::TerminateAbilityResult(int startId)
113 {
114 return false;
115 }
116
117 /**
118 * @brief Obtains the bundle name of the ability that called the current ability.
119 * You can use the obtained bundle name to check whether the calling ability is allowed to receive the data you will
120 * send. If you did not use Ability.startAbilityForResult(ohos.aafwk.content.Want, int,
121 * ohos.aafwk.ability.startsetting.AbilityStartSetting) to start the calling ability, null is returned.
122 *
123 * @return Returns the bundle name of the calling ability; returns null if no calling ability is available.
124 */
GetCallingBundle()125 std::string ApplicationContext::GetCallingBundle()
126 {
127 return "";
128 }
129
130 /**
131 * @brief Connects the current ability to an ability
132 *
133 * @param want Indicates the want containing information about the ability to connect
134 *
135 * @param conn Indicates the callback object when the target ability is connected.
136 *
137 * @return True means success and false means failure
138 */
ConnectAbility(const Want & want,const sptr<AAFwk::IAbilityConnection> & conn)139 bool ApplicationContext::ConnectAbility(const Want &want, const sptr<AAFwk::IAbilityConnection> &conn)
140 {
141 return false;
142 }
143
144 /**
145 * @brief Disconnects the current ability from an ability
146 *
147 * @param conn Indicates the IAbilityConnection callback object passed by connectAbility after the connection
148 * is set up. The IAbilityConnection object uniquely identifies a connection between two abilities.
149 *
150 * @return errCode ERR_OK on success, others on failure.
151 */
DisconnectAbility(const sptr<AAFwk::IAbilityConnection> & conn)152 ErrCode ApplicationContext::DisconnectAbility(const sptr<AAFwk::IAbilityConnection> &conn)
153 {
154 return ERR_INVALID_VALUE;
155 }
156
157 /**
158 * @brief Destroys another ability that uses the AbilityInfo.AbilityType.SERVICE template.
159 * The current ability using either the AbilityInfo.AbilityType.SERVICE or AbilityInfo.AbilityType.PAGE
160 * template can call this method to destroy another ability that uses the AbilityInfo.AbilityType.SERVICE
161 * template. The current ability itself can be destroyed by calling the terminateAbility() method.
162 *
163 * @param want Indicates the Want containing information about the ability to destroy.
164 *
165 * @return Returns true if the ability is destroyed successfully; returns false otherwise.
166 */
StopAbility(const AAFwk::Want & want)167 bool ApplicationContext::StopAbility(const AAFwk::Want &want)
168 {
169 return false;
170 }
171
GetToken()172 sptr<IRemoteObject> ApplicationContext::GetToken()
173 {
174 return nullptr;
175 }
176
177 /**
178 * @brief Starts multiple abilities.
179 *
180 * @param wants Indicates the Want containing information array about the target ability to start.
181 */
StartAbilities(const std::vector<AAFwk::Want> & wants)182 void ApplicationContext::StartAbilities(const std::vector<AAFwk::Want> &wants)
183 {}
184
185 /**
186 * @brief Checks whether this ability is the first ability in a mission.
187 *
188 * @return Returns true is first in Mission.
189 */
IsFirstInMission()190 bool ApplicationContext::IsFirstInMission()
191 {
192 return false;
193 }
194
195 /**
196 * @brief Obtains the unique ID of the mission containing this ability.
197 *
198 * @return Returns the unique mission ID.
199 */
GetMissionId()200 int ApplicationContext::GetMissionId()
201 {
202 return -1;
203 }
204
205 /**
206 * @brief Creates a parallel task dispatcher with a specified priority.
207 *
208 * @param name Indicates the task dispatcher name. This parameter is used to locate problems.
209 * @param priority Indicates the priority of all tasks dispatched by the parallel task dispatcher.
210 *
211 * @return Returns a parallel task dispatcher.
212 */
CreateParallelTaskDispatcher(const std::string & name,const TaskPriority & priority)213 std::shared_ptr<TaskDispatcher> ApplicationContext::CreateParallelTaskDispatcher(
214 const std::string &name, const TaskPriority &priority)
215 {
216 HILOG_INFO("ApplicationContext::CreateParallelTaskDispatcher begin");
217 if (taskDispatcherContext_ == nullptr) {
218 std::lock_guard<std::mutex> lock_l(mutex_);
219 if (taskDispatcherContext_ == nullptr) {
220 taskDispatcherContext_ = std::make_shared<TaskDispatcherContext>();
221 HILOG_INFO("ApplicationContext::CreateParallelTaskDispatcher threadpool create");
222 }
223
224 if (taskDispatcherContext_ == nullptr) {
225 HILOG_ERROR("ApplicationContext::CreateParallelTaskDispatcher taskDispatcherContext_ is nullptr");
226 return nullptr;
227 }
228 }
229
230 std::shared_ptr<TaskDispatcher> task = taskDispatcherContext_->CreateParallelDispatcher(name, priority);
231 HILOG_INFO("ApplicationContext::CreateParallelTaskDispatcher end");
232 return task;
233 }
234
235 /**
236 * @brief Creates a serial task dispatcher with a specified priority.
237 *
238 * @param name Indicates the task dispatcher name. This parameter is used to locate problems.
239 * @param priority Indicates the priority of all tasks dispatched by the created task dispatcher.
240 *
241 * @return Returns a serial task dispatcher.
242 */
CreateSerialTaskDispatcher(const std::string & name,const TaskPriority & priority)243 std::shared_ptr<TaskDispatcher> ApplicationContext::CreateSerialTaskDispatcher(
244 const std::string &name, const TaskPriority &priority)
245 {
246 HILOG_INFO("ApplicationContext::CreateSerialTaskDispatcher begin");
247 if (taskDispatcherContext_ == nullptr) {
248 std::lock_guard<std::mutex> lock_l(mutex_);
249 if (taskDispatcherContext_ == nullptr) {
250 taskDispatcherContext_ = std::make_shared<TaskDispatcherContext>();
251 HILOG_INFO("ApplicationContext::CreateSerialTaskDispatcher threadpool create");
252 }
253
254 if (taskDispatcherContext_ == nullptr) {
255 HILOG_ERROR("ApplicationContext::CreateSerialTaskDispatcher taskDispatcherContext_ is nullptr");
256 return nullptr;
257 }
258 }
259
260 std::shared_ptr<TaskDispatcher> task = taskDispatcherContext_->CreateSerialDispatcher(name, priority);
261 HILOG_INFO("ApplicationContext::CreateSerialTaskDispatcher end");
262 return task;
263 }
264
265 /**
266 * @brief Obtains a global task dispatcher with a specified priority.
267 *
268 * @param priority Indicates the priority of all tasks dispatched by the global task dispatcher.
269 *
270 * @return Returns a global task dispatcher.
271 */
GetGlobalTaskDispatcher(const TaskPriority & priority)272 std::shared_ptr<TaskDispatcher> ApplicationContext::GetGlobalTaskDispatcher(const TaskPriority &priority)
273 {
274 HILOG_INFO("ApplicationContext::GetGlobalTaskDispatcher begin");
275 if (taskDispatcherContext_ == nullptr) {
276 std::lock_guard<std::mutex> lock_l(mutex_);
277 if (taskDispatcherContext_ == nullptr) {
278 taskDispatcherContext_ = std::make_shared<TaskDispatcherContext>();
279 HILOG_INFO("ApplicationContext::GetGlobalTaskDispatcher threadpool create");
280 }
281
282 if (taskDispatcherContext_ == nullptr) {
283 HILOG_ERROR("ApplicationContext::GetGlobalTaskDispatcher taskDispatcherContext_ is nullptr");
284 return nullptr;
285 }
286 }
287
288 std::shared_ptr<TaskDispatcher> task = taskDispatcherContext_->GetGlobalTaskDispatcher(priority);
289 HILOG_INFO("ApplicationContext::GetGlobalTaskDispatcher end");
290 return task;
291 }
292 } // namespace AppExecFwk
293 } // namespace OHOS
294