1 /*
2  * Copyright (C) 2022 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 OHOS_MEDIALIBRARY_UNISTORE_MANAGER_H
17 #define OHOS_MEDIALIBRARY_UNISTORE_MANAGER_H
18 
19 #include <memory>
20 
21 #include "media_log.h"
22 #include "medialibrary_errno.h"
23 #include "medialibrary_rdbstore.h"
24 #include "medialibrary_unistore.h"
25 
26 namespace OHOS {
27 namespace Media {
28 #define EXPORT __attribute__ ((visibility ("default")))
29 class MediaLibraryUnistoreManager {
30 public:
GetInstance()31     EXPORT static MediaLibraryUnistoreManager &GetInstance()
32     {
33         static MediaLibraryUnistoreManager instance;
34         return instance;
35     }
36 
Init(const std::shared_ptr<OHOS::AbilityRuntime::Context> & context)37     EXPORT int32_t Init(const std::shared_ptr<OHOS::AbilityRuntime::Context> &context)
38     {
39         if (rdbStorePtr_) {
40             return E_OK;
41         }
42 
43         rdbStorePtr_ = std::make_shared<MediaLibraryRdbStore>(context);
44         if (!rdbStorePtr_) {
45             MEDIA_ERR_LOG("create rdbStore failed");
46             return E_ERR;
47         }
48         return rdbStorePtr_->Init();
49     }
50 
Init(const std::shared_ptr<OHOS::AbilityRuntime::Context> & context,const NativeRdb::RdbStoreConfig & config,int version,NativeRdb::RdbOpenCallback & openCallback)51     EXPORT int32_t Init(const std::shared_ptr<OHOS::AbilityRuntime::Context> &context,
52         const NativeRdb::RdbStoreConfig &config, int version, NativeRdb::RdbOpenCallback &openCallback)
53     {
54         if (rdbStorePtr_) {
55             return E_OK;
56         }
57 
58         rdbStorePtr_ = std::make_shared<MediaLibraryRdbStore>(context);
59         if (!rdbStorePtr_) {
60             MEDIA_ERR_LOG("create rdbStore failed");
61             return E_ERR;
62         }
63         return rdbStorePtr_->Init(config, version, openCallback);
64     }
65 
Stop()66     EXPORT void Stop()
67     {
68         if (rdbStorePtr_) {
69             rdbStorePtr_->Stop();
70         }
71         rdbStorePtr_ = nullptr;
72     }
73 
GetRdbStore()74     EXPORT std::shared_ptr<MediaLibraryRdbStore> GetRdbStore() const
75     {
76         if (rdbStorePtr_ != nullptr && rdbStorePtr_->CheckRdbStore()) {
77             return rdbStorePtr_;
78         }
79         MEDIA_ERR_LOG("MediaLibraryRdbStore or rdbStore is nullptr");
80         return nullptr;
81     }
82 
83 private:
84     MediaLibraryUnistoreManager() = default;
85     virtual ~MediaLibraryUnistoreManager() = default;
86 
87     std::shared_ptr<MediaLibraryRdbStore> rdbStorePtr_;
88 };
89 } // namespace Media
90 } // namespace OHOS
91 
92 #endif // OHOS_MEDIALIBRARY_UNISTORE_MANAGER_H
93