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
16#include "watcher_manager_stub.h"
17#include "watcher_proxy.h"
18#include "watcher_utils.h"
19#ifdef HICOLLIE_ENABLE
20#include "xcollie/xcollie.h"
21#include "xcollie/xcollie_define.h"
22#endif
23
24namespace OHOS {
25namespace init_param {
26int32_t WatcherManagerStub::OnRemoteRequest(uint32_t code,
27    MessageParcel &data, MessageParcel &reply, MessageOption &option)
28{
29    std::u16string myDescriptor = IWatcherManager::GetDescriptor();
30    std::u16string remoteDescriptor = data.ReadInterfaceToken();
31    WATCHER_CHECK(myDescriptor == remoteDescriptor, return -1, "Invalid remoteDescriptor %u", code);
32
33    WATCHER_LOGV("OnRemoteRequest code %u", code);
34    switch (code) {
35        case static_cast<uint32_t>(ParamWatcherInterfaceCode::ADD_WATCHER): {
36            std::string key = data.ReadString();
37#ifdef HICOLLIE_ENABLE
38            const int dfxDelayS = 20;
39            int id = HiviewDFX::XCollie::GetInstance().SetTimer("ParamWatcher_AddWatcher",
40                dfxDelayS, nullptr, nullptr, HiviewDFX::XCOLLIE_FLAG_LOG);
41#endif
42            int ret = AddWatcher(key, data.ReadUint32());
43#ifdef HICOLLIE_ENABLE
44            HiviewDFX::XCollie::GetInstance().CancelTimer(id);
45#endif
46            reply.WriteInt32(ret);
47            break;
48        }
49        case static_cast<uint32_t>(ParamWatcherInterfaceCode::DEL_WATCHER): {
50            std::string key = data.ReadString();
51            int ret = DelWatcher(key, data.ReadUint32());
52            reply.WriteInt32(ret);
53            break;
54        }
55        case static_cast<uint32_t>(ParamWatcherInterfaceCode::ADD_REMOTE_AGENT): {
56            auto remote = data.ReadRemoteObject();
57            // 0 is invalid watcherId
58            uint32_t id = data.ReadUint32();
59            sptr<IWatcher> watcher = new WatcherProxy(remote);
60            uint32_t remoteWatcherId = AddRemoteWatcher(id, watcher);
61            reply.WriteUint32(remoteWatcherId);
62            break;
63        }
64        case static_cast<uint32_t>(ParamWatcherInterfaceCode::DEL_REMOTE_AGENT): {
65            int ret = DelRemoteWatcher(data.ReadUint32());
66            reply.WriteInt32(ret);
67            break;
68        }
69        case static_cast<uint32_t>(ParamWatcherInterfaceCode::REFRESH_WATCHER): {
70            std::string key = data.ReadString();
71            int ret = RefreshWatcher(key, data.ReadUint32());
72            reply.WriteInt32(ret);
73            break;
74        }
75        default: {
76            return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
77        }
78    }
79    return 0;
80}
81}
82}
83