• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "bundle_resource_event_subscriber.h"
17 
18 #include <fstream>
19 #include <thread>
20 
21 #include "app_log_wrapper.h"
22 #include "bundle_constants.h"
23 #include "bundle_parser.h"
24 #include "bundle_resource_callback.h"
25 #include "bundle_resource_constants.h"
26 #include "common_event_support.h"
27 #include "json_util.h"
28 
29 namespace OHOS {
30 namespace AppExecFwk {
31 namespace {
32 constexpr const char* OLD_USER_ID = "oldId";
33 }
BundleResourceEventSubscriber(const EventFwk::CommonEventSubscribeInfo & subscribeInfo)34 BundleResourceEventSubscriber::BundleResourceEventSubscriber(
35     const EventFwk::CommonEventSubscribeInfo &subscribeInfo) : EventFwk::CommonEventSubscriber(subscribeInfo)
36 {}
37 
~BundleResourceEventSubscriber()38 BundleResourceEventSubscriber::~BundleResourceEventSubscriber()
39 {}
40 
OnReceiveEvent(const EventFwk::CommonEventData & data)41 void BundleResourceEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &data)
42 {
43     std::string action = data.GetWant().GetAction();
44     BundleResourceCallback callback;
45     if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
46         int32_t oldUserId = Constants::UNSPECIFIED_USERID;
47         std::string oldId = data.GetWant().GetStringParam(OLD_USER_ID);
48         if (oldId.empty() || !OHOS::StrToInt(oldId, oldUserId)) {
49             APP_LOGE("oldId:%{public}s parse failed", oldId.c_str());
50             oldUserId = Constants::UNSPECIFIED_USERID;
51         }
52         int32_t userId = data.GetCode();
53         // when boot, user 0 or -1 switch to user 100, no need to flush resource rdb
54         if (((oldUserId == Constants::DEFAULT_USERID) || (oldUserId == Constants::INVALID_USERID) ||
55             (oldUserId == Constants::U1)) && !CheckUserSwitchWhenReboot(userId, oldUserId)) {
56             APP_LOGI("switch userId %{public}d to %{public}d, no need to process", oldUserId, userId);
57             return;
58         }
59         APP_LOGI("switch userId %{public}d to %{public}d", oldUserId, userId);
60         std::thread userIdChangedThread(OnUserIdChanged, oldUserId, userId);
61         userIdChangedThread.detach();
62     }
63     // for other event
64 }
65 
OnUserIdChanged(const int32_t oldUserId,const int32_t newUserId)66 void BundleResourceEventSubscriber::OnUserIdChanged(const int32_t oldUserId, const int32_t newUserId)
67 {
68     BundleResourceCallback callback;
69     callback.OnUserIdSwitched(oldUserId, newUserId);
70 }
71 
CheckUserSwitchWhenReboot(const int32_t userId,int32_t & oldUserId)72 bool BundleResourceEventSubscriber::CheckUserSwitchWhenReboot(const int32_t userId, int32_t &oldUserId)
73 {
74     std::string path = std::string(BundleResourceConstants::BUNDLE_RESOURCE_RDB_PATH) +
75         std::string(BundleResourceConstants::USER_FILE_NAME);
76     nlohmann::json jsonBuf;
77     if (!BundleParser::ReadFileIntoJson(path, jsonBuf)) {
78         APP_LOGW("read user file failed, errno %{public}d", errno);
79         return false;
80     }
81     const auto &jsonBufEnd = jsonBuf.end();
82     int32_t parseResult = ERR_OK;
83     int32_t lastUserId = 0;
84     GetValueIfFindKey<int32_t>(jsonBuf, jsonBufEnd, BundleResourceConstants::USER, lastUserId,
85         JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
86     if ((parseResult == ERR_OK) && (lastUserId != userId)) {
87         // The user before reboot and the current user do not match, need add resources.
88         oldUserId = lastUserId;
89         return true;
90     }
91     return false;
92 }
93 }  // namespace AppExecFwk
94 }  // namespace OHOS
95