• 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 "drive_kit.h"
17 #include "dk_cloud_callback.h"
18 #include "dk_container.h"
19 
20 namespace DriveKit {
21 std::mutex DriveKitNative::drivekitMutex_;
22 std::map<int, std::shared_ptr<DriveKitNative>> DriveKitNative::driveKits_;
GetDefaultContainer(DKAppBundleName bundleName)23 std::shared_ptr<DKContainer> DriveKitNative::GetDefaultContainer(DKAppBundleName bundleName)
24 {
25     DKContainerName containerName;
26     {
27         std::lock_guard<std::mutex> lck(appInfoMutex_);
28         auto itor = appInfos_.find(bundleName);
29         if (itor != appInfos_.end()) {
30             containerName = itor->second.defaultContainer.containerName;
31         }
32     }
33     if (containerName.empty()) {
34         std::map<DKAppBundleName, DKAppInfo> appInfo;
35         if (GetCloudAppInfo({bundleName}, appInfo).HasError()) {
36             return nullptr;
37         }
38         auto itor = appInfo.find(bundleName);
39         if (itor == appInfo.end()) {
40             return nullptr;
41         }
42         containerName = itor->second.defaultContainer.containerName;
43     }
44     std::string key = bundleName + containerName;
45     std::lock_guard<std::mutex> lck(containMutex_);
46     auto it = containers_.find(key);
47     if (it != containers_.end()) {
48         return it->second;
49     }
50     std::shared_ptr<DKContainer> container =
51         std::make_shared<DKContainer>(bundleName, containerName, shared_from_this());
52     container->Init();
53     containers_[key.c_str()] = container;
54     return container;
55 }
56 
GetContainer(DKAppBundleName bundleName,DKContainerName containerName)57 std::shared_ptr<DKContainer> DriveKitNative::GetContainer(DKAppBundleName bundleName, DKContainerName containerName)
58 {
59     std::string key = bundleName + containerName;
60     auto it = containers_.find(key);
61     if (it != containers_.end()) {
62         return it->second;
63     }
64     std::shared_ptr<DKContainer> container =
65         std::make_shared<DKContainer>(bundleName, containerName, shared_from_this());
66     container->Init();
67     containers_[key.c_str()] = container;
68     return container;
69 }
70 
GetCloudUserInfo(DKUserInfo & userInfo)71 DKError DriveKitNative::GetCloudUserInfo(DKUserInfo &userInfo)
72 {
73     userInfo.cloudStatus = DKCloudStatus::DK_CLOUD_STATUS_LOGIN;
74     userInfo.spaceStatus = DKSpaceStatus::DK_SPACE_STATUS_NORMAL;
75     return DKError();
76 }
77 
GetCloudAppInfo(const std::vector<DKAppBundleName> & bundleNames,std::map<DKAppBundleName,DKAppInfo> & appInfos)78 DKError DriveKitNative::GetCloudAppInfo(const std::vector<DKAppBundleName> &bundleNames,
79                                         std::map<DKAppBundleName, DKAppInfo> &appInfos)
80 {
81     DKAppInfo appInfo;
82     appInfo.enableCloud = 1;
83     appInfo.switchStatus = DKAppSwitchStatus::DK_APP_SWITCH_STATUS_OPEN;
84     appInfo.defaultContainer.containerName = "defaultContainer";
85     for (const auto &name : bundleNames) {
86         appInfos[name] = appInfo;
87     }
88     return DKError();
89 }
90 
GetCloudAppSwitches(const std::vector<DKAppBundleName> & bundleNames,std::map<DKAppBundleName,DKAppSwitchStatus> & appSwitchs)91 DKError DriveKitNative::GetCloudAppSwitches(const std::vector<DKAppBundleName> &bundleNames,
92                                             std::map<DKAppBundleName, DKAppSwitchStatus> &appSwitchs)
93 {
94     return DKError();
95 }
96 
GetServerTime(time_t & time)97 DKError DriveKitNative::GetServerTime(time_t &time)
98 {
99     return DKError();
100 }
101 
ResolveNotificationEvent(const std::string & extraData,DKRecordChangeEvent & event)102 DKError DriveKitNative::ResolveNotificationEvent(const std::string &extraData, DKRecordChangeEvent &event)
103 {
104     return DKError();
105 }
106 
GetAppConfigParams(const DKAppBundleName & bundleNames,std::map<std::string,std::string> & param)107 DKError DriveKitNative::GetAppConfigParams(const DKAppBundleName &bundleNames,
108                                            std::map<std::string, std::string> &param)
109 {
110     return DKError();
111 }
112 
GetInstance(int userId)113 std::shared_ptr<DriveKitNative> DriveKitNative::GetInstance(int userId)
114 {
115     std::lock_guard<std::mutex> lck(drivekitMutex_);
116     auto it = driveKits_.find(userId);
117     if (it != driveKits_.end()) {
118         return it->second;
119     }
120     struct EnableMakeShared : public DriveKitNative {
121         explicit EnableMakeShared(int userId) : DriveKitNative(userId) {}
122     };
123     std::shared_ptr<DriveKitNative> driveKit = std::make_shared<EnableMakeShared>(userId);
124     driveKits_[userId] = driveKit;
125     return driveKit;
126 }
127 
GetInstance(int userId,DKCloudSyncDemon syncDemon)128 std::shared_ptr<DriveKitNative> DriveKitNative::GetInstance(int userId, DKCloudSyncDemon syncDemon)
129 {
130     auto drivekit = GetInstance(userId);
131     if (drivekit) {
132         drivekit->syncDemon_ = syncDemon;
133     }
134     return drivekit;
135 }
136 
DriveKitNative(int userId)137 DriveKitNative::DriveKitNative(int userId)
138 {
139     userId_ = userId;
140 }
141 
~DriveKitNative()142 DriveKitNative::~DriveKitNative() {}
143 
OnUploadAsset(const std::string & request,const std::string & result)144 int32_t DriveKitNative::OnUploadAsset(const std::string &request, const std::string &result)
145 {
146     return 0;
147 }
148 
ReleaseDefaultContainer(DKAppBundleName bundleName)149 void DriveKitNative::ReleaseDefaultContainer(DKAppBundleName bundleName)
150 {
151     return;
152 }
153 
ReleaseContainer(DKAppBundleName bundleName,DKContainerName containerName)154 void DriveKitNative::ReleaseContainer(DKAppBundleName bundleName, DKContainerName containerName)
155 {
156     return;
157 }
ReleaseCloudUserInfo()158 void DriveKitNative::ReleaseCloudUserInfo()
159 {
160     return;
161 }
CleanCloudUserInfo()162 void DriveKitNative::CleanCloudUserInfo()
163 {
164     return;
165 }
166 } // namespace DriveKit
167