!1163 watcher支持多个回调

Merge pull request !1163 from cheng_jinsong/watchercallback
This commit is contained in:
openharmony_ci 2022-09-01 08:01:14 +00:00 committed by Gitee
commit 3f5849c796
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
20 changed files with 295 additions and 90 deletions

View File

@ -94,8 +94,8 @@ int WaitParameter(const char *key, const char *value, int timeout);
*
* You can use this function to watch system parameter values.\n
*
* @param keyprefix Indicates the key prefix for the parameter to be watched.
* If keyprefix is not a full name, "A.B." for example, it means to watch for all parameter started with "A.B.".
* @param keyPrefix Indicates the key prefix for the parameter to be watched.
* If keyPrefix is not a full name, "A.B." for example, it means to watch for all parameter started with "A.B.".
* @param callback Indicates value change callback.
* If callback is NULL, it means to cancel the watch.
* @return Returns <b>0</b> if the operation is successful;
@ -104,7 +104,23 @@ int WaitParameter(const char *key, const char *value, int timeout);
* @version 1.1
*/
typedef void (*ParameterChgPtr)(const char *key, const char *value, void *context);
int WatchParameter(const char *keyprefix, ParameterChgPtr callback, void *context);
int WatchParameter(const char *keyPrefix, ParameterChgPtr callback, void *context);
/**
* @brief Remove parameter watcher.
*
* You can use this function to remove system parameter watcher.\n
*
* @param keyPrefix Indicates the key prefix for the parameter to be watched.
* If keyPrefix is not a full name, "A.B." for example, it means to watch for all parameter started with "A.B.".
* @param callback Indicates value change callback.
* If callback is NULL, it means to cancel the watch.
* @return Returns <b>0</b> if the operation is successful;
* returns <b>-1</b> in other scenarios.
* @since 1.1
* @version 1.1
*/
int RemoveParameterWatcher(const char *keyPrefix, ParameterChgPtr callback, void *context);
const char *GetSecurityPatchTag(void);
const char *GetOSFullName(void);

View File

@ -549,10 +549,10 @@ static int32_t CompareData_(const struct tagTriggerNode_ *trigger, const void *d
PARAM_CHECK(trigger != NULL && data != NULL, return -1, "Invalid trigger");
if (trigger->type == TRIGGER_PARAM_WAIT) {
WaitNode *node = (WaitNode *)trigger;
return node->waitId == *(uint32_t *)data;
return node->waitId - *(uint32_t *)data;
} else if (trigger->type == TRIGGER_PARAM_WATCH) {
WatchNode *node = (WatchNode *)trigger;
return node->watchId == *(uint32_t *)data;
return node->watchId - *(uint32_t *)data;
}
return -1;
}

View File

