• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "core/components/ability_component/resource/ability_component_delegate.h"
17 
18 namespace OHOS::Ace {
19 namespace {
20 
21 constexpr char ABILITY_COMPONENT_RESOURCE_NAME[] = "abilitycomponent";
22 
23 constexpr char ABILITY_COMPONENT_METHOD_UPDATE_RENDER_RECT[] = "updateRenderRect";
24 constexpr char ABILITY_COMPONENT_METHOD_START_ABILITY[] = "startAbility";
25 constexpr char ABILITY_COMPONENT_METHOD_PERFORM_BACK_PRESS[] = "performBackPress";
26 constexpr char ABILITY_COMPONENT_METHOD_GET_STACK_COUNT[] = "getStackCount";
27 
28 constexpr char ABILITY_COMPONENT_EVENT_READY[] = "onReady";
29 constexpr char ABILITY_COMPONENT_EVENT_DESTROY[] = "onDestroy";
30 constexpr char ABILITY_COMPONENT_EVENT_ABILITY_CREATED[] = "onAbilityCreated";
31 constexpr char ABILITY_COMPONENT_EVENT_ABILITY_MOVED_FRONT[] = "onAbilityMovedFront";
32 constexpr char ABILITY_COMPONENT_EVENT_ABILITY_WILL_REMOVE[] = "onAbilityWillRemove";
33 
34 constexpr char NTC_PARAM_LEFT[] = "left";
35 constexpr char NTC_PARAM_TOP[] = "top";
36 constexpr char NTC_PARAM_WIDTH[] = "width";
37 constexpr char NTC_PARAM_HEIGHT[] = "height";
38 constexpr char NTC_PARAM_WANT[] = "want";
39 constexpr char NTC_PARAM_ABILITY_COMPONENT[] = "abilitycomponent";
40 
41 } // namespace
42 
AbilityComponentDelegate(const RefPtr<AbilityComponent> & abilityComponent,const WeakPtr<PipelineContext> & context,const std::string & type)43 AbilityComponentDelegate::AbilityComponentDelegate(
44     const RefPtr<AbilityComponent>& abilityComponent, const WeakPtr<PipelineContext>& context, const std::string& type)
45     : AbilityComponentResource(type, context), abilityComponent_(abilityComponent), state_(State::WAITING_FOR_SIZE)
46 {
47     InitControllerImpl();
48 }
49 
~AbilityComponentDelegate()50 AbilityComponentDelegate::~AbilityComponentDelegate()
51 {
52     OnDestroy();
53     ReleasePlatformResource();
54 }
55 
InitControllerImpl()56 void AbilityComponentDelegate::InitControllerImpl()
57 {
58     if (!abilityComponent_) {
59         return;
60     }
61     auto controller = abilityComponent_->GetController();
62     if (!controller) {
63         return;
64     }
65     controller->SetStartAbilityImpl([weak = WeakClaim(this)](const std::string& want) {
66         auto delegate = weak.Upgrade();
67         if (delegate) {
68             delegate->StartAbility(want);
69         }
70     });
71     controller->SetPerformBackPressImpl([weak = WeakClaim(this)]() {
72         auto delegate = weak.Upgrade();
73         if (delegate) {
74             delegate->PerformBackPress();
75         }
76     });
77     controller->SetGetStackCountImpl([weak = WeakClaim(this)]() {
78         auto delegate = weak.Upgrade();
79         if (delegate) {
80             return delegate->GetStackCount();
81         }
82         return 0;
83     });
84 }
85 
ReleasePlatformResource()86 void AbilityComponentDelegate::ReleasePlatformResource()
87 {
88     Stop();
89     Release();
90 }
91 
Stop()92 void AbilityComponentDelegate::Stop()
93 {
94     auto context = context_.Upgrade();
95     if (!context) {
96         LOGE("fail to get context when stop");
97         return;
98     }
99     auto platformTaskExecutor = SingleTaskExecutor::Make(context->GetTaskExecutor(), TaskExecutor::TaskType::PLATFORM);
100     if (platformTaskExecutor.IsRunOnCurrentThread()) {
101         UnregisterEvent();
102     } else {
103         platformTaskExecutor.PostTask(
104             [weak = WeakClaim(this)] {
105                 auto delegate = weak.Upgrade();
106                 if (delegate) {
107                     delegate->UnregisterEvent();
108                 }
109             },
110             "ArkUIAbilityComponentStop");
111     }
112 }
113 
RegisterEvent()114 void AbilityComponentDelegate::RegisterEvent()
115 {
116     auto context = context_.Upgrade();
117     if (!context) {
118         return;
119     }
120     auto resRegister = context->GetPlatformResRegister();
121     if (!resRegister) {
122         return;
123     }
124     resRegister->RegisterEvent(
125         MakeEventHash(ABILITY_COMPONENT_EVENT_READY), [weak = WeakClaim(this)](const std::string& param) {
126             auto delegate = weak.Upgrade();
127             if (delegate) {
128                 delegate->OnReady();
129             }
130         });
131     resRegister->RegisterEvent(
132         MakeEventHash(ABILITY_COMPONENT_EVENT_DESTROY), [weak = WeakClaim(this)](const std::string& param) {
133             auto delegate = weak.Upgrade();
134             if (delegate) {
135                 delegate->OnDestroy();
136             }
137         });
138     resRegister->RegisterEvent(
139         MakeEventHash(ABILITY_COMPONENT_EVENT_ABILITY_CREATED), [weak = WeakClaim(this)](const std::string& param) {
140             auto delegate = weak.Upgrade();
141             if (delegate) {
142                 delegate->OnAbilityCreated(param);
143             }
144         });
145     resRegister->RegisterEvent(
146         MakeEventHash(ABILITY_COMPONENT_EVENT_ABILITY_MOVED_FRONT), [weak = WeakClaim(this)](const std::string& param) {
147             auto delegate = weak.Upgrade();
148             if (delegate) {
149                 delegate->OnAbilityMovedFront();
150             }
151         });
152     resRegister->RegisterEvent(
153         MakeEventHash(ABILITY_COMPONENT_EVENT_ABILITY_WILL_REMOVE), [weak = WeakClaim(this)](const std::string& param) {
154             auto delegate = weak.Upgrade();
155             if (delegate) {
156                 delegate->OnAbilityWillRemove();
157             }
158         });
159 }
160 
UnregisterEvent()161 void AbilityComponentDelegate::UnregisterEvent()
162 {
163     auto context = context_.Upgrade();
164     if (!context) {
165         LOGE("fail to get context when unregister event");
166         return;
167     }
168     auto resRegister = context->GetPlatformResRegister();
169     if (!resRegister) {
170         LOGE("fail to get resRegister when unregister event");
171         return;
172     }
173     resRegister->UnregisterEvent(MakeEventHash(ABILITY_COMPONENT_EVENT_READY));
174     resRegister->UnregisterEvent(MakeEventHash(ABILITY_COMPONENT_EVENT_DESTROY));
175     resRegister->UnregisterEvent(MakeEventHash(ABILITY_COMPONENT_EVENT_ABILITY_CREATED));
176     resRegister->UnregisterEvent(MakeEventHash(ABILITY_COMPONENT_EVENT_ABILITY_MOVED_FRONT));
177     resRegister->UnregisterEvent(MakeEventHash(ABILITY_COMPONENT_EVENT_ABILITY_WILL_REMOVE));
178 }
179 
UpdateRenderRect(const Rect & rect)180 void AbilityComponentDelegate::UpdateRenderRect(const Rect& rect)
181 {
182     if (id_ == INVALID_ID) {
183         return;
184     }
185 
186     hash_ = MakeResourceHash();
187     Method updateRenderRectMethod = MakeMethodHash(ABILITY_COMPONENT_METHOD_UPDATE_RENDER_RECT);
188     std::stringstream paramStream;
189     paramStream << NTC_PARAM_LEFT << ABILITY_COMPONENT_PARAM_EQUALS << (int32_t)rect.Left()
190                 << ABILITY_COMPONENT_PARAM_AND << NTC_PARAM_TOP << ABILITY_COMPONENT_PARAM_EQUALS << (int32_t)rect.Top()
191                 << ABILITY_COMPONENT_PARAM_AND << NTC_PARAM_WIDTH << ABILITY_COMPONENT_PARAM_EQUALS
192                 << (int32_t)rect.Width() << ABILITY_COMPONENT_PARAM_AND << NTC_PARAM_HEIGHT
193                 << ABILITY_COMPONENT_PARAM_EQUALS << (int32_t)rect.Height();
194     std::string param = paramStream.str();
195     CallResRegisterMethod(updateRenderRectMethod, param, false);
196 }
197 
CreatePlatformResource(const WeakPtr<PipelineContext> & context,const Rect & rect)198 void AbilityComponentDelegate::CreatePlatformResource(const WeakPtr<PipelineContext>& context, const Rect& rect)
199 {
200     context_ = context;
201     CreatePluginResource(context, rect);
202 }
203 
CreatePluginResource(const WeakPtr<PipelineContext> & context,const Rect & rect)204 void AbilityComponentDelegate::CreatePluginResource(const WeakPtr<PipelineContext>& context, const Rect& rect)
205 {
206     state_ = State::CREATING;
207 
208     if (!abilityComponent_) {
209         state_ = State::CREATE_FAILED;
210         return;
211     }
212 
213     auto pipelineContext = context.Upgrade();
214     if (!pipelineContext) {
215         state_ = State::CREATE_FAILED;
216         return;
217     }
218     context_ = context;
219     auto platformTaskExecutor =
220         SingleTaskExecutor::Make(pipelineContext->GetTaskExecutor(), TaskExecutor::TaskType::PLATFORM);
221     auto resRegister = pipelineContext->GetPlatformResRegister();
222     auto weakRes = AceType::WeakClaim(AceType::RawPtr(resRegister));
223     platformTaskExecutor.PostTask(
224         [weak = WeakClaim(this), weakRes, rect] {
225             auto delegate = weak.Upgrade();
226             if (!delegate) {
227                 return;
228             }
229             auto component = delegate->abilityComponent_;
230             if (!component) {
231                 return;
232             }
233             auto resRegister = weakRes.Upgrade();
234             if (!resRegister) {
235                 return;
236             }
237             auto context = delegate->context_.Upgrade();
238             if (!context) {
239                 return;
240             }
241 
242             delegate->id_ = CREATING_ID;
243 
244             std::stringstream paramStream;
245             paramStream << NTC_PARAM_ABILITY_COMPONENT << ABILITY_COMPONENT_PARAM_EQUALS << delegate->id_
246                         << ABILITY_COMPONENT_PARAM_AND << NTC_PARAM_LEFT << ABILITY_COMPONENT_PARAM_EQUALS
247                         << (int32_t)rect.Left() << ABILITY_COMPONENT_PARAM_AND << NTC_PARAM_TOP
248                         << ABILITY_COMPONENT_PARAM_EQUALS << (int32_t)rect.Top() << ABILITY_COMPONENT_PARAM_AND
249                         << NTC_PARAM_WIDTH << ABILITY_COMPONENT_PARAM_EQUALS << (int32_t)rect.Width()
250                         << ABILITY_COMPONENT_PARAM_AND << NTC_PARAM_HEIGHT << ABILITY_COMPONENT_PARAM_EQUALS
251                         << (int32_t)rect.Height() << ABILITY_COMPONENT_PARAM_AND << NTC_PARAM_WANT
252                         << ABILITY_COMPONENT_PARAM_EQUALS << component->GetWant();
253 
254             std::string param = paramStream.str();
255             delegate->id_ = resRegister->CreateResource(ABILITY_COMPONENT_RESOURCE_NAME, param);
256             if (delegate->id_ == INVALID_ID) {
257                 return;
258             }
259             delegate->state_ = State::CREATED;
260             delegate->hash_ = delegate->MakeResourceHash();
261             delegate->RegisterEvent();
262         },
263         "ArkUIAbilityComponentCreatePluginResource");
264 }
265 
OnReady() const266 void AbilityComponentDelegate::OnReady() const
267 {
268     auto context = context_.Upgrade();
269     if (!context || !abilityComponent_ || abilityComponent_->GetOnReady() == nullptr) {
270         return;
271     }
272     auto onReady = *abilityComponent_->GetOnReady();
273     context->GetTaskExecutor()->PostTask(
274         [onReady]() {
275             if (onReady) {
276                 onReady();
277             }
278         },
279         TaskExecutor::TaskType::JS, "ArkUIAbilityComponentReady");
280 }
281 
OnDestroy() const282 void AbilityComponentDelegate::OnDestroy() const
283 {
284     auto context = context_.Upgrade();
285     if (!context || !abilityComponent_ || abilityComponent_->GetOnDestroy() == nullptr) {
286         return;
287     }
288     auto onDestroy = *abilityComponent_->GetOnDestroy();
289     context->GetTaskExecutor()->PostTask(
290         [onDestroy]() {
291             if (onDestroy) {
292                 onDestroy();
293             }
294         },
295         TaskExecutor::TaskType::JS, "ArkUIAbilityComponentDestroy");
296 }
297 
OnAbilityCreated(const std::string & param) const298 void AbilityComponentDelegate::OnAbilityCreated(const std::string& param) const
299 {
300     auto context = context_.Upgrade();
301     if (!context || !abilityComponent_ || abilityComponent_->GetOnAbilityCreated() == nullptr) {
302         return;
303     }
304     auto onAbilityCreated = *abilityComponent_->GetOnAbilityCreated();
305     context->GetTaskExecutor()->PostTask(
306         [onAbilityCreated, param]() {
307             if (onAbilityCreated) {
308                 onAbilityCreated(param);
309             }
310         },
311         TaskExecutor::TaskType::JS, "ArkUIAbilityComponentAbilityCreated");
312 }
313 
OnAbilityMovedFront() const314 void AbilityComponentDelegate::OnAbilityMovedFront() const
315 {
316     auto context = context_.Upgrade();
317     if (!context || !abilityComponent_ || abilityComponent_->GetOnAbilityMovedFront() == nullptr) {
318         return;
319     }
320     auto onAbilityMovedFront = *abilityComponent_->GetOnAbilityMovedFront();
321     context->GetTaskExecutor()->PostTask(
322         [onAbilityMovedFront]() {
323             if (onAbilityMovedFront) {
324                 onAbilityMovedFront();
325             }
326         },
327         TaskExecutor::TaskType::JS, "ArkUIAbilityComponentAbilityMovedFront");
328 }
329 
OnAbilityWillRemove() const330 void AbilityComponentDelegate::OnAbilityWillRemove() const
331 {
332     auto context = context_.Upgrade();
333     if (!context || !abilityComponent_ || abilityComponent_->GetOnAbilityWillRemove() == nullptr) {
334         return;
335     }
336     auto onAbilityWillRemove = *abilityComponent_->GetOnAbilityWillRemove();
337     context->GetTaskExecutor()->PostTask(
338         [onAbilityWillRemove]() {
339             if (onAbilityWillRemove) {
340                 onAbilityWillRemove();
341             }
342         },
343         TaskExecutor::TaskType::JS, "ArkUIAbilityComponentAbilityWillRemove");
344 }
345 
StartAbility(const std::string & want)346 void AbilityComponentDelegate::StartAbility(const std::string& want)
347 {
348     if (id_ == INVALID_ID) {
349         return;
350     }
351 
352     hash_ = MakeResourceHash();
353     Method startAbilityMethod = MakeMethodHash(ABILITY_COMPONENT_METHOD_START_ABILITY);
354     std::stringstream paramStream;
355     paramStream << NTC_PARAM_WANT << ABILITY_COMPONENT_PARAM_EQUALS << want;
356     std::string param = paramStream.str();
357     CallResRegisterMethod(startAbilityMethod, param, false);
358 }
359 
PerformBackPress()360 void AbilityComponentDelegate::PerformBackPress()
361 {
362     if (id_ == INVALID_ID) {
363         return;
364     }
365 
366     hash_ = MakeResourceHash();
367     Method performBackPressMethod = MakeMethodHash(ABILITY_COMPONENT_METHOD_PERFORM_BACK_PRESS);
368     CallResRegisterMethod(performBackPressMethod, "", false);
369 }
370 
GetStackCount()371 int32_t AbilityComponentDelegate::GetStackCount()
372 {
373     if (id_ == INVALID_ID) {
374         return 0;
375     }
376 
377     hash_ = MakeResourceHash();
378     Method getStackCountMethod = MakeMethodHash(ABILITY_COMPONENT_METHOD_GET_STACK_COUNT);
379     auto result = CallResRegisterMethod(getStackCountMethod, "", true);
380     return StringUtils::StringToInt(result);
381 }
382 
383 } // namespace OHOS::Ace
384