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 #include "extension_impl.h"
17
18 #include "ability_manager_client.h"
19 #include "ability_local_record.h"
20 #include "bytrace.h"
21 #include "extension_context.h"
22 #include "hilog_wrapper.h"
23
24 namespace OHOS {
25 namespace AbilityRuntime {
Init(std::shared_ptr<AppExecFwk::OHOSApplication> & application,const std::shared_ptr<AppExecFwk::AbilityLocalRecord> & record,std::shared_ptr<Extension> & extension,std::shared_ptr<AppExecFwk::AbilityHandler> & handler,const sptr<IRemoteObject> & token)26 void ExtensionImpl::Init(std::shared_ptr<AppExecFwk::OHOSApplication> &application,
27 const std::shared_ptr<AppExecFwk::AbilityLocalRecord> &record,
28 std::shared_ptr<Extension> &extension,
29 std::shared_ptr<AppExecFwk::AbilityHandler> &handler,
30 const sptr<IRemoteObject> &token)
31 {
32 BYTRACE_NAME(BYTRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
33 HILOG_INFO("ExtensionImpl Init begin.");
34 if ((token == nullptr) || (application == nullptr) || (handler == nullptr) || (record == nullptr) ||
35 extension == nullptr) {
36 HILOG_ERROR("ExtensionImpl::init failed, some object is nullptr");
37 return;
38 }
39
40 token_ = record->GetToken();
41 extension_ = extension;
42 extension_->Init(record, application, handler, token);
43 lifecycleState_ = AAFwk::ABILITY_STATE_INITIAL;
44 HILOG_INFO("ExtensionImpl Init end.");
45 }
46
47 /**
48 * @brief Handling the life cycle switching of Extension.
49 *
50 * @param want Indicates the structure containing information about the extension.
51 * @param targetState The life cycle state to switch to.
52 *
53 */
HandleExtensionTransaction(const Want & want,const AAFwk::LifeCycleStateInfo & targetState)54 void ExtensionImpl::HandleExtensionTransaction(const Want &want,
55 const AAFwk::LifeCycleStateInfo &targetState)
56 {
57 HILOG_INFO("ExtensionImpl::HandleExtensionTransaction begin sourceState:%{public}d; targetState: %{public}d; "
58 "isNewWant: %{public}d",
59 lifecycleState_,
60 targetState.state,
61 targetState.isNewWant);
62 if (lifecycleState_ == targetState.state) {
63 HILOG_ERROR("Org lifeCycleState equals to Dst lifeCycleState.");
64 return;
65 }
66
67 bool ret = true;
68
69 switch (targetState.state) {
70 case AAFwk::ABILITY_STATE_INITIAL: {
71 if (lifecycleState_ == AAFwk::ABILITY_STATE_ACTIVE) {
72 Stop();
73 }
74 break;
75 }
76 case AAFwk::ABILITY_STATE_INACTIVE: {
77 if (lifecycleState_ == AAFwk::ABILITY_STATE_INITIAL) {
78 Start(want);
79 }
80 break;
81 }
82 default: {
83 ret = false;
84 HILOG_ERROR("ExtensionImpl::HandleAbilityTransaction state is error");
85 break;
86 }
87 }
88
89 if (ret) {
90 HILOG_INFO("ExtensionImpl::HandleAbilityTransaction before AbilityManagerClient->AbilityTransitionDone");
91 AAFwk::PacMap restoreData;
92 AAFwk::AbilityManagerClient::GetInstance()->AbilityTransitionDone(token_, targetState.state, restoreData);
93 HILOG_INFO("ExtensionImpl::HandleAbilityTransaction after AbilityManagerClient->AbilityTransitionDone");
94 }
95 HILOG_INFO("ExtensionImpl::HandleAbilityTransaction end");
96 }
97
ScheduleUpdateConfiguration(const AppExecFwk::Configuration & config)98 void ExtensionImpl::ScheduleUpdateConfiguration(const AppExecFwk::Configuration &config)
99 {
100 HILOG_INFO("%{public}s begin.", __func__);
101
102 if (extension_ == nullptr) {
103 HILOG_ERROR("ExtensionImpl::ScheduleUpdateConfiguration extension_ is nullptr");
104 return;
105 }
106
107 if (lifecycleState_ != AAFwk::ABILITY_STATE_INITIAL) {
108 extension_->OnConfigurationUpdated(config);
109 }
110
111 HILOG_INFO("%{public}s end.", __func__);
112 }
113
114 /**
115 * @brief Toggles the lifecycle status of Extension to AAFwk::ABILITY_STATE_INACTIVE. And notifies the application
116 * that it belongs to of the lifecycle status.
117 *
118 * @param want The Want object to switch the life cycle.
119 */
Start(const Want & want)120 void ExtensionImpl::Start(const Want &want)
121 {
122 HILOG_INFO("%{public}s begin.", __func__);
123 if (extension_ == nullptr) {
124 HILOG_ERROR("ExtensionImpl::Start extension_ is nullptr");
125 return;
126 }
127
128 HILOG_INFO("ExtensionImpl::Start");
129 extension_->OnStart(want);
130 lifecycleState_ = AAFwk::ABILITY_STATE_INACTIVE;
131 HILOG_INFO("%{public}s end.", __func__);
132 }
133
134 /**
135 * @brief Toggles the lifecycle status of Extension to AAFwk::ABILITY_STATE_INITIAL. And notifies the application
136 * that it belongs to of the lifecycle status.
137 *
138 */
Stop()139 void ExtensionImpl::Stop()
140 {
141 HILOG_INFO("%{public}s begin.", __func__);
142 if (extension_ == nullptr) {
143 HILOG_ERROR("ExtensionImpl::Stop extension_ is nullptr");
144 return;
145 }
146
147 HILOG_INFO("ExtensionImpl::Stop");
148 extension_->OnStop();
149 lifecycleState_ = AAFwk::ABILITY_STATE_INITIAL;
150 HILOG_INFO("%{public}s end.", __func__);
151 }
152
153 /**
154 * @brief Connect the extension. and Calling information back to Extension.
155 *
156 * @param want The Want object to connect to.
157 *
158 */
ConnectExtension(const Want & want)159 sptr<IRemoteObject> ExtensionImpl::ConnectExtension(const Want &want)
160 {
161 HILOG_INFO("%{public}s begin.", __func__);
162 if (extension_ == nullptr) {
163 HILOG_ERROR("ExtensionImpl::ConnectAbility extension_ is nullptr");
164 return nullptr;
165 }
166
167 HILOG_INFO("ExtensionImpl:: ConnectAbility");
168 sptr<IRemoteObject> object = extension_->OnConnect(want);
169 lifecycleState_ = AAFwk::ABILITY_STATE_ACTIVE;
170 HILOG_INFO("%{public}s end.", __func__);
171
172 return object;
173 }
174
175 /**
176 * @brief Disconnects the connected object.
177 *
178 * @param want The Want object to disconnect to.
179 */
DisconnectExtension(const Want & want)180 void ExtensionImpl::DisconnectExtension(const Want &want)
181 {
182 HILOG_INFO("%{public}s begin.", __func__);
183 if (extension_ == nullptr) {
184 HILOG_ERROR("ExtensionImpl::DisconnectAbility extension_ is nullptr");
185 return;
186 }
187
188 extension_->OnDisconnect(want);
189 HILOG_INFO("%{public}s end.", __func__);
190 }
191
192 /**
193 * @brief Command the Extension. and Calling information back to Extension.
194 *
195 * @param want The Want object to command to.
196 *
197 * * @param restart Indicates the startup mode. The value true indicates that Service is restarted after being
198 * destroyed, and the value false indicates a normal startup.
199 *
200 * @param startId Indicates the number of times the Service Extension has been started. The startId is incremented by 1
201 * every time the Extension is started. For example, if the Extension has been started for six times,
202 * the value of startId is 6.
203 */
CommandExtension(const Want & want,bool restart,int startId)204 void ExtensionImpl::CommandExtension(const Want &want, bool restart, int startId)
205 {
206 HILOG_INFO("%{public}s begin.", __func__);
207 if (extension_ == nullptr) {
208 HILOG_ERROR("ExtensionImpl::CommandAbility extension_ is nullptr");
209 return;
210 }
211
212 HILOG_INFO("ExtensionImpl:: CommandAbility");
213 extension_->OnCommand(want, restart, startId);
214 lifecycleState_ = AAFwk::ABILITY_STATE_ACTIVE;
215 HILOG_INFO("%{public}s end.", __func__);
216 }
217 }
218 }