1 /*
2 * Copyright (c) 2023 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 <string>
17 #include <vector>
18 #include <map>
19
20 #include "want.h"
21 #include "cJSON.h"
22 #include "ad_constant.h"
23 #include "ad_hilog_wreapper.h"
24 #include "ad_inner_error_code.h"
25 #include "ad_json_util.h"
26 #include "ad_load_callback_stub.h"
27
28 namespace OHOS {
29 namespace Cloud {
30 static const int32_t LOAD_AD_SUCCESS = 200;
31
AdLoadCallbackStub()32 AdLoadCallbackStub::AdLoadCallbackStub() {}
~AdLoadCallbackStub()33 AdLoadCallbackStub::~AdLoadCallbackStub() {}
34
Str16ToStr8(const std::u16string &str)35 inline std::string Str16ToStr8(const std::u16string &str)
36 {
37 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
38 std::string result = convert.to_bytes(str);
39 return result;
40 }
41
CommonParse(AAFwk::Want &want, cJSON *item)42 void CommonParse(AAFwk::Want &want, cJSON *item)
43 {
44 cJSON *node = nullptr;
45 cJSON_ArrayForEach(node, item)
46 {
47 if (node == nullptr || node->string == nullptr) {
48 continue;
49 }
50 std::string valuestring;
51 bool boolValue;
52 std::string defaultValue;
53 switch (node->type) {
54 case cJSON_Number:
55 want.SetParam(node->string, cJSON_GetNumberValue(node));
56 break;
57 case cJSON_String:
58 valuestring = node->valuestring;
59 want.SetParam(node->string, valuestring);
60 break;
61 case cJSON_True:
62 case cJSON_False:
63 boolValue = cJSON_IsTrue(node);
64 want.SetParam(node->string, boolValue);
65 break;
66 default:
67 defaultValue = AdJsonUtil::ToString(node);
68 want.SetParam(node->string, defaultValue);
69 break;
70 }
71 }
72 }
73
ParseSingleAd(std::vector<AAFwk::Want> &ads, cJSON *item)74 inline void ParseSingleAd(std::vector<AAFwk::Want> &ads, cJSON *item)
75 {
76 AAFwk::Want want;
77 CommonParse(want, item);
78 ads.emplace_back(want);
79 }
80
ParseAdArray(std::string adsString, std::vector<AAFwk::Want> &ads)81 void ParseAdArray(std::string adsString, std::vector<AAFwk::Want> &ads)
82 {
83 cJSON *root = cJSON_Parse(adsString.c_str());
84 if (!AdJsonUtil::IsValid(root)) {
85 ADS_HILOGE(OHOS::Cloud::ADS_MODULE_COMMON, "parse kit return ad array failed");
86 cJSON_Delete(root);
87 return;
88 }
89 if (cJSON_IsArray(root)) {
90 int size = cJSON_GetArraySize(root);
91 ADS_HILOGI(OHOS::Cloud::ADS_MODULE_COMMON, "ads size is: %{public}d.", size);
92 for (int i = 0; i < size; i++) {
93 cJSON *item = cJSON_GetArrayItem(root, i);
94 ParseSingleAd(ads, item);
95 }
96 }
97 cJSON_Delete(root);
98 }
99
ParseAdMap(std::string adsString, std::map<std::string, std::vector<AAFwk::Want>> &ads)100 void ParseAdMap(std::string adsString, std::map<std::string, std::vector<AAFwk::Want>> &ads)
101 {
102 ADS_HILOGI(OHOS::Cloud::ADS_MODULE_COMMON, "multi solts kit return enter");
103 cJSON *root = cJSON_Parse(adsString.c_str());
104 if (!AdJsonUtil::IsValid(root)) {
105 ADS_HILOGE(OHOS::Cloud::ADS_MODULE_COMMON, "parse kit return ad map failed");
106 cJSON_Delete(root);
107 return;
108 }
109 cJSON *item = nullptr;
110 cJSON_ArrayForEach(item, root)
111 {
112 if (item == nullptr || item->string == nullptr) {
113 continue;
114 }
115 std::string key = item->string;
116 std::vector<AAFwk::Want> want;
117 std::string value = AdJsonUtil::ToString(item);
118 ParseAdArray(value, want);
119 ads[key] = want;
120 }
121 cJSON_Delete(root);
122 }
123
OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)124 int AdLoadCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
125 {
126 switch (code) {
127 case static_cast<uint32_t>(IAdLoadCallback::Message::AD_LOAD): {
128 int32_t resultCode = data.ReadInt32();
129 ADS_HILOGI(OHOS::Cloud::ADS_MODULE_COMMON, "single slot kit return code = %{public}u", resultCode);
130 std::string resultMsg = Str16ToStr8(data.ReadString16());
131 if (resultCode == LOAD_AD_SUCCESS) {
132 OnAdLoadSuccess(resultMsg);
133 } else {
134 OnAdLoadFailure(resultCode, resultMsg);
135 }
136 break;
137 }
138 case static_cast<uint32_t>(IAdLoadCallback::Message::MULTI_AD_LOAD): {
139 int32_t resultCode = data.ReadInt32();
140 ADS_HILOGI(OHOS::Cloud::ADS_MODULE_COMMON, "multi slots kit return code = %{public}u", resultCode);
141 std::string msg = Str16ToStr8(data.ReadString16());
142 if (resultCode == LOAD_AD_SUCCESS) {
143 OnAdLoadMultiSlotsSuccess(msg);
144 } else {
145 OnAdLoadFailure(resultCode, msg);
146 }
147 break;
148 }
149 default:
150 ADS_HILOGI(OHOS::Cloud::ADS_MODULE_COMMON, "default, code = %{public}u, flags = %{public}u", code,
151 option.GetFlags());
152 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
153 }
154 return ERR_NONE;
155 }
156 } // namespace Cloud
157 } // namespace OHOS