1 /*
2  * Copyright (c) 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 #ifndef APP_FWK_UPDATE_SERVICE_H
17 #define APP_FWK_UPDATE_SERVICE_H
18 
19 #include <string>
20 
21 #include "app_fwk_update_service_stub.h"
22 #include "common_event_manager.h"
23 #include "event_handler.h"
24 #include "refbase.h"
25 #include "system_ability.h"
26 
27 namespace OHOS::NWeb {
28 
29 struct PackageCommonEventCallback {
30     std::function<void(const std::string bundleName, const std::string hapPath)> OnPackageChangedEvent;
31 };
32 
33 class PackageChangedReceiver : public EventFwk::CommonEventSubscriber {
34 public:
35     explicit PackageChangedReceiver(
36         const EventFwk::CommonEventSubscribeInfo& subscribeInfo, const PackageCommonEventCallback& callback);
37     ~PackageChangedReceiver() = default;
38     void OnReceiveEvent(const EventFwk::CommonEventData& data) override;
39 
40 private:
41     PackageCommonEventCallback callback_;
42 };
43 
44 class AppFwkUpdateService : public SystemAbility, public AppFwkUpdateServiceStub {
45     DECLARE_SYSTEM_ABILITY(AppFwkUpdateService);
46 
47 public:
48     AppFwkUpdateService(int32_t saId, bool runOnCreate);
49     ~AppFwkUpdateService();
50 
51     ErrCode VerifyPackageInstall(const std::string& bundleName, const std::string& hapPath, int32_t& success) override;
52     void SubscribePackageChangedEvent();
53     void OnPackageChangedEvent(const std::string& bunldeName, const std::string& hapPath);
54 
55 protected:
56     void OnStart(const SystemAbilityOnDemandReason& startReason) override;
57     void OnStop() override;
58     void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
59     void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
60     void PostDelayUnloadTask();
61 
62 private:
63     bool Init(const SystemAbilityOnDemandReason& startReason);
64 
65     int SetWebInstallPath(const std::string& path);
66     int SetWebCorePackageName(const std::string& packageName);
67     int SendAppSpawnMessage(const std::string& packageName);
68     int SendNWebSpawnMesage(const std::string& packageName);
69     std::shared_ptr<AppExecFwk::EventHandler> unloadHandler_;
70     std::shared_ptr<AppExecFwk::EventRunner> runner_;
71     bool registerToService_ = false;
72     std::shared_ptr<EventFwk::CommonEventSubscriber> pkgSubscriber_;
73 };
74 } // namespace OHOS::NWeb
75 
76 #endif // APP_FWK_UPDATE_SERVICE_H
77