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 #include "ipc/cloud_sync_callback_manager.h"
17 #include "cloud_sync_constants.h"
18 #include "dfs_error.h"
19 #include "utils_log.h"
20
21 namespace OHOS::FileManagement::CloudSync {
22 using namespace std;
23 using namespace placeholders;
24 static const string GALLERY_BUNDLE_NAME = "com.ohos.photos";
25
GetInstance()26 CloudSyncCallbackManager &CloudSyncCallbackManager::GetInstance()
27 {
28 static CloudSyncCallbackManager instance;
29 return instance;
30 }
31
Convert2Bundle(const string & bundleName,string & bundle)32 static void Convert2Bundle(const string &bundleName, string &bundle)
33 {
34 string photo = ".photos";
35 if (bundleName.size() > photo.size() && (bundleName.substr(bundleName.size() - photo.size()) == photo)) {
36 bundle = GALLERY_BUNDLE_NAME;
37 }
38 }
39
RemoveCallback(const std::string & bundleName)40 void CloudSyncCallbackManager::RemoveCallback(const std::string &bundleName)
41 {
42 CallbackInfo cbInfo;
43 string bundle = bundleName;
44 Convert2Bundle(bundleName, bundle);
45 if (callbackListMap_.Find(bundle, cbInfo)) {
46 auto remoteObject = cbInfo.callbackProxy->AsObject();
47 remoteObject->RemoveDeathRecipient(cbInfo.deathRecipient);
48 callbackListMap_.Erase(bundle);
49 }
50 }
51
AddCallback(const std::string & bundleName,const int32_t userId,const sptr<ICloudSyncCallback> & callback)52 void CloudSyncCallbackManager::AddCallback(const std::string &bundleName,
53 const int32_t userId,
54 const sptr<ICloudSyncCallback> &callback)
55 {
56 CallbackInfo callbackInfo;
57 string bundle = bundleName;
58 Convert2Bundle(bundleName, bundle);
59 if (callbackListMap_.Find(bundle, callbackInfo)) {
60 auto remoteObject = callbackInfo.callbackProxy->AsObject();
61 remoteObject->RemoveDeathRecipient(callbackInfo.deathRecipient);
62 }
63 callbackInfo.callbackProxy = callback;
64 callbackInfo.callerUserId = userId;
65 SetDeathRecipient(bundle, callbackInfo);
66 /*Delete and then insert when the key exists, ensuring that the final value is inserted.*/
67 callbackListMap_.EnsureInsert(bundle, callbackInfo);
68 }
69
SetDeathRecipient(const std::string & bundleName,CallbackInfo & cbInfo)70 void CloudSyncCallbackManager::SetDeathRecipient(const std::string &bundleName, CallbackInfo &cbInfo)
71 {
72 auto remoteObject = cbInfo.callbackProxy->AsObject();
73 if (!remoteObject) {
74 LOGE("Remote object can't be nullptr");
75 return;
76 }
77
78 auto deathCb = [this, bundleName{bundleName}](const wptr<IRemoteObject> &obj) {
79 callbackListMap_.Erase(bundleName);
80 LOGE("client died, map size:%{public}d, bundleName:%{private}s", callbackListMap_.Size(), bundleName.c_str());
81 };
82 cbInfo.deathRecipient = sptr(new SvcDeathRecipient(deathCb));
83 remoteObject->AddDeathRecipient(cbInfo.deathRecipient);
84 }
85
Notify(const std::string bundleName,CallbackInfo & callbackInfo,const CloudSyncState state,const ErrorType error)86 void CloudSyncCallbackManager::Notify(const std::string bundleName,
87 CallbackInfo &callbackInfo,
88 const CloudSyncState state,
89 const ErrorType error)
90 {
91 auto callbackProxy = callbackInfo.callbackProxy;
92 if (!callbackProxy) {
93 LOGE("not found object, bundleName = %{private}s", bundleName.c_str());
94 return;
95 }
96 callbackProxy->OnSyncStateChanged(state, error);
97 }
98
NotifySyncStateChanged(const std::string & bundleName,const CloudSyncState state,const ErrorType error)99 void CloudSyncCallbackManager::NotifySyncStateChanged(const std::string &bundleName,
100 const CloudSyncState state, const ErrorType error)
101 {
102 auto callback = bind(&CloudSyncCallbackManager::Notify, this, _1, _2, state, error);
103 CallbackInfo callbackInfo;
104 if (!callbackListMap_.Find(bundleName, callbackInfo)) {
105 LOGE("not found %{private}s in callbackList", bundleName.c_str());
106 return;
107 }
108 callback(bundleName, callbackInfo);
109 }
110 } // namespace OHOS::FileManagement::CloudSync
111