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