1 /* 2 * Copyright (c) 2025 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_FILEMGMT_CLOUD_SYNC_CALLBACK_ANI_H 17 #define OHOS_FILEMGMT_CLOUD_SYNC_CALLBACK_ANI_H 18 19 #include <ani.h> 20 #include <ani_signature_builder.h> 21 22 #include "cloud_sync_callback_middle.h" 23 #include "cloud_optimize_callback_middle.h" 24 #include "data_ability_observer_interface.h" 25 #include "data_ability_observer_stub.h" 26 #include "dataobs_mgr_client.h" 27 #include "securec.h" 28 #include "uri.h" 29 30 namespace OHOS::FileManagement::CloudSync { 31 32 class CloudNotifyObserver; 33 34 struct BundleEntity { 35 std::string bundleName_; BundleEntityBundleEntity36 explicit BundleEntity(const std::string bundleName) : bundleName_(bundleName) {}; 37 }; 38 39 struct CloudChangeListener { 40 NotifyType notifyType; 41 AAFwk::ChangeInfo changeInfo; 42 std::string strUri; 43 }; 44 45 struct RegisterParams { 46 std::string uri; 47 bool recursion; 48 ani_ref cbOnRef; 49 }; 50 51 class ObserverImpl : public AAFwk::DataAbilityObserverStub { 52 public: ObserverImpl(const std::shared_ptr<CloudNotifyObserver> cloudNotifyObserver)53 explicit ObserverImpl(const std::shared_ptr<CloudNotifyObserver> cloudNotifyObserver) 54 : cloudNotifyObserver_(cloudNotifyObserver) {}; 55 void OnChange(); 56 void OnChangeExt(const AAFwk::ChangeInfo &info); 57 static sptr<ObserverImpl> GetObserver(const Uri &uri, const std::shared_ptr<CloudNotifyObserver> &observer); 58 static bool FindObserver(const Uri &uri, const std::shared_ptr<CloudNotifyObserver> &observer); 59 static bool DeleteObserver(const Uri &uri, const std::shared_ptr<CloudNotifyObserver> &observer); 60 61 private: 62 struct ObserverParam { 63 sptr<ObserverImpl> obs_; 64 std::list<Uri> uris_; 65 }; 66 std::shared_ptr<CloudNotifyObserver> cloudNotifyObserver_; 67 static ConcurrentMap<CloudNotifyObserver *, ObserverParam> observers_; 68 }; 69 70 class CloudSyncCallbackAniImpl : public CloudSyncCallbackMiddle, 71 public std::enable_shared_from_this<CloudSyncCallbackAniImpl> { 72 public: 73 CloudSyncCallbackAniImpl(ani_env *env, ani_ref fun); 74 ~CloudSyncCallbackAniImpl() override = default; 75 void OnSyncStateChanged(SyncType type, SyncPromptState state) override; 76 void OnSyncStateChanged(CloudSyncState state, ErrorType error) override; 77 void DeleteReference() override; 78 79 private: 80 void GetSyncProgress(CloudSyncState state, ErrorType error, const ani_class &cls, ani_object &pg); 81 ani_vm *vm_ = nullptr; 82 ani_ref cbOnRef_ = nullptr; 83 }; 84 85 class ChangeListenerAni { 86 public: ChangeListenerAni(ani_env * env)87 explicit ChangeListenerAni(ani_env *env) 88 { 89 env_ = env; 90 } 91 ChangeListenerAni(const ChangeListenerAni & listener)92 ChangeListenerAni(const ChangeListenerAni &listener) 93 { 94 this->env_ = listener.env_; 95 this->cbOnRef_ = listener.cbOnRef_; 96 this->cbOffRef_ = listener.cbOffRef_; 97 this->observers_ = listener.observers_; 98 } 99 100 ChangeListenerAni &operator=(const ChangeListenerAni &listener) 101 { 102 this->env_ = listener.env_; 103 this->cbOnRef_ = listener.cbOnRef_; 104 this->cbOffRef_ = listener.cbOffRef_; 105 this->observers_ = listener.observers_; 106 return *this; 107 } 108 ~ChangeListenerAni()109 ~ChangeListenerAni() {}; 110 void OnChange(CloudChangeListener &listener, const ani_ref cbRef); 111 ani_status GetChangeDataObject( 112 ani_env *env, CloudChangeListener &listener, ani_class cls, ani_object &changeData); 113 ani_status SetIsDir(ani_env *env, const std::shared_ptr<MessageParcel> parcel, ani_object &isDirectory); 114 ani_status SetValueArray(ani_env *env, const std::list<Uri> listValue, ani_object &uris); 115 ani_ref cbOnRef_ = nullptr; 116 ani_ref cbOffRef_ = nullptr; 117 std::vector<std::shared_ptr<CloudNotifyObserver>> observers_; 118 119 private: 120 ani_env *env_; 121 }; 122 123 class CloudNotifyObserver { 124 public: CloudNotifyObserver(const ChangeListenerAni & listObj,std::string uri,ani_ref ref)125 CloudNotifyObserver(const ChangeListenerAni &listObj, std::string uri, ani_ref ref) 126 : listObj_(listObj), uri_(uri), ref_(ref) 127 { 128 } 129 ~CloudNotifyObserver() = default; 130 131 void OnChange(); 132 void OnChangeExt(const AAFwk::ChangeInfo &changeInfo); 133 134 ChangeListenerAni listObj_; 135 std::string uri_; 136 ani_ref ref_; 137 }; 138 139 class CloudOptimizeCallbackAniImpl : public CloudOptimizeCallbackMiddle, 140 public std::enable_shared_from_this<CloudOptimizeCallbackAniImpl> { 141 public: CloudOptimizeCallbackAniImpl(ani_env * env,ani_ref fun)142 CloudOptimizeCallbackAniImpl(ani_env *env, ani_ref fun) : env_(env), cbOnRef_(fun) {} 143 ~CloudOptimizeCallbackAniImpl() = default; 144 void OnOptimizeProcess(const OptimizeState state, const int32_t progress) override; 145 146 private: 147 ani_status GetOptimProgress(ani_env *env, OptimizeState state, int32_t progress, ani_class cls, ani_object &data); 148 ani_env *env_; 149 ani_ref cbOnRef_ = nullptr; 150 }; 151 } // namespace OHOS::FileManagement::CloudSync 152 #endif // OHOS_FILEMGMT_CLOUD_SYNC_CALLBACK_ANI_H