watcher_manager_kits.cpp 7.2 KB
Newer Older
S
sun_fan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "watcher_manager_kits.h"

#include "if_system_ability_manager.h"
M
Mupceet 已提交
18
#include "init_param.h"
S
sun_fan 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#include "iservice_registry.h"
#include "iwatcher.h"
#include "iwatcher_manager.h"
#include "system_ability_definition.h"
#include "watcher_utils.h"

namespace OHOS {
namespace init_param {
WatcherManagerKits &WatcherManagerKits::GetInstance()
{
    return DelayedRefSingleton<WatcherManagerKits>::GetInstance();
}

WatcherManagerKits::WatcherManagerKits() {}

WatcherManagerKits::~WatcherManagerKits() {}

void WatcherManagerKits::ResetService(const wptr<IRemoteObject> &remote)
{
    WATCHER_LOGI("Remote is dead, reset service instance");
X
add ut  
xionglei6 已提交
39 40 41 42 43 44 45 46 47 48
    bool resetService = false;
    {
        std::lock_guard<std::mutex> lock(lock_);
        if (watcherManager_ != nullptr) {
            sptr<IRemoteObject> object = watcherManager_->AsObject();
            if ((object != nullptr) && (remote == object)) {
                object->RemoveDeathRecipient(deathRecipient_);
                watcherManager_ = nullptr;
                resetService = true;
            }
S
sun_fan 已提交
49 50
        }
    }
X
add ut  
xionglei6 已提交
51 52 53
    if (resetService) {
        ReAddWatcher();
    }
S
sun_fan 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
}

sptr<IWatcherManager> WatcherManagerKits::GetService()
{
    std::lock_guard<std::mutex> lock(lock_);
    if (watcherManager_ != nullptr) {
        return watcherManager_;
    }

    sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
    WATCHER_CHECK(samgr != nullptr, return nullptr, "Get samgr failed");
    sptr<IRemoteObject> object = samgr->GetSystemAbility(PARAM_WATCHER_DISTRIBUTED_SERVICE_ID);
    WATCHER_CHECK(object != nullptr, return nullptr, "Get watcher manager object from samgr failed");
    if (deathRecipient_ == nullptr) {
        deathRecipient_ = new DeathRecipient();
    }

    if ((object->IsProxyObject()) && (!object->AddDeathRecipient(deathRecipient_))) {
        WATCHER_LOGE("Failed to add death recipient");
    }
    watcherManager_ = iface_cast<IWatcherManager>(object);
    if (watcherManager_ == nullptr) {
        WATCHER_LOGE("watcher manager iface_cast failed");
    }
    return watcherManager_;
}

void WatcherManagerKits::DeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
{
    DelayedRefSingleton<WatcherManagerKits>::GetInstance().ResetService(remote);
}

WatcherManagerKits::ParamWatcherKitPtr WatcherManagerKits::GetParamWatcher(const std::string &keyPrefix)
{
    std::lock_guard<std::mutex> lock(mutex_);
    if (watchers_.find(keyPrefix) == watchers_.end()) {
        return nullptr;
    }
    return watchers_[keyPrefix];
}

void WatcherManagerKits::SetParamWatcher(const std::string &keyPrefix, ParamWatcherKitPtr watcher)
{
    std::lock_guard<std::mutex> lock(mutex_);
98
    if (watchers_.find(keyPrefix) == watchers_.end()) {
S
sun_fan 已提交
99 100 101 102
        watchers_[keyPrefix] = watcher;
    }
}

X
add ut  
xionglei6 已提交
103 104
void WatcherManagerKits::ReAddWatcher()
{
X
xionglei6 已提交
105
    WATCHER_LOGV("ReAddWatcher ");
X
add ut  
xionglei6 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
    int count = 0;
    const int maxRetryCount = 100;
    const int sleepTime = 100;
    auto watcherManager = GetService();
    while (watcherManager == nullptr && count < maxRetryCount) {
        watcherManager = GetService();
        usleep(sleepTime);
        count++;
    }
    WATCHER_CHECK(watcherManager != nullptr, return, "Failed to get watcher manager");
    std::lock_guard<std::mutex> lock(mutex_);
    for (auto iter = watchers_.begin(); iter != watchers_.end(); iter++) {
        if (iter->second == nullptr) {
            continue;
        }
X
xionglei6 已提交
121
        WATCHER_LOGV("ReAddWatcher keyPrefix %s ", iter->first.c_str());
X
add ut  
xionglei6 已提交
122 123 124 125 126 127
        uint32_t watcherId = watcherManager->AddWatcher(iter->first, iter->second);
        WATCHER_CHECK(watcherId != 0, continue, "Failed to add watcher for %s", iter->first.c_str());
        iter->second->SetWatcherId(watcherId);
    }
}

S
sun_fan 已提交
128 129 130 131
int32_t WatcherManagerKits::AddWatcher(const std::string &keyPrefix, ParameterChangePtr callback, void *context)
{
    WATCHER_LOGI("AddWatcher keyPrefix %s", keyPrefix.c_str());
    ParamWatcherKitPtr watcher = GetParamWatcher(keyPrefix);
X
xionglei6 已提交
132 133
    WATCHER_CHECK(watcher == nullptr, return watcher->GetWatcherId(),
        "Has been watched by keyPrefix %s", keyPrefix.c_str());
X
add ut  
xionglei6 已提交
134

S
sun_fan 已提交
135 136 137
    watcher = new ParamWatcher(keyPrefix, callback, context);
    WATCHER_CHECK(watcher != nullptr, return -1, "Failed to create watcher for %s", keyPrefix.c_str());
    auto watcherManager = GetService();
X
xionglei6 已提交
138
    WATCHER_CHECK(watcherManager != nullptr, delete watcher; return -1, "Failed to get watcher manager");
S
sun_fan 已提交
139
    uint32_t watcherId = watcherManager->AddWatcher(keyPrefix, watcher);
X
xionglei6 已提交
140
    WATCHER_CHECK(watcherId != 0, delete watcher; return -1, "Failed to add watcher for %s", keyPrefix.c_str());
S
sun_fan 已提交
141 142 143 144 145 146 147 148
    watcher->SetWatcherId(watcherId);
    SetParamWatcher(keyPrefix, watcher);
    return watcher->GetWatcherId();
}

int32_t WatcherManagerKits::DelWatcher(const std::string &keyPrefix)
{
    ParamWatcherKitPtr watcher = GetParamWatcher(keyPrefix);
X
add ut  
xionglei6 已提交
149
    WATCHER_CHECK(watcher != nullptr, return 0, "Can not find watcher for keyPrefix %s", keyPrefix.c_str());
S
sun_fan 已提交
150 151
    auto watcherManager = GetService();
    WATCHER_CHECK(watcherManager != nullptr, return -1, "Failed to get watcher manager");
X
xionglei6 已提交
152
    WATCHER_LOGV("DelWatcher keyPrefix_ %s ", keyPrefix.c_str());
S
sun_fan 已提交
153 154
    int ret = watcherManager->DelWatcher(keyPrefix, watcher->GetWatcherId());
    WATCHER_CHECK(ret == 0, return -1, "Failed to delete watcher for %s", keyPrefix.c_str());
X
xionglei6 已提交
155 156 157 158 159 160 161
    {
        std::lock_guard<std::mutex> lock(mutex_);
        auto iter = watchers_.find(keyPrefix);
        if (iter != watchers_.end()) {
            watchers_.erase(iter);
        }
    }
S
sun_fan 已提交
162 163 164 165 166
    return 0;
}

void WatcherManagerKits::ParamWatcher::OnParamerterChange(const std::string &name, const std::string &value)
{
S
sun_fan 已提交
167
    Watcher::OnParamerterChange(name, value);
X
xionglei6 已提交
168
    WATCHER_LOGV("OnParamerterChange name %s value %s", name.c_str(), value.c_str());
S
sun_fan 已提交
169 170 171 172 173 174 175 176 177
    if (callback_ != nullptr) {
        callback_(name.c_str(), value.c_str(), context_);
    }
}
} // namespace init_param
} // namespace OHOS

int SystemWatchParameter(const char *keyPrefix, ParameterChangePtr callback, void *context)
{
X
add ut  
xionglei6 已提交
178
    WATCHER_CHECK(keyPrefix != nullptr, return PARAM_CODE_INVALID_PARAM, "Invalid prefix");
S
sun_fan 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
    int ret = 0;
    std::string key(keyPrefix);
    if (key.rfind("*") == key.length() - 1) {
        ret = WatchParamCheck(key.substr(0, key.length() - 1).c_str());
    } else {
        ret = WatchParamCheck(keyPrefix);
    }
    if (ret != 0) {
        return ret;
    }
    OHOS::init_param::WatcherManagerKits &instance = OHOS::init_param::WatcherManagerKits::GetInstance();
    if (callback != nullptr) {
        ret = (instance.AddWatcher(keyPrefix, callback, context) > 0) ? 0 : -1;
    } else {
        ret = instance.DelWatcher(keyPrefix);
    }
    return ret;
}