Hold file descriptors in unique_fd

This removes aidl-cpp's dependency on ScopedFd, a construct from
libnativehelper.

Bug: 27804373
Test: aidl builds and unittests/integration tests pass

Change-Id: I8b3cd0a7971c028216842728764a863549c11395
diff --git a/docs/aidl-cpp.md b/docs/aidl-cpp.md
index 71cc70c..10e5a3f 100644
--- a/docs/aidl-cpp.md
+++ b/docs/aidl-cpp.md
@@ -97,7 +97,7 @@
 | List<String>          | vector<String16>    | inout |                                                       |
 | PersistableBundle     | PersistableBundle   | inout | binder/PersistableBundle.h                            |
 | List<IBinder>         | vector<sp<IBinder>> | inout |                                                       |
-| FileDescriptor        | ScopedFd            | inout | nativehelper/ScopedFd.h                               |
+| FileDescriptor        | unique_fd           | inout | android-base/unique_fd.h from libbase                 |
 
 Note that java.util.Map and java.utils.List are not good candidates for cross
 language communication because they may contain arbitrary types on the Java
diff --git a/generate_cpp_unittest.cpp b/generate_cpp_unittest.cpp
index 388db6f..b15c319 100644
--- a/generate_cpp_unittest.cpp
+++ b/generate_cpp_unittest.cpp
@@ -77,8 +77,8 @@
 ::android::binder::Status NullableBinder(::android::sp<::foo::IFooType>* _aidl_return) override;
 ::android::binder::Status StringListMethod(const ::std::vector<::android::String16>& input, ::std::vector<::android::String16>* output, ::std::vector<::android::String16>* _aidl_return) override;
 ::android::binder::Status BinderListMethod(const ::std::vector<::android::sp<::android::IBinder>>& input, ::std::vector<::android::sp<::android::IBinder>>* output, ::std::vector<::android::sp<::android::IBinder>>* _aidl_return) override;
-::android::binder::Status TakesAFileDescriptor(const ::ScopedFd& f, ::ScopedFd* _aidl_return) override;
-::android::binder::Status TakesAFileDescriptorArray(const ::std::vector<::ScopedFd>& f, ::std::vector<::ScopedFd>* _aidl_return) override;
+::android::binder::Status TakesAFileDescriptor(const ::android::base::unique_fd& f, ::android::base::unique_fd* _aidl_return) override;
+::android::binder::Status TakesAFileDescriptorArray(const ::std::vector<::android::base::unique_fd>& f, ::std::vector<::android::base::unique_fd>* _aidl_return) override;
 };  // class BpComplexTypeInterface
 
 }  // namespace os
@@ -303,7 +303,7 @@
 return _aidl_status;
 }
 
