1 /*
2  * Copyright (c) 2021-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 #include "base/utils/utils.h"
17 #include "core/common/storage/storage_proxy.h"
18 
19 namespace OHOS::Ace {
20 
21 StorageProxy* StorageProxy::inst_ = nullptr;
22 
GetInstance()23 StorageProxy* StorageProxy::GetInstance()
24 {
25     if (inst_ == nullptr) {
26         inst_ = new StorageProxy();
27     }
28     return (inst_);
29 }
30 
SetDelegate(std::unique_ptr<StorageInterface> && delegate)31 void StorageProxy::SetDelegate(std::unique_ptr<StorageInterface>&& delegate)
32 {
33     delegate_ = std::move(delegate);
34 }
35 
SetDistributedDelegate(std::unique_ptr<StorageInterface> && delegate)36 void StorageProxy::SetDistributedDelegate(std::unique_ptr<StorageInterface>&& delegate)
37 {
38     distributedDelegate_ = std::move(delegate);
39 }
40 
GetStorage() const41 RefPtr<Storage> StorageProxy::GetStorage() const
42 {
43     CHECK_NULL_RETURN(delegate_, nullptr);
44     return delegate_->GetStorage();
45 }
46 
GetStorage(const std::string & sessionId,std::function<void (const std::string &)> && notifier,const RefPtr<TaskExecutor> & taskExecutor) const47 RefPtr<Storage> StorageProxy::GetStorage(const std::string& sessionId,
48     std::function<void(const std::string&)>&& notifier, const RefPtr<TaskExecutor>& taskExecutor) const
49 {
50     CHECK_NULL_RETURN(distributedDelegate_, nullptr);
51     return distributedDelegate_->GetStorage(sessionId, std::move(notifier), taskExecutor);
52 }
53 
54 } // namespace OHOS::Ace
55