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 #include "core/common/recorder/exposure_processor.h"
16
17 #include "core/common/recorder/event_recorder.h"
18 #include "core/common/recorder/node_data_cache.h"
19
20 namespace OHOS::Ace::Recorder {
ExposureProcessor(const std::string & pageUrl,const std::string & inspectorId)21 ExposureProcessor::ExposureProcessor(const std::string& pageUrl, const std::string& inspectorId) : pageUrl_(pageUrl)
22 {
23 if (!inspectorId.empty()) {
24 NodeDataCache::Get().GetExposureCfg(pageUrl, inspectorId, cfg_);
25 }
26 }
27
ExposureProcessor(const std::string & pageUrl,const std::string & inspectorId,double ratio,int duration)28 ExposureProcessor::ExposureProcessor(
29 const std::string& pageUrl, const std::string& inspectorId, double ratio, int duration)
30 : cfg_({ inspectorId, ratio, duration }), pageUrl_(pageUrl)
31 {}
32
ExposureProcessor(const RefPtr<ExposureProcessor> & processor)33 ExposureProcessor::ExposureProcessor(const RefPtr<ExposureProcessor>& processor)
34 {
35 pageUrl_ = processor->pageUrl_;
36 cfg_.id = processor->cfg_.id;
37 cfg_.ratio = processor->cfg_.ratio;
38 cfg_.duration = processor->cfg_.duration;
39 containerId_ = processor->containerId_;
40 }
41
IsNeedRecord() const42 bool ExposureProcessor::IsNeedRecord() const
43 {
44 return !cfg_.id.empty();
45 }
46
GetRatio() const47 double ExposureProcessor::GetRatio() const
48 {
49 return cfg_.ratio;
50 }
51
OnVisibleChange(bool isVisible,const std::string & param)52 void ExposureProcessor::OnVisibleChange(bool isVisible, const std::string& param)
53 {
54 auto current = GetCurrentTimestamp();
55 if (isVisible) {
56 startTime_ = current;
57 navDstName_ = EventRecorder::Get().GetNavDstName();
58 } else if (startTime_ > 0) {
59 auto duration = current - startTime_;
60 if (duration >= cfg_.duration) {
61 EventParamsBuilder builder;
62 builder.SetId(cfg_.id)
63 .SetDescription(param)
64 .SetPageUrl(pageUrl_)
65 .SetNavDst(navDstName_)
66 .SetExtra(KEY_DURATION, std::to_string(duration));
67 EventRecorder::Get().OnExposure(std::move(builder));
68 }
69 }
70 }
71 } // namespace OHOS::Ace::Recorder
72