1 /* 2 * Copyright (c) 2024 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 BACKGROUND_TASK_MGR_JS_BACKGROUND_SUBSCRIBER_H 17 #define BACKGROUND_TASK_MGR_JS_BACKGROUND_SUBSCRIBER_H 18 19 #include <memory> 20 #include <atomic> 21 #include "background_task_mgr_helper.h" 22 #include "system_ability_status_change_stub.h" 23 24 namespace OHOS { 25 namespace BackgroundTaskMgr { 26 class JsBackgroundTaskSubscriber : public OHOS::BackgroundTaskMgr::BackgroundTaskSubscriber, 27 public std::enable_shared_from_this<JsBackgroundTaskSubscriber> { 28 public: 29 explicit JsBackgroundTaskSubscriber(napi_env env); 30 virtual ~JsBackgroundTaskSubscriber(); 31 void OnContinuousTaskStop(const std::shared_ptr<ContinuousTaskCallbackInfo> &continuousTaskCallbackInfo) override; 32 void HandleOnContinuousTaskStop(const std::shared_ptr<ContinuousTaskCallbackInfo> &continuousTaskCallbackInfo); 33 void OnContinuousTaskSuspend( 34 const std::shared_ptr<ContinuousTaskCallbackInfo> &continuousTaskCallbackInfo) override; 35 void HandleOnContinuousTaskSuspend(const std::shared_ptr<ContinuousTaskCallbackInfo> &continuousTaskCallbackInfo); 36 void OnContinuousTaskActive( 37 const std::shared_ptr<ContinuousTaskCallbackInfo> &continuousTaskCallbackInfo) override; 38 void HandleOnContinuousTaskActive(const std::shared_ptr<ContinuousTaskCallbackInfo> &continuousTaskCallbackInfo); 39 void CallJsFunction(const napi_value value, const char *methodName, const napi_value *argv, const size_t argc); 40 void AddJsObserverObject(const std::string cbType, const napi_value &jsObserverObject); 41 void RemoveJsObserverObject(const std::string cbType, const napi_value &jsObserverObject); 42 void RemoveJsObserverObjects(const std::string cbType); 43 std::shared_ptr<NativeReference> GetObserverObject(const std::string cbType, const napi_value &jsObserverObject); 44 bool IsEmpty(); 45 bool IsTypeEmpty(const std::string &cbType); 46 void SubscriberBgtaskSaStatusChange(); 47 void UnSubscriberBgtaskSaStatusChange(); 48 void SetFlag(uint32_t flag, bool isSubscriber); 49 void GetFlag(int32_t &flag) override; 50 51 private: 52 class JsBackgroudTaskSystemAbilityStatusChange : public SystemAbilityStatusChangeStub { 53 public: 54 explicit JsBackgroudTaskSystemAbilityStatusChange(std::shared_ptr<JsBackgroundTaskSubscriber> subscriber_); 55 virtual ~JsBackgroudTaskSystemAbilityStatusChange(); 56 void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 57 void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 58 private: 59 std::weak_ptr<JsBackgroundTaskSubscriber> subscriber_; 60 }; 61 62 private: 63 napi_env env_; 64 std::mutex jsObserverObjectSetLock_; 65 std::map<std::string, std::set<std::shared_ptr<NativeReference>>> jsObserverObjectMap_; 66 sptr<JsBackgroudTaskSystemAbilityStatusChange> jsSaListner_ = nullptr; 67 std::atomic<bool> needRestoreSubscribeStatus_ = false; 68 std::mutex flagLock_; 69 }; // JsBackgroundTaskSubscriber 70 } // BackgroundTaskMgr 71 } // OHOS 72 #endif // BACKGROUND_TASK_MGR_JS_BACKGROUND_SUBSCRIBER_H