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 CallJsFunction(const napi_value value, const char *methodName, const napi_value *argv, const size_t argc); 34 void AddJsObserverObject(const napi_value &jsObserverObject); 35 void RemoveJsObserverObject(const napi_value &jsObserverObject); 36 void RemoveAllJsObserverObjects(); 37 std::shared_ptr<NativeReference> GetObserverObject(const napi_value &jsObserverObject); 38 bool IsEmpty(); 39 void SubscriberBgtaskSaStatusChange(); 40 void UnSubscriberBgtaskSaStatusChange(); 41 42 private: 43 class JsBackgroudTaskSystemAbilityStatusChange : public SystemAbilityStatusChangeStub { 44 public: 45 explicit JsBackgroudTaskSystemAbilityStatusChange(std::shared_ptr<JsBackgroundTaskSubscriber> subscriber_); 46 virtual ~JsBackgroudTaskSystemAbilityStatusChange(); 47 void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 48 void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 49 private: 50 std::weak_ptr<JsBackgroundTaskSubscriber> subscriber_; 51 }; 52 53 private: 54 napi_env env_; 55 std::mutex jsObserverObjectSetLock_; 56 std::set<std::shared_ptr<NativeReference>> jsObserverObjectSet_ {}; 57 sptr<JsBackgroudTaskSystemAbilityStatusChange> jsSaListner_ = nullptr; 58 std::atomic<bool> needRestoreSubscribeStatus_ = false; 59 }; // JsBackgroundTaskSubscriber 60 } // BackgroundTaskMgr 61 } // OHOS 62 #endif // BACKGROUND_TASK_MGR_JS_BACKGROUND_SUBSCRIBER_H