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
GetInstance(int userId)102 std::shared_ptr<DriveKitNative> DriveKitNative::GetInstance(int userId)
103 {
104 std::lock_guard<std::mutex> lck(drivekitMutex_);
105 auto it = driveKits_.find(userId);
106 if (it != driveKits_.end()) {
107 return it->second;
108 }
109 struct EnableMakeShared : public DriveKitNative {
110 explicit EnableMakeShared(int userId) : DriveKitNative(userId) {}
111 };
112 std::shared_ptr<DriveKitNative> driveKit = std::make_shared<EnableMakeShared>(userId);
113 driveKits_[userId] = driveKit;
114 return driveKit;
115 }
116
DriveKitNative(int userId)117 DriveKitNative::DriveKitNative(int userId)
118 {
119 userId_ = userId;
120 }
121
~DriveKitNative()122 DriveKitNative::~DriveKitNative() {}
123
OnUploadAsset(const std::string & request,const std::string & result)124 int32_t DriveKitNative::OnUploadAsset(const std::string &request, const std::string &result)
125 {
126 return 0;
127 }
128 } // namespace DriveKit
129