1 /*
2 * Copyright (c) 2025 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 "bundle_active_event_reporter.h"
17 #include "file_ex.h"
18 #include "hisysevent.h"
19 #include "bundle_active_log.h"
20 #include "bundle_active_util.h"
21 #include "bundle_active_constant.h"
22
23
24 namespace OHOS {
25 namespace DeviceUsageStats {
26 namespace {
27 static const int64_t FIRST_REPORT_TIME = 10 * 1000 * 1000;
28 static const int64_t ONE_DAY_MICRO_SECOND = static_cast<int64_t>(24) * 60 * 60 * 1000 * 1000;
29 static const char* DATA_FILE_PATH = "/data";
30 static const char* FILE_SIZE_REPORTER_RECORDER = "/data/service/el1/public/bundle_usage/file_size_report_time";
31 }
32
33 IMPLEMENT_SINGLE_INSTANCE(BundleActiveEventReporter);
34
ReportFileSizeEvent()35 void BundleActiveEventReporter::ReportFileSizeEvent()
36 {
37 std::lock_guard<ffrt::mutex> autoLock(mutex_);
38 if (!isTaskSubmit_) {
39 fileSizeRecorderName_ = std::string(FILE_SIZE_REPORTER_RECORDER);
40 SubmitDelayTask(FIRST_REPORT_TIME);
41 isTaskSubmit_ = true;
42 }
43 }
44
SubmitDelayTask(int64_t delayTime)45 void BundleActiveEventReporter::SubmitDelayTask(int64_t delayTime)
46 {
47 // LCOV_EXCL_START
48 ffrt::submit([]() {
49 BundleActiveEventReporter::GetInstance().ReportFileSizeDaily();
50 }, ffrt::task_attr().delay(delayTime));
51 // LCOV_EXCL_STOP
52 }
53
54 // LCOV_EXCL_START
ReportFileSizeDaily()55 void BundleActiveEventReporter::ReportFileSizeDaily()
56 {
57 std::string lastReportTime;
58 LoadStringFromFile(fileSizeRecorderName_, lastReportTime);
59 if (lastReportTime.empty()) {
60 ReportFileSizeInner();
61 } else {
62 int64_t lastReportTimeValue = BundleActiveUtil::StringToInt64(lastReportTime);
63 if (lastReportTimeValue <= 0) {
64 ReportFileSizeInner();
65 } else {
66 int64_t nowTime = BundleActiveUtil::GetSteadyTime();
67 if (nowTime - lastReportTimeValue < ONE_DAY_MICRO_SECOND) {
68 int64_t nextReportTime = ONE_DAY_MICRO_SECOND - (nowTime - lastReportTimeValue);
69 SubmitDelayTask(nextReportTime);
70 } else {
71 ReportFileSizeInner();
72 }
73 }
74 }
75 }
76
ReportFileSizeInner()77 void BundleActiveEventReporter::ReportFileSizeInner()
78 {
79 BUNDLE_ACTIVE_LOGI("ReportFileSizeInner call");
80 std::lock_guard<ffrt::mutex> autoLock(mutex_);
81 std::vector<std::string> filesPath;
82 std::vector<uint64_t> filesPathSize;
83 filesPath.emplace_back(BUNDLE_ACTIVE_DATABASE_DIR);
84 filesPathSize.emplace_back(BundleActiveUtil::GetFolderOrFileSize(BUNDLE_ACTIVE_DATABASE_DIR));
85 double remainSize = BundleActiveUtil::GetDeviceValidSize(DATA_FILE_PATH);
86 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::FILEMANAGEMENT, "USER_DATA_SIZE",
87 HiviewDFX::HiSysEvent::EventType::STATISTIC,
88 "COMPONENT_NAME", "device_usage_statistics",
89 "PARTITION_NAME", DATA_FILE_PATH,
90 "REMAIN_PARTITION_SIZE", remainSize,
91 "FILE_OR_FOLDER_PATH", filesPath,
92 "FILE_OR_FOLDER_SIZE", filesPathSize);
93 int64_t nowMicroTime = BundleActiveUtil::GetNowMicroTime();
94 SaveStringToFile(fileSizeRecorderName_, std::to_string(nowMicroTime));
95 SubmitDelayTask(ONE_DAY_MICRO_SECOND);
96 }
97 // LCOV_EXCL_STOP
98 } // namespace ResourceSchedule
99 } // namespace OHOS