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 #ifndef ADVANCED_UI_COMPONENT_NAVPUSHPATHHELPER_INCLUDE_SILENT_INSTALL_CALLBACK_H 16 #define ADVANCED_UI_COMPONENT_NAVPUSHPATHHELPER_INCLUDE_SILENT_INSTALL_CALLBACK_H 17 18 #include "atomic_service_status_callback.h" 19 #include "errors.h" 20 #include "iremote_broker.h" 21 #include "iremote_object.h" 22 #include "iremote_stub.h" 23 #include "base/log/log.h" 24 #include "want_params_wrapper.h" 25 #include "want.h" 26 27 namespace OHOS::NavPushPathHelper { 28 constexpr int32_t SILENT_INSTALL_SUCCESS = 0; 29 constexpr int32_t SILENT_INSTALL_FAIL_CODE = 300001; 30 constexpr char SILENT_INSTALL_FAIL_MESSAGE[] = "hsp silent install fail"; 31 32 /** 33 * @class IAtomicServiceStatusCallback 34 * IAtomicServiceStatusCallback is used to notify caller ability that free install is complete. 35 */ 36 class IAtomicServiceStatusCallback : public OHOS::IRemoteBroker { 37 public: 38 DECLARE_INTERFACE_DESCRIPTOR(u"ohos.IAtomicServiceStatusCallback"); 39 40 /** 41 * @brief OnActionEvent. 42 */ 43 virtual int32_t OnActionEvent() = 0; 44 /** 45 * @brief OnError. 46 * @param code The code. 47 * @param msg The msg. 48 */ 49 virtual int32_t OnError(int32_t code, const std::string& msg) = 0; 50 }; 51 52 /** 53 * @class AtomicServiceStatusCallbackStub 54 * AtomicServiceStatusCallbackStub. 55 */ 56 class AtomicServiceStatusCallbackStub : public OHOS::IRemoteStub<IAtomicServiceStatusCallback> { 57 public: AtomicServiceStatusCallbackStub()58 AtomicServiceStatusCallbackStub() 59 { 60 handleOnActionEventFunc_ = &AtomicServiceStatusCallbackStub::HandleOnActionEvent; 61 handleOnErrorFunc_ = &AtomicServiceStatusCallbackStub::HandleOnError; 62 } ~AtomicServiceStatusCallbackStub()63 ~AtomicServiceStatusCallbackStub() override 64 { 65 handleOnActionEventFunc_ = nullptr; 66 handleOnErrorFunc_ = nullptr; 67 } 68 OnRemoteRequest(uint32_t code,OHOS::MessageParcel & data,OHOS::MessageParcel & reply,OHOS::MessageOption & option)69 int32_t OnRemoteRequest(uint32_t code, OHOS::MessageParcel &data, OHOS::MessageParcel &reply, 70 OHOS::MessageOption &option) override 71 { 72 TAG_LOGD(OHOS::Ace::AceLogTag::ACE_DEFAULT_DOMAIN, 73 "AtomicServiceStatusCallbackStub::OnReceived, code = %{public}u, flags= %{public}d.", 74 code, option.GetFlags()); 75 std::u16string descriptor = AtomicServiceStatusCallbackStub::GetDescriptor(); 76 std::u16string remoteDescriptor = data.ReadInterfaceToken(); 77 if (descriptor != remoteDescriptor) { 78 TAG_LOGE(OHOS::Ace::AceLogTag::ACE_DEFAULT_DOMAIN, 79 "%{public}s failed, local descriptor is not equal to remote", __func__); 80 return OHOS::ERR_INVALID_VALUE; 81 } 82 83 auto resultCode = data.ReadInt32(); 84 if (resultCode == SILENT_INSTALL_SUCCESS) { 85 if (handleOnActionEventFunc_ != nullptr) { 86 return (this->*handleOnActionEventFunc_)(); 87 } 88 } 89 90 if (resultCode < SILENT_INSTALL_SUCCESS) { 91 if (handleOnErrorFunc_ != nullptr) { 92 return (this->*handleOnErrorFunc_)(); 93 } 94 } 95 return IPCObjectStub::OnRemoteRequest(code, data, reply, option); 96 } 97 98 private: HandleOnActionEvent()99 int32_t HandleOnActionEvent() 100 { 101 return OnActionEvent(); 102 } HandleOnError()103 int32_t HandleOnError() 104 { 105 return OnError(SILENT_INSTALL_FAIL_CODE, SILENT_INSTALL_FAIL_MESSAGE); 106 } 107 108 using HandleOnActionEventFunc = int32_t (AtomicServiceStatusCallbackStub::*)(); 109 HandleOnActionEventFunc handleOnActionEventFunc_; 110 111 using HandleOnErrorFunc = int32_t (AtomicServiceStatusCallbackStub::*)(); 112 HandleOnErrorFunc handleOnErrorFunc_; 113 114 DISALLOW_COPY_AND_MOVE(AtomicServiceStatusCallbackStub); 115 }; 116 117 /** 118 * @class AtomicServiceStatusCallback 119 * AtomicServiceStatusCallback. 120 */ 121 class AtomicServiceStatusCallback : public AtomicServiceStatusCallbackStub { 122 public: 123 AtomicServiceStatusCallback() = default; 124 ~AtomicServiceStatusCallback() override = default; 125 126 /** 127 * @brief OnActionEvent. 128 */ OnActionEvent()129 int32_t OnActionEvent() override 130 { 131 if (!actionEventHandler_) { 132 TAG_LOGE(OHOS::Ace::AceLogTag::ACE_DEFAULT_DOMAIN, "actionEventHandler_ is null."); 133 return OHOS::ERR_INVALID_VALUE; 134 } 135 actionEventHandler_(); 136 return OHOS::ERR_OK; 137 } 138 /** 139 * @brief OnError. 140 * @param code The code. 141 * @param msg The msg. 142 */ OnError(int32_t code,const std::string & msg)143 int32_t OnError(int32_t code, const std::string& msg) override 144 { 145 TAG_LOGI(OHOS::Ace::AceLogTag::ACE_DEFAULT_DOMAIN, "OnError code: %{public}d, msg: %{public}s", 146 code, msg.c_str()); 147 if (!errorEventHandler_) { 148 TAG_LOGE(OHOS::Ace::AceLogTag::ACE_DEFAULT_DOMAIN, "errorEventHandler_ is null"); 149 return OHOS::ERR_INVALID_VALUE; 150 } 151 152 errorEventHandler_(code, msg); 153 return OHOS::ERR_OK; 154 } 155 SetActionEventHandler(const std::function<void ()> & listener)156 void SetActionEventHandler(const std::function<void()>& listener) 157 { 158 actionEventHandler_ = listener; 159 } SetErrorEventHandler(const std::function<void (int32_t,const std::string &)> & listener)160 void SetErrorEventHandler(const std::function<void(int32_t, const std::string&)>& listener) 161 { 162 errorEventHandler_ = listener; 163 } 164 165 private: 166 std::function<void()> actionEventHandler_; 167 std::function<void(int32_t, const std::string&)> errorEventHandler_; 168 }; 169 } 170 #endif