1eace7efcSopenharmony_ci/*
2eace7efcSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
3eace7efcSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4eace7efcSopenharmony_ci * you may not use this file except in compliance with the License.
5eace7efcSopenharmony_ci * You may obtain a copy of the License at
6eace7efcSopenharmony_ci *
7eace7efcSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8eace7efcSopenharmony_ci *
9eace7efcSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10eace7efcSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11eace7efcSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12eace7efcSopenharmony_ci * See the License for the specific language governing permissions and
13eace7efcSopenharmony_ci * limitations under the License.
14eace7efcSopenharmony_ci */
15eace7efcSopenharmony_ci
16eace7efcSopenharmony_ci#include "ability_start_setting.h"
17eace7efcSopenharmony_ci
18eace7efcSopenharmony_ci#include "string_ex.h"
19eace7efcSopenharmony_ci
20eace7efcSopenharmony_ciusing namespace OHOS;
21eace7efcSopenharmony_ci
22eace7efcSopenharmony_cinamespace OHOS {
23eace7efcSopenharmony_cinamespace AAFwk {
24eace7efcSopenharmony_ciconst std::string AbilityStartSetting::BOUNDS_KEY = "bounds";
25eace7efcSopenharmony_ciconst std::string AbilityStartSetting::WINDOW_DISPLAY_ID_KEY = "windowId";
26eace7efcSopenharmony_ciconst std::string AbilityStartSetting::WINDOW_MODE_KEY = "windowMode";
27eace7efcSopenharmony_ciconst std::string AbilityStartSetting::IS_START_BY_SCB_KEY = "isStartByScb";
28eace7efcSopenharmony_ciconstexpr uint32_t CYCLE_LIMIT = 1000;
29eace7efcSopenharmony_ci
30eace7efcSopenharmony_ci/**
31eace7efcSopenharmony_ci * @brief Construct copy function.
32eace7efcSopenharmony_ci * @param other indicates instance of abilitystartsetting object
33eace7efcSopenharmony_ci * @return none.
34eace7efcSopenharmony_ci */
35eace7efcSopenharmony_ciAbilityStartSetting::AbilityStartSetting(const AbilityStartSetting &other)
36eace7efcSopenharmony_ci{
37eace7efcSopenharmony_ci    abilityStarKey_.clear();
38eace7efcSopenharmony_ci    abilityStarKey_ = other.abilityStarKey_;
39eace7efcSopenharmony_ci}
40eace7efcSopenharmony_ci/**
41eace7efcSopenharmony_ci * @brief Overload assignment operation.
42eace7efcSopenharmony_ci * @param other indicates instance of abilitystartsetting object.
43eace7efcSopenharmony_ci * @return Returns current instance of abilitystartsetting object.
44eace7efcSopenharmony_ci */
45eace7efcSopenharmony_ciAbilityStartSetting &AbilityStartSetting::operator=(const AbilityStartSetting &other)
46eace7efcSopenharmony_ci{
47eace7efcSopenharmony_ci    if (this != &other) {
48eace7efcSopenharmony_ci        abilityStarKey_.clear();
49eace7efcSopenharmony_ci        abilityStarKey_ = other.abilityStarKey_;
50eace7efcSopenharmony_ci    }
51eace7efcSopenharmony_ci    return *this;
52eace7efcSopenharmony_ci}
53eace7efcSopenharmony_ci/**
54eace7efcSopenharmony_ci * @brief Inner function to create AbilityStartSetting
55eace7efcSopenharmony_ci *
56eace7efcSopenharmony_ci * @return Returns the shared_ptr of AbilityStartSetting object.
57eace7efcSopenharmony_ci */
58eace7efcSopenharmony_cistd::shared_ptr<AbilityStartSetting> AbilityStartSettingCreator()
59eace7efcSopenharmony_ci{
60eace7efcSopenharmony_ci    std::shared_ptr<AbilityStartSetting> abilityStartSetting {new (std::nothrow) AbilityStartSetting()};
61eace7efcSopenharmony_ci    return abilityStartSetting;
62eace7efcSopenharmony_ci}
63eace7efcSopenharmony_ci
64eace7efcSopenharmony_ci/**
65eace7efcSopenharmony_ci * @brief Obtains an empty AbilityStartSetting object.
66eace7efcSopenharmony_ci *
67eace7efcSopenharmony_ci * @return Returns the btains an empty AbilityStartSetting object.
68eace7efcSopenharmony_ci */
69eace7efcSopenharmony_cistd::shared_ptr<AbilityStartSetting> AbilityStartSetting::GetEmptySetting()
70eace7efcSopenharmony_ci{
71eace7efcSopenharmony_ci    return AbilityStartSettingCreator();
72eace7efcSopenharmony_ci}
73eace7efcSopenharmony_ci
74eace7efcSopenharmony_ci/**
75eace7efcSopenharmony_ci * @brief Obtains the names of all the attributes that have been added to this AbilityStartSetting object.
76eace7efcSopenharmony_ci *
77eace7efcSopenharmony_ci * @return Returns the set of attribute names included in this AbilityStartSetting object.
78eace7efcSopenharmony_ci */
79eace7efcSopenharmony_cistd::set<std::string> AbilityStartSetting::GetPropertiesKey()
80eace7efcSopenharmony_ci{
81eace7efcSopenharmony_ci    std::set<std::string> abilityStartSet;
82eace7efcSopenharmony_ci    abilityStartSet.clear();
83eace7efcSopenharmony_ci
84eace7efcSopenharmony_ci    for (auto it : abilityStarKey_) {
85eace7efcSopenharmony_ci        abilityStartSet.emplace(it.first);
86eace7efcSopenharmony_ci    }
87eace7efcSopenharmony_ci    return abilityStartSet;
88eace7efcSopenharmony_ci}
89eace7efcSopenharmony_ci
90eace7efcSopenharmony_ci/**
91eace7efcSopenharmony_ci * @brief Checks whether this AbilityStartSetting object is empty.
92eace7efcSopenharmony_ci *
93eace7efcSopenharmony_ci * @return Returns true if this AbilityStartSetting object is empty and animatorOption is null; returns false otherwise.
94eace7efcSopenharmony_ci */
95eace7efcSopenharmony_cibool AbilityStartSetting::IsEmpty()
96eace7efcSopenharmony_ci{
97eace7efcSopenharmony_ci    return (abilityStarKey_.size() == 0);
98eace7efcSopenharmony_ci}
99eace7efcSopenharmony_ci
100eace7efcSopenharmony_ci/**
101eace7efcSopenharmony_ci * @brief Sets the names of all the attributes of the AbilityStartSetting object.
102eace7efcSopenharmony_ci *
103eace7efcSopenharmony_ci * @param key Indicates the name of the key.
104eace7efcSopenharmony_ci * @param value The window display mode of the values.
105eace7efcSopenharmony_ci */
106eace7efcSopenharmony_civoid AbilityStartSetting::AddProperty(const std::string &key, const std::string &value)
107eace7efcSopenharmony_ci{
108eace7efcSopenharmony_ci    abilityStarKey_[key] = value;
109eace7efcSopenharmony_ci}
110eace7efcSopenharmony_ci
111eace7efcSopenharmony_ci/**
112eace7efcSopenharmony_ci * @brief Gets the name of the attributes of the AbilityStartSetting object.
113eace7efcSopenharmony_ci *
114eace7efcSopenharmony_ci * @param key Indicates the name of the key.
115eace7efcSopenharmony_ci * @return Returns value Indicates the value of the attributes of the AbilityStartSetting object
116eace7efcSopenharmony_ci */
117eace7efcSopenharmony_cistd::string AbilityStartSetting::GetProperty(const std::string &key)
118eace7efcSopenharmony_ci{
119eace7efcSopenharmony_ci    auto it = abilityStarKey_.find(key);
120eace7efcSopenharmony_ci    if (it == abilityStarKey_.end()) {
121eace7efcSopenharmony_ci        return std::string();
122eace7efcSopenharmony_ci    }
123eace7efcSopenharmony_ci    return abilityStarKey_[key];
124eace7efcSopenharmony_ci}
125eace7efcSopenharmony_ci
126eace7efcSopenharmony_ci/**
127eace7efcSopenharmony_ci * @brief Write the data of AbilityStartSetting to the file stream
128eace7efcSopenharmony_ci * @param parcel indicates write the data of AbilityStartSetting to the file stream through parcel
129eace7efcSopenharmony_ci * @return bool
130eace7efcSopenharmony_ci */
131eace7efcSopenharmony_cibool AbilityStartSetting::Marshalling(Parcel &parcel) const
132eace7efcSopenharmony_ci{
133eace7efcSopenharmony_ci    size_t size = abilityStarKey_.size();
134eace7efcSopenharmony_ci
135eace7efcSopenharmony_ci    // 1. Number of key value pairs written
136eace7efcSopenharmony_ci    parcel.WriteUint32((uint32_t)size);
137eace7efcSopenharmony_ci
138eace7efcSopenharmony_ci    std::map<std::string, std::string>::const_iterator it;
139eace7efcSopenharmony_ci
140eace7efcSopenharmony_ci    // 2. Write the key and value strings
141eace7efcSopenharmony_ci    for (auto pair : abilityStarKey_) {
142eace7efcSopenharmony_ci        // 1.key
143eace7efcSopenharmony_ci        parcel.WriteString16(Str8ToStr16(pair.first));
144eace7efcSopenharmony_ci        // 2.data content
145eace7efcSopenharmony_ci        parcel.WriteString16(Str8ToStr16(pair.second));
146eace7efcSopenharmony_ci    }
147eace7efcSopenharmony_ci
148eace7efcSopenharmony_ci    return true;
149eace7efcSopenharmony_ci}
150eace7efcSopenharmony_ci
151eace7efcSopenharmony_ci/**
152eace7efcSopenharmony_ci * @brief Reading file stream through parcel to generate AbilityStartSetting instance
153eace7efcSopenharmony_ci * @param parcel indicates reading file stream through parcel to generate AbilityStartSetting instance
154eace7efcSopenharmony_ci * @return AbilityStartSetting shared_ptr
155eace7efcSopenharmony_ci */
156eace7efcSopenharmony_ciAbilityStartSetting *AbilityStartSetting::Unmarshalling(Parcel &parcel)
157eace7efcSopenharmony_ci{
158eace7efcSopenharmony_ci    AbilityStartSetting *abilityStartSetting = new (std::nothrow) AbilityStartSetting();
159eace7efcSopenharmony_ci    if (abilityStartSetting == nullptr) {
160eace7efcSopenharmony_ci        return nullptr;
161eace7efcSopenharmony_ci    }
162eace7efcSopenharmony_ci    // 1. Number of key value pairs read
163eace7efcSopenharmony_ci    uint32_t size = 0;
164eace7efcSopenharmony_ci    parcel.ReadUint32(size);
165eace7efcSopenharmony_ci
166eace7efcSopenharmony_ci    if (size > CYCLE_LIMIT) {
167eace7efcSopenharmony_ci        delete abilityStartSetting;
168eace7efcSopenharmony_ci        return nullptr;
169eace7efcSopenharmony_ci    }
170eace7efcSopenharmony_ci
171eace7efcSopenharmony_ci    std::u16string keyReadString16;
172eace7efcSopenharmony_ci    std::u16string dataReadString16;
173eace7efcSopenharmony_ci    for (size_t i = 0; (i < size) && abilityStartSetting; i++) {
174eace7efcSopenharmony_ci        // 1.key
175eace7efcSopenharmony_ci        keyReadString16 = parcel.ReadString16();
176eace7efcSopenharmony_ci        // 2.data content
177eace7efcSopenharmony_ci        dataReadString16 = parcel.ReadString16();
178eace7efcSopenharmony_ci        abilityStartSetting->abilityStarKey_[Str16ToStr8(keyReadString16)] = Str16ToStr8(dataReadString16);
179eace7efcSopenharmony_ci        keyReadString16.clear();
180eace7efcSopenharmony_ci        dataReadString16.clear();
181eace7efcSopenharmony_ci    }
182eace7efcSopenharmony_ci
183eace7efcSopenharmony_ci    return abilityStartSetting;
184eace7efcSopenharmony_ci}
185eace7efcSopenharmony_ci}  // namespace AAFwk
186eace7efcSopenharmony_ci}  // namespace OHOS
187