Home | History | Annotate | Download | only in common
      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 #ifndef COMMON_BINDER_UTILS_H_
     16 #define COMMON_BINDER_UTILS_H_
     17 
     18 #include <memory>
     19 #include <string>
     20 
     21 #include <base/values.h>
     22 #include <binder/Status.h>
     23 #include <utils/String8.h>
     24 #include <utils/String16.h>
     25 #include <brillo/errors/error.h>
     26 
     27 namespace weave {
     28 class Error;  // Forward declaration.
     29 using ErrorPtr = std::unique_ptr<Error>;
     30 }  // namespace weave
     31 
     32 namespace weaved {
     33 namespace binder_utils {
     34 
     35 // Converts the result of weave API call into a binder Status object.
     36 // If |success| is true, return binder::Status::ok(), otherwise the method
     37 // constructs a service-specific failure status with an error message obtained
     38 // from the |error| object.
     39 android::binder::Status ToStatus(bool success, weave::ErrorPtr* error);
     40 
     41 // Converts a binder status code to a Brillo error object. Returns true if the
     42 // status was isOk(), otherwise returns false and provides error information
     43 // in the |error| object.
     44 bool StatusToError(android::binder::Status status, brillo::ErrorPtr* error);
     45 
     46 // Converts binder's UTF16 string into a regular UTF8-encoded standard string.
     47 inline std::string ToString(const android::String16& value) {
     48   return android::String8{value}.string();
     49 }
     50 
     51 // Converts regular UTF8-encoded standard string into a binder's UTF16 string.
     52 inline android::String16 ToString16(const std::string& value) {
     53   return android::String16{value.c_str()};
     54 }
     55 
     56 // Serializes a dictionary to a string for transferring over binder.
     57 android::String16 ToString16(const base::Value& value);
     58 
     59 // De-serializes a dictionary from a binder string.
     60 android::binder::Status ParseDictionary(
     61     const android::String16& json,
     62     std::unique_ptr<base::DictionaryValue>* dict);
     63 
     64 }  // namespace binder_utils
     65 }  // namespace weaved
     66 
     67 #endif  // COMMON_BINDER_UTILS_H_
     68