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 #ifndef DISTRIBUTEDDATAMGR_INSTALLER_IMPL_H
17 #define DISTRIBUTEDDATAMGR_INSTALLER_IMPL_H
18 
19 #include "common_event_subscriber.h"
20 #include "kvstore_data_service.h"
21 #include "installer.h"
22 
23 namespace OHOS::DistributedKv {
24 class InstallEventSubscriber : public EventFwk::CommonEventSubscriber {
25 public:
26 using InstallEventCallback = void (InstallEventSubscriber::*)
27     (const std::string &bundleName, int32_t userId, int32_t appIndex);
28     InstallEventSubscriber(const EventFwk::CommonEventSubscribeInfo &info, KvStoreDataService *kvStoreDataService);
29 
~InstallEventSubscriber()30     ~InstallEventSubscriber() {}
31     void OnReceiveEvent(const EventFwk::CommonEventData &event) override;
32 
33 private:
34     static constexpr const char *USER_ID = "userId";
35     static constexpr const char *SANDBOX_APP_INDEX = "sandbox_app_index";
36     void OnUninstall(const std::string &bundleName, int32_t userId, int32_t appIndex);
37     void OnUpdate(const std::string &bundleName, int32_t userId, int32_t appIndex);
38     void OnInstall(const std::string &bundleName, int32_t userId, int32_t appIndex);
39     std::map<std::string, InstallEventCallback> callbacks_;
40     KvStoreDataService *kvStoreDataService_;
41 };
42 
43 class InstallerImpl : public Installer {
44 public:
45     InstallerImpl() = default;
46     ~InstallerImpl();
47 
48     Status Init(KvStoreDataService *kvStoreDataService, std::shared_ptr<ExecutorPool> executors) override;
49 
50     void UnsubscribeEvent() override;
51 
52 private:
53     static constexpr int32_t RETRY_TIME = 300;
54     static constexpr int32_t RETRY_INTERVAL = 100;
55     int32_t retryTime_ = 0;
56     ExecutorPool::Task GetTask();
57     std::shared_ptr<InstallEventSubscriber> subscriber_ {};
58     std::shared_ptr<ExecutorPool> executors_ {};
59 };
60 } // namespace OHOS::DistributedKv
61 #endif // DISTRIBUTEDDATAMGR_INSTALLER_IMPL_H
62