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 <memory>
17
18 #include "oaid_service.h"
19 #include "oaid_common.h"
20 #include "oaid_observer_manager.h"
21
22 namespace OHOS {
23 namespace Cloud {
24 const std::string OAID_VIRTUAL_STR = "-****-****-****-************";
OaidObserverManager()25 OaidObserverManager::OaidObserverManager()
26 {
27 OAID_HILOGI(OAID_MODULE_SERVICE, "OaidObserverManager construct");
28 }
29
~OaidObserverManager()30 OaidObserverManager::~OaidObserverManager()
31 {
32 OAID_HILOGI(OAID_MODULE_SERVICE, "OaidObserverManager destruct");
33 }
34
RegisterObserver(const sptr<IRemoteConfigObserver> & observer)35 int32_t OaidObserverManager::RegisterObserver(const sptr<IRemoteConfigObserver> &observer)
36 {
37 if (observer == nullptr) {
38 OAID_HILOGI(OAID_MODULE_SERVICE, "observer is null");
39 return ERR_INVALID_PARAM;
40 }
41 std::unique_lock<std::shared_mutex> lockRegister(observerMutex_);
42 observer_ = observer;
43
44 auto oaid = OAIDService::GetInstance()->GetOAID();
45 std::string target = oaid.substr(0, 9).append(OAID_VIRTUAL_STR);
46 OAID_HILOGI(OAID_MODULE_SERVICE, "registerObserver success, getOaid is: %{public}s", target.c_str());
47 observer->OnOaidUpdated(oaid);
48 return ERR_OK;
49 }
50
OnUpdateOaid(const std::string & oaid)51 void OaidObserverManager::OnUpdateOaid(const std::string &oaid)
52 {
53 std::shared_lock<std::shared_mutex> lockUpdate(observerMutex_);
54 if (observer_ == nullptr) {
55 OAID_HILOGI(OAID_MODULE_SERVICE, "observer is null, error code is: %{public}d", ERR_NULL_POINTER);
56 return;
57 }
58 std::string target = oaid.substr(0, 9).append(OAID_VIRTUAL_STR);
59 OAID_HILOGI(OAID_MODULE_SERVICE, "OnOaidUpdated success oaid is: %{public}s", target.c_str());
60 observer_->OnOaidUpdated(oaid);
61 }
62 } // namespace Cloud
63 } // namespace OHOS