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 #include "distributed_object_impl.h"
17 
18 #include "hitrace.h"
19 #include "objectstore_errors.h"
20 #include "string_utils.h"
21 #include "dev_manager.h"
22 #include "bytes_utils.h"
23 #include "object_radar_reporter.h"
24 
25 namespace OHOS::ObjectStore {
~DistributedObjectImpl()26 DistributedObjectImpl::~DistributedObjectImpl()
27 {
28 }
29 
PutDouble(const std::string & key,double value)30 uint32_t DistributedObjectImpl::PutDouble(const std::string &key, double value)
31 {
32     DataObjectHiTrace trace("DistributedObjectImpl::PutDouble");
33     return flatObjectStore_->PutDouble(sessionId_, key, value);
34 }
35 
PutBoolean(const std::string & key,bool value)36 uint32_t DistributedObjectImpl::PutBoolean(const std::string &key, bool value)
37 {
38     DataObjectHiTrace trace("DistributedObjectImpl::PutBoolean");
39     return flatObjectStore_->PutBoolean(sessionId_, key, value);
40 }
41 
PutString(const std::string & key,const std::string & value)42 uint32_t DistributedObjectImpl::PutString(const std::string &key, const std::string &value)
43 {
44     DataObjectHiTrace trace("DistributedObjectImpl::PutString");
45     if (key.find(ASSET_DOT) != std::string::npos) {
46         PutDeviceId();
47     }
48     return flatObjectStore_->PutString(sessionId_, key, value);
49 }
50 
GetDouble(const std::string & key,double & value)51 uint32_t DistributedObjectImpl::GetDouble(const std::string &key, double &value)
52 {
53     return flatObjectStore_->GetDouble(sessionId_, key, value);
54 }
55 
GetBoolean(const std::string & key,bool & value)56 uint32_t DistributedObjectImpl::GetBoolean(const std::string &key, bool &value)
57 {
58     return flatObjectStore_->GetBoolean(sessionId_, key, value);
59 }
60 
GetString(const std::string & key,std::string & value)61 uint32_t DistributedObjectImpl::GetString(const std::string &key, std::string &value)
62 {
63     return flatObjectStore_->GetString(sessionId_, key, value);
64 }
65 
GetType(const std::string & key,Type & type)66 uint32_t DistributedObjectImpl::GetType(const std::string &key, Type &type)
67 {
68     return flatObjectStore_->GetType(sessionId_, key, type);
69 }
70 
GetSessionId()71 std::string &DistributedObjectImpl::GetSessionId()
72 {
73     return sessionId_;
74 }
75 
DistributedObjectImpl(const std::string & sessionId,FlatObjectStore * flatObjectStore)76 DistributedObjectImpl::DistributedObjectImpl(const std::string &sessionId, FlatObjectStore *flatObjectStore)
77     : sessionId_(sessionId), flatObjectStore_(flatObjectStore)
78 {
79 }
80 
PutComplex(const std::string & key,const std::vector<uint8_t> & value)81 uint32_t DistributedObjectImpl::PutComplex(const std::string &key, const std::vector<uint8_t> &value)
82 {
83     DataObjectHiTrace trace("DistributedObjectImpl::PutComplex");
84     return flatObjectStore_->PutComplex(sessionId_, key, value);
85 }
86 
GetComplex(const std::string & key,std::vector<uint8_t> & value)87 uint32_t DistributedObjectImpl::GetComplex(const std::string &key, std::vector<uint8_t> &value)
88 {
89     return flatObjectStore_->GetComplex(sessionId_, key, value);
90 }
91 
Save(const std::string & deviceId)92 uint32_t DistributedObjectImpl::Save(const std::string &deviceId)
93 {
94     uint32_t status = flatObjectStore_->Save(sessionId_, deviceId);
95     if (status != SUCCESS) {
96         LOG_ERROR("DistributedObjectImpl:Save failed. status = %{public}d", status);
97         return status;
98     }
99     return status;
100 }
101 
RevokeSave()102 uint32_t DistributedObjectImpl::RevokeSave()
103 {
104     uint32_t status = flatObjectStore_->RevokeSave(sessionId_);
105     if (status != SUCCESS) {
106         LOG_ERROR("DistributedObjectImpl:RevokeSave failed. status = %{public}d", status);
107         return status;
108     }
109     return status;
110 }
111 
PutDeviceId()112 uint32_t DistributedObjectImpl::PutDeviceId()
113 {
114     DevManager::DetailInfo detailInfo = DevManager::GetInstance()->GetLocalDevice();
115     return flatObjectStore_->PutString(sessionId_, DEVICEID_KEY, detailInfo.networkId);
116 }
117 
GetAssetValue(const std::string & assetKey,Asset & assetValue)118 uint32_t DistributedObjectImpl::GetAssetValue(const std::string &assetKey, Asset &assetValue)
119 {
120     double assetStatus = 0.0;
121     auto status = GetDouble(assetKey + STATUS_SUFFIX, assetStatus);
122     if (status == SUCCESS) {
123         assetValue.status = static_cast<uint32_t>(assetStatus);
124     }
125     status = GetString(assetKey + NAME_SUFFIX, assetValue.name);
126     LOG_ERROR_RETURN(status == SUCCESS, "get name failed!", status);
127     status = GetString(assetKey + URI_SUFFIX, assetValue.uri);
128     LOG_ERROR_RETURN(status == SUCCESS, "get uri failed!", status);
129     status = GetString(assetKey + PATH_SUFFIX, assetValue.path);
130     LOG_ERROR_RETURN(status == SUCCESS, "get path failed!", status);
131     status = GetString(assetKey + CREATE_TIME_SUFFIX, assetValue.createTime);
132     LOG_ERROR_RETURN(status == SUCCESS, "get createTime failed!", status);
133     status = GetString(assetKey + MODIFY_TIME_SUFFIX, assetValue.modifyTime);
134     LOG_ERROR_RETURN(status == SUCCESS, "get modifyTime failed!", status);
135     status = GetString(assetKey + SIZE_SUFFIX, assetValue.size);
136     LOG_ERROR_RETURN(status == SUCCESS, "get size failed!", status);
137     RemovePrefix(assetValue);
138     return status;
139 }
140 
RemovePrefix(Asset & assetValue)141 void DistributedObjectImpl::RemovePrefix(Asset &assetValue)
142 {
143     assetValue.name = assetValue.name.substr(STRING_PREFIX_LEN);
144     assetValue.uri = assetValue.uri.substr(STRING_PREFIX_LEN);
145     assetValue.path = assetValue.path.substr(STRING_PREFIX_LEN);
146     assetValue.createTime = assetValue.createTime.substr(STRING_PREFIX_LEN);
147     assetValue.modifyTime = assetValue.modifyTime.substr(STRING_PREFIX_LEN);
148     assetValue.size = assetValue.size.substr(STRING_PREFIX_LEN);
149     assetValue.hash = assetValue.modifyTime + "_" + assetValue.size;
150 }
151 
BindAssetStore(const std::string & assetKey,AssetBindInfo & bindInfo)152 uint32_t DistributedObjectImpl::BindAssetStore(const std::string &assetKey, AssetBindInfo &bindInfo)
153 {
154     Asset assetValue;
155     auto status = GetAssetValue(assetKey, assetValue);
156     if (status != SUCCESS) {
157         LOG_ERROR("DistributedObjectImpl:GetAssetValue failed. status = %{public}d", status);
158         return status;
159     }
160     status = flatObjectStore_->BindAssetStore(sessionId_, bindInfo, assetValue);
161     if (status != SUCCESS) {
162         LOG_ERROR("DistributedObjectImpl:BindAssetStore failed. status = %{public}d", status);
163         return status;
164     }
165     return status;
166 }
167 } // namespace OHOS::ObjectStore