• 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 
16 #include "dataobs_mgr_proxy.h"
17 
18 #include "errors.h"
19 #include "string_ex.h"
20 
21 #include "data_ability_observer_proxy.h"
22 #include "data_ability_observer_stub.h"
23 #include "dataobs_mgr_errors.h"
24 
25 namespace OHOS {
26 namespace AAFwk {
27 
WriteInterfaceToken(MessageParcel & data)28 bool DataObsManagerProxy::WriteInterfaceToken(MessageParcel &data)
29 {
30     if (!data.WriteInterfaceToken(DataObsManagerProxy::GetDescriptor())) {
31         HILOG_ERROR("write interface token failed");
32         return false;
33     }
34     return true;
35 }
36 
RegisterObserver(const Uri & uri,const sptr<IDataAbilityObserver> & dataObserver)37 int DataObsManagerProxy::RegisterObserver(const Uri &uri, const sptr<IDataAbilityObserver> &dataObserver)
38 {
39     int error;
40     MessageParcel data;
41     MessageParcel reply;
42     MessageOption option;
43 
44     if (!WriteInterfaceToken(data)) {
45         return DATAOBS_PROXY_INNER_ERR;
46     }
47     if (!data.WriteParcelable(&uri)) {
48         HILOG_ERROR("register observer fail, uri error");
49         return ERR_INVALID_VALUE;
50     }
51     if (dataObserver == nullptr) {
52         HILOG_ERROR("register observer fail, dataObserver is nullptr");
53         return ERR_INVALID_VALUE;
54     }
55 
56     if (!data.WriteParcelable(dataObserver->AsObject())) {
57         HILOG_ERROR("register observer fail, dataObserver error");
58         return ERR_INVALID_VALUE;
59     }
60 
61     error = Remote()->SendRequest(IDataObsMgr::REGISTER_OBSERVER, data, reply, option);
62     if (error != NO_ERROR) {
63         HILOG_ERROR("register observer fail, error: %d", error);
64         return error;
65     }
66     return reply.ReadInt32();
67 }
68 
UnregisterObserver(const Uri & uri,const sptr<IDataAbilityObserver> & dataObserver)69 int DataObsManagerProxy::UnregisterObserver(const Uri &uri, const sptr<IDataAbilityObserver> &dataObserver)
70 {
71     int error;
72     MessageParcel data;
73     MessageParcel reply;
74     MessageOption option;
75 
76     if (!WriteInterfaceToken(data)) {
77         return DATAOBS_PROXY_INNER_ERR;
78     }
79     if (!data.WriteParcelable(&uri)) {
80         HILOG_ERROR("unregister observer fail, uri error");
81         return ERR_INVALID_VALUE;
82     }
83     if (dataObserver == nullptr) {
84         HILOG_ERROR("unregister observer fail, dataObserver is nullptr");
85         return ERR_INVALID_VALUE;
86     }
87 
88     if (!data.WriteParcelable(dataObserver->AsObject())) {
89         HILOG_ERROR("unregister observer fail, dataObserver error");
90         return ERR_INVALID_VALUE;
91     }
92 
93     error = Remote()->SendRequest(IDataObsMgr::UNREGISTER_OBSERVER, data, reply, option);
94     if (error != NO_ERROR) {
95         HILOG_ERROR("unregister observer fail, error: %d", error);
96         return error;
97     }
98     return reply.ReadInt32();
99 }
100 
NotifyChange(const Uri & uri)101 int DataObsManagerProxy::NotifyChange(const Uri &uri)
102 {
103     int error;
104     MessageParcel data;
105     MessageParcel reply;
106     MessageOption option;
107 
108     if (!WriteInterfaceToken(data)) {
109         return DATAOBS_PROXY_INNER_ERR;
110     }
111     if (!data.WriteParcelable(&uri)) {
112         HILOG_ERROR("notifyChange fail, uri error");
113         return ERR_INVALID_VALUE;
114     }
115 
116     error = Remote()->SendRequest(IDataObsMgr::NOTIFY_CHANGE, data, reply, option);
117     if (error != NO_ERROR) {
118         HILOG_ERROR("notifyChange fail, error: %d", error);
119         return error;
120     }
121     return reply.ReadInt32();
122 }
123 
124 }  // namespace AAFwk
125 }  // namespace OHOS
126