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 using ChangesNotifyCallback = std::function<void(std::shared_ptr<DKContext>, const DKError &)>; 61 62 class DriveKitNative; 63 class DKContainer : public std::enable_shared_from_this<DKContainer> { 64 friend class DriveKitNative; 65 public: 66 std::shared_ptr<DKDatabase> GetPrivateDatabase(); 67 std::shared_ptr<DKDatabase> GetDatabaseWithdatabaseScope(DKDatabaseScope databaseScope); 68 DKLocalErrorCode SaveSubscription(std::shared_ptr<DKContext> contex, 69 DKSubscription &subscription, 70 SaveSubscriptionCallback callback); 71 DKLocalErrorCode DeleteSubscription(std::shared_ptr<DKContext> context, 72 DKSubscriptionId id, 73 DelSubscriptionCallback callback); 74 DKLocalErrorCode ChangesNotify(std::shared_ptr<DKContext> context, 75 DKSubscriptionId id, 76 ChangesNotifyCallback callback); 77 void Release(); 78 79 public: DKContainer(DKAppBundleName bundleName,DKContainerName containerName,std::shared_ptr<DriveKitNative> driveKit)80 DKContainer(DKAppBundleName bundleName, DKContainerName containerName, std::shared_ptr<DriveKitNative> driveKit) 81 { 82 bundleName_ = bundleName; 83 containerName_ = containerName; 84 driveKit_ = driveKit; 85 } 86 protected: 87 void Init(); 88 89 public: GetAppBundleName()90 DKAppBundleName GetAppBundleName() const 91 { 92 return bundleName_; 93 } GetContainerName()94 DKContainerName GetContainerName() const 95 { 96 return containerName_; 97 } GetDriveKitNative()98 std::shared_ptr<DriveKitNative> GetDriveKitNative() const 99 { 100 return driveKit_; 101 } 102 103 private: 104 std::shared_ptr<DriveKitNative> driveKit_; 105 std::shared_ptr<DKDatabase> publicDatabase_ = nullptr; 106 std::shared_ptr<DKDatabase> privateDatabase_ = nullptr; 107 std::shared_ptr<DKDatabase> sharedDatabase_ = nullptr; 108 DKAppBundleName bundleName_; 109 DKContainerName containerName_; 110 }; 111 } // namespace DriveKit 112 #endif 113