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 OHOS_ABILITY_RUNTIME_ABILITY_STAGE_H
17 #define OHOS_ABILITY_RUNTIME_ABILITY_STAGE_H
18 
19 #include <functional>
20 #include <map>
21 #include <memory>
22 #include <string>
23 
24 #include "ability_local_record.h"
25 #include "configuration.h"
26 #include "hap_module_info.h"
27 #include "want.h"
28 
29 namespace OHOS {
30 namespace AppExecFwk {
31 class OHOSApplication;
32 }
33 namespace AbilityRuntime {
34 class Context;
35 class Runtime;
36 /**
37  * @brief the hap level entity
38  *
39  * To share the data among the abilities of a hap,
40  * and supply lifecycle func for the developers.
41  */
42 class AbilityStage {
43 public:
44     static std::shared_ptr<AbilityStage> Create(
45         const std::unique_ptr<Runtime>& runtime, const AppExecFwk::HapModuleInfo& hapModuleInfo);
46 
47     AbilityStage() = default;
48     virtual ~AbilityStage() = default;
49     virtual void OnCreate(const AAFwk::Want &want) const;
50     virtual void OnDestroy() const;
51     virtual std::string OnAcceptWant(const AAFwk::Want &want);
52     virtual std::string OnNewProcessRequest(const AAFwk::Want &want);
53     virtual void Init(const std::shared_ptr<Context> &context,
54         const std::weak_ptr<AppExecFwk::OHOSApplication> application);
55     std::shared_ptr<Context> GetContext() const;
56     void AddAbility(const sptr<IRemoteObject> &token,
57         const std::shared_ptr<AppExecFwk::AbilityLocalRecord> &abilityRecord);
58     void RemoveAbility(const sptr<IRemoteObject> &token);
59     bool ContainsAbility() const;
60     virtual void OnConfigurationUpdated(const AppExecFwk::Configuration& configuration);
61     virtual void OnMemoryLevel(int level);
62     virtual int32_t RunAutoStartupTask(const std::function<void()> &callback, bool &isAsyncCallback,
63         const std::shared_ptr<Context> &stageContext);
64 
65 private:
66     friend class JsAbilityStage;
67     std::shared_ptr<Context> context_;
68     std::map<sptr<IRemoteObject>, std::shared_ptr<AppExecFwk::AbilityLocalRecord>> abilityRecords_;
69     std::weak_ptr<AppExecFwk::OHOSApplication> application_;
70 };
71 }  // namespace AbilityRuntime
72 }  // namespace OHOS
73 #endif  // OHOS_ABILITY_RUNTIME_ABILITY_STAGE_H
74