1 /*
2  * Copyright (c) 2021 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 "ability_start_setting.h"
17 
18 #include "string_ex.h"
19 
20 using namespace OHOS;
21 
22 namespace OHOS {
23 namespace AAFwk {
24 const std::string AbilityStartSetting::BOUNDS_KEY = "bounds";
25 const std::string AbilityStartSetting::WINDOW_DISPLAY_ID_KEY = "windowId";
26 const std::string AbilityStartSetting::WINDOW_MODE_KEY = "windowMode";
27 const std::string AbilityStartSetting::IS_START_BY_SCB_KEY = "isStartByScb";
28 constexpr uint32_t CYCLE_LIMIT = 1000;
29 
30 /**
31  * @brief Construct copy function.
32  * @param other indicates instance of abilitystartsetting object
33  * @return none.
34  */
AbilityStartSetting(const AbilityStartSetting & other)35 AbilityStartSetting::AbilityStartSetting(const AbilityStartSetting &other)
36 {
37     abilityStarKey_.clear();
38     abilityStarKey_ = other.abilityStarKey_;
39 }
40 /**
41  * @brief Overload assignment operation.
42  * @param other indicates instance of abilitystartsetting object.
43  * @return Returns current instance of abilitystartsetting object.
44  */
operator =(const AbilityStartSetting & other)45 AbilityStartSetting &AbilityStartSetting::operator=(const AbilityStartSetting &other)
46 {
47     if (this != &other) {
48         abilityStarKey_.clear();
49         abilityStarKey_ = other.abilityStarKey_;
50     }
51     return *this;
52 }
53 /**
54  * @brief Inner function to create AbilityStartSetting
55  *
56  * @return Returns the shared_ptr of AbilityStartSetting object.
57  */
AbilityStartSettingCreator()58 std::shared_ptr<AbilityStartSetting> AbilityStartSettingCreator()
59 {
60     std::shared_ptr<AbilityStartSetting> abilityStartSetting {new (std::nothrow) AbilityStartSetting()};
61     return abilityStartSetting;
62 }
63 
64 /**
65  * @brief Obtains an empty AbilityStartSetting object.
66  *
67  * @return Returns the btains an empty AbilityStartSetting object.
68  */
GetEmptySetting()69 std::shared_ptr<AbilityStartSetting> AbilityStartSetting::GetEmptySetting()
70 {
71     return AbilityStartSettingCreator();
72 }
73 
74 /**
75  * @brief Obtains the names of all the attributes that have been added to this AbilityStartSetting object.
76  *
77  * @return Returns the set of attribute names included in this AbilityStartSetting object.
78  */
GetPropertiesKey()79 std::set<std::string> AbilityStartSetting::GetPropertiesKey()
80 {
81     std::set<std::string> abilityStartSet;
82     abilityStartSet.clear();
83 
84     for (auto it : abilityStarKey_) {
85         abilityStartSet.emplace(it.first);
86     }
87     return abilityStartSet;
88 }
89 
90 /**
91  * @brief Checks whether this AbilityStartSetting object is empty.
92  *
93  * @return Returns true if this AbilityStartSetting object is empty and animatorOption is null; returns false otherwise.
94  */
IsEmpty()95 bool AbilityStartSetting::IsEmpty()
96 {
97     return (abilityStarKey_.size() == 0);
98 }
99 
100 /**
101  * @brief Sets the names of all the attributes of the AbilityStartSetting object.
102  *
103  * @param key Indicates the name of the key.
104  * @param value The window display mode of the values.
105  */
AddProperty(const std::string & key,const std::string & value)106 void AbilityStartSetting::AddProperty(const std::string &key, const std::string &value)
107 {
108     abilityStarKey_[key] = value;
109 }
110 
111 /**
112  * @brief Gets the name of the attributes of the AbilityStartSetting object.
113  *
114  * @param key Indicates the name of the key.
115  * @return Returns value Indicates the value of the attributes of the AbilityStartSetting object
116  */
GetProperty(const std::string & key)117 std::string AbilityStartSetting::GetProperty(const std::string &key)
118 {
119     auto it = abilityStarKey_.find(key);
120     if (it == abilityStarKey_.end()) {
121         return std::string();
122     }
123     return abilityStarKey_[key];
124 }
125 
126 /**
127  * @brief Write the data of AbilityStartSetting to the file stream
128  * @param parcel indicates write the data of AbilityStartSetting to the file stream through parcel
129  * @return bool
130  */
Marshalling(Parcel & parcel) const131 bool AbilityStartSetting::Marshalling(Parcel &parcel) const
132 {
133     size_t size = abilityStarKey_.size();
134 
135     // 1. Number of key value pairs written
136     parcel.WriteUint32((uint32_t)size);
137 
138     std::map<std::string, std::string>::const_iterator it;
139 
140     // 2. Write the key and value strings
141     for (auto pair : abilityStarKey_) {
142         // 1.key
143         parcel.WriteString16(Str8ToStr16(pair.first));
144         // 2.data content
145         parcel.WriteString16(Str8ToStr16(pair.second));
146     }
147 
148     return true;
149 }
150 
151 /**
152  * @brief Reading file stream through parcel to generate AbilityStartSetting instance
153  * @param parcel indicates reading file stream through parcel to generate AbilityStartSetting instance
154  * @return AbilityStartSetting shared_ptr
155  */
Unmarshalling(Parcel & parcel)156 AbilityStartSetting *AbilityStartSetting::Unmarshalling(Parcel &parcel)
157 {
158     AbilityStartSetting *abilityStartSetting = new (std::nothrow) AbilityStartSetting();
159     if (abilityStartSetting == nullptr) {
160         return nullptr;
161     }
162     // 1. Number of key value pairs read
163     uint32_t size = 0;
164     parcel.ReadUint32(size);
165 
166     if (size > CYCLE_LIMIT) {
167         delete abilityStartSetting;
168         return nullptr;
169     }
170 
171     std::u16string keyReadString16;
172     std::u16string dataReadString16;
173     for (size_t i = 0; (i < size) && abilityStartSetting; i++) {
174         // 1.key
175         keyReadString16 = parcel.ReadString16();
176         // 2.data content
177         dataReadString16 = parcel.ReadString16();
178         abilityStartSetting->abilityStarKey_[Str16ToStr8(keyReadString16)] = Str16ToStr8(dataReadString16);
179         keyReadString16.clear();
180         dataReadString16.clear();
181     }
182 
183     return abilityStartSetting;
184 }
185 }  // namespace AAFwk
186 }  // namespace OHOS
187