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 #ifndef OHOS_CLOUD_ADVERTISING_LOAD_AD_H
17 #define OHOS_CLOUD_ADVERTISING_LOAD_AD_H
18 
19 #include <mutex>
20 #include <string>
21 
22 #include "request_data.h"
23 #include "bundle_mgr_interface.h"
24 #include "ability_connect_callback_stub.h"
25 #include "system_ability.h"
26 #include "errors.h"
27 #include "refbase.h"
28 #include "ad_load_proxy.h"
29 #include "iad_load_proxy.h"
30 #include "iad_load_callback.h"
31 #include "iad_request_body.h"
32 
33 namespace OHOS {
34 namespace CloudNapi {
35 namespace AdsNapi {
36 struct AdServiceElementName {
37     std::string bundleName;
38     std::string extensionName;
39     std::string apiServiceName;
40     int userId;
41 };
42 
43 class AdLoadService : public RefBase {
44 public:
45     static sptr<AdLoadService> GetInstance();
46     AdLoadService();
47     ~AdLoadService();
48     ErrCode LoadAd(const std::string &request, const std::string &options, const sptr<Cloud::IAdLoadCallback> &callback,
49         int32_t loadAdType);
50     int32_t RequestAdBody(const std::string &request, const std::string &options,
51         const sptr<Cloud::IAdRequestBody> &callback);
52     void GetAdServiceElement(AdServiceElementName &adServiceElementName);
53 
54 private:
55     bool ConnectAdKit(const sptr<Cloud::AdRequestData> &data, const sptr<Cloud::IAdLoadCallback> &callback,
56         int32_t loadAdType);
57     void GetConfigItem(const char *path, AdServiceElementName &adServiceElementName);
58     static std::mutex lock_;
59     static sptr<AdLoadService> instance_;
60     AdServiceElementName adServiceElementName_;
61 };
62 
63 class AdRequestConnection : public AAFwk::AbilityConnectionStub {
64 public:
AdRequestConnection(const sptr<Cloud::AdRequestData> &data, const sptr<Cloud::IAdLoadCallback> &callback, int32_t loadAdType, AdServiceElementName &element)65     AdRequestConnection(const sptr<Cloud::AdRequestData> &data, const sptr<Cloud::IAdLoadCallback> &callback,
66         int32_t loadAdType, AdServiceElementName &element)
67         : data_(data), callback_(callback), loadAdType_(loadAdType), currAdServiceElementName_(element){};
AdRequestConnection(const sptr<Cloud::AdRequestData> &data, const sptr<Cloud::IAdRequestBody> &callback, AdServiceElementName &element)68     AdRequestConnection(const sptr<Cloud::AdRequestData> &data, const sptr<Cloud::IAdRequestBody> &callback,
69         AdServiceElementName &element)
70         : data_(data), bodyCallback_(callback), currAdServiceElementName_(element){};
71     ~AdRequestConnection() = default;
72     void OnAbilityConnectDone(const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject,
73         int32_t resultCode) override;
74     void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) override;
75 
76 private:
77     sptr<Cloud::AdRequestData> data_;
78     sptr<Cloud::IAdLoadCallback> callback_;
79     sptr<Cloud::IAdRequestBody> bodyCallback_;
80     int32_t loadAdType_{0};
81     sptr<Cloud::AdLoadSendRequestProxy> proxy_{ nullptr };
82     sptr<Cloud::AdRequestBodySendProxy> bodyProxy_{ nullptr };
83     AdServiceElementName currAdServiceElementName_;
84 };
85 } // namespace AdsNapi
86 } // namespace CloudNapi
87 } // namespace OHOS
88 #endif // OHOS_CLOUD_ADVERTISING_LOAD_AD_H