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 <vector> 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(const sptr<IFreeInstallObserver> &observer); 34 35 int32_t RemoveObserver(const sptr<IFreeInstallObserver> &observer); 36 37 void OnInstallFinished(const std::string &bundleName, const std::string &abilityName, 38 const std::string &startTime, const int &resultCode); 39 40 private: 41 bool ObserverExistLocked(const sptr<IFreeInstallObserver> &observer); 42 43 void OnObserverDied(const wptr<IRemoteObject> &remote); 44 45 void HandleOnInstallFinished(const std::string &bundleName, const std::string &abilityName, 46 const std::string &startTime, const int &resultCode); 47 48 ffrt::mutex observerLock_; 49 sptr<IRemoteObject::DeathRecipient> deathRecipient_; 50 std::vector<sptr<IFreeInstallObserver>> observerList_; 51 }; 52 53 class FreeInstallObserverRecipient : public IRemoteObject::DeathRecipient { 54 public: 55 using RemoteDiedHandler = std::function<void(const wptr<IRemoteObject> &)>; 56 explicit FreeInstallObserverRecipient(RemoteDiedHandler handler); 57 virtual ~FreeInstallObserverRecipient(); 58 virtual void OnRemoteDied(const wptr<IRemoteObject> &remote); 59 60 private: 61 RemoteDiedHandler handler_; 62 }; 63 } // namespace AAFwk 64 } // namespace OHOS 65 #endif // OHOS_ABILITY_RUNTIME_FREE_INSTALL_OBSERVER_MANAGER_H