1 /*
2  * Copyright (c) 2023 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_FREE_INSTALL_OBSERVER_MANAGER_H
17 #define OHOS_ABILITY_RUNTIME_FREE_INSTALL_OBSERVER_MANAGER_H
18 
19 #include <map>
20 #include <mutex>
21 #include <unordered_map>
22 #include "cpp/mutex.h"
23 
24 #include "free_install_observer_interface.h"
25 #include "singleton.h"
26 
27 namespace OHOS {
28 namespace AAFwk {
29 using namespace OHOS::AbilityRuntime;
30 class FreeInstallObserverManager : public std::enable_shared_from_this<FreeInstallObserverManager> {
31     DECLARE_DELAYED_SINGLETON(FreeInstallObserverManager)
32 public:
33     int32_t AddObserver(int32_t recordId, const sptr<IFreeInstallObserver> &observer);
34 
35     int32_t RemoveObserver(const sptr<IFreeInstallObserver> &observer);
36 
37     void OnInstallFinished(int32_t recordId, const std::string &bundleName, const std::string &abilityName,
38         const std::string &startTime, const int &resultCode);
39 
40     void OnInstallFinishedByUrl(int32_t recordId, const std::string &startTime, const std::string &url,
41         const int &resultCode);
42 
43 private:
44     void OnObserverDied(const wptr<IRemoteObject> &remote);
45 
46     void HandleOnInstallFinished(int32_t recordId, const std::string &bundleName, const std::string &abilityName,
47         const std::string &startTime, const int &resultCode);
48 
49     void HandleOnInstallFinishedByUrl(int32_t recordId, const std::string &startTime, const std::string &url,
50         const int &resultCode);
51 
52     ffrt::mutex observerLock_;
53     sptr<IRemoteObject::DeathRecipient> deathRecipient_;
54     std::unordered_map<int32_t, sptr<IFreeInstallObserver>> observerMap_;
55 };
56 
57 class FreeInstallObserverRecipient : public IRemoteObject::DeathRecipient {
58 public:
59     using RemoteDiedHandler = std::function<void(const wptr<IRemoteObject> &)>;
60     explicit FreeInstallObserverRecipient(RemoteDiedHandler handler);
61     virtual ~FreeInstallObserverRecipient();
62     virtual void OnRemoteDied(const wptr<IRemoteObject> &remote);
63 
64 private:
65     RemoteDiedHandler handler_;
66 };
67 } // namespace AAFwk
68 } // namespace OHOS
69 #endif // OHOS_ABILITY_RUNTIME_FREE_INSTALL_OBSERVER_MANAGER_H