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 "data_sync_notifier.h"
17
18 #include <mutex>
19 #include "dataobs_mgr_client.h"
20 #include "data_sync_const.h"
21 #include "dfs_error.h"
22 #include "utils_log.h"
23 #include "uri.h"
24
25 namespace OHOS::FileManagement::CloudSync {
26 using namespace std;
27 using ChangeType = AAFwk::ChangeInfo::ChangeType;
28 using NotifyDataMap = unordered_map<ChangeType, list<Uri>>;
29 unordered_map<ChangeType, list<Uri>> DataSyncNotifier::notifyListMap_ = {};
30 mutex DataSyncNotifier::mtx_{};
31 constexpr int NOTIFY_INTERVALS = 10;
32
GetInstance()33 DataSyncNotifier &DataSyncNotifier::GetInstance()
34 {
35 static DataSyncNotifier instance;
36 return instance;
37 }
38
TryNotifyChange()39 static int32_t TryNotifyChange()
40 {
41 static int32_t recordAdded = 0;
42 recordAdded++;
43 if (recordAdded % NOTIFY_INTERVALS == 0) {
44 auto obsMgrClient = AAFwk::DataObsMgrClient::GetInstance();
45 if (obsMgrClient == nullptr) {
46 LOGE("%{public}s obsMgrClient is nullptr", __func__);
47 return E_SA_LOAD_FAILED;
48 }
49 for (auto it = DataSyncNotifier::notifyListMap_.begin();
50 it != DataSyncNotifier::notifyListMap_.end(); ++it) {
51 obsMgrClient->NotifyChangeExt({it->first, it->second});
52 }
53 DataSyncNotifier::notifyListMap_.clear();
54 recordAdded = 0;
55 }
56 return E_OK;
57 }
58
NotifyFileAssetChange(const string & uri,const ChangeType changeType)59 static int32_t NotifyFileAssetChange(const string &uri, const ChangeType changeType)
60 {
61 std::lock_guard<mutex> lock(DataSyncNotifier::mtx_);
62 auto iterator = DataSyncNotifier::notifyListMap_.find(changeType);
63 if (iterator != DataSyncNotifier::notifyListMap_.end()) {
64 iterator ->second.emplace_back(Uri(uri));
65 } else {
66 list<Uri> newList;
67 newList.emplace_back(Uri(uri));
68 DataSyncNotifier::notifyListMap_.insert(make_pair(changeType, newList));
69 }
70 return TryNotifyChange();
71 }
72
NotifyAlbumChange(const string & uri,const ChangeType changeType)73 static int32_t NotifyAlbumChange(const string &uri, const ChangeType changeType)
74 {
75 return NotifyFileAssetChange(uri, changeType);
76 }
77
NotifyAlbumMapChange(const string & uri,const ChangeType changeType,const string & fileAssetId)78 static int32_t NotifyAlbumMapChange(const string &uri, const ChangeType changeType, const string &fileAssetId)
79 {
80 return E_OK;
81 }
82
TryNotify(const string & uri,const ChangeType changeType,const string & fileAssetId)83 int32_t DataSyncNotifier::TryNotify(const string &uri, const ChangeType changeType, const string &fileAssetId)
84 {
85 int ret = E_NOTIFY;
86 if (uri.find(DataSyncConst::PHOTO_URI_PREFIX) != string::npos) {
87 ret = NotifyFileAssetChange(uri, changeType);
88 } else if (uri.find(DataSyncConst::ALBUM_URI_PREFIX) != string::npos && fileAssetId == DataSyncConst::INVALID_ID) {
89 ret = NotifyAlbumChange(uri, changeType);
90 } else {
91 ret = NotifyAlbumMapChange(uri, changeType, fileAssetId);
92 }
93 return ret;
94 }
95
FinalNotify()96 int32_t DataSyncNotifier::FinalNotify()
97 {
98 auto obsMgrClient = AAFwk::DataObsMgrClient::GetInstance();
99 if (obsMgrClient == nullptr) {
100 LOGE("%{public}s obsMgrClient is nullptr", __func__);
101 return E_SA_LOAD_FAILED;
102 }
103 std::lock_guard<mutex> lock(DataSyncNotifier::mtx_);
104 for (auto it = DataSyncNotifier::notifyListMap_.begin();
105 it != DataSyncNotifier::notifyListMap_.end(); ++it) {
106 obsMgrClient->NotifyChangeExt({it->first, it->second});
107 }
108 DataSyncNotifier::notifyListMap_.clear();
109 return E_OK;
110 }
111 } // namespace OHOS::FileManagement::CloudSync