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 OHOS_ABILITY_RUNTIME_JS_QUERY_ERMS_OBSERVER_H 17 #define OHOS_ABILITY_RUNTIME_JS_QUERY_ERMS_OBSERVER_H 18 19 #include <memory> 20 #include <string> 21 #include <vector> 22 23 #include "native_engine/native_engine.h" 24 #include "query_erms_observer_stub.h" 25 #include "js_runtime_utils.h" 26 27 namespace OHOS { 28 namespace AbilityRuntime { 29 struct JsQueryERMSObserverObject { 30 std::string appId; 31 std::string startTime; 32 napi_deferred deferred; 33 }; 34 35 class JsQueryERMSObserver : public QueryERMSObserverStub { 36 public: 37 /** 38 * JsQueryERMSObserver, constructor. 39 * 40 */ 41 explicit JsQueryERMSObserver(napi_env env); 42 43 /** 44 * JsQueryERMSObserver, destructor. 45 * 46 */ 47 ~JsQueryERMSObserver(); 48 49 /** 50 * OnQueryFinished, return free install result. 51 * 52 * @param appId Query ERMS app id. 53 * @param startTime Free install start request time. 54 * @param rule The ERMS query result. 55 * @param resultCode The result code. 56 */ 57 void OnQueryFinished(const std::string &appId, const std::string &startTime, 58 const AtomicServiceStartupRule &rule, int32_t resultCode) override; 59 60 /** 61 * @brief Use for context to add an callback into the observer. 62 * 63 * @param appId Query ERMS app id. 64 * @param startTime The start time. 65 * @param result the promise to return. 66 */ 67 void AddJsObserverObject(const std::string &appId, const std::string &startTime, napi_value* result); 68 69 private: 70 /** 71 * CallPromise, resolve promise. 72 * 73 * @param deferred The promise that is to be resolved. 74 * @param rule The ERMS query result. 75 * @param resultCode The result code. 76 */ 77 void CallPromise(napi_deferred deferred, const AtomicServiceStartupRule &rule, int32_t resultCode); 78 79 /** 80 * HandleOnQueryFinished, handle the event of free install upon finished. 81 * 82 * @param appId Query ERMS app id. 83 * @param startTime The start time. 84 * @param rule The ERMS query result. 85 * @param resultCode The result code. 86 */ 87 void HandleOnQueryFinished(const std::string &appId, const std::string &startTime, 88 const AtomicServiceStartupRule &rule, int32_t resultCode); 89 90 /** 91 * CreateJsAtomicServiceStartupRule, create js atomic service startup rule. 92 * 93 * @param env The env. 94 * @param rule The ERMS query result. 95 */ 96 napi_value CreateJsAtomicServiceStartupRule(napi_env env, const AbilityRuntime::AtomicServiceStartupRule &rule); 97 98 napi_env env_; 99 std::mutex jsObserverObjectListLock_; 100 std::vector<JsQueryERMSObserverObject> jsObserverObjectList_; 101 }; 102 } // namespace AbilityRuntime 103 } // namespace OHOS 104 105 #endif // OHOS_ABILITY_RUNTIME_JS_QUERY_ERMS_OBSERVER_H