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 "form_info_storage.h"
17
18 #include "fms_log_wrapper.h"
19 #include "json_serializer.h"
20
21 namespace OHOS {
22 namespace AAFwk {
23 namespace {
24 const std::string JSON_KEY_USER_ID = "userId";
25 const std::string JSON_KEY_FORM_INFO = "formInfos";
26 } // namespace
27
FormInfoStorage(int32_t userId,const std::vector<AppExecFwk::FormInfo> & formInfos)28 FormInfoStorage::FormInfoStorage(int32_t userId, const std::vector<AppExecFwk::FormInfo> &formInfos)
29 {
30 this->userId = userId;
31 for (const auto &item : formInfos) {
32 this->formInfos.push_back(item);
33 }
34 }
35
GetAllFormsInfo(int32_t userId,std::vector<AppExecFwk::FormInfo> & formInfos) const36 void FormInfoStorage::GetAllFormsInfo(int32_t userId, std::vector<AppExecFwk::FormInfo> &formInfos) const
37 {
38 HILOG_DEBUG("Get all forms infos, current userId is%{public}d, this userId is %{public}d.", userId, this->userId);
39 if (this->userId != userId && this->userId != AppExecFwk::Constants::DEFAULT_USERID) {
40 return;
41 }
42 for (const auto &item : this->formInfos) {
43 formInfos.push_back(item);
44 }
45 }
46
GetFormsInfoByModule(int32_t userId,const std::string & moduleName,std::vector<AppExecFwk::FormInfo> & formInfos) const47 void FormInfoStorage::GetFormsInfoByModule(int32_t userId, const std::string &moduleName,
48 std::vector<AppExecFwk::FormInfo> &formInfos) const
49 {
50 if (this->userId != userId && this->userId != AppExecFwk::Constants::DEFAULT_USERID) {
51 return;
52 }
53 for (const auto &item : this->formInfos) {
54 if (item.moduleName == moduleName) {
55 formInfos.push_back(item);
56 }
57 }
58 }
59
to_json(nlohmann::json & jsonObject,const FormInfoStorage & formInfoStorage)60 void to_json(nlohmann::json &jsonObject, const FormInfoStorage &formInfoStorage)
61 {
62 jsonObject = nlohmann::json {
63 {JSON_KEY_USER_ID, formInfoStorage.userId},
64 {JSON_KEY_FORM_INFO, formInfoStorage.formInfos}
65 };
66 }
67
from_json(const nlohmann::json & jsonObject,FormInfoStorage & formInfoStorage)68 void from_json(const nlohmann::json &jsonObject, FormInfoStorage &formInfoStorage)
69 {
70 if (!jsonObject.at(JSON_KEY_USER_ID).is_null() && jsonObject.at(JSON_KEY_USER_ID).is_number_integer()) {
71 formInfoStorage.userId = jsonObject.at(JSON_KEY_USER_ID).get<int32_t>();
72 }
73 if (!jsonObject.at(JSON_KEY_FORM_INFO).is_null() && jsonObject.at(JSON_KEY_FORM_INFO).is_array()) {
74 formInfoStorage.formInfos = jsonObject.at(JSON_KEY_FORM_INFO).get<std::vector<AppExecFwk::FormInfo>>();
75 }
76 }
77 } // namespace AppExecFwk
78 } // namespace OHOS
79