• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 #include "watcher_manager_proxy.h"
16 #include "watcher_utils.h"
17 
18 namespace OHOS {
19 namespace init_param {
AddWatcher(const std::string & keyPrefix,const sptr<IWatcher> & watcher)20 uint32_t WatcherManagerProxy::AddWatcher(const std::string &keyPrefix, const sptr<IWatcher> &watcher)
21 {
22     WATCHER_CHECK(watcher != nullptr, return ERR_INVALID_VALUE, "Invalid param");
23     WATCHER_LOGV("WatcherManagerProxy::AddWatcher %s", keyPrefix.c_str());
24     auto remote = Remote();
25     WATCHER_CHECK(remote != nullptr, return 0, "Can not get remote");
26 
27     MessageParcel data;
28     data.WriteInterfaceToken(WatcherManagerProxy::GetDescriptor());
29     data.WriteString(keyPrefix);
30     bool ret = data.WriteRemoteObject(watcher->AsObject());
31     WATCHER_CHECK(ret, return 0, "Can not get remote");
32 
33     MessageParcel reply;
34     MessageOption option { MessageOption::TF_SYNC };
35     int32_t res = remote->SendRequest(ADD_WATCHER, data, reply, option);
36     WATCHER_CHECK(res == ERR_OK, return 0, "Transact error");
37     return reply.ReadUint32();
38 }
DelWatcher(const std::string & keyPrefix,uint32_t watcherId)39 int32_t WatcherManagerProxy::DelWatcher(const std::string &keyPrefix, uint32_t watcherId)
40 {
41     WATCHER_LOGV("WatcherManagerProxy::DelWatcher %s", keyPrefix.c_str());
42     auto remote = Remote();
43     WATCHER_CHECK(remote != nullptr, return ERR_FLATTEN_OBJECT, "Can not get remote");
44 
45     MessageParcel data;
46     data.WriteInterfaceToken(WatcherManagerProxy::GetDescriptor());
47     data.WriteString(keyPrefix);
48     data.WriteUint32(watcherId);
49     MessageParcel reply;
50     MessageOption option { MessageOption::TF_SYNC };
51     int32_t res = remote->SendRequest(DEL_WATCHER, data, reply, option);
52     WATCHER_CHECK(res == ERR_OK, return ERR_FLATTEN_OBJECT, "Transact error");
53     return reply.ReadInt32();
54 }
55 }
56 } // namespace OHOS
57