1 /* 2 * Copyright (c) 2023 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_FREE_INSTALL_OBSERVER_H 17 #define OHOS_ABILITY_RUNTIME_JS_FREE_INSTALL_OBSERVER_H 18 19 #include <memory> 20 #include <string> 21 #include <vector> 22 23 #include "native_engine/native_engine.h" 24 #include "free_install_observer_stub.h" 25 #include "js_runtime_utils.h" 26 27 namespace OHOS { 28 namespace AbilityRuntime { 29 struct JsFreeInstallObserverObject { 30 std::string bundleName; 31 std::string abilityName; 32 std::string startTime; 33 std::string url; 34 napi_deferred deferred; 35 napi_ref callback; 36 bool isAbilityResult = false; 37 }; 38 39 class JsFreeInstallObserver : public FreeInstallObserverStub { 40 public: 41 explicit JsFreeInstallObserver(napi_env env); 42 ~JsFreeInstallObserver(); 43 44 /** 45 * OnInstallFinished, return free install result. 46 * 47 * @param bundleName Free install bundleName. 48 * @param abilityName Free install abilityName. 49 * @param startTime Free install start request time. 50 * @param resultCode The result of this free install. 51 */ 52 void OnInstallFinished(const std::string &bundleName, const std::string &abilityName, 53 const std::string &startTime, const int &resultCode) override; 54 55 /** 56 * OnInstallFinishedByUrl, return free install result. 57 * 58 * @param startTime Free install start request time. 59 * @param url Free install url. 60 * @param resultCode The result of this free install. 61 */ 62 void OnInstallFinishedByUrl(const std::string &startTime, const std::string &url, 63 const int &resultCode) override; 64 65 /** 66 * OnInstallFinished, return free install result. 67 * 68 * @param bundleName Free install bundleName. 69 * @param abilityName Free install abilityName. 70 * @param startTime Free install start request time. 71 * @param abilityResult The result of this free install. 72 */ 73 void OnInstallFinished(const std::string &bundleName, const std::string &abilityName, 74 const std::string &startTime, napi_value abilityResult); 75 76 /** 77 * @brief Use for context to add an callback into the observer. 78 * 79 * @param bundleName The bundleName of want. 80 * @param abilityName The abilityName of want. 81 * @param startTime The startTime that want requested. 82 * @param jsObserverObject The js object instance. 83 * @param result the promise to return. 84 */ 85 void AddJsObserverObject(const std::string &bundleName, const std::string &abilityName, 86 const std::string &startTime, napi_value jsObserverObject, napi_value* result, bool isAbilityResult = false); 87 88 /** 89 * @brief Use for context to add an callback into the observer. 90 * 91 * @param startTime The startTime that want requested. 92 * @param url Free install url. 93 * @param jsObserverObject The js object instance. 94 * @param result the promise to return. 95 */ 96 void AddJsObserverObject(const std::string &startTime, const std::string &url, 97 napi_value jsObserverObject, napi_value* result, bool isAbilityResult = false); 98 99 private: 100 void CallPromise(napi_deferred deferred, int32_t resultCode); 101 void CallPromise(napi_deferred deferred, napi_value abilityResult); 102 void CallCallback(napi_ref callback, int32_t resultCode); 103 void CallCallback(napi_ref callback, napi_value abilityResult); 104 void HandleOnInstallFinished(const std::string &bundleName, const std::string &abilityName, 105 const std::string &startTime, const int &resultCode); 106 void HandleOnInstallFinishedByUrl(const std::string &startTime, const std::string &url, 107 const int &resultCode); 108 void AddJsObserverCommon(JsFreeInstallObserverObject &object, 109 napi_value jsObserverObject, napi_value* result, bool isAbilityResult); 110 napi_env env_; 111 std::mutex jsObserverObjectListLock_; 112 std::vector<JsFreeInstallObserverObject> jsObserverObjectList_; 113 }; 114 } // namespace AbilityRuntime 115 } // namespace OHOS 116 117 #endif // OHOS_ABILITY_RUNTIME_JS_FREE_INSTALL_OBSERVER_H