1 /*
2  * Copyright (c) 2024 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 "open_link_options.h"
17 
18 namespace OHOS {
19 namespace AAFwk {
OpenLinkOptions(const OpenLinkOptions & other)20 OpenLinkOptions::OpenLinkOptions(const OpenLinkOptions &other)
21 {
22     appLinkingOnly_ = other.appLinkingOnly_;
23     parameters_ = other.parameters_;
24 }
25 
operator =(const OpenLinkOptions & other)26 OpenLinkOptions &OpenLinkOptions::operator=(const OpenLinkOptions &other)
27 {
28     if (this != &other) {
29         appLinkingOnly_ = other.appLinkingOnly_;
30         parameters_ = other.parameters_;
31     }
32     return *this;
33 }
34 
ReadParameters(Parcel & parcel)35 bool OpenLinkOptions::ReadParameters(Parcel &parcel)
36 {
37     int empty = VALUE_NULL;
38     if (!parcel.ReadInt32(empty)) {
39         return false;
40     }
41 
42     if (empty == VALUE_OBJECT) {
43         auto params = parcel.ReadParcelable<WantParams>();
44         if (params != nullptr) {
45             SetParameters(*params);
46             delete params;
47             params = nullptr;
48         } else {
49             return false;
50         }
51     }
52 
53     return true;
54 }
55 
ReadFromParcel(Parcel & parcel)56 bool OpenLinkOptions::ReadFromParcel(Parcel &parcel)
57 {
58     bool appLinkingOnly;
59     if (!parcel.ReadBool(appLinkingOnly)) {
60         return false;
61     }
62     SetAppLinkingOnly(appLinkingOnly);
63 
64     if (!ReadParameters(parcel)) {
65         return false;
66     }
67 
68     return true;
69 }
70 
Unmarshalling(Parcel & parcel)71 OpenLinkOptions *OpenLinkOptions::Unmarshalling(Parcel &parcel)
72 {
73     OpenLinkOptions *option = new (std::nothrow) OpenLinkOptions();
74     if (option == nullptr) {
75         return nullptr;
76     }
77 
78     if (!option->ReadFromParcel(parcel)) {
79         delete option;
80         option = nullptr;
81     }
82 
83     return option;
84 }
85 
WriteParameters(const WantParams & parameters,Parcel & parcel) const86 bool OpenLinkOptions::WriteParameters(const WantParams &parameters, Parcel &parcel) const
87 {
88     if (parameters.Size() == 0) {
89         if (!parcel.WriteInt32(VALUE_NULL)) {
90             return false;
91         }
92     } else {
93         if (!parcel.WriteInt32(VALUE_OBJECT)) {
94             return false;
95         }
96         if (!parcel.WriteParcelable(&parameters)) {
97             return false;
98         }
99     }
100 
101     return true;
102 }
103 
Marshalling(Parcel & parcel) const104 bool OpenLinkOptions::Marshalling(Parcel &parcel) const
105 {
106     // write GetAppLinkingOnly
107     if (!parcel.WriteBool(GetAppLinkingOnly())) {
108         return false;
109     }
110     // write parameters
111     if (!WriteParameters(GetParameters(), parcel)) {
112         return false;
113     }
114 
115     return true;
116 }
117 
SetAppLinkingOnly(bool appLinkingOnly)118 void OpenLinkOptions::SetAppLinkingOnly(bool appLinkingOnly)
119 {
120     appLinkingOnly_ = appLinkingOnly;
121 }
122 
GetAppLinkingOnly() const123 bool OpenLinkOptions::GetAppLinkingOnly() const
124 {
125     return appLinkingOnly_;
126 }
127 
SetParameters(WantParams parameters)128 void OpenLinkOptions::SetParameters(WantParams parameters)
129 {
130     parameters_ = parameters;
131 }
132 
GetParameters() const133 WantParams OpenLinkOptions::GetParameters() const
134 {
135     return parameters_;
136 }
137 }  // namespace AAFwk
138 }  // namespace OHOS
139