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
18 #include <functional>
19 #include <gtest/gtest.h>
20 #include <iostream>
21
22 namespace DriveKit {
23
24 namespace {
25 constexpr int SCHED_NUM = 1;
26 constexpr int FILE_NUM = 2;
27 static int32_t g_nPrivateDatabaseNum = 0;
28 } // namespace
29
GetPrivateDatabase()30 std::shared_ptr<DKDatabase> DKContainer::GetPrivateDatabase()
31 {
32 GTEST_LOG_(INFO) << "GetPrivateDatabase";
33 g_nPrivateDatabaseNum++;
34 if (g_nPrivateDatabaseNum % FILE_NUM == SCHED_NUM) {
35 GTEST_LOG_(INFO) << "GetPrivateDatabase is false";
36 return nullptr;
37 }
38 GTEST_LOG_(INFO) << "GetPrivateDatabase is true";
39 return privateDatabase_;
40 }
41
GetDatabaseWithdatabaseScope(DKDatabaseScope databaseScope)42 std::shared_ptr<DKDatabase> DKContainer::GetDatabaseWithdatabaseScope(DKDatabaseScope databaseScope)
43 {
44 std::shared_ptr<DKDatabase> database = nullptr;
45 switch (databaseScope) {
46 case DKDatabaseScope::DK_PUBLIC_DATABASE:
47 database = publicDatabase_;
48 break;
49 case DKDatabaseScope::DK_PRIVATE_DATABASE:
50 database = privateDatabase_;
51 break;
52 case DKDatabaseScope::DK_SHARED_DATABASE:
53 database = sharedDatabase_;
54 break;
55 default:
56 break;
57 }
58 return database;
59 }
60
Init()61 void DKContainer::Init()
62 {
63 if (driveKit_) {
64 publicDatabase_ = std::make_shared<DKDatabase>(shared_from_this(), DKDatabaseScope::DK_PUBLIC_DATABASE);
65 if (publicDatabase_) {
66 publicDatabase_->Init();
67 }
68 privateDatabase_ = std::make_shared<DKDatabase>(shared_from_this(), DKDatabaseScope::DK_PRIVATE_DATABASE);
69 if (privateDatabase_) {
70 privateDatabase_->Init();
71 }
72 sharedDatabase_ = std::make_shared<DKDatabase>(shared_from_this(), DKDatabaseScope::DK_SHARED_DATABASE);
73 if (sharedDatabase_) {
74 sharedDatabase_->Init();
75 }
76 }
77 }
78
SaveSubscription(std::shared_ptr<DKContext> contex,DKSubscription & subscription,SaveSubscriptionCallback callback)79 DKLocalErrorCode DKContainer::SaveSubscription(std::shared_ptr<DKContext> contex,
80 DKSubscription &subscription,
81 SaveSubscriptionCallback callback)
82 {
83 GTEST_LOG_(INFO) << "SaveSubscription begin";
84 if (callback) {
85 return DKLocalErrorCode::IPC_CONNECT_FAILED;
86 }
87 return DKLocalErrorCode::NO_ERROR;
88 }
89
DeleteSubscription(std::shared_ptr<DKContext> contex,DKSubscriptionId id,DelSubscriptionCallback callback)90 DKLocalErrorCode DKContainer::DeleteSubscription(std::shared_ptr<DKContext> contex,
91 DKSubscriptionId id,
92 DelSubscriptionCallback callback)
93 {
94 GTEST_LOG_(INFO) << "DeleteSubscription begin";
95 if (callback) {
96 return DKLocalErrorCode::IPC_CONNECT_FAILED;
97 }
98 return DKLocalErrorCode::NO_ERROR;
99 }
Release()100 void DKContainer::Release() {}
101 } // namespace DriveKit
102