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