1 /*
2  * Copyright (c) 2024 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 <file_ex.h>
17 
18 #include "system_upgrade_scene_recognizer.h"
19 #include "ffrt_inner.h"
20 #include "parameters.h"
21 #include "plugin_mgr.h"
22 #include "res_sched_log.h"
23 #include "res_sched_mgr.h"
24 #include "res_type.h"
25 
26 
27 namespace OHOS {
28 namespace ResourceSchedule {
29 namespace {
30     static const std::string OLD_SYSTEM_FINGERPRINT_PATH = "/data/service/el1/public/ressched/device_version";
31     static const std::string SEPARATOR = "|";
32     const std::vector<std::string> FINGERPRINTS = {
33         "const.product.software.version",
34         "const.product.build.type",
35         "const.product.brand",
36         "const.product.name",
37         "const.product.devicetype",
38         "const.product.incremental.version",
39         "const.comp.hl.product_base_version.real"
40     };
41 }
42 
~SystemUpgradeSceneRecognizer()43 SystemUpgradeSceneRecognizer::~SystemUpgradeSceneRecognizer()
44 {
45     RESSCHED_LOGI("~UpdatingSceneRecognizer");
46 }
47 
SystemUpgradeSceneRecognizer()48 SystemUpgradeSceneRecognizer::SystemUpgradeSceneRecognizer()
49 {
50     Init();
51 }
52 
Init()53 void SystemUpgradeSceneRecognizer::Init()
54 {
55     std::string oldSystemFingerprint;
56     OHOS::LoadStringFromFile(OLD_SYSTEM_FINGERPRINT_PATH, oldSystemFingerprint);
57     std::string curSystemFingerprint = GetCurSystemFingerprint();
58     isSystemUpgraded_ = oldSystemFingerprint.empty() || (oldSystemFingerprint != curSystemFingerprint);
59     if (isSystemUpgraded_) {
60         OHOS::SaveStringToFile(OLD_SYSTEM_FINGERPRINT_PATH, curSystemFingerprint, true);
61     }
62 }
63 
GetCurSystemFingerprint()64 std::string SystemUpgradeSceneRecognizer::GetCurSystemFingerprint()
65 {
66     std::string curSystemFingerprint;
67     for (const auto &item : FINGERPRINTS) {
68         std::string itemFingerprint = OHOS::system::GetParameter(item, "");
69         if (itemFingerprint.empty()) {
70             continue;
71         }
72         if (!curSystemFingerprint.empty()) {
73             curSystemFingerprint.append(SEPARATOR);
74         }
75         curSystemFingerprint.append(itemFingerprint);
76     }
77     return curSystemFingerprint;
78 }
79 
OnDispatchResource(uint32_t resType,int64_t value,const nlohmann::json & payload)80 void SystemUpgradeSceneRecognizer::OnDispatchResource(uint32_t resType, int64_t value, const nlohmann::json& payload)
81 {
82     if (resType != ResType::RES_TYPE_BOOT_COMPLETED) {
83         return;
84     }
85     if (isSystemUpgraded_) {
86         RESSCHED_LOGI("enter updating scene");
87         nlohmann::json payload;
88         ResSchedMgr::GetInstance().ReportData(ResType::RES_TYPE_FIRST_BOOT_AFTER_SYSTEM_UPGRADE, 0, payload);
89     }
90 }
91 } // namespace ResourceSchedule
92 } // namespace OHOS