• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
GetInstance()25 CloudSyncCallbackManager &CloudSyncCallbackManager::GetInstance()
26 {
27     static CloudSyncCallbackManager instance;
28     return instance;
29 }
30 
RemoveCallback(const std::string & bundleName)31 void CloudSyncCallbackManager::RemoveCallback(const std::string &bundleName)
32 {
33     CallbackInfo cbInfo;
34     if (callbackListMap_.Find(bundleName, cbInfo)) {
35         auto remoteObject = cbInfo.callbackProxy->AsObject();
36         remoteObject->RemoveDeathRecipient(cbInfo.deathRecipient);
37         callbackListMap_.Erase(bundleName);
38     }
39 }
40 
AddCallback(const std::string & bundleName,const int32_t userId,const sptr<ICloudSyncCallback> & callback)41 void CloudSyncCallbackManager::AddCallback(const std::string &bundleName,
42                                            const int32_t userId,
43                                            const sptr<ICloudSyncCallback> &callback)
44 {
45     CallbackInfo callbackInfo;
46     if (callbackListMap_.Find(bundleName, callbackInfo)) {
47         auto remoteObject = callbackInfo.callbackProxy->AsObject();
48         remoteObject->RemoveDeathRecipient(callbackInfo.deathRecipient);
49     }
50     callbackInfo.callbackProxy = callback;
51     callbackInfo.callerUserId = userId;
52     SetDeathRecipient(bundleName, callbackInfo);
53     /*Delete and then insert when the key exists, ensuring that the final value is inserted.*/
54     callbackListMap_.EnsureInsert(bundleName, callbackInfo);
55 }
56 
SetDeathRecipient(const std::string & bundleName,CallbackInfo & cbInfo)57 void CloudSyncCallbackManager::SetDeathRecipient(const std::string &bundleName, CallbackInfo &cbInfo)
58 {
59     auto remoteObject = cbInfo.callbackProxy->AsObject();
60     if (!remoteObject) {
61         LOGE("Remote object can't be nullptr");
62         return;
63     }
64 
65     auto deathCb = [this, bundleName{bundleName}](const wptr<IRemoteObject> &obj) {
66         callbackListMap_.Erase(bundleName);
67         LOGE("client died, map size:%{public}d, bundleName:%{private}s", callbackListMap_.Size(), bundleName.c_str());
68     };
69     cbInfo.deathRecipient = sptr(new SvcDeathRecipient(deathCb));
70     remoteObject->AddDeathRecipient(cbInfo.deathRecipient);
71 }
72 
Notify(const std::string bundleName,CallbackInfo & callbackInfo,const CloudSyncState state,const ErrorType error)73 void CloudSyncCallbackManager::Notify(const std::string bundleName,
74                                       CallbackInfo &callbackInfo,
75                                       const CloudSyncState state,
76                                       const ErrorType error)
77 {
78     auto callbackProxy = callbackInfo.callbackProxy;
79     if (!callbackProxy) {
80         LOGE("not found object, bundleName = %{private}s", bundleName.c_str());
81         return;
82     }
83     callbackProxy->OnSyncStateChanged(state, error);
84 }
85 
NotifySyncStateChanged(const CloudSyncState state,const ErrorType error)86 void CloudSyncCallbackManager::NotifySyncStateChanged(const CloudSyncState state, const ErrorType error)
87 {
88     auto callback = bind(&CloudSyncCallbackManager::Notify, this, _1, _2, state, error);
89     callbackListMap_.Iterate(callback);
90 }
91 } // namespace OHOS::FileManagement::CloudSync
92