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 "dk_container.h"
17 #include "drive_kit.h"
18
19 namespace DriveKit {
20 std::mutex DriveKitNative::drivekitMutex_;
21 std::map<int, std::shared_ptr<DriveKitNative>> DriveKitNative::driveKits_;
GetDefaultContainer(DKAppBundleName bundleName)22 std::shared_ptr<DKContainer> DriveKitNative::GetDefaultContainer(DKAppBundleName bundleName)
23 {
24 if (bundleName == "com.ohos.test.mock") {
25 return nullptr;
26 }
27 DKContainerName containerName;
28 {
29 std::lock_guard<std::mutex> lck(appInfoMutex_);
30 auto itor = appInfos_.find(bundleName);
31 if (itor != appInfos_.end()) {
32 containerName = itor->second.defaultContainer.containerName;
33 }
34 }
35 if (containerName.empty()) {
36 std::map<DKAppBundleName, DKAppInfo> appInfo;
37 if (GetCloudAppInfo({bundleName}, appInfo).HasError()) {
38 return nullptr;
39 }
40 auto itor = appInfo.find(bundleName);
41 if (itor == appInfo.end()) {
42 return nullptr;
43 }
44 containerName = itor->second.defaultContainer.containerName;
45 }
46 std::string key = bundleName + containerName;
47 std::lock_guard<std::mutex> lck(containMutex_);
48 auto it = containers_.find(key);
49 if (it != containers_.end()) {
50 return it->second;
51 }
52 std::shared_ptr<DKContainer> container =
53 std::make_shared<DKContainer>(bundleName, containerName, shared_from_this());
54 container->Init();
55 containers_[key.c_str()] = container;
56 return container;
57 }
58
GetContainer(DKAppBundleName bundleName,DKContainerName containerName)59 std::shared_ptr<DKContainer> DriveKitNative::GetContainer(DKAppBundleName bundleName, DKContainerName containerName)
60 {
61 std::string key = bundleName + containerName;
62 auto it = containers_.find(key);
63 if (it != containers_.end()) {
64 return it->second;
65 }
66 std::shared_ptr<DKContainer> container =
67 std::make_shared<DKContainer>(bundleName, containerName, shared_from_this());
68 container->Init();
69 containers_[key.c_str()] = container;
70 return container;
71 }
72
GetCloudUserInfo(DKUserInfo & userInfo)73 DKError DriveKitNative::GetCloudUserInfo(DKUserInfo &userInfo)
74 {
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.defaultContainer.containerName = "defaultContainer";
83 for (const auto &name : bundleNames) {
84 appInfos[name] = appInfo;
85 }
86 return DKError();
87 }
88
GetCloudAppSwitches(const std::vector<DKAppBundleName> & bundleNames,std::map<DKAppBundleName,DKAppSwitchStatus> & appSwitchs)89 DKError DriveKitNative::GetCloudAppSwitches(const std::vector<DKAppBundleName> &bundleNames,
90 std::map<DKAppBundleName, DKAppSwitchStatus> &appSwitchs)
91 {
92 return DKError();
93 }
94
GetServerTime(time_t & time)95 DKError DriveKitNative::GetServerTime(time_t &time)
96 {
97 return DKError();
98 }
99
GetInstance(int userId)100 std::shared_ptr<DriveKitNative> DriveKitNative::GetInstance(int userId)
101 {
102 if (userId < 0) {
103 return nullptr;
104 }
105 std::lock_guard<std::mutex> lck(drivekitMutex_);
106 auto it = driveKits_.find(userId);
107 if (it != driveKits_.end()) {
108 return it->second;
109 }
110 struct EnableMakeShared : public DriveKitNative {
111 explicit EnableMakeShared(int userId) : DriveKitNative(userId) {}
112 };
113 std::shared_ptr<DriveKitNative> driveKit = std::make_shared<EnableMakeShared>(userId);
114 driveKits_[userId] = driveKit;
115 return driveKit;
116 }
117
DriveKitNative(int userId)118 DriveKitNative::DriveKitNative(int userId)
119 {
120 userId_ = userId;
121 }
122
~DriveKitNative()123 DriveKitNative::~DriveKitNative() {}
124
OnUploadAsset(const std::string & request,const std::string & result)125 int32_t DriveKitNative::OnUploadAsset(const std::string &request, const std::string &result)
126 {
127 return 0;
128 }
129 } // namespace DriveKit
130