1 /*
2  * Copyright (c) 2021-2024 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 "ability_stage.h"
17 
18 #include "ability_runtime/context/context.h"
19 #include "hilog_tag_wrapper.h"
20 #include "hitrace_meter.h"
21 #ifdef CJ_FRONTEND
22 #include "cj_ability_stage.h"
23 #endif
24 #include "js_ability_stage.h"
25 #include "runtime.h"
26 
27 namespace OHOS {
28 namespace AbilityRuntime {
Create(const std::unique_ptr<Runtime> & runtime,const AppExecFwk::HapModuleInfo & hapModuleInfo)29 std::shared_ptr<AbilityStage> AbilityStage::Create(
30     const std::unique_ptr<Runtime>& runtime, const AppExecFwk::HapModuleInfo& hapModuleInfo)
31 {
32     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
33     if (!runtime) {
34         return std::make_shared<AbilityStage>();
35     }
36 
37     switch (runtime->GetLanguage()) {
38         case Runtime::Language::JS:
39             return JsAbilityStage::Create(runtime, hapModuleInfo);
40 #ifdef CJ_FRONTEND
41         case Runtime::Language::CJ:
42             return CJAbilityStage::Create(runtime, hapModuleInfo);
43 #endif
44         default:
45             return std::make_shared<AbilityStage>();
46     }
47 }
48 
OnCreate(const AAFwk::Want & want) const49 void AbilityStage::OnCreate(const AAFwk::Want &want) const
50 {}
51 
OnDestroy() const52 void AbilityStage::OnDestroy() const
53 {}
54 
GetContext() const55 std::shared_ptr<Context> AbilityStage::GetContext() const
56 {
57     return context_;
58 }
59 
Init(const std::shared_ptr<Context> & context,const std::weak_ptr<AppExecFwk::OHOSApplication> application)60 void AbilityStage::Init(const std::shared_ptr<Context>& context,
61     const std::weak_ptr<AppExecFwk::OHOSApplication> application)
62 {
63     context_ = context;
64     application_ = application;
65 }
66 
AddAbility(const sptr<IRemoteObject> & token,const std::shared_ptr<AppExecFwk::AbilityLocalRecord> & abilityRecord)67 void AbilityStage::AddAbility(const sptr<IRemoteObject> &token,
68     const std::shared_ptr<AppExecFwk::AbilityLocalRecord> &abilityRecord)
69 {
70     if (token == nullptr) {
71         TAG_LOGE(AAFwkTag::APPKIT, "token is nullptr");
72         return;
73     }
74 
75     if (abilityRecord == nullptr) {
76         TAG_LOGE(AAFwkTag::APPKIT, "abilityRecord is nullptr");
77         return;
78     }
79 
80     abilityRecords_[token] = abilityRecord;
81 }
82 
RemoveAbility(const sptr<IRemoteObject> & token)83 void AbilityStage::RemoveAbility(const sptr<IRemoteObject> &token)
84 {
85     if (token == nullptr) {
86         TAG_LOGE(AAFwkTag::APPKIT, "token is nullptr");
87         return;
88     }
89     abilityRecords_.erase(token);
90 }
91 
ContainsAbility() const92 bool AbilityStage::ContainsAbility() const
93 {
94     return !abilityRecords_.empty();
95 }
96 
OnAcceptWant(const AAFwk::Want & want)97 std::string AbilityStage::OnAcceptWant(const AAFwk::Want &want)
98 {
99     return "";
100 }
101 
OnNewProcessRequest(const AAFwk::Want & want)102 std::string AbilityStage::OnNewProcessRequest(const AAFwk::Want &want)
103 {
104     return "";
105 }
106 
OnConfigurationUpdated(const AppExecFwk::Configuration & configuration)107 void AbilityStage::OnConfigurationUpdated(const AppExecFwk::Configuration& configuration)
108 {}
109 
OnMemoryLevel(int level)110 void AbilityStage::OnMemoryLevel(int level)
111 {}
112 
RunAutoStartupTask(const std::function<void ()> & callback,bool & isAsyncCallback,const std::shared_ptr<Context> & stageContext)113 int32_t AbilityStage::RunAutoStartupTask(const std::function<void()> &callback, bool &isAsyncCallback,
114     const std::shared_ptr<Context> &stageContext)
115 {
116     isAsyncCallback = false;
117     return ERR_OK;
118 }
119 }  // namespace AbilityRuntime
120 }  // namespace OHOS
121