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