1 /* 2 * Copyright (c) 2022 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 ACCESS_APP_MANAGER_ACCESS_CLIENT_H 17 #define ACCESS_APP_MANAGER_ACCESS_CLIENT_H 18 19 #include <mutex> 20 #include <string> 21 22 #include "app_status_change_callback.h" 23 #include "app_manager_death_callback.h" 24 #include "app_manager_death_recipient.h" 25 #include "app_manager_access_proxy.h" 26 #include "nocopyable.h" 27 28 namespace OHOS { 29 namespace Security { 30 namespace AccessToken { 31 class AppMgrDeathRecipientObserver : public IRemoteBroker { 32 public: 33 DECLARE_INTERFACE_DESCRIPTOR(u"ohos.appexecfwk.IApplicationStateObserver"); 34 35 virtual void OnForegroundApplicationChanged(const AppStateData &appStateData) = 0; 36 virtual void OnProcessDied(const ProcessData &processData) = 0; 37 38 enum class Message { 39 TRANSACT_ON_FOREGROUND_APPLICATION_CHANGED = 0, 40 TRANSACT_ON_PROCESS_DIED = 5, 41 }; 42 }; 43 44 class AppManagerAccessClient final { 45 public: 46 static AppManagerAccessClient& GetInstance(); 47 virtual ~AppManagerAccessClient(); 48 49 int32_t RegisterApplicationStateObserver(const sptr<IApplicationStateObserver>& observer); 50 int32_t UnregisterApplicationStateObserver(const sptr<IApplicationStateObserver>& observer); 51 int32_t GetForegroundApplications(std::vector<AppStateData>& list); 52 void RegisterDeathCallbak(const std::shared_ptr<AppManagerDeathCallback>& callback); 53 void OnRemoteDiedHandle(); 54 55 private: 56 AppManagerAccessClient(); 57 DISALLOW_COPY_AND_MOVE(AppManagerAccessClient); 58 59 void InitProxy(); 60 sptr<IAppMgr> GetProxy(); 61 62 sptr<AppMgrDeathRecipient> serviceDeathObserver_ = nullptr; 63 std::mutex proxyMutex_; 64 std::mutex deathCallbackMutex_; 65 sptr<IAppMgr> proxy_ = nullptr; 66 std::vector<std::shared_ptr<AppManagerDeathCallback>> appManagerDeathCallbackList_; 67 }; 68 } // namespace AccessToken 69 } // namespace Security 70 } // namespace OHOS 71 #endif // ACCESS_APP_MANAGER_ACCESS_CLIENT_H 72 73