1 /*
2  * Copyright (c) 2023 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 "data_proxy_observer_stub.h"
17 
18 #include "datashare_itypes_utils.h"
19 #include "datashare_log.h"
20 
21 namespace OHOS {
22 namespace DataShare {
23 static constexpr int REQUEST_CODE = 0;
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)24 int RdbObserverStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
25 {
26     std::u16string descriptor = RdbObserverStub::GetDescriptor();
27     std::u16string remoteDescriptor = data.ReadInterfaceToken();
28     if (descriptor != remoteDescriptor) {
29         LOG_ERROR("local descriptor is not equal to remote");
30         return ERR_INVALID_STATE;
31     }
32     if (code != REQUEST_CODE) {
33         LOG_ERROR("not support code:%u", code);
34         return ERR_INVALID_STATE;
35     }
36     RdbChangeNode changeNode;
37     if (!ITypesUtil::Unmarshal(data, changeNode)) {
38         LOG_ERROR("Unmarshalling  is nullptr");
39         return ERR_INVALID_VALUE;
40     }
41     OnChangeFromRdb(changeNode);
42     return ERR_OK;
43 }
44 
OnChangeFromRdb(const RdbChangeNode & changeNode)45 void RdbObserverStub::OnChangeFromRdb(const RdbChangeNode &changeNode)
46 {
47     std::lock_guard<decltype(mutex_)> lock(mutex_);
48     if (callback_) {
49         callback_(changeNode);
50     }
51 }
52 
RdbObserverStub(RdbCallback callback)53 RdbObserverStub::RdbObserverStub(RdbCallback callback) : callback_(callback)
54 {
55 }
56 
~RdbObserverStub()57 RdbObserverStub::~RdbObserverStub()
58 {
59     ClearCallback();
60 }
61 
ClearCallback()62 void RdbObserverStub::ClearCallback()
63 {
64     std::lock_guard<decltype(mutex_)> lock(mutex_);
65     callback_ = nullptr;
66 }
67 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)68 int PublishedDataObserverStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
69     MessageOption &option)
70 {
71     std::u16string descriptor = PublishedDataObserverStub::GetDescriptor();
72     std::u16string remoteDescriptor = data.ReadInterfaceToken();
73     if (descriptor != remoteDescriptor) {
74         LOG_ERROR("local descriptor is not equal to remote");
75         return ERR_INVALID_STATE;
76     }
77     if (code != REQUEST_CODE) {
78         LOG_ERROR("not support code:%u", code);
79         return ERR_INVALID_STATE;
80     }
81     PublishedDataChangeNode changeNode;
82     if (!ITypesUtil::Unmarshal(data, changeNode)) {
83         LOG_ERROR("Unmarshalling  is nullptr");
84         return ERR_INVALID_VALUE;
85     }
86     OnChangeFromPublishedData(changeNode);
87     return ERR_OK;
88 }
89 
OnChangeFromPublishedData(PublishedDataChangeNode & changeNode)90 void PublishedDataObserverStub::OnChangeFromPublishedData(PublishedDataChangeNode &changeNode)
91 {
92     std::lock_guard<decltype(mutex_)> lock(mutex_);
93     if (callback_) {
94         callback_(changeNode);
95     }
96 }
97 
ClearCallback()98 void PublishedDataObserverStub::ClearCallback()
99 {
100     std::lock_guard<decltype(mutex_)> lock(mutex_);
101     callback_ = nullptr;
102 }
103 
~PublishedDataObserverStub()104 PublishedDataObserverStub::~PublishedDataObserverStub()
105 {
106     ClearCallback();
107 }
108 } // namespace DataShare
109 } // namespace OHOS
110