1c07ff4deSopenharmony_ci/*
2c07ff4deSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3c07ff4deSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4c07ff4deSopenharmony_ci * you may not use this file except in compliance with the License.
5c07ff4deSopenharmony_ci * You may obtain a copy of the License at
6c07ff4deSopenharmony_ci *
7c07ff4deSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8c07ff4deSopenharmony_ci *
9c07ff4deSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10c07ff4deSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11c07ff4deSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12c07ff4deSopenharmony_ci * See the License for the specific language governing permissions and
13c07ff4deSopenharmony_ci * limitations under the License.
14c07ff4deSopenharmony_ci */
15c07ff4deSopenharmony_ci
16c07ff4deSopenharmony_ci#include <string>
17c07ff4deSopenharmony_ci#include <vector>
18c07ff4deSopenharmony_ci#include <map>
19c07ff4deSopenharmony_ci
20c07ff4deSopenharmony_ci#include "want.h"
21c07ff4deSopenharmony_ci#include "cJSON.h"
22c07ff4deSopenharmony_ci#include "ad_constant.h"
23c07ff4deSopenharmony_ci#include "ad_hilog_wreapper.h"
24c07ff4deSopenharmony_ci#include "ad_inner_error_code.h"
25c07ff4deSopenharmony_ci#include "ad_json_util.h"
26c07ff4deSopenharmony_ci#include "ad_load_callback_stub.h"
27c07ff4deSopenharmony_ci
28c07ff4deSopenharmony_cinamespace OHOS {
29c07ff4deSopenharmony_cinamespace Cloud {
30c07ff4deSopenharmony_cistatic const int32_t LOAD_AD_SUCCESS = 200;
31c07ff4deSopenharmony_ci
32c07ff4deSopenharmony_ciAdLoadCallbackStub::AdLoadCallbackStub() {}
33c07ff4deSopenharmony_ciAdLoadCallbackStub::~AdLoadCallbackStub() {}
34c07ff4deSopenharmony_ci
35c07ff4deSopenharmony_ciinline std::string Str16ToStr8(const std::u16string &str)
36c07ff4deSopenharmony_ci{
37c07ff4deSopenharmony_ci    std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
38c07ff4deSopenharmony_ci    std::string result = convert.to_bytes(str);
39c07ff4deSopenharmony_ci    return result;
40c07ff4deSopenharmony_ci}
41c07ff4deSopenharmony_ci
42c07ff4deSopenharmony_civoid CommonParse(AAFwk::Want &want, cJSON *item)
43c07ff4deSopenharmony_ci{
44c07ff4deSopenharmony_ci    cJSON *node = nullptr;
45c07ff4deSopenharmony_ci    cJSON_ArrayForEach(node, item)
46c07ff4deSopenharmony_ci    {
47c07ff4deSopenharmony_ci        if (node == nullptr || node->string == nullptr) {
48c07ff4deSopenharmony_ci            continue;
49c07ff4deSopenharmony_ci        }
50c07ff4deSopenharmony_ci        std::string valuestring;
51c07ff4deSopenharmony_ci        bool boolValue;
52c07ff4deSopenharmony_ci        std::string defaultValue;
53c07ff4deSopenharmony_ci        switch (node->type) {
54c07ff4deSopenharmony_ci            case cJSON_Number:
55c07ff4deSopenharmony_ci                want.SetParam(node->string, cJSON_GetNumberValue(node));
56c07ff4deSopenharmony_ci                break;
57c07ff4deSopenharmony_ci            case cJSON_String:
58c07ff4deSopenharmony_ci                valuestring = node->valuestring;
59c07ff4deSopenharmony_ci                want.SetParam(node->string, valuestring);
60c07ff4deSopenharmony_ci                break;
61c07ff4deSopenharmony_ci            case cJSON_True:
62c07ff4deSopenharmony_ci            case cJSON_False:
63c07ff4deSopenharmony_ci                boolValue = cJSON_IsTrue(node);
64c07ff4deSopenharmony_ci                want.SetParam(node->string, boolValue);
65c07ff4deSopenharmony_ci                break;
66c07ff4deSopenharmony_ci            default:
67c07ff4deSopenharmony_ci                defaultValue = AdJsonUtil::ToString(node);
68c07ff4deSopenharmony_ci                want.SetParam(node->string, defaultValue);
69c07ff4deSopenharmony_ci                break;
70c07ff4deSopenharmony_ci        }
71c07ff4deSopenharmony_ci    }
72c07ff4deSopenharmony_ci}
73c07ff4deSopenharmony_ci
74c07ff4deSopenharmony_ciinline void ParseSingleAd(std::vector<AAFwk::Want> &ads, cJSON *item)
75c07ff4deSopenharmony_ci{
76c07ff4deSopenharmony_ci    AAFwk::Want want;
77c07ff4deSopenharmony_ci    CommonParse(want, item);
78c07ff4deSopenharmony_ci    ads.emplace_back(want);
79c07ff4deSopenharmony_ci}
80c07ff4deSopenharmony_ci
81c07ff4deSopenharmony_civoid ParseAdArray(std::string adsString, std::vector<AAFwk::Want> &ads)
82c07ff4deSopenharmony_ci{
83c07ff4deSopenharmony_ci    cJSON *root = cJSON_Parse(adsString.c_str());
84c07ff4deSopenharmony_ci    if (!AdJsonUtil::IsValid(root)) {
85c07ff4deSopenharmony_ci        ADS_HILOGE(OHOS::Cloud::ADS_MODULE_COMMON, "parse kit return ad array failed");
86c07ff4deSopenharmony_ci        cJSON_Delete(root);
87c07ff4deSopenharmony_ci        return;
88c07ff4deSopenharmony_ci    }
89c07ff4deSopenharmony_ci    if (cJSON_IsArray(root)) {
90c07ff4deSopenharmony_ci        int size = cJSON_GetArraySize(root);
91c07ff4deSopenharmony_ci        ADS_HILOGI(OHOS::Cloud::ADS_MODULE_COMMON, "ads size is: %{public}d.", size);
92c07ff4deSopenharmony_ci        for (int i = 0; i < size; i++) {
93c07ff4deSopenharmony_ci            cJSON *item = cJSON_GetArrayItem(root, i);
94c07ff4deSopenharmony_ci            ParseSingleAd(ads, item);
95c07ff4deSopenharmony_ci        }
96c07ff4deSopenharmony_ci    }
97c07ff4deSopenharmony_ci    cJSON_Delete(root);
98c07ff4deSopenharmony_ci}
99c07ff4deSopenharmony_ci
100c07ff4deSopenharmony_civoid ParseAdMap(std::string adsString, std::map<std::string, std::vector<AAFwk::Want>> &ads)
101c07ff4deSopenharmony_ci{
102c07ff4deSopenharmony_ci    ADS_HILOGI(OHOS::Cloud::ADS_MODULE_COMMON, "multi solts kit return enter");
103c07ff4deSopenharmony_ci    cJSON *root = cJSON_Parse(adsString.c_str());
104c07ff4deSopenharmony_ci    if (!AdJsonUtil::IsValid(root)) {
105c07ff4deSopenharmony_ci        ADS_HILOGE(OHOS::Cloud::ADS_MODULE_COMMON, "parse kit return ad map failed");
106c07ff4deSopenharmony_ci        cJSON_Delete(root);
107c07ff4deSopenharmony_ci        return;
108c07ff4deSopenharmony_ci    }
109c07ff4deSopenharmony_ci    cJSON *item = nullptr;
110c07ff4deSopenharmony_ci    cJSON_ArrayForEach(item, root)
111c07ff4deSopenharmony_ci    {
112c07ff4deSopenharmony_ci        if (item == nullptr || item->string == nullptr) {
113c07ff4deSopenharmony_ci            continue;
114c07ff4deSopenharmony_ci        }
115c07ff4deSopenharmony_ci        std::string key = item->string;
116c07ff4deSopenharmony_ci        std::vector<AAFwk::Want> want;
117c07ff4deSopenharmony_ci        std::string value = AdJsonUtil::ToString(item);
118c07ff4deSopenharmony_ci        ParseAdArray(value, want);
119c07ff4deSopenharmony_ci        ads[key] = want;
120c07ff4deSopenharmony_ci    }
121c07ff4deSopenharmony_ci    cJSON_Delete(root);
122c07ff4deSopenharmony_ci}
123c07ff4deSopenharmony_ci
124c07ff4deSopenharmony_ciint AdLoadCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
125c07ff4deSopenharmony_ci{
126c07ff4deSopenharmony_ci    switch (code) {
127c07ff4deSopenharmony_ci        case static_cast<uint32_t>(IAdLoadCallback::Message::AD_LOAD): {
128c07ff4deSopenharmony_ci            int32_t resultCode = data.ReadInt32();
129c07ff4deSopenharmony_ci            ADS_HILOGI(OHOS::Cloud::ADS_MODULE_COMMON, "single slot kit return code = %{public}u", resultCode);
130c07ff4deSopenharmony_ci            std::string resultMsg = Str16ToStr8(data.ReadString16());
131c07ff4deSopenharmony_ci            if (resultCode == LOAD_AD_SUCCESS) {
132c07ff4deSopenharmony_ci                OnAdLoadSuccess(resultMsg);
133c07ff4deSopenharmony_ci            } else {
134c07ff4deSopenharmony_ci                OnAdLoadFailure(resultCode, resultMsg);
135c07ff4deSopenharmony_ci            }
136c07ff4deSopenharmony_ci            break;
137c07ff4deSopenharmony_ci        }
138c07ff4deSopenharmony_ci        case static_cast<uint32_t>(IAdLoadCallback::Message::MULTI_AD_LOAD): {
139c07ff4deSopenharmony_ci            int32_t resultCode = data.ReadInt32();
140c07ff4deSopenharmony_ci            ADS_HILOGI(OHOS::Cloud::ADS_MODULE_COMMON, "multi slots kit return code = %{public}u", resultCode);
141c07ff4deSopenharmony_ci            std::string msg = Str16ToStr8(data.ReadString16());
142c07ff4deSopenharmony_ci            if (resultCode == LOAD_AD_SUCCESS) {
143c07ff4deSopenharmony_ci                OnAdLoadMultiSlotsSuccess(msg);
144c07ff4deSopenharmony_ci            } else {
145c07ff4deSopenharmony_ci                OnAdLoadFailure(resultCode, msg);
146c07ff4deSopenharmony_ci            }
147c07ff4deSopenharmony_ci            break;
148c07ff4deSopenharmony_ci        }
149c07ff4deSopenharmony_ci        default:
150c07ff4deSopenharmony_ci            ADS_HILOGI(OHOS::Cloud::ADS_MODULE_COMMON, "default, code = %{public}u, flags = %{public}u", code,
151c07ff4deSopenharmony_ci                option.GetFlags());
152c07ff4deSopenharmony_ci            return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
153c07ff4deSopenharmony_ci    }
154c07ff4deSopenharmony_ci    return ERR_NONE;
155c07ff4deSopenharmony_ci}
156c07ff4deSopenharmony_ci} // namespace Cloud
157c07ff4deSopenharmony_ci} // namespace OHOS