Merge changes I0ef88864,I7b892a22
am: ed1fa850cf

Change-Id: Ibecadf0d72b327b1e381de92078472980b39ec84
diff --git a/libs/binder/IPCThreadState.cpp b/libs/binder/IPCThreadState.cpp
index 1c3fab4..50974ef 100644
--- a/libs/binder/IPCThreadState.cpp
+++ b/libs/binder/IPCThreadState.cpp
@@ -571,7 +571,7 @@
                                   uint32_t code, const Parcel& data,
                                   Parcel* reply, uint32_t flags)
 {
-    status_t err = data.errorCheck();
+    status_t err;
 
     flags |= TF_ACCEPT_FDS;
 
@@ -582,11 +582,9 @@
             << indent << data << dedent << endl;
     }
 
-    if (err == NO_ERROR) {
-        LOG_ONEWAY(">>>> SEND from pid %d uid %d %s", getpid(), getuid(),
-            (flags & TF_ONE_WAY) == 0 ? "READ REPLY" : "ONE WAY");
-        err = writeTransactionData(BC_TRANSACTION, flags, handle, code, data, NULL);
-    }
+    LOG_ONEWAY(">>>> SEND from pid %d uid %d %s", getpid(), getuid(),
+        (flags & TF_ONE_WAY) == 0 ? "READ REPLY" : "ONE WAY");
+    err = writeTransactionData(BC_TRANSACTION, flags, handle, code, data, NULL);
 
     if (err != NO_ERROR) {
         if (reply) reply->setError(err);
@@ -720,16 +718,6 @@
 {
 }
 
-status_t IPCThreadState::sendReply(const Parcel& reply, uint32_t flags)
-{
-    status_t err;
-    status_t statusBuffer;
-    err = writeTransactionData(BC_REPLY, flags, -1, 0, reply, &statusBuffer);
-    if (err < NO_ERROR) return err;
-
-    return waitForResponse(NULL, NULL);
-}
-
 status_t IPCThreadState::waitForResponse(Parcel *reply, status_t *acquireResult)
 {
     uint32_t cmd;
@@ -1052,68 +1040,78 @@
                 "Not enough command data for brTRANSACTION");
             if (result != NO_ERROR) break;
 
-            Parcel buffer;
-            buffer.ipcSetDataReference(
-                reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
-                tr.data_size,
-                reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
-                tr.offsets_size/sizeof(binder_size_t), freeBuffer, this);
-
-            const pid_t origPid = mCallingPid;
-            const uid_t origUid = mCallingUid;
-            const int32_t origStrictModePolicy = mStrictModePolicy;
-            const int32_t origTransactionBinderFlags = mLastTransactionBinderFlags;
-
-            mCallingPid = tr.sender_pid;
-            mCallingUid = tr.sender_euid;
-            mLastTransactionBinderFlags = tr.flags;
-
-            //ALOGI(">>>> TRANSACT from pid %d uid %d\n", mCallingPid, mCallingUid);
-
             Parcel reply;
-            status_t error;
-            IF_LOG_TRANSACTIONS() {
-                TextOutput::Bundle _b(alog);
-                alog << "BR_TRANSACTION thr " << (void*)pthread_self()
-                    << " / obj " << tr.target.ptr << " / code "
-                    << TypeCode(tr.code) << ": " << indent << buffer
-                    << dedent << endl
-                    << "Data addr = "
-                    << reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer)
-                    << ", offsets addr="
-                    << reinterpret_cast<const size_t*>(tr.data.ptr.offsets) << endl;
-            }
-            if (tr.target.ptr) {
-                // We only have a weak reference on the target object, so we must first try to
-                // safely acquire a strong reference before doing anything else with it.
-                if (reinterpret_cast<RefBase::weakref_type*>(
-                        tr.target.ptr)->attemptIncStrong(this)) {
-                    error = reinterpret_cast<BBinder*>(tr.cookie)->transact(tr.code, buffer,
-                            &reply, tr.flags);
-                    reinterpret_cast<BBinder*>(tr.cookie)->decStrong(this);
+            {
+                Parcel buffer;
+                const pid_t origPid = mCallingPid;
+                const uid_t origUid = mCallingUid;
+                const int32_t origStrictModePolicy = mStrictModePolicy;
+                const int32_t origTransactionBinderFlags = mLastTransactionBinderFlags;
+
+                buffer.ipcSetDataReference(
+                    reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
+                    tr.data_size,
+                    reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
+                    tr.offsets_size/sizeof(binder_size_t), freeBuffer, this);
+
+                mCallingPid = tr.sender_pid;
+                mCallingUid = tr.sender_euid;
+                mLastTransactionBinderFlags = tr.flags;
+
+                //ALOGI(">>>> TRANSACT from pid %d uid %d\n", mCallingPid, mCallingUid);
+
+                status_t error;
+                IF_LOG_TRANSACTIONS() {
+                    TextOutput::Bundle _b(alog);
+                    alog << "BR_TRANSACTION thr " << (void*)pthread_self()
+                        << " / obj " << tr.target.ptr << " / code "
+                        << TypeCode(tr.code) << ": " << indent << buffer
+                        << dedent << endl
+                        << "Data addr = "
+                        << reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer)
+                        << ", offsets addr="
+                        << reinterpret_cast<const size_t*>(tr.data.ptr.offsets) << endl;
+                }
+                if (tr.target.ptr) {
+                    // We only have a weak reference on the target object, so we must first try to
+                    // safely acquire a strong reference before doing anything else with it.
+                    if (reinterpret_cast<RefBase::weakref_type*>(
+                            tr.target.ptr)->attemptIncStrong(this)) {
+                        error = reinterpret_cast<BBinder*>(tr.cookie)->transact(tr.code, buffer,
+                                &reply, tr.flags);
+                        reinterpret_cast<BBinder*>(tr.cookie)->decStrong(this);
+                    } else {
+                        error = UNKNOWN_TRANSACTION;
+                    }
+
                 } else {
-                    error = UNKNOWN_TRANSACTION;
+                    error = the_context_object->transact(tr.code, buffer, &reply, tr.flags);
                 }
 
-            } else {
-                error = the_context_object->transact(tr.code, buffer, &reply, tr.flags);
+                //ALOGI("<<<< TRANSACT from pid %d restore pid %d uid %d\n",
+                //     mCallingPid, origPid, origUid);
+
+                if ((tr.flags & TF_ONE_WAY) == 0) {
+                    LOG_ONEWAY("Sending reply to %d!", mCallingPid);
+                    if (error < NO_ERROR) reply.setError(error);
+
+                    status_t statusBuffer;
+                    result = writeTransactionData(BC_REPLY, 0, -1, 0, reply, &statusBuffer);
+                } else {
+                    LOG_ONEWAY("NOT sending reply to %d!", mCallingPid);
+                }
+
+                mCallingPid = origPid;
+                mCallingUid = origUid;
+                mStrictModePolicy = origStrictModePolicy;
+                mLastTransactionBinderFlags = origTransactionBinderFlags;
             }
 
-            //ALOGI("<<<< TRANSACT from pid %d restore pid %d uid %d\n",
-            //     mCallingPid, origPid, origUid);
+            if (result != NO_ERROR)
+                break;
 
-            if ((tr.flags & TF_ONE_WAY) == 0) {
-                LOG_ONEWAY("Sending reply to %d!", mCallingPid);
-                if (error < NO_ERROR) reply.setError(error);
-                sendReply(reply, 0);
-            } else {
-                LOG_ONEWAY("NOT sending reply to %d!", mCallingPid);
-            }
-
-            mCallingPid = origPid;
-            mCallingUid = origUid;
-            mStrictModePolicy = origStrictModePolicy;
-            mLastTransactionBinderFlags = origTransactionBinderFlags;
+            if ((tr.flags & TF_ONE_WAY) == 0)
+                waitForResponse(NULL, NULL);
 
             IF_LOG_TRANSACTIONS() {
                 TextOutput::Bundle _b(alog);
diff --git a/libs/binder/include/binder/IPCThreadState.h b/libs/binder/include/binder/IPCThreadState.h
index 245607e..9fb13bb 100644
--- a/libs/binder/include/binder/IPCThreadState.h
+++ b/libs/binder/include/binder/IPCThreadState.h
@@ -93,7 +93,6 @@
                                 IPCThreadState();
                                 ~IPCThreadState();
 
-            status_t            sendReply(const Parcel& reply, uint32_t flags);
             status_t            waitForResponse(Parcel *reply,
                                                 status_t *acquireResult=NULL);
             status_t            talkWithDriver(bool doReceive=true);