@ -20,7 +20,6 @@ ohos_prebuilt_etc("param_watcher.rc") {
ohos_shared_library("param_watcher") {
sources = [
"//base/startup/init/services/utils/list.c",
"proxy/watcher_manager.cpp",
"proxy/watcher_manager_stub.cpp",
"proxy/watcher_proxy.cpp",

View File

@ -17,7 +17,7 @@
namespace OHOS {
namespace init_param {
void Watcher::OnParamerterChange(const std::string &name, const std::string &value)
void Watcher::OnParameterChange(const std::string &name, const std::string &value)
{
UNUSED(name);
UNUSED(value);

View File

@ -24,9 +24,9 @@ namespace init_param {
class Watcher : public WatcherStub {
public:
Watcher() = default;
~Watcher() = default;
virtual ~Watcher() = default;
void OnParamerterChange(const std::string &name, const std::string &value) override;
void OnParameterChange(const std::string &name, const std::string &value) override;
};
} // namespace init_param
} // namespace OHOS

View File

@ -21,6 +21,7 @@
#include "iwatcher_manager.h"
#include "system_ability_definition.h"
#include "watcher_utils.h"
#include "parameter.h"
namespace OHOS {
namespace init_param {
@ -92,14 +93,6 @@ WatcherManagerKits::ParamWatcherKitPtr WatcherManagerKits::GetParamWatcher(const
return watchers_[keyPrefix];
}
void WatcherManagerKits::SetParamWatcher(const std::string &keyPrefix, ParamWatcherKitPtr watcher)
{
std::lock_guard<std::mutex> lock(mutex_);
if (watchers_.find(keyPrefix) == watchers_.end()) {
watchers_[keyPrefix] = watcher;
}
}
void WatcherManagerKits::ReAddWatcher()
{
WATCHER_LOGV("ReAddWatcher ");
@ -127,45 +120,142 @@ void WatcherManagerKits::ReAddWatcher()
int32_t WatcherManagerKits::AddWatcher(const std::string &keyPrefix, ParameterChangePtr callback, void *context)
{
WATCHER_LOGI("AddWatcher keyPrefix %s", keyPrefix.c_str());
ParamWatcherKitPtr watcher = GetParamWatcher(keyPrefix);
WATCHER_CHECK(watcher == nullptr, return -1,
"Has been watched by keyPrefix %s", keyPrefix.c_str());
watcher = new ParamWatcher(keyPrefix, callback, context);
WATCHER_CHECK(watcher != nullptr, return -1, "Failed to create watcher for %s", keyPrefix.c_str());
WATCHER_LOGV("AddWatcher keyPrefix %s", keyPrefix.c_str());
auto watcherManager = GetService();
WATCHER_CHECK(watcherManager != nullptr, return -1, "Failed to get watcher manager");
uint32_t watcherId = watcherManager->AddWatcher(keyPrefix, watcher);
WATCHER_CHECK(watcherId != 0, return -1, "Failed to add watcher for %s", keyPrefix.c_str());
watcher->SetWatcherId(watcherId);
SetParamWatcher(keyPrefix, watcher);
return watcher->GetWatcherId();
bool newWatcher = false;
ParamWatcherKitPtr watcher = nullptr;
{
std::lock_guard<std::mutex> lock(mutex_);
if (watchers_.find(keyPrefix) == watchers_.end()) {
watcher = new ParamWatcher(keyPrefix);
WATCHER_CHECK(watcher != nullptr, return -1, "Failed to create watcher for %s", keyPrefix.c_str());
watchers_[keyPrefix] = watcher;
newWatcher = true;
int ret = watcher->AddParameterListener(callback, context);
WATCHER_CHECK(ret == 0, return -1, "Failed to add callback for %s ", keyPrefix.c_str());
uint32_t watcherId = watcherManager->AddWatcher(keyPrefix, watcher);
WATCHER_CHECK(watcherId != 0, return -1, "Failed to add watcher for %s", keyPrefix.c_str());
watcher->SetWatcherId(watcherId);
} else {
watcher = watchers_[keyPrefix];
}
}
// save callback
if (!newWatcher) { // refresh
int ret = watcher->AddParameterListener(callback, context);
WATCHER_CHECK(ret == 0, return -1, "Failed to add callback for %s ", keyPrefix.c_str());
ret = watcherManager->RefreshWatcher(keyPrefix, watcher->GetWatcherId());
WATCHER_CHECK(ret == 0, return -1, "Failed to refresh watcher for %s", keyPrefix.c_str());
}
WATCHER_LOGI("AddWatcher keyPrefix %s watcherId %u success", keyPrefix.c_str(), watcher->GetWatcherId());
return 0;
}
int32_t WatcherManagerKits::DelWatcher(const std::string &keyPrefix)
int32_t WatcherManagerKits::DelWatcher(const std::string &keyPrefix, ParameterChangePtr callback, void *context)
{
ParamWatcherKitPtr watcher = GetParamWatcher(keyPrefix);
WATCHER_CHECK(watcher != nullptr, return 0, "Can not find watcher for keyPrefix %s", keyPrefix.c_str());
auto watcherManager = GetService();
WATCHER_CHECK(watcherManager != nullptr, return -1, "Failed to get watcher manager");
WATCHER_LOGV("DelWatcher keyPrefix_ %s ", keyPrefix.c_str());
int ret = watcherManager->DelWatcher(keyPrefix, watcher->GetWatcherId());
WATCHER_CHECK(ret == 0, return -1, "Failed to delete watcher for %s", keyPrefix.c_str());
{
std::lock_guard<std::mutex> lock(mutex_);
auto iter = watchers_.find(keyPrefix);
if (iter != watchers_.end()) {
watchers_.erase(iter);
int count = watcher->DelParameterListener(callback, context);
WATCHER_LOGI("DelWatcher keyPrefix_ %s count %d", keyPrefix.c_str(), count);
if (count == 0) {
int ret = watcherManager->DelWatcher(keyPrefix, watcher->GetWatcherId());
WATCHER_CHECK(ret == 0, return -1, "Failed to delete watcher for %s", keyPrefix.c_str());
{
std::lock_guard<std::mutex> lock(mutex_);
auto iter = watchers_.find(keyPrefix);
if (iter != watchers_.end()) {
watchers_.erase(iter);
}
}
}
return 0;
}
void WatcherManagerKits::ParamWatcher::OnParamerterChange(const std::string &name, const std::string &value)
WatcherManagerKits::ParameterChangeListener *WatcherManagerKits::ParamWatcher::GetParameterListener(uint32_t *idx)
{
std::lock_guard<std::mutex> lock(mutex_);
uint32_t index = *idx;
while (index < static_cast<uint32_t>(parameterChangeListeners.size())) {
auto it = parameterChangeListeners.find(index);
if (it != parameterChangeListeners.end()) {
*idx = index;
return parameterChangeListeners[index].get();
}
index++;
}
return nullptr;
}
void WatcherManagerKits::ParamWatcher::RemoveParameterListener(uint32_t idx)
{
std::lock_guard<std::mutex> lock(mutex_);
if (idx >= static_cast<uint32_t>(parameterChangeListeners.size())) {
return;
}
parameterChangeListeners.erase(idx);
}
void WatcherManagerKits::ParamWatcher::OnParameterChange(const std::string &name, const std::string &value)
{
Watcher::OnParameterChange(name, value);
WATCHER_LOGI("OnParameterChange name %s value %s", name.c_str(), value.c_str());
uint32_t index = 0;
ParameterChangeListener *listener = GetParameterListener(&index);
while (listener != NULL) {
if (!listener->CheckValueChange(value)) {
listener->OnParameterChange(name, value);
}
index++;
listener = GetParameterListener(&index);
}
}
int WatcherManagerKits::ParamWatcher::AddParameterListener(ParameterChangePtr callback, void *context)
{
WATCHER_CHECK(callback != nullptr, return -1, "Invalid callback ");
WATCHER_LOGV("AddParameterListener listenerId_ %d callback %p context %p", listenerId_, callback, context);
std::lock_guard<std::mutex> lock(mutex_);
for (auto it = parameterChangeListeners.begin(); it != parameterChangeListeners.end(); it++) {
if (it->second == nullptr) {
continue;
}
if (it->second->IsEqual(callback, context)) {
WATCHER_LOGI("AddParameterListener callback %p context %p exist", callback, context);
return -1;
}
}
std::shared_ptr<ParameterChangeListener> changeNode =
std::make_shared<ParameterChangeListener>(callback, context);
WATCHER_CHECK(changeNode != nullptr, return -1, "Failed to create listener");
parameterChangeListeners[listenerId_] = changeNode;
listenerId_++;
return 0;
}
int WatcherManagerKits::ParamWatcher::DelParameterListener(ParameterChangePtr callback, void *context)
{
uint32_t index = 0;
ParameterChangeListener *listener = GetParameterListener(&index);
while (listener != NULL) {
if ((callback == nullptr && context == nullptr)) {
RemoveParameterListener(index);
} else if (listener->IsEqual(callback, context)) {
WATCHER_LOGI("DelParameterListener callback %p context %p", callback, context);
RemoveParameterListener(index);
break;
}
index++;
listener = GetParameterListener(&index);
}
return static_cast<int>(parameterChangeListeners.size());
}
void WatcherManagerKits::ParameterChangeListener::OnParameterChange(const std::string &name, const std::string &value)
{
Watcher::OnParamerterChange(name, value);
WATCHER_LOGV("OnParamerterChange name %s value %s", name.c_str(), value.c_str());
if (callback_ != nullptr) {
callback_(name.c_str(), value.c_str(), context_);
}
@ -188,9 +278,26 @@ int SystemWatchParameter(const char *keyPrefix, ParameterChangePtr callback, voi
}
OHOS::init_param::WatcherManagerKits &instance = OHOS::init_param::WatcherManagerKits::GetInstance();
if (callback != nullptr) {
ret = (instance.AddWatcher(keyPrefix, callback, context) > 0) ? 0 : -1;
ret = instance.AddWatcher(keyPrefix, callback, context);
} else {
ret = instance.DelWatcher(keyPrefix);
ret = instance.DelWatcher(keyPrefix, nullptr, nullptr);
}
return ret;
}
int RemoveParameterWatcher(const char *keyPrefix, ParameterChgPtr callback, void *context)
{
WATCHER_CHECK(keyPrefix != nullptr, return PARAM_CODE_INVALID_PARAM, "Invalid prefix");
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();
return instance.DelWatcher(keyPrefix, (ParameterChangePtr)callback, context);
}

View File

@ -35,18 +35,43 @@ public:
static WatcherManagerKits &GetInstance();
int32_t AddWatcher(const std::string &keyPrefix, ParameterChangePtr callback, void *context);
int32_t DelWatcher(const std::string &keyPrefix);
int32_t DelWatcher(const std::string &keyPrefix, ParameterChangePtr callback, void *context);
void ReAddWatcher();
#ifndef STARTUP_INIT_TEST
private:
#endif
class ParameterChangeListener {
public:
ParameterChangeListener(ParameterChangePtr callback, void *context)
: callback_(callback), context_(context) {}
~ParameterChangeListener() = default;
bool IsEqual(ParameterChangePtr callback, void *context)
{
return (callback == callback_ && context == context_);
}
void OnParameterChange(const std::string &name, const std::string &value);
bool CheckValueChange(const std::string &value)
{
bool ret = (value_ == value);
value_ = value;
return ret;
}
private:
std::string value_ {};
ParameterChangePtr callback_ { nullptr };
void *context_ { nullptr };
};
class ParamWatcher final : public Watcher {
public:
ParamWatcher(const std::string &key, ParameterChangePtr callback, void *context)
: keyPrefix_(key), callback_(callback), context_(context) {}
~ParamWatcher() = default;
explicit ParamWatcher(const std::string &key) : keyPrefix_(key) {}
virtual ~ParamWatcher()
{
parameterChangeListeners.clear();
};
void OnParamerterChange(const std::string &name, const std::string &value) override;
void OnParameterChange(const std::string &name, const std::string &value) override;
void SetWatcherId(uint32_t watcherId)
{
@ -56,12 +81,17 @@ private:
{
return watcherId_;
}
int AddParameterListener(ParameterChangePtr callback, void *context);
int DelParameterListener(ParameterChangePtr callback, void *context);
private:
ParameterChangeListener *GetParameterListener(uint32_t *idx);
void RemoveParameterListener(uint32_t idx);
uint32_t watcherId_ { 0 };
std::string keyPrefix_ {};
ParameterChangePtr callback_ { nullptr };
void *context_ { nullptr };
std::mutex mutex_;
uint32_t listenerId_ { 0 };
std::map<uint32_t, std::shared_ptr<ParameterChangeListener>> parameterChangeListeners;
};
using ParamWatcherKitPtr = sptr<WatcherManagerKits::ParamWatcher>;
ParamWatcherKitPtr GetParamWatcher(const std::string &keyPrefix);
@ -80,7 +110,6 @@ private:
}
private:
void SetParamWatcher(const std::string &keyPrefix, ParamWatcherKitPtr watcher);
void ResetService(const wptr<IRemoteObject> &remote);
sptr<IWatcherManager> GetService();
std::mutex lock_;

View File

@ -52,5 +52,20 @@ int32_t WatcherManagerProxy::DelWatcher(const std::string &keyPrefix, uint32_t w
WATCHER_CHECK(res == ERR_OK, return ERR_FLATTEN_OBJECT, "Transact error");
return reply.ReadInt32();
}
int32_t WatcherManagerProxy::RefreshWatcher(const std::string &keyPrefix, uint32_t watcherId)
{
auto remote = Remote();
WATCHER_CHECK(remote != nullptr, return ERR_FLATTEN_OBJECT, "Can not get remote");
MessageParcel data;
data.WriteInterfaceToken(WatcherManagerProxy::GetDescriptor());
data.WriteString(keyPrefix);
data.WriteUint32(watcherId);
MessageParcel reply;
MessageOption option { MessageOption::TF_SYNC };
int32_t res = remote->SendRequest(REFRESH_WATCHER, data, reply, option);
WATCHER_CHECK(res == ERR_OK, return ERR_FLATTEN_OBJECT, "Transact error");
return 0;
}
}
} // namespace OHOS

View File

@ -27,6 +27,7 @@ public:
uint32_t AddWatcher(const std::string &keyPrefix, const sptr<IWatcher> &watcher) override;
int32_t DelWatcher(const std::string &keyPrefix, uint32_t watcherId) override;
int32_t RefreshWatcher(const std::string &keyPrefix, uint32_t watcherId) override;
private:
static inline BrokerDelegator<WatcherManagerProxy> delegator_;
};

View File

@ -30,7 +30,7 @@ int32_t WatcherStub::OnRemoteRequest(uint32_t code, MessageParcel &data, Message
case PARAM_CHANGE: {
std::string name = data.ReadString();
std::string value = data.ReadString();
OnParamerterChange(name, value);
OnParameterChange(name, value);
break;
}
default: {

View File

@ -31,7 +31,7 @@ public:
DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.Startup.IWatcher");
public:
virtual void OnParamerterChange(const std::string &name, const std::string &value) = 0;
virtual void OnParameterChange(const std::string &name, const std::string &value) = 0;
};
} // namespace init_param
} // namespace OHOS

View File

@ -27,13 +27,15 @@ class IWatcherManager : public OHOS::IRemoteBroker {
public:
enum {
ADD_WATCHER,
DEL_WATCHER
DEL_WATCHER,
REFRESH_WATCHER
};
DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.Startup.IWatcherManager");
public:
virtual uint32_t AddWatcher(const std::string &keyPrefix, const sptr<IWatcher> &watcher) = 0;
virtual int32_t DelWatcher(const std::string &keyPrefix, uint32_t watcherId) = 0;
virtual int32_t RefreshWatcher(const std::string &keyPrefix, uint32_t watcherId) = 0;
};
} // namespace update_engine
} // namespace OHOS

View File

@ -64,17 +64,17 @@ uint32_t WatcherManager::AddWatcher(const std::string &keyPrefix, const sptr<IWa
group = std::make_shared<WatcherGroup>(groupId, keyPrefix);
WATCHER_CHECK(group != nullptr, return 0, "Failed to create group for %s", keyPrefix.c_str());
} else {
newGroup = group->Emptry();
newGroup = group->Empty();
}
ParamWatcherPtr paramWather = std::make_shared<ParamWatcher>(watcherId, watcher, group);
WATCHER_CHECK(paramWather != nullptr, return 0, "Failed to create watcher for %s", keyPrefix.c_str());
AddParamWatcher(keyPrefix, group, paramWather);
ParamWatcherPtr paramWatcher = std::make_shared<ParamWatcher>(watcherId, watcher, group);
WATCHER_CHECK(paramWatcher != nullptr, return 0, "Failed to create watcher for %s", keyPrefix.c_str());
AddParamWatcher(keyPrefix, group, paramWatcher);
if (newGroup) {
StartLoop();
SendMessage(group, MSG_ADD_WATCHER);
}
SendLocalChange(keyPrefix, paramWather);
WATCHER_LOGI("AddWatcher %s watcherId: %u success", keyPrefix.c_str(), watcherId);
SendLocalChange(keyPrefix, paramWatcher);
WATCHER_LOGI("AddWatcher %s watcherId: %u groupId %u success", keyPrefix.c_str(), watcherId, group->GetGroupId());
return watcherId;
}
@ -83,7 +83,7 @@ int32_t WatcherManager::DelWatcher(const std::string &keyPrefix, uint32_t watche
auto group = GetWatcherGroup(keyPrefix);
WATCHER_CHECK(group != nullptr, return 0, "Can not find group %s", keyPrefix.c_str());
auto paramWatcher = GetWatcher(watcherId);
WATCHER_CHECK(group != nullptr, return 0, "Can not find watcher %s %d", keyPrefix.c_str(), watcherId);
WATCHER_CHECK(paramWatcher != nullptr, return 0, "Can not find watcher %s %d", keyPrefix.c_str(), watcherId);
WATCHER_LOGV("DelWatcher prefix %s watcherId %u", keyPrefix.c_str(), watcherId);
return DelWatcher(group, paramWatcher);
}
@ -97,7 +97,7 @@ int32_t WatcherManager::DelWatcher(WatcherGroupPtr group, ParamWatcherPtr watche
}
WATCHER_LOGI("DelWatcher watcherId %u prefix %s", watcher->GetWatcherId(), group->GetKeyPrefix().c_str());
DelParamWatcher(watcher);
if (group->Emptry()) {
if (group->Empty()) {
SendMessage(group, MSG_DEL_WATCHER);
DelWatcherGroup(group);
}
@ -112,7 +112,7 @@ int WatcherManager::SendMessage(WatcherGroupPtr group, int type)
request->id.watcherId = group->GetGroupId();
request->msgSize = sizeof(ParamMessage);
WATCHER_LOGV("SendMessage %s", group->GetKeyPrefix().c_str());
WATCHER_LOGV("SendMessage %s groupId %d type %d", group->GetKeyPrefix().c_str(), group->GetGroupId(), type);
int ret = PARAM_CODE_ERROR;
int fd = GetServerFd(false);
if (fd != INVALID_SOCKET) {
@ -423,5 +423,16 @@ int WatcherManager::GetGroupId(uint32_t &groupId)
groupId = groupId_;
return 0;
}
int32_t WatcherManager::RefreshWatcher(const std::string &keyPrefix, uint32_t watcherId)
{
WATCHER_LOGV("RefreshWatcher %s watcherId: %u", keyPrefix.c_str(), watcherId);
auto group = GetWatcherGroup(keyPrefix);
WATCHER_CHECK(group != nullptr, return 0, "Can not find group %s", keyPrefix.c_str());
auto paramWatcher = GetWatcher(watcherId);
WATCHER_CHECK(paramWatcher != nullptr, return 0, "Can not find watcher %s %d", keyPrefix.c_str(), watcherId);
SendLocalChange(keyPrefix, paramWatcher);
return 0;
}
} // namespace init_param
} // namespace OHOS

View File

@ -90,7 +90,7 @@ public:
void ProcessParameterChange(const std::string &name, const std::string &value)
{
#ifndef STARTUP_INIT_TEST
watcher_->OnParamerterChange(name, value);
watcher_->OnParameterChange(name, value);
#endif
}
private:
@ -114,7 +114,7 @@ public:
{
return keyPrefix_;
}
bool Emptry() const
bool Empty() const
{
return ListEmpty(watchers_);
}
@ -134,6 +134,7 @@ public:
uint32_t AddWatcher(const std::string &keyPrefix, const sptr<IWatcher> &watcher) override;
int32_t DelWatcher(const std::string &keyPrefix, uint32_t watcherId) override;
int32_t RefreshWatcher(const std::string &keyPrefix, uint32_t watcherId) override;
int32_t DelWatcher(WatcherGroupPtr group, ParamWatcherPtr watcher);
ParamWatcherPtr GetWatcher(uint32_t watcherId);
ParamWatcherPtr GetWatcher(const wptr<IRemoteObject> &remote);
@ -160,6 +161,7 @@ private:
int GetServerFd(bool retry);
int GetWatcherId(uint32_t &watcherId);
int GetGroupId(uint32_t &groupId);
private:
std::atomic<uint32_t> watcherId_ { 0 };
std::atomic<uint32_t> groupId_ { 0 };

View File

@ -43,6 +43,13 @@ int32_t WatcherManagerStub::OnRemoteRequest(uint32_t code,
reply.WriteInt32(ret);
break;
}
case REFRESH_WATCHER: {
std::string key = data.ReadString();
uint32_t watcherId = data.ReadUint32();
int ret = RefreshWatcher(key, watcherId);
reply.WriteInt32(ret);
break;
}
default: {
return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
}

View File

@ -23,9 +23,9 @@
namespace OHOS {
namespace init_param {
void WatcherProxy::OnParamerterChange(const std::string &name, const std::string &value)
void WatcherProxy::OnParameterChange(const std::string &name, const std::string &value)
{
WATCHER_LOGV("WatcherProxy::OnParamerterChange %s %s", name.c_str(), value.c_str());
WATCHER_LOGV("WatcherProxy::OnParameterChange %s %s", name.c_str(), value.c_str());
MessageParcel data;
MessageParcel reply;
MessageOption option { MessageOption::TF_ASYNC };

View File

@ -27,7 +27,7 @@ public:
virtual ~WatcherProxy() = default;
void OnParamerterChange(const std::string &name, const std::string &value) override;
void OnParameterChange(const std::string &name, const std::string &value) override;
private:
static inline BrokerDelegator<WatcherProxy> delegator_;

View File

@ -84,25 +84,17 @@ static void HandleParamChange(const char *key, const char *value, void *context)
printf("Receive parameter commit %lld change %s %s \n", commit, key, value);
}
static void *CmdWatcher(void *args)
{
char *name = (char *)args;
int ret = SystemWatchParameter(name, HandleParamChange, NULL);
if (ret != 0) {
return 0;
}
while (1) {
pause();
}
return NULL;
}
static int32_t BShellParamCmdWatch(BShellHandle shell, int32_t argc, char *argv[])
{
PLUGIN_CHECK(argc >= 1, return -1, "Invalid parameter");
PLUGIN_LOGV("BShellParamCmdWatch %s", argv[1]);
pthread_t thread;
pthread_create(&thread, NULL, CmdWatcher, argv[1]);
static int index = 0;
int ret = SystemWatchParameter(argv[1], HandleParamChange, (void *)index);
if (ret != 0) {
PLUGIN_LOGE("Failed to watch %s", argv[1]);
return 0;
}
index++;
return 0;
}

View File

@ -20,6 +20,7 @@
#include "iwatcher.h"
#include "iwatcher_manager.h"
#include "message_parcel.h"
#include "parameter.h"
#include "param_manager.h"
#include "param_utils.h"
#include "system_ability_definition.h"
@ -53,10 +54,33 @@ public:
int TestAddWatcher()
{
int ret = SystemWatchParameter("test.permission.watcher.test1", TestParameterChange, nullptr);
size_t index = 1;
int ret = SystemWatchParameter("test.permission.watcher.test1", TestParameterChange, (void *)index);
EXPECT_EQ(ret, 0);
ret = SystemWatchParameter("test.permission.watcher.test1*", TestParameterChange, nullptr);
ret = SystemWatchParameter("test.permission.watcher.test1*", TestParameterChange, (void *)index);
EXPECT_EQ(ret, 0);
// repeat add, return fail
ret = SystemWatchParameter("test.permission.watcher.test1", TestParameterChange, (void *)index);
EXPECT_NE(ret, 0);
index++;
ret = SystemWatchParameter("test.permission.watcher.test1", TestParameterChange, (void *)index);
EXPECT_EQ(ret, 0);
index++;
ret = SystemWatchParameter("test.permission.watcher.test1", TestParameterChange, (void *)index);
EXPECT_EQ(ret, 0);
// delete
ret = RemoveParameterWatcher("test.permission.watcher.test1", TestParameterChange, (void *)index);
EXPECT_EQ(ret, 0);
index--;
ret = RemoveParameterWatcher("test.permission.watcher.test1", TestParameterChange, (void *)index);
EXPECT_EQ(ret, 0);
index--;
ret = RemoveParameterWatcher("test.permission.watcher.test1", TestParameterChange, (void *)index);
EXPECT_EQ(ret, 0);
ret = RemoveParameterWatcher("test.permission.watcher.test1", TestParameterChange, (void *)index);
EXPECT_EQ(ret, 0);
// 非法
ret = SystemWatchParameter("test.permission.watcher.tes^^^^t1*", TestParameterChange, nullptr);
EXPECT_NE(ret, 0);
@ -102,7 +126,7 @@ public:
if (watcher != nullptr) {
watcher->OnRemoteRequest(IWatcher::PARAM_CHANGE, data, reply, option);
watcher->OnRemoteRequest(IWatcher::PARAM_CHANGE + 1, data, reply, option);
watcher->OnParamerterChange("testname", "testvalue");
watcher->OnParameterChange("testname", "testvalue");
}
return 0;
}

View File

@ -41,9 +41,9 @@ public:
TestWatcher() {}
~TestWatcher() = default;
void OnParamerterChange(const std::string &name, const std::string &value) override
void OnParameterChange(const std::string &name, const std::string &value) override
{
printf("TestWatcher::OnParamerterChange name %s %s \n", name.c_str(), value.c_str());
printf("TestWatcher::OnParameterChange name %s %s \n", name.c_str(), value.c_str());
}
};
@ -124,7 +124,7 @@ public:
EXPECT_NE(remoteObj, nullptr);
WatcherProxy *watcher = new WatcherProxy(remoteObj);
if (watcher != nullptr) {
watcher->OnParamerterChange(name, value);
watcher->OnParameterChange(name, value);
delete watcher;
}
return 0;