• 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 #ifndef DRIVE_KIT_CONTAINER_H
17 #define DRIVE_KIT_CONTAINER_H
18 
19 #include <map>
20 #include <string>
21 
22 #include "dk_cloud_callback.h"
23 #include "dk_context.h"
24 #include "dk_database.h"
25 #include "dk_error.h"
26 #include "dk_result.h"
27 
28 namespace DriveKit {
29 using DKSubscriptionId = std::string;
30 struct DKSubscription {
31     int64_t expirationTime;
32 };
33 class DKSubscriptionResult : public DKResult {
34 public:
GetDKSubscription()35     DKSubscription GetDKSubscription()
36     {
37         return subscription_;
38     }
GetDKSubscriptionId()39     DKSubscriptionId GetDKSubscriptionId()
40     {
41         return id_;
42     }
43 
SetDKSubscription(DKSubscription subscription)44     void SetDKSubscription(DKSubscription subscription)
45     {
46         subscription_ = subscription;
47     }
SetDKSubscriptionId(DKSubscriptionId id)48     void SetDKSubscriptionId(DKSubscriptionId id)
49     {
50         id_ = id;
51     }
52 
53 private:
54     DKSubscription subscription_;
55     DKSubscriptionId id_;
56 };
57 using SaveSubscriptionCallback = std::function<void(std::shared_ptr<DKContext>,
58     std::shared_ptr<DKContainer>, DKSubscriptionResult &)>;
59 using DelSubscriptionCallback = std::function<void(std::shared_ptr<DKContext>, const DKError &)>;
60 
61 class DriveKitNative;
62 class DKContainer : public std::enable_shared_from_this<DKContainer> {
63     friend class DriveKitNative;
64 public:
65     std::shared_ptr<DKDatabase> GetPrivateDatabase();
66     std::shared_ptr<DKDatabase> GetDatabaseWithdatabaseScope(DKDatabaseScope databaseScope);
67     DKLocalErrorCode SaveSubscription(std::shared_ptr<DKContext> contex,
68                                       DKSubscription &subscription,
69                                       SaveSubscriptionCallback callback);
70     DKLocalErrorCode DeleteSubscription(std::shared_ptr<DKContext> context,
71                                         DKSubscriptionId id,
72                                         DelSubscriptionCallback callback);
73 
74 public:
DKContainer(DKAppBundleName bundleName,DKContainerName containerName,std::shared_ptr<DriveKitNative> driveKit)75     DKContainer(DKAppBundleName bundleName, DKContainerName containerName, std::shared_ptr<DriveKitNative> driveKit)
76     {
77         bundleName_ = bundleName;
78         containerName_ = containerName;
79         driveKit_ = driveKit;
80     }
81 protected:
82     void Init();
83 
84 public:
GetAppBundleName()85     DKAppBundleName GetAppBundleName() const
86     {
87         return bundleName_;
88     }
GetContainerName()89     DKContainerName GetContainerName() const
90     {
91         return containerName_;
92     }
GetDriveKitNative()93     std::shared_ptr<DriveKitNative> GetDriveKitNative() const
94     {
95         return driveKit_;
96     }
97 
98 private:
99     std::shared_ptr<DriveKitNative> driveKit_;
100     std::shared_ptr<DKDatabase> publicDatabase_ = nullptr;
101     std::shared_ptr<DKDatabase> privateDatabase_ = nullptr;
102     std::shared_ptr<DKDatabase> sharedDatabase_ = nullptr;
103     DKAppBundleName bundleName_;
104     DKContainerName containerName_;
105 };
106 } // namespace DriveKit
107 #endif
108