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 "sync_rule/cloud_status.h"
17
18 #include "dfs_error.h"
19 #include "drive_kit.h"
20 #include "utils_log.h"
21
22 namespace OHOS::FileManagement::CloudSync {
23 using namespace DriveKit;
GetCurrentCloudInfo(const std::string & bundleName,const int32_t userId)24 int32_t CloudStatus::GetCurrentCloudInfo(const std::string &bundleName, const int32_t userId)
25 {
26 auto driveKit = DriveKit::DriveKitNative::GetInstance(userId);
27 if (driveKit == nullptr) {
28 LOGE("sdk helper get drive kit instance fail");
29 return E_CLOUD_SDK;
30 }
31
32 auto err = driveKit->GetCloudUserInfo(userInfo_);
33 if (err.HasError()) {
34 LOGE("GetCloudUserInfo failed, server err:%{public}d and dk err:%{public}d", err.serverErrorCode,
35 err.dkErrorCode);
36 return E_CLOUD_SDK;
37 }
38
39 if (!((userInfo_.cloudStatus == DKCloudStatus::DK_CLOUD_STATUS_LOGIN) &&
40 (userInfo_.spaceStatus == DKSpaceStatus::DK_SPACE_STATUS_NORMAL))) {
41 LOGE("cloudstatus:%{public}d, spaceStatus:%{public}d", userInfo_.cloudStatus, userInfo_.spaceStatus);
42 return E_CLOUD_SDK;
43 }
44
45 std::map<DriveKit::DKAppBundleName, DriveKit::DKAppInfo> appInfos;
46 err = driveKit->GetCloudAppInfo({bundleName}, appInfos);
47 if (err.HasError()) {
48 LOGE("GetCloudAppInfo failed, server err:%{public}d and dk err:%{public}d", err.serverErrorCode,
49 err.dkErrorCode);
50 return E_CLOUD_SDK;
51 }
52 auto appInfo = appInfos[bundleName];
53 if (appInfo.enableCloud != 1) {
54 LOGE("unexpected status:%{public}d, bundleName:%{private}s", appInfo.enableCloud, bundleName.c_str());
55 return E_CLOUD_SDK;
56 }
57 auto switchStatus = appInfo.switchStatus;
58 /* Record the log when the switch is not open */
59 if (switchStatus != DKAppSwitchStatus::DK_APP_SWITCH_STATUS_OPEN) {
60 LOGI("bundleName:%{private}s, switchStatus:%{public}d", bundleName.c_str(), switchStatus);
61 }
62 /* insert key-value */
63 appSwitches_.insert(std::make_pair(bundleName, switchStatus == DKAppSwitchStatus::DK_APP_SWITCH_STATUS_OPEN));
64 userId_ = userId;
65 return E_OK;
66 }
67
IsCloudStatusOkay(const std::string & bundleName,const int32_t userId)68 bool CloudStatus::IsCloudStatusOkay(const std::string &bundleName, const int32_t userId)
69 {
70 std::lock_guard<std::mutex> lock(mutex_);
71 /* User switching */
72 if (userId_ != userId) {
73 appSwitches_.erase(bundleName);
74 }
75
76 /* Obtain cloud information only during first sync */
77 auto iter = appSwitches_.find(bundleName);
78 if (iter == appSwitches_.end()) {
79 LOGI("appSwitches unknown, bundleName:%{private}s, userId:%{public}d", bundleName.c_str(), userId);
80 auto ret = GetCurrentCloudInfo(bundleName, userId);
81 if (ret) {
82 return false;
83 }
84 }
85
86 LOGI("bundleName:%{private}s, cloudSatus:%{public}d, spaceStatus:%{public}d, switcheStatus:%{public}d",
87 bundleName.c_str(), userInfo_.cloudStatus, userInfo_.spaceStatus, appSwitches_[bundleName]);
88 return appSwitches_[bundleName];
89 }
90
ChangeAppSwitch(const std::string & bundleName,const int32_t userId,bool appSwitchStatus)91 int32_t CloudStatus::ChangeAppSwitch(const std::string &bundleName, const int32_t userId, bool appSwitchStatus)
92 {
93 std::lock_guard<std::mutex> lock(mutex_);
94 if (appSwitchStatus == true) {
95 auto iter = appSwitches_.find(bundleName);
96 if (iter != appSwitches_.end()) {
97 LOGI("change app swtich, originStatus:%{public}d, currentStatus:%{public}d", appSwitches_[bundleName],
98 appSwitchStatus);
99 appSwitches_[bundleName] = appSwitchStatus;
100 }
101 } else {
102 /* Actively obtaining cloud information when next sync */
103 appSwitches_.erase(bundleName);
104 }
105
106 return E_OK;
107 }
108
IsAccountIdChanged(const std::string & accountId)109 bool CloudStatus::IsAccountIdChanged(const std::string &accountId)
110 {
111 std::lock_guard<std::mutex> lock(mutex_);
112 if ((userInfo_.accountId != "") && (userInfo_.accountId != accountId)) {
113 /* accountId Changed, clear init flag */
114 appSwitches_.clear();
115 return true;
116 }
117 return false;
118 }
119 } // namespace OHOS::FileManagement::CloudSync