OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chromeos/binder/command_broker.h" | 5 #include "chromeos/binder/command_broker.h" |
6 | 6 |
7 #include <linux/android/binder.h> | 7 #include <linux/android/binder.h> |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "chromeos/binder/driver.h" | 13 #include "chromeos/binder/driver.h" |
14 #include "chromeos/binder/local_object.h" | 14 #include "chromeos/binder/local_object.h" |
15 #include "chromeos/binder/transaction_data.h" | 15 #include "chromeos/binder/transaction_data.h" |
16 #include "chromeos/binder/transaction_status.h" | |
16 | 17 |
17 namespace binder { | 18 namespace binder { |
18 | 19 |
19 namespace { | 20 namespace { |
20 | 21 |
21 // Converts TransactionData to binder_transaction_data struct. | 22 // Converts TransactionData to binder_transaction_data struct. |
22 binder_transaction_data ConvertTransactionDataToStruct( | 23 binder_transaction_data ConvertTransactionDataToStruct( |
23 const TransactionData& data) { | 24 const TransactionData& data) { |
24 binder_transaction_data result = {}; | 25 binder_transaction_data result = {}; |
25 result.code = data.GetCode(); | 26 result.code = data.GetCode(); |
(...skipping 14 matching lines...) Expand all Loading... | |
40 | 41 |
41 } // namespace | 42 } // namespace |
42 | 43 |
43 CommandBroker::CommandBroker(Driver* driver) | 44 CommandBroker::CommandBroker(Driver* driver) |
44 : command_stream_(driver, this), weak_ptr_factory_(this) {} | 45 : command_stream_(driver, this), weak_ptr_factory_(this) {} |
45 | 46 |
46 CommandBroker::~CommandBroker() { | 47 CommandBroker::~CommandBroker() { |
47 DCHECK(thread_checker_.CalledOnValidThread()); | 48 DCHECK(thread_checker_.CalledOnValidThread()); |
48 } | 49 } |
49 | 50 |
51 bool CommandBroker::EnterLooper() { | |
52 command_stream_.AppendOutgoingCommand(BC_ENTER_LOOPER, nullptr, 0); | |
53 return command_stream_.Flush(); | |
54 } | |
55 | |
56 bool CommandBroker::ExitLooper() { | |
57 command_stream_.AppendOutgoingCommand(BC_EXIT_LOOPER, nullptr, 0); | |
58 return command_stream_.Flush(); | |
59 } | |
60 | |
61 bool CommandBroker::PollCommands() { | |
62 DCHECK(!command_stream_.CanProcessIncomingCommand()); | |
satorux1
2016/01/13 04:33:20
so it's not ok to call this function while CanProc
hashimoto
2016/01/13 05:46:46
I put this DCHECK here to ensure that all previous
| |
63 // Fetch and process commands. | |
64 if (!command_stream_.Fetch()) { | |
65 LOG(ERROR) << "Failed to fetch commands."; | |
66 return false; | |
67 } | |
68 while (command_stream_.CanProcessIncomingCommand()) { | |
69 if (!command_stream_.ProcessIncomingCommand()) { | |
70 LOG(ERROR) << "Failed to process command."; | |
71 return false; | |
72 } | |
73 } | |
74 // Flush outgoing commands. | |
75 if (!command_stream_.Flush()) { | |
76 LOG(ERROR) << "Failed to flush commands."; | |
77 return false; | |
78 } | |
79 return true; | |
80 } | |
81 | |
50 bool CommandBroker::Transact(int32_t handle, | 82 bool CommandBroker::Transact(int32_t handle, |
51 const TransactionData& request, | 83 const TransactionData& request, |
52 scoped_ptr<TransactionData>* reply) { | 84 scoped_ptr<TransactionData>* reply) { |
53 DCHECK(thread_checker_.CalledOnValidThread()); | 85 DCHECK(thread_checker_.CalledOnValidThread()); |
54 // Send transaction. | 86 // Send transaction. |
55 binder_transaction_data tr = ConvertTransactionDataToStruct(request); | 87 binder_transaction_data tr = ConvertTransactionDataToStruct(request); |
56 tr.target.handle = handle; | 88 tr.target.handle = handle; |
57 command_stream_.AppendOutgoingCommand(BC_TRANSACTION, &tr, sizeof(tr)); | 89 command_stream_.AppendOutgoingCommand(BC_TRANSACTION, &tr, sizeof(tr)); |
58 if (!command_stream_.Flush()) { | 90 if (!command_stream_.Flush()) { |
59 LOG(ERROR) << "Failed to write"; | 91 LOG(ERROR) << "Failed to write"; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
93 command_stream_.AppendOutgoingCommand(BC_RELEASE, &handle, sizeof(handle)); | 125 command_stream_.AppendOutgoingCommand(BC_RELEASE, &handle, sizeof(handle)); |
94 // Decrement weak reference count. | 126 // Decrement weak reference count. |
95 command_stream_.AppendOutgoingCommand(BC_DECREFS, &handle, sizeof(handle)); | 127 command_stream_.AppendOutgoingCommand(BC_DECREFS, &handle, sizeof(handle)); |
96 } | 128 } |
97 | 129 |
98 base::Closure CommandBroker::GetReleaseReferenceClosure(int32_t handle) { | 130 base::Closure CommandBroker::GetReleaseReferenceClosure(int32_t handle) { |
99 return base::Bind(&CommandBroker::ReleaseReference, | 131 return base::Bind(&CommandBroker::ReleaseReference, |
100 weak_ptr_factory_.GetWeakPtr(), handle); | 132 weak_ptr_factory_.GetWeakPtr(), handle); |
101 } | 133 } |
102 | 134 |
135 bool CommandBroker::OnTransaction(const TransactionData& data) { | |
136 LocalObject* object = reinterpret_cast<LocalObject*>(data.GetCookie()); | |
137 scoped_ptr<TransactionData> reply; | |
138 if (!object->Transact(this, data, &reply)) { | |
139 LOG(ERROR) << "Failed to transact."; | |
140 return false; | |
141 } | |
142 if (!data.IsOneWay()) { | |
143 // Send reply. | |
144 if (!reply) { | |
145 reply.reset(new TransactionStatus(Status::FAILED_TRANSACTION)); | |
146 } | |
147 binder_transaction_data tr = ConvertTransactionDataToStruct(*reply); | |
148 tr.target.handle = -1; | |
satorux1
2016/01/13 04:33:20
maybe document why -1?
hashimoto
2016/01/13 05:46:46
Done.
| |
149 command_stream_.AppendOutgoingCommand(BC_REPLY, &tr, sizeof(tr)); | |
150 if (!command_stream_.Flush()) { | |
151 LOG(ERROR) << "Failed to write"; | |
152 return false; | |
153 } | |
154 scoped_ptr<TransactionData> response_data; | |
155 ResponseType response_type = WaitForResponse(&response_data); | |
156 LOG_IF(ERROR, response_type != RESPONSE_TYPE_TRANSACTION_COMPLETE) | |
157 << "Error on the other end when sending reply: " << response_type; | |
satorux1
2016/01/13 04:33:20
in this case, should this return true?
hashimoto
2016/01/13 05:46:46
Added a comment.
| |
158 } | |
159 return true; | |
160 } | |
161 | |
103 void CommandBroker::OnReply(scoped_ptr<TransactionData> data) { | 162 void CommandBroker::OnReply(scoped_ptr<TransactionData> data) { |
104 DCHECK(thread_checker_.CalledOnValidThread()); | 163 DCHECK(thread_checker_.CalledOnValidThread()); |
105 DCHECK_EQ(response_type_, RESPONSE_TYPE_NONE); | 164 DCHECK_EQ(response_type_, RESPONSE_TYPE_NONE); |
106 DCHECK(!response_data_); | 165 DCHECK(!response_data_); |
107 response_type_ = RESPONSE_TYPE_TRANSACTION_REPLY; | 166 response_type_ = RESPONSE_TYPE_TRANSACTION_REPLY; |
108 response_data_ = std::move(data); | 167 response_data_ = std::move(data); |
109 } | 168 } |
110 | 169 |
111 void CommandBroker::OnDeadReply() { | 170 void CommandBroker::OnDeadReply() { |
112 DCHECK(thread_checker_.CalledOnValidThread()); | 171 DCHECK(thread_checker_.CalledOnValidThread()); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
160 } | 219 } |
161 } | 220 } |
162 } | 221 } |
163 ResponseType response_type = response_type_; | 222 ResponseType response_type = response_type_; |
164 response_type_ = RESPONSE_TYPE_NONE; | 223 response_type_ = RESPONSE_TYPE_NONE; |
165 *data = std::move(response_data_); | 224 *data = std::move(response_data_); |
166 return response_type; | 225 return response_type; |
167 } | 226 } |
168 | 227 |
169 } // namespace binder | 228 } // namespace binder |
OLD | NEW |