1 /*
2 * Copyright (c) 2024-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 "datashare_manager.h"
17
18 #include "distributed_sched_utils.h"
19 #include "dsched_continue_manager.h"
20 #include "dtbschedmgr_log.h"
21 #include "mission/dms_continue_condition_manager.h"
22 #include "mission/notification/dms_continue_send_manager.h"
23 #include "os_account_manager.h"
24 #include "switch_status_dependency.h"
25
26 namespace OHOS {
27 namespace DistributedSchedule {
28 IMPLEMENT_SINGLE_INSTANCE(DataShareManager);
29 namespace {
30 const std::string TAG = "DMSDataShareManager";
31 constexpr static int32_t INVALID_ACCOUNT_ID = -1;
32 }
33 SettingObserver::SettingObserver() = default;
34 SettingObserver::~SettingObserver() = default;
35
OnChange()36 void SettingObserver::OnChange()
37 {
38 HILOGI("DataShareRegisterObserver OnChange start");
39 if (observerCallback_ != nullptr) {
40 observerCallback_();
41 }
42 HILOGI("DataShareRegisterObserver OnChange done");
43 }
44
SetObserverCallback(ObserverCallback & observerCallback)45 void SettingObserver::SetObserverCallback(ObserverCallback &observerCallback)
46 {
47 observerCallback_ = observerCallback;
48 }
49
RegisterObserver(SettingObserver::ObserverCallback & observerCallback)50 void DataShareManager::RegisterObserver(SettingObserver::ObserverCallback &observerCallback)
51 {
52 std::lock_guard<std::mutex> lock(observerMutex_);
53 if (observer_ != nullptr) {
54 HILOGI("observer_ has been created.");
55 return;
56 }
57 observer_ = new SettingObserver();
58 if (observer_ == nullptr) {
59 HILOGE("Register observer failed, observer is null");
60 return;
61 }
62 observer_->SetObserverCallback(observerCallback);
63 HILOGI("observer_ create success.");
64 }
65
CreateDataShareHelper()66 std::shared_ptr<DataShare::DataShareHelper> DataShareManager::CreateDataShareHelper()
67 {
68 HILOGI("DataShareManager CreateDataShareHelper start");
69 DataShare::CreateOptions options;
70 options.isProxy_ = true;
71 return DataShare::DataShareHelper::Creator(SwitchStatusDependency::SETTINGS_USER_SECURE_URI, options);
72 }
73
RegisterObserver(const std::string & key,SettingObserver::ObserverCallback & observerCallback)74 void DataShareManager::RegisterObserver(const std::string &key, SettingObserver::ObserverCallback &observerCallback)
75 {
76 HILOGI("DataShareManager RegisterObserver start");
77 RegisterObserver(observerCallback);
78 if (observer_ != nullptr) {
79 HILOGI("Observer is already registered with key is %{public}s", key.c_str());
80 UnregisterObserver(key);
81 }
82 std::shared_ptr<DataShare::DataShareHelper> dataShareHelper = CreateDataShareHelper();
83 if (dataShareHelper == nullptr) {
84 HILOGE("Register observer failed, dataShareHelper is null");
85 return;
86 }
87 int32_t userId = GetLocalAccountId();
88 Uri uri(AssembleUserSecureUri(userId, key));
89 dataShareHelper->RegisterObserver(uri, observer_);
90 dataShareHelper->Release();
91 HILOGI("DataShareManager RegisterObserver success with key is %{public}s", key.c_str());
92 }
93
UnregisterObserver(const std::string & key)94 void DataShareManager::UnregisterObserver(const std::string &key)
95 {
96 HILOGI("DataShareManager UnregisterObserver start");
97 if (observer_ == nullptr) {
98 HILOGI("UnregisterObserver, observer is nullptr with key is %{public}s", key.c_str());
99 return;
100 }
101 std::shared_ptr<DataShare::DataShareHelper> dataShareHelper = CreateDataShareHelper();
102 if (dataShareHelper == nullptr) {
103 HILOGE("Unregister observer failed with key is %{public}s", key.c_str());
104 return;
105 }
106 int32_t userId = GetLocalAccountId();
107 Uri uri(AssembleUserSecureUri(userId, key));
108 dataShareHelper->UnregisterObserver(uri, observer_);
109 dataShareHelper->Release();
110 HILOGI("DataShareManager UnregisterObserver success with key is %{public}s", key.c_str());
111 }
112
UnInit()113 void DataShareManager::UnInit()
114 {
115 HILOGI("DataShareManager UnInit start");
116 UnregisterObserver(SwitchStatusDependency::GetInstance().CONTINUE_SWITCH_STATUS_KEY);
117 observer_ = nullptr;
118 HILOGI("DataShareManager UnInit end");
119 }
120
AssembleUserSecureUri(int userId,const std::string & key)121 Uri DataShareManager::AssembleUserSecureUri(int userId, const std::string &key)
122 {
123 Uri uri(SwitchStatusDependency::SETTINGS_USER_SECURE_URI + "_" + std::to_string(userId) + "?Proxy=true&key=" + key);
124 return uri;
125 }
126
GetLocalAccountId()127 int32_t DataShareManager::GetLocalAccountId()
128 {
129 int32_t id = INVALID_ACCOUNT_ID;
130 ErrCode err = AccountSA::OsAccountManager::GetForegroundOsAccountLocalId(id);
131 if (err != ERR_OK || id == INVALID_ACCOUNT_ID) {
132 HILOGE("GetLocalAccountId passing param invalid or return error!, err : %{public}d", err);
133 return INVALID_PARAMETERS_ERR;
134 }
135 return id;
136 }
137
IsCurrentContinueSwitchOn()138 bool DataShareManager::IsCurrentContinueSwitchOn()
139 {
140 HILOGD("IsCurrentContinueSwitchOn start");
141 return isCurrentContinueSwitchOn_.load();
142 }
143
SetCurrentContinueSwitch(bool status)144 void DataShareManager::SetCurrentContinueSwitch(bool status)
145 {
146 HILOGD("SetCurrentContinueSwitch start, status : %{public}d", status);
147 isCurrentContinueSwitchOn_.store(status);
148 DmsContinueConditionMgr::GetInstance().UpdateSystemStatus(SYS_EVENT_CONTINUE_SWITCH, status);
149 }
150 } // namespace DistributedSchedule
151 } // namespace OHOS
152