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_FORM_FWK_FORM_OBSERVER_RECORD_H 17 #define OHOS_FORM_FWK_FORM_OBSERVER_RECORD_H 18 19 #include <map> 20 #include <singleton.h> 21 #include <vector> 22 23 #include "fms_log_wrapper.h" 24 #include "iremote_object.h" 25 #include "running_form_info.h" 26 27 namespace OHOS { 28 namespace AppExecFwk { 29 enum class FormEventId { 30 FORM_EVENT_NON, 31 FORM_EVENT_CALL, 32 FORM_EVENT_MESSAGE, 33 FORM_EVENT_ROUTER, 34 FORM_EVENT_FORM_ADD, 35 FORM_EVENT_FORM_REMOVE 36 }; 37 38 class FormObserverRecordInner final { 39 public: FormObserverRecordInner(const sptr<IRemoteObject> & remote)40 FormObserverRecordInner(const sptr<IRemoteObject> &remote) : remote_(remote) {} ~FormObserverRecordInner()41 ~FormObserverRecordInner() {} 42 bool IsFollowEvents(FormEventId type) const; 43 void PushEvent(FormEventId type); 44 void RemoveEvent(FormEventId type); 45 GetRemote()46 sptr<IRemoteObject> GetRemote() const 47 { 48 return remote_; 49 }; 50 NonFollowEvents()51 bool NonFollowEvents() 52 { 53 return eventGroup_.empty(); 54 } 55 BindHostBundle()56 std::string BindHostBundle() const 57 { 58 return bindHostBundle; 59 } 60 SetBindHostBundle(const std::string & hostBundleName)61 void SetBindHostBundle(const std::string &hostBundleName) 62 { 63 bindHostBundle = hostBundleName; 64 } 65 66 bool operator==(const FormObserverRecordInner &other) 67 { 68 return (remote_ == other.GetRemote() && bindHostBundle == other.BindHostBundle()); 69 } 70 private: 71 std::string bindHostBundle; 72 sptr<IRemoteObject> remote_ {nullptr}; 73 std::vector<FormEventId> eventGroup_; 74 }; 75 76 /** 77 * @class FormObserverRecord 78 * Form observer record. 79 */ 80 class FormObserverRecord : public DelayedRefSingleton<FormObserverRecord> { 81 public: 82 /** 83 * @brief Set value of deathRecipient_. 84 * @param callerToken Caller ability token. 85 * @param deathRecipient DeathRecipient object. 86 */ 87 void SetDeathRecipient(const sptr<IRemoteObject> &callerToken, 88 const sptr<IRemoteObject::DeathRecipient> &deathRecipient); 89 90 /** 91 * @brief set form add observer. 92 * @param bundleName BundleName of the form host. 93 * @param callerToken Caller ability token. 94 * @return Returns ERR_OK if this function is successfully called; returns others failed. 95 */ 96 ErrCode SetFormAddObserver(const std::string bundleName, const sptr<IRemoteObject> &callerToken); 97 98 /** 99 * @brief set form remove observer. 100 * @param bundleName BundleName of the form host. 101 * @param callerToken Caller ability token. 102 * @return Returns ERR_OK if this function is successfully called; returns others failed. 103 */ 104 ErrCode SetFormRemoveObserver(const std::string bundleName, const sptr<IRemoteObject> &callerToken); 105 106 /** 107 * @brief send form add message to form observer. 108 * @param bundleName BundleName of the form host. 109 */ 110 void onFormAdd(const std::string bundleName, RunningFormInfo &runningFormInfo); 111 112 /** 113 * @brief send form remove message to form observer. 114 * @param bundleName BundleName of the form host. 115 */ 116 void onFormRemove(const std::string bundleName, const RunningFormInfo runningFormInfo); 117 118 /** 119 * @brief when form observer died clean the resource. 120 * @param remote remote object. 121 */ 122 void CleanResource(const wptr<IRemoteObject> &remote); 123 124 /** 125 * @brief Notify form event. 126 * @param bundleName BundleName of the form host. 127 * @param formEventType Form event type. 128 * @param runningFormInfo Current form data. 129 * @return Returns ERR_OK on success, others on failure. 130 */ 131 void HandleFormEvent( 132 const std::string &bundleName, const std::string &formEventType, RunningFormInfo &runningFormInfo); 133 134 /** 135 * @brief Register form event callback observer. 136 * @param bundleName BundleName of the form host. 137 * @param formEventType Form event type. 138 * @param callerToken Caller ability token. 139 * @return Returns ERR_OK on success, others on failure. 140 */ 141 ErrCode SetFormEventObserver( 142 const std::string &bundleName, const std::string &formEventType, const sptr<IRemoteObject> &callerToken); 143 144 /** 145 * @brief Remove form event callback observer. 146 * @param bundleName BundleName of the form host. 147 * @param formEventType Form event type. 148 * @param callerToken Caller ability token. 149 * @return Returns ERR_OK on success, others on failure. 150 */ 151 ErrCode RemoveFormEventObserver( 152 const std::string &bundleName, const std::string &formEventType, const sptr<IRemoteObject> &callerToken); 153 154 private: 155 FormEventId ConvertToFormEventId(const std::string &formEventType); 156 157 void NotifyFormEvent( 158 const FormObserverRecordInner &recordInner, FormEventId formEventId, RunningFormInfo &runningFormInfo, 159 const std::string &formEventType); 160 161 ErrCode SetFormEventObserverLocked( 162 const std::string &bundleName, FormEventId formEventType, const sptr<IRemoteObject> &callerToken); 163 164 ErrCode RemoveFormEventObserverLocked( 165 const std::string &bundleName, FormEventId formEventType, const sptr<IRemoteObject> &callerToken); 166 167 void ClearDeathRemoteObserver(const wptr<IRemoteObject> &remote); 168 169 private: 170 mutable std::mutex formAddObserverMutex_; 171 mutable std::mutex formRemoveObserverMutex_; 172 mutable std::mutex deathRecipientsMutex_; 173 std::map<std::string, std::vector<sptr<IRemoteObject>>> formAddObservers_; 174 std::map<std::string, std::vector<sptr<IRemoteObject>>> formRemoveObservers_; 175 std::map<sptr<IRemoteObject>, sptr<IRemoteObject::DeathRecipient>> deathRecipients_; 176 mutable std::mutex formEventObserversMutex_; 177 std::unordered_map<std::string, std::vector<FormObserverRecordInner>> formEventObservers_; 178 179 const std::unordered_map<std::string, FormEventId> formEventMap = { 180 {"call", FormEventId::FORM_EVENT_CALL}, 181 {"message", FormEventId::FORM_EVENT_MESSAGE}, 182 {"router", FormEventId::FORM_EVENT_ROUTER}, 183 {"formAdd", FormEventId::FORM_EVENT_FORM_ADD}, 184 {"formRemove", FormEventId::FORM_EVENT_FORM_REMOVE}, 185 }; 186 187 /** 188 * @class ClientDeathRecipient 189 * notices IRemoteBroker died. 190 */ 191 class ClientDeathRecipient : public IRemoteObject::DeathRecipient { 192 public: 193 /** 194 * @brief Constructor 195 */ 196 ClientDeathRecipient() = default; 197 virtual ~ClientDeathRecipient() = default; 198 /** 199 * @brief handle remote object died event. 200 * @param remote remote object. 201 */ 202 void OnRemoteDied(const wptr<IRemoteObject> &remote) override; 203 }; 204 }; 205 } // namespace AppExecFwk 206 } // namespace OHOS 207 208 #endif // OHOS_FORM_FWK_FORM_OBSERVER_RECORD_H 209