1// Copyright 2016 The Android Open Source Project
2//
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 "buffet/binder_command_proxy.h"
16
17#include <weave/enum_to_string.h>
18
19#include "buffet/weave_error_conversion.h"
20#include "common/binder_utils.h"
21
22using weaved::binder_utils::ParseDictionary;
23using weaved::binder_utils::ToStatus;
24using weaved::binder_utils::ToString;
25using weaved::binder_utils::ToString16;
26
27namespace buffet {
28
29namespace {
30
31android::binder::Status ReportDestroyedError() {
32  return android::binder::Status::fromServiceSpecificError(
33      1, android::String8{"Command has been destroyed"});
34}
35
36}  // anonymous namespace
37
38BinderCommandProxy::BinderCommandProxy(
39    const std::weak_ptr<weave::Command>& command) : command_{command} {}
40
41android::binder::Status BinderCommandProxy::getId(android::String16* id) {
42  auto command = command_.lock();
43  if (!command)
44    return ReportDestroyedError();
45  *id = ToString16(command->GetID());
46  return android::binder::Status::ok();
47}
48
49android::binder::Status BinderCommandProxy::getName(android::String16* name) {
50  auto command = command_.lock();
51  if (!command)
52    return ReportDestroyedError();
53  *name = ToString16(command->GetName());
54  return android::binder::Status::ok();
55}
56
57android::binder::Status BinderCommandProxy::getComponent(
58    android::String16* component) {
59  auto command = command_.lock();
60  if (!command)
61    return ReportDestroyedError();
62  *component = ToString16(command->GetComponent());
63  return android::binder::Status::ok();
64}
65
66android::binder::Status BinderCommandProxy::getState(android::String16* state) {
67  auto command = command_.lock();
68  if (!command)
69    return ReportDestroyedError();
70  *state = ToString16(EnumToString(command->GetState()));
71  return android::binder::Status::ok();
72}
73
74android::binder::Status BinderCommandProxy::getOrigin(
75    android::String16* origin) {
76  auto command = command_.lock();
77  if (!command)
78    return ReportDestroyedError();
79  *origin = ToString16(EnumToString(command->GetOrigin()));
80  return android::binder::Status::ok();
81}
82
83android::binder::Status BinderCommandProxy::getParameters(
84    android::String16* parameters) {
85  auto command = command_.lock();
86  if (!command)
87    return ReportDestroyedError();
88  *parameters = ToString16(command->GetParameters());
89  return android::binder::Status::ok();
90}
91
92android::binder::Status BinderCommandProxy::getProgress(
93    android::String16* progress) {
94  auto command = command_.lock();
95  if (!command)
96    return ReportDestroyedError();
97  *progress = ToString16(command->GetProgress());
98  return android::binder::Status::ok();
99}
100
101android::binder::Status BinderCommandProxy::getResults(
102    android::String16* results) {
103  auto command = command_.lock();
104  if (!command)
105    return ReportDestroyedError();
106  *results = ToString16(command->GetResults());
107  return android::binder::Status::ok();
108}
109
110android::binder::Status BinderCommandProxy::setProgress(
111    const android::String16& progress) {
112  auto command = command_.lock();
113  if (!command)
114    return ReportDestroyedError();
115  std::unique_ptr<base::DictionaryValue> dict;
116  auto status = ParseDictionary(progress, &dict);
117  if (status.isOk()) {
118    weave::ErrorPtr error;
119    status = ToStatus(command->SetProgress(*dict, &error), &error);
120  }
121  return status;
122}
123
124android::binder::Status BinderCommandProxy::complete(
125    const android::String16& results) {
126  auto command = command_.lock();
127  if (!command)
128    return ReportDestroyedError();
129  std::unique_ptr<base::DictionaryValue> dict;
130  auto status = ParseDictionary(results, &dict);
131  if (status.isOk()) {
132    weave::ErrorPtr error;
133    status = ToStatus(command->Complete(*dict, &error), &error);
134  }
135  return status;
136}
137
138android::binder::Status BinderCommandProxy::abort(
139    const android::String16& errorCode,
140    const android::String16& errorMessage) {
141  auto command = command_.lock();
142  if (!command)
143    return ReportDestroyedError();
144  weave::ErrorPtr command_error;
145  weave::Error::AddTo(&command_error, FROM_HERE, ToString(errorCode),
146                      ToString(errorMessage));
147  weave::ErrorPtr error;
148  return ToStatus(command->Abort(command_error.get(), &error), &error);
149}
150
151android::binder::Status BinderCommandProxy::cancel() {
152  auto command = command_.lock();
153  if (!command)
154    return ReportDestroyedError();
155  weave::ErrorPtr error;
156  return ToStatus(command->Cancel(&error), &error);
157}
158
159android::binder::Status BinderCommandProxy::pause() {
160  auto command = command_.lock();
161  if (!command)
162    return ReportDestroyedError();
163  weave::ErrorPtr error;
164  return ToStatus(command->Pause(&error), &error);
165}
166
167android::binder::Status BinderCommandProxy::setError(
168    const android::String16& errorCode,
169    const android::String16& errorMessage) {
170  auto command = command_.lock();
171  if (!command)
172    return ReportDestroyedError();
173  weave::ErrorPtr command_error;
174  weave::Error::AddTo(&command_error, FROM_HERE, ToString(errorCode),
175                      ToString(errorMessage));
176  weave::ErrorPtr error;
177  return ToStatus(command->SetError(command_error.get(), &error), &error);
178}
179
180}  // namespace buffet
181