1 /* 2 * Copyright (c) 2025 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_ABILITY_START_WITH_WAIT_OBSERVER_MANAGER 17 #define OHOS_ABILITY_RUNTIME_ABILITY_START_WITH_WAIT_OBSERVER_MANAGER 18 19 #include <map> 20 #include <mutex> 21 22 #include "ability_record.h" 23 #include "ability_start_with_wait_observer_stub.h" 24 #include "ability_start_with_wait_observer_utils.h" 25 #include "nocopyable.h" 26 #include "want.h" 27 28 namespace OHOS { 29 namespace AAFwk { 30 using TerminateReason = AbilityStartWithWaitObserverUtil::TerminateReason; 31 struct AbilityForegroundInfo { 32 bool coldStart = false; 33 int32_t observerId = 0; 34 sptr<AbilityStartWithWaitObserverRecipient> deathRecipient = nullptr; 35 int64_t startTime = 0; 36 int64_t foregroundTime = 0; // ability foreground time 37 std::string bundleName; 38 std::string abilityName; 39 }; 40 41 class AbilityStartWithWaitObserverManager { 42 public: 43 static AbilityStartWithWaitObserverManager& GetInstance(); 44 int32_t RegisterObserver(Want &want, sptr<IAbilityStartWithWaitObserver> observer); 45 void UnregisterObserver(sptr<IAbilityStartWithWaitObserver> observer); 46 void NotifyAATerminateWait(Want& want, TerminateReason reason = TerminateReason::TERMINATE_FOR_NONE); 47 void NotifyAATerminateWait(std::shared_ptr<AbilityRecord> abilityRecord, 48 TerminateReason reason = TerminateReason::TERMINATE_FOR_NONE); 49 void SetColdStartForShellCall(std::shared_ptr<AbilityRecord> abilityRecord); 50 51 private: 52 AbilityStartWithWaitObserverManager() = default; 53 ~AbilityStartWithWaitObserverManager() = default; 54 55 sptr<AbilityStartWithWaitObserverRecipient> GenerateDeathRecipient(sptr<IAbilityStartWithWaitObserver> observer); 56 57 std::mutex oberverMutex_; 58 int32_t observerId_ = 0; 59 std::map<sptr<IAbilityStartWithWaitObserver>, AbilityForegroundInfo> observerList_; 60 DISALLOW_COPY_AND_MOVE(AbilityStartWithWaitObserverManager); 61 }; 62 } // namespace AAFwk 63 } // namespace OHOS 64 #endif // OHOS_ABILITY_RUNTIME_ABILITY_START_WITH_WAIT_OBSERVER_MANAGER