-::android::binder::Status BpComplexTypeInterface::TakesAFileDescriptor(const ::ScopedFd& f, ::ScopedFd* _aidl_return) {
+::android::binder::Status BpComplexTypeInterface::TakesAFileDescriptor(const ::android::base::unique_fd& f, ::android::base::unique_fd* _aidl_return) {
 ::android::Parcel _aidl_data;
 ::android::Parcel _aidl_reply;
 ::android::status_t _aidl_ret_status = ::android::OK;
@@ -336,7 +336,7 @@
 return _aidl_status;
 }
 
-::android::binder::Status BpComplexTypeInterface::TakesAFileDescriptorArray(const ::std::vector<::ScopedFd>& f, ::std::vector<::ScopedFd>* _aidl_return) {
+::android::binder::Status BpComplexTypeInterface::TakesAFileDescriptorArray(const ::std::vector<::android::base::unique_fd>& f, ::std::vector<::android::base::unique_fd>* _aidl_return) {
 ::android::Parcel _aidl_data;
 ::android::Parcel _aidl_reply;
 ::android::status_t _aidl_ret_status = ::android::OK;
@@ -573,8 +573,8 @@
 break;
 case Call::TAKESAFILEDESCRIPTOR:
 {
-::ScopedFd in_f;
-::ScopedFd _aidl_return;
+::android::base::unique_fd in_f;
+::android::base::unique_fd _aidl_return;
 if (!(_aidl_data.checkInterface(this))) {
 _aidl_ret_status = ::android::BAD_TYPE;
 break;
@@ -599,8 +599,8 @@
 break;
 case Call::TAKESAFILEDESCRIPTORARRAY:
 {
-::std::vector<::ScopedFd> in_f;
-::std::vector<::ScopedFd> _aidl_return;
+::std::vector<::android::base::unique_fd> in_f;
+::std::vector<::android::base::unique_fd> _aidl_return;
 if (!(_aidl_data.checkInterface(this))) {
 _aidl_ret_status = ::android::BAD_TYPE;
 break;
@@ -644,12 +644,12 @@
 R"(#ifndef AIDL_GENERATED_ANDROID_OS_I_COMPLEX_TYPE_INTERFACE_H_
 #define AIDL_GENERATED_ANDROID_OS_I_COMPLEX_TYPE_INTERFACE_H_
 
+#include <android-base/unique_fd.h>
 #include <binder/IBinder.h>
 #include <binder/IInterface.h>
 #include <binder/Status.h>
 #include <cstdint>
 #include <foo/IFooType.h>
-#include <nativehelper/ScopedFd.h>
 #include <utils/String16.h>
 #include <utils/StrongPointer.h>
 #include <vector>
@@ -670,8 +670,8 @@
 virtual ::android::binder::Status NullableBinder(::android::sp<::foo::IFooType>* _aidl_return) = 0;
 virtual ::android::binder::Status StringListMethod(const ::std::vector<::android::String16>& input, ::std::vector<::android::String16>* output, ::std::vector<::android::String16>* _aidl_return) = 0;
 virtual ::android::binder::Status BinderListMethod(const ::std::vector<::android::sp<::android::IBinder>>& input, ::std::vector<::android::sp<::android::IBinder>>* output, ::std::vector<::android::sp<::android::IBinder>>* _aidl_return) = 0;
-virtual ::android::binder::Status TakesAFileDescriptor(const ::ScopedFd& f, ::ScopedFd* _aidl_return) = 0;
-virtual ::android::binder::Status TakesAFileDescriptorArray(const ::std::vector<::ScopedFd>& f, ::std::vector<::ScopedFd>* _aidl_return) = 0;
+virtual ::android::binder::Status TakesAFileDescriptor(const ::android::base::unique_fd& f, ::android::base::unique_fd* _aidl_return) = 0;
+virtual ::android::binder::Status TakesAFileDescriptorArray(const ::std::vector<::android::base::unique_fd>& f, ::std::vector<::android::base::unique_fd>* _aidl_return) = 0;
 enum Call {
   SEND = ::android::IBinder::FIRST_CALL_TRANSACTION + 0,
   PIFF = ::android::IBinder::FIRST_CALL_TRANSACTION + 1,
diff --git a/tests/aidl_test_client_file_descriptors.cpp b/tests/aidl_test_client_file_descriptors.cpp
index 491bd5a..afe1f54 100644
--- a/tests/aidl_test_client_file_descriptors.cpp
+++ b/tests/aidl_test_client_file_descriptors.cpp
@@ -23,7 +23,10 @@
 #include <string.h>
 #include <unistd.h>
 
-#include <nativehelper/ScopedFd.h>
+#include <android-base/unique_fd.h>
+
+// libbase
+using android::base::unique_fd;
 
 // libutils:
 using android::sp;
@@ -47,7 +50,7 @@
 
 #define FdByName(_fd) #_fd, _fd
 
-bool DoWrite(string name, const ScopedFd& fd, const string& buf) {
+bool DoWrite(string name, const unique_fd& fd, const string& buf) {
   int wrote;
 
   while ((wrote = write(fd.get(), buf.data(), buf.size())) < 0 && errno == EINTR);
@@ -66,7 +69,7 @@
   return false;
 }
 
-bool DoRead(string name, const ScopedFd& fd, const string& expected) {
+bool DoRead(string name, const unique_fd& fd, const string& expected) {
   size_t length = expected.size();
   int got;
   string buf;
@@ -87,9 +90,9 @@
   return true;
 }
 
-bool DoPipe(ScopedFd* read_side, ScopedFd* write_side) {
+bool DoPipe(unique_fd* read_side, unique_fd* write_side) {
   int fds[2];
-  ScopedFd return_fd;
+  unique_fd return_fd;
 
   if (pipe(fds)) {
     cout << "Error creating pipes: " << strerror(errno) << endl;
@@ -105,9 +108,9 @@
   Status status;
   cout << "Confirming passing and returning file descriptors works." << endl;
 
-  ScopedFd return_fd;
-  ScopedFd read_fd;
-  ScopedFd write_fd;
+  unique_fd return_fd;
+  unique_fd read_fd;
+  unique_fd write_fd;
 
   if (!DoPipe(&read_fd, &write_fd)) {
     return false;
@@ -136,15 +139,15 @@
   Status status;
   cout << "Confirming passing and returning file descriptor arrays works." << endl;
 
-  vector<ScopedFd> array;
+  vector<unique_fd> array;
   array.resize(2);
 
   if (!DoPipe(&array[0], &array[1])) {
     return false;
   }
 
-  vector<ScopedFd> repeated;
-  vector<ScopedFd> reversed;
+  vector<unique_fd> repeated;
+  vector<unique_fd> reversed;
 
   status = s->ReverseFileDescriptorArray(array, &repeated, &reversed);
 
diff --git a/tests/aidl_test_service.cpp b/tests/aidl_test_service.cpp
index 3b9412f..5253ad7 100644
--- a/tests/aidl_test_service.cpp
+++ b/tests/aidl_test_service.cpp
@@ -21,12 +21,12 @@
 
 #include <unistd.h>
 
+#include <android-base/unique_fd.h>
 #include <binder/IInterface.h>
 #include <binder/IPCThreadState.h>
 #include <binder/IServiceManager.h>
 #include <binder/ProcessState.h>
 #include <binder/Status.h>
-#include <nativehelper/ScopedFd.h>
 #include <utils/Errors.h>
 #include <utils/Log.h>
 #include <utils/Looper.h>
@@ -42,6 +42,9 @@
 #undef LOG_TAG
 #define LOG_TAG "aidl_native_service"
 
+// libbase
+using android::base::unique_fd;
+
 // libutils:
 using android::Looper;
 using android::LooperCallback;
@@ -282,20 +285,20 @@
     return ReverseArray(input, repeated, _aidl_return);
   }
 
-  Status RepeatFileDescriptor(const ScopedFd& read,
-                              ScopedFd* _aidl_return) override {
+  Status RepeatFileDescriptor(const unique_fd& read,
+                              unique_fd* _aidl_return) override {
     ALOGE("Repeating file descriptor");
-    *_aidl_return = ScopedFd(dup(read.get()));
+    *_aidl_return = unique_fd(dup(read.get()));
     return Status::ok();
   }
 
-  Status ReverseFileDescriptorArray(const vector<ScopedFd>& input,
-                                    vector<ScopedFd>* repeated,
-                                    vector<ScopedFd>* _aidl_return) override {
+  Status ReverseFileDescriptorArray(const vector<unique_fd>& input,
+                                    vector<unique_fd>* repeated,
+                                    vector<unique_fd>* _aidl_return) override {
     ALOGI("Reversing descriptor array of length %zu", input.size());
     for (const auto& item : input) {
-      repeated->push_back(ScopedFd(dup(item.get())));
-      _aidl_return->push_back(ScopedFd(dup(item.get())));
+      repeated->push_back(unique_fd(dup(item.get())));
+      _aidl_return->push_back(unique_fd(dup(item.get())));
     }
     std::reverse(_aidl_return->begin(), _aidl_return->end());
     return Status::ok();
diff --git a/type_cpp.cpp b/type_cpp.cpp
index 5f6faff..7070b01 100644
--- a/type_cpp.cpp
+++ b/type_cpp.cpp
@@ -511,12 +511,13 @@
 
   Type* fd_vector_type = new ArrayType(
       ValidatableType::KIND_BUILT_IN, kNoPackage, "FileDescriptor[]",
-      {"nativehelper/ScopedFd.h", "vector"}, "::std::vector<::ScopedFd>",
+      {"android-base/unique_fd.h", "vector"},
+      "::std::vector<::android::base::unique_fd>",
       "readUniqueFileDescriptorVector", "writeUniqueFileDescriptorVector");
 
   Add(new Type(
       ValidatableType::KIND_BUILT_IN, kNoPackage, "FileDescriptor",
-      {"nativehelper/ScopedFd.h"}, "::ScopedFd",
+      {"android-base/unique_fd.h"}, "::android::base::unique_fd",
       "readUniqueFileDescriptor", "writeUniqueFileDescriptor",
       fd_vector_type));