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 "adapter/preview/osal/stage_card_parser.h"
17 #include "base/log/log_wrapper.h"
18 
19 namespace OHOS::Ace {
20 
StageCardParser()21 StageCardParser::StageCardParser() : manifestWindow_(Referenced::MakeRefPtr<Framework::ManifestWindow>())
22 {}
23 
Parse(const std::string & contents,const std::string & selectUrl)24 void StageCardParser::Parse(const std::string& contents, const std::string& selectUrl)
25 {
26     auto rootJson = JsonUtil::ParseJsonString(contents);
27     if (!rootJson || !rootJson->IsValid()) {
28         LOGE("the form config is illegal");
29         return;
30     }
31     std::unique_ptr<JsonValue> formConfig;
32     auto formConfigs = rootJson->GetValue("forms");
33     int32_t index = 0;
34     if (formConfigs && formConfigs->IsArray()) {
35         for (; index < formConfigs->GetArraySize(); ++index) {
36             formConfig = formConfigs->GetArrayItem(index);
37             if (formConfig && formConfig->Contains("src") && formConfig->GetString("src") == selectUrl) {
38                 break;
39             }
40         }
41     }
42     if (formConfigs && index == formConfigs->GetArraySize()) {
43         TAG_LOGW(AceLogTag::ACE_FORM, "The configuration information for the url %{public}s does not exist",
44             selectUrl.c_str());
45         return;
46     }
47     if (!formConfig) {
48         return;
49     }
50     auto supportDimensions = formConfig->GetValue("supportDimensions");
51     if (supportDimensions && supportDimensions->IsArray()) {
52         for (index = 0; index < supportDimensions->GetArraySize(); ++index) {
53             auto supportDimension = supportDimensions->GetArrayItem(index);
54             if (supportDimension && supportDimension->IsString()) {
55                 supportDimensions_.push_back(supportDimension->GetString());
56             }
57         }
58     }
59 
60     colorMode_ = formConfig->GetString("colorMode", "auto");
61     defaultDimension_ = formConfig->GetString("defaultDimension");
62     description_ = formConfig->GetString("description");
63     formConfigAbility_ = formConfig->GetString("formConfigAbility");
64     isDefault_ = formConfig->GetBool("updateEnabled", true);
65     name_ = formConfig->GetString("name");
66     scheduledUpdateTime_ = formConfig->GetString("scheduledUpdateTime");
67     src_ = formConfig->GetString("src");
68     updateDuration_ = formConfig->GetUInt("updateDuration", 1);
69     updateEnabled_ = formConfig->GetBool("updateEnabled", true);
70     manifestWindow_->WindowParse(formConfig);
71 }
72 
GetColorMode() const73 const std::string& StageCardParser::GetColorMode() const
74 {
75     return colorMode_;
76 }
77 
GetDefaultDimension() const78 const std::string& StageCardParser::GetDefaultDimension() const
79 {
80     return defaultDimension_;
81 }
82 
GetDescription() const83 const std::string& StageCardParser::GetDescription() const
84 {
85     return description_;
86 }
87 
GetFormConfigAbility() const88 const std::string& StageCardParser::GetFormConfigAbility() const
89 {
90     return formConfigAbility_;
91 }
92 
GetIsDefault() const93 bool StageCardParser::GetIsDefault() const
94 {
95     return isDefault_;
96 }
97 
GetName() const98 const std::string& StageCardParser::GetName() const
99 {
100     return name_;
101 }
102 
GetScheduledUpdateTime() const103 const std::string& StageCardParser::GetScheduledUpdateTime() const
104 {
105     return scheduledUpdateTime_;
106 }
107 
GetSrc() const108 const std::string& StageCardParser::GetSrc() const
109 {
110     return src_;
111 }
112 
GetSupportDimensions() const113 const std::vector<std::string>& StageCardParser::GetSupportDimensions() const
114 {
115     return supportDimensions_;
116 }
117 
GetUpdateDuration() const118 uint32_t StageCardParser::GetUpdateDuration() const
119 {
120     return updateDuration_;
121 }
122 
GetUpdateEnabled() const123 bool StageCardParser::GetUpdateEnabled() const
124 {
125     return updateEnabled_;
126 }
127 
GetWindowConfig() const128 WindowConfig& StageCardParser::GetWindowConfig() const
129 {
130     return manifestWindow_->GetWindowConfig();
131 }
132 
133 } // namespace OHOS::Ace
134