Christopher Wiley | 3a9da17 | 2016-01-29 11:10:49 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016, The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 17 | #include "aidl.h" |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 18 | #include "aidl_to_java.h" |
ThiƩbaud Weksteen | 5a4db21 | 2021-09-02 17:09:34 +0200 | [diff] [blame] | 19 | #include "aidl_typenames.h" |
Steven Moreland | a7764e5 | 2020-10-27 17:29:29 +0000 | [diff] [blame] | 20 | #include "ast_java.h" |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 21 | #include "generate_java.h" |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 22 | #include "logging.h" |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 23 | #include "options.h" |
ThiƩbaud Weksteen | 5a4db21 | 2021-09-02 17:09:34 +0200 | [diff] [blame] | 24 | #include "parser.h" |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 25 | |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | |
Andreas Gampe | e9c816e | 2018-03-14 09:05:48 -0700 | [diff] [blame] | 30 | #include <algorithm> |
| 31 | #include <unordered_set> |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 32 | #include <utility> |
| 33 | #include <vector> |
Andreas Gampe | e9c816e | 2018-03-14 09:05:48 -0700 | [diff] [blame] | 34 | |
Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 35 | #include <android-base/stringprintf.h> |
Christopher Wiley | 8b2d3ee | 2015-09-23 15:43:01 -0700 | [diff] [blame] | 36 | |
Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 37 | using android::base::Join; |
Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 38 | using android::base::StringPrintf; |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 39 | |
Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 40 | using std::string; |
| 41 | using std::unique_ptr; |
| 42 | using std::vector; |
Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 43 | |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 44 | namespace android { |
| 45 | namespace aidl { |
Christopher Wiley | db154a5 | 2015-09-28 16:32:25 -0700 | [diff] [blame] | 46 | namespace java { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 47 | |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 48 | // ================================================= |
Jiyong Park | 2c44f07 | 2018-07-30 21:52:21 +0900 | [diff] [blame] | 49 | class VariableFactory { |
| 50 | public: |
| 51 | using Variable = ::android::aidl::java::Variable; |
Jiyong Park | 2c44f07 | 2018-07-30 21:52:21 +0900 | [diff] [blame] | 52 | |
| 53 | explicit VariableFactory(const std::string& base) : base_(base), index_(0) {} |
Daniel Norman | 716d311 | 2019-09-10 13:11:56 -0700 | [diff] [blame] | 54 | std::shared_ptr<Variable> Get(const AidlTypeSpecifier& type, const AidlTypenames& typenames) { |
| 55 | auto v = std::make_shared<Variable>(JavaSignatureOf(type, typenames), |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 56 | StringPrintf("%s%d", base_.c_str(), index_)); |
Jiyong Park | 2c44f07 | 2018-07-30 21:52:21 +0900 | [diff] [blame] | 57 | vars_.push_back(v); |
| 58 | index_++; |
| 59 | return v; |
| 60 | } |
| 61 | |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 62 | std::shared_ptr<Variable> Get(int index) { return vars_[index]; } |
Jiyong Park | 2c44f07 | 2018-07-30 21:52:21 +0900 | [diff] [blame] | 63 | |
| 64 | private: |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 65 | std::vector<std::shared_ptr<Variable>> vars_; |
Jiyong Park | 2c44f07 | 2018-07-30 21:52:21 +0900 | [diff] [blame] | 66 | std::string base_; |
| 67 | int index_; |
Jiyong Park | 2c44f07 | 2018-07-30 21:52:21 +0900 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | // ================================================= |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 71 | class StubClass : public Class { |
| 72 | public: |
Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 73 | StubClass(const AidlInterface* interfaceType, const Options& options); |
Yi Kong | de13891 | 2019-03-30 01:38:17 -0700 | [diff] [blame] | 74 | ~StubClass() override = default; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 75 | |
Jiyong Park | d800fef | 2020-07-22 18:09:43 +0900 | [diff] [blame] | 76 | // non-copyable, non-movable |
| 77 | StubClass(const StubClass&) = delete; |
| 78 | StubClass(StubClass&&) = delete; |
| 79 | StubClass& operator=(const StubClass&) = delete; |
| 80 | StubClass& operator=(StubClass&&) = delete; |
| 81 | |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 82 | std::shared_ptr<Variable> transact_code; |
| 83 | std::shared_ptr<Variable> transact_data; |
| 84 | std::shared_ptr<Variable> transact_reply; |
| 85 | std::shared_ptr<Variable> transact_flags; |
Jiyong Park | a7ea8bf | 2021-01-05 10:36:18 +0900 | [diff] [blame] | 86 | std::shared_ptr<SwitchStatement> transact_switch_meta; |
| 87 | std::shared_ptr<SwitchStatement> transact_switch_user; |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 88 | std::shared_ptr<StatementBlock> transact_statements; |
| 89 | std::shared_ptr<SwitchStatement> code_to_method_name_switch; |
Christopher Wiley | 8b2d3ee | 2015-09-23 15:43:01 -0700 | [diff] [blame] | 90 | |
Andreas Gampe | 1b865af | 2017-11-22 11:31:47 -0800 | [diff] [blame] | 91 | // Where onTransact cases should be generated as separate methods. |
| 92 | bool transact_outline; |
Andreas Gampe | e9c816e | 2018-03-14 09:05:48 -0700 | [diff] [blame] | 93 | // Specific methods that should be outlined when transact_outline is true. |
| 94 | std::unordered_set<const AidlMethod*> outline_methods; |
| 95 | // Number of all methods. |
| 96 | size_t all_method_count; |
Andreas Gampe | 1b865af | 2017-11-22 11:31:47 -0800 | [diff] [blame] | 97 | |
Andreas Gampe | a8a66fe | 2017-11-22 12:17:00 -0800 | [diff] [blame] | 98 | // Finish generation. This will add a default case to the switch. |
Jooyung Han | 35a3418 | 2021-10-07 18:31:03 +0900 | [diff] [blame] | 99 | void Finish(); |
Andreas Gampe | a8a66fe | 2017-11-22 12:17:00 -0800 | [diff] [blame] | 100 | |
Jooyung Han | 35a3418 | 2021-10-07 18:31:03 +0900 | [diff] [blame] | 101 | std::shared_ptr<Expression> GetTransactDescriptor(const AidlMethod* method); |
Andreas Gampe | 7fab0d1 | 2017-11-22 17:50:17 -0800 | [diff] [blame] | 102 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 103 | private: |
Jooyung Han | 35a3418 | 2021-10-07 18:31:03 +0900 | [diff] [blame] | 104 | void MakeAsInterface(const AidlInterface* interfaceType); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 105 | |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 106 | std::shared_ptr<Variable> transact_descriptor; |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 107 | const Options& options_; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 108 | }; |
| 109 | |
Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 110 | StubClass::StubClass(const AidlInterface* interfaceType, const Options& options) |
Olivier Gaillard | 1140140 | 2018-07-05 15:01:34 +0100 | [diff] [blame] | 111 | : Class(), options_(options) { |
Andreas Gampe | 7fab0d1 | 2017-11-22 17:50:17 -0800 | [diff] [blame] | 112 | transact_descriptor = nullptr; |
Andreas Gampe | 1b865af | 2017-11-22 11:31:47 -0800 | [diff] [blame] | 113 | transact_outline = false; |
Andreas Gampe | e9c816e | 2018-03-14 09:05:48 -0700 | [diff] [blame] | 114 | all_method_count = 0; // Will be set when outlining may be enabled. |
Andreas Gampe | 7fab0d1 | 2017-11-22 17:50:17 -0800 | [diff] [blame] | 115 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 116 | this->comment = "/** Local-side IPC implementation stub class. */"; |
| 117 | this->modifiers = PUBLIC | ABSTRACT | STATIC; |
| 118 | this->what = Class::CLASS; |
Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 119 | this->type = interfaceType->GetCanonicalName() + ".Stub"; |
| 120 | this->extends = "android.os.Binder"; |
| 121 | this->interfaces.push_back(interfaceType->GetCanonicalName()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 122 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 123 | // ctor |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 124 | auto ctor = std::make_shared<Method>(); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 125 | ctor->modifiers = PUBLIC; |
| 126 | ctor->comment = |
| 127 | "/** Construct the stub at attach it to the " |
| 128 | "interface. */"; |
| 129 | ctor->name = "Stub"; |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 130 | ctor->statements = std::make_shared<StatementBlock>(); |
Steven Moreland | e2fcb8e | 2019-11-27 18:09:15 -0800 | [diff] [blame] | 131 | if (interfaceType->IsVintfStability()) { |
| 132 | auto stability = std::make_shared<LiteralStatement>("this.markVintfStability();\n"); |
| 133 | ctor->statements->Add(stability); |
| 134 | } |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 135 | auto attach = std::make_shared<MethodCall>( |
| 136 | THIS_VALUE, "attachInterface", |
| 137 | std::vector<std::shared_ptr<Expression>>{THIS_VALUE, |
| 138 | std::make_shared<LiteralExpression>("DESCRIPTOR")}); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 139 | ctor->statements->Add(attach); |
| 140 | this->elements.push_back(ctor); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 141 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 142 | // asInterface |
Jooyung Han | 35a3418 | 2021-10-07 18:31:03 +0900 | [diff] [blame] | 143 | MakeAsInterface(interfaceType); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 144 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 145 | // asBinder |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 146 | auto asBinder = std::make_shared<Method>(); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 147 | asBinder->modifiers = PUBLIC | OVERRIDE; |
Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 148 | asBinder->returnType = "android.os.IBinder"; |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 149 | asBinder->name = "asBinder"; |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 150 | asBinder->statements = std::make_shared<StatementBlock>(); |
| 151 | asBinder->statements->Add(std::make_shared<ReturnStatement>(THIS_VALUE)); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 152 | this->elements.push_back(asBinder); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 153 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 154 | if (options_.GenTransactionNames()) { |
Olivier Gaillard | 83d7cd3 | 2018-07-30 16:20:57 +0100 | [diff] [blame] | 155 | // getDefaultTransactionName |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 156 | auto getDefaultTransactionName = std::make_shared<Method>(); |
Jiyong Park | e0b2803 | 2019-04-10 03:08:41 +0900 | [diff] [blame] | 157 | getDefaultTransactionName->comment = "/** @hide */"; |
Olivier Gaillard | 83d7cd3 | 2018-07-30 16:20:57 +0100 | [diff] [blame] | 158 | getDefaultTransactionName->modifiers = PUBLIC | STATIC; |
Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 159 | getDefaultTransactionName->returnType = "java.lang.String"; |
Olivier Gaillard | 83d7cd3 | 2018-07-30 16:20:57 +0100 | [diff] [blame] | 160 | getDefaultTransactionName->name = "getDefaultTransactionName"; |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 161 | auto code = std::make_shared<Variable>("int", "transactionCode"); |
Olivier Gaillard | 83d7cd3 | 2018-07-30 16:20:57 +0100 | [diff] [blame] | 162 | getDefaultTransactionName->parameters.push_back(code); |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 163 | getDefaultTransactionName->statements = std::make_shared<StatementBlock>(); |
| 164 | this->code_to_method_name_switch = std::make_shared<SwitchStatement>(code); |
Olivier Gaillard | 83d7cd3 | 2018-07-30 16:20:57 +0100 | [diff] [blame] | 165 | getDefaultTransactionName->statements->Add(this->code_to_method_name_switch); |
| 166 | this->elements.push_back(getDefaultTransactionName); |
| 167 | |
| 168 | // getTransactionName |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 169 | auto getTransactionName = std::make_shared<Method>(); |
Jiyong Park | e0b2803 | 2019-04-10 03:08:41 +0900 | [diff] [blame] | 170 | getTransactionName->comment = "/** @hide */"; |
Olivier Gaillard | 1140140 | 2018-07-05 15:01:34 +0100 | [diff] [blame] | 171 | getTransactionName->modifiers = PUBLIC; |
Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 172 | getTransactionName->returnType = "java.lang.String"; |
Olivier Gaillard | 1140140 | 2018-07-05 15:01:34 +0100 | [diff] [blame] | 173 | getTransactionName->name = "getTransactionName"; |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 174 | auto code2 = std::make_shared<Variable>("int", "transactionCode"); |
Olivier Gaillard | 83d7cd3 | 2018-07-30 16:20:57 +0100 | [diff] [blame] | 175 | getTransactionName->parameters.push_back(code2); |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 176 | getTransactionName->statements = std::make_shared<StatementBlock>(); |
| 177 | getTransactionName->statements->Add(std::make_shared<ReturnStatement>( |
| 178 | std::make_shared<MethodCall>(THIS_VALUE, "getDefaultTransactionName", |
| 179 | std::vector<std::shared_ptr<Expression>>{code2}))); |
Olivier Gaillard | 1140140 | 2018-07-05 15:01:34 +0100 | [diff] [blame] | 180 | this->elements.push_back(getTransactionName); |
| 181 | } |
| 182 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 183 | // onTransact |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 184 | this->transact_code = std::make_shared<Variable>("int", "code"); |
| 185 | this->transact_data = std::make_shared<Variable>("android.os.Parcel", "data"); |
| 186 | this->transact_reply = std::make_shared<Variable>("android.os.Parcel", "reply"); |
| 187 | this->transact_flags = std::make_shared<Variable>("int", "flags"); |
| 188 | auto onTransact = std::make_shared<Method>(); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 189 | onTransact->modifiers = PUBLIC | OVERRIDE; |
Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 190 | onTransact->returnType = "boolean"; |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 191 | onTransact->name = "onTransact"; |
| 192 | onTransact->parameters.push_back(this->transact_code); |
| 193 | onTransact->parameters.push_back(this->transact_data); |
| 194 | onTransact->parameters.push_back(this->transact_reply); |
| 195 | onTransact->parameters.push_back(this->transact_flags); |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 196 | onTransact->statements = std::make_shared<StatementBlock>(); |
Andreas Gampe | 7fab0d1 | 2017-11-22 17:50:17 -0800 | [diff] [blame] | 197 | transact_statements = onTransact->statements; |
Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 198 | onTransact->exceptions.push_back("android.os.RemoteException"); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 199 | this->elements.push_back(onTransact); |
Jiyong Park | a7ea8bf | 2021-01-05 10:36:18 +0900 | [diff] [blame] | 200 | this->transact_switch_meta = std::make_shared<SwitchStatement>(this->transact_code); |
| 201 | this->transact_switch_user = std::make_shared<SwitchStatement>(this->transact_code); |
Andreas Gampe | a8a66fe | 2017-11-22 12:17:00 -0800 | [diff] [blame] | 202 | } |
| 203 | |
Jooyung Han | 35a3418 | 2021-10-07 18:31:03 +0900 | [diff] [blame] | 204 | void StubClass::Finish() { |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 205 | auto default_case = std::make_shared<Case>(); |
Andreas Gampe | a8a66fe | 2017-11-22 12:17:00 -0800 | [diff] [blame] | 206 | |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 207 | auto superCall = std::make_shared<MethodCall>( |
| 208 | SUPER_VALUE, "onTransact", |
| 209 | std::vector<std::shared_ptr<Expression>>{this->transact_code, this->transact_data, |
| 210 | this->transact_reply, this->transact_flags}); |
| 211 | default_case->statements->Add(std::make_shared<ReturnStatement>(superCall)); |
Kevin Jeon | f2551d8 | 2021-07-27 16:06:07 +0000 | [diff] [blame] | 212 | |
| 213 | auto case_count = transact_switch_user->cases.size(); |
Jiyong Park | a7ea8bf | 2021-01-05 10:36:18 +0900 | [diff] [blame] | 214 | transact_switch_user->cases.push_back(default_case); |
Andreas Gampe | 7fab0d1 | 2017-11-22 17:50:17 -0800 | [diff] [blame] | 215 | |
Kevin Jeon | f2551d8 | 2021-07-27 16:06:07 +0000 | [diff] [blame] | 216 | // Interface token validation is done for user-defined transactions. |
| 217 | if (case_count > 0) { |
| 218 | auto ifStatement = std::make_shared<IfStatement>(); |
| 219 | ifStatement->expression = std::make_shared<LiteralExpression>( |
| 220 | "code >= android.os.IBinder.FIRST_CALL_TRANSACTION && " |
| 221 | "code <= android.os.IBinder.LAST_CALL_TRANSACTION"); |
| 222 | ifStatement->statements = std::make_shared<StatementBlock>(); |
| 223 | ifStatement->statements->Add(std::make_shared<MethodCall>( |
| 224 | this->transact_data, "enforceInterface", |
Jooyung Han | 35a3418 | 2021-10-07 18:31:03 +0900 | [diff] [blame] | 225 | std::vector<std::shared_ptr<Expression>>{this->GetTransactDescriptor(nullptr)})); |
Kevin Jeon | f2551d8 | 2021-07-27 16:06:07 +0000 | [diff] [blame] | 226 | transact_statements->Add(ifStatement); |
| 227 | } |
| 228 | |
Jiyong Park | a7ea8bf | 2021-01-05 10:36:18 +0900 | [diff] [blame] | 229 | // Meta transactions are looked up prior to user-defined transactions. |
| 230 | transact_statements->Add(this->transact_switch_meta); |
| 231 | transact_statements->Add(this->transact_switch_user); |
Olivier Gaillard | 1140140 | 2018-07-05 15:01:34 +0100 | [diff] [blame] | 232 | |
| 233 | // getTransactionName |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 234 | if (options_.GenTransactionNames()) { |
Olivier Gaillard | 1140140 | 2018-07-05 15:01:34 +0100 | [diff] [blame] | 235 | // Some transaction codes are common, e.g. INTERFACE_TRANSACTION or DUMP_TRANSACTION. |
| 236 | // Common transaction codes will not be resolved to a string by getTransactionName. The method |
| 237 | // will return NULL in this case. |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 238 | auto code_switch_default_case = std::make_shared<Case>(); |
| 239 | code_switch_default_case->statements->Add(std::make_shared<ReturnStatement>(NULL_VALUE)); |
Olivier Gaillard | 1140140 | 2018-07-05 15:01:34 +0100 | [diff] [blame] | 240 | this->code_to_method_name_switch->cases.push_back(code_switch_default_case); |
| 241 | } |
Kevin Jeon | f2551d8 | 2021-07-27 16:06:07 +0000 | [diff] [blame] | 242 | |
| 243 | // There will be at least one statement for the default, but if we emit a |
| 244 | // return true after that default, it will be unreachable. |
| 245 | if (case_count > 0) { |
| 246 | transact_statements->Add(std::make_shared<ReturnStatement>(TRUE_VALUE)); |
| 247 | } |
Andreas Gampe | 7fab0d1 | 2017-11-22 17:50:17 -0800 | [diff] [blame] | 248 | } |
| 249 | |
Andreas Gampe | e9c816e | 2018-03-14 09:05:48 -0700 | [diff] [blame] | 250 | // The the expression for the interface's descriptor to be used when |
| 251 | // generating code for the given method. Null is acceptable for method |
| 252 | // and stands for synthetic cases. |
Jooyung Han | 35a3418 | 2021-10-07 18:31:03 +0900 | [diff] [blame] | 253 | std::shared_ptr<Expression> StubClass::GetTransactDescriptor(const AidlMethod* method) { |
Andreas Gampe | 1b865af | 2017-11-22 11:31:47 -0800 | [diff] [blame] | 254 | if (transact_outline) { |
Andreas Gampe | e9c816e | 2018-03-14 09:05:48 -0700 | [diff] [blame] | 255 | if (method != nullptr) { |
| 256 | // When outlining, each outlined method needs its own literal. |
| 257 | if (outline_methods.count(method) != 0) { |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 258 | return std::make_shared<LiteralExpression>("DESCRIPTOR"); |
Andreas Gampe | e9c816e | 2018-03-14 09:05:48 -0700 | [diff] [blame] | 259 | } |
| 260 | } else { |
| 261 | // Synthetic case. A small number is assumed. Use its own descriptor |
| 262 | // if there are only synthetic cases. |
| 263 | if (outline_methods.size() == all_method_count) { |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 264 | return std::make_shared<LiteralExpression>("DESCRIPTOR"); |
Andreas Gampe | e9c816e | 2018-03-14 09:05:48 -0700 | [diff] [blame] | 265 | } |
| 266 | } |
Andreas Gampe | 1b865af | 2017-11-22 11:31:47 -0800 | [diff] [blame] | 267 | } |
| 268 | |
Andreas Gampe | e9c816e | 2018-03-14 09:05:48 -0700 | [diff] [blame] | 269 | // When not outlining, store the descriptor literal into a local variable, in |
| 270 | // an effort to save const-string instructions in each switch case. |
Andreas Gampe | 7fab0d1 | 2017-11-22 17:50:17 -0800 | [diff] [blame] | 271 | if (transact_descriptor == nullptr) { |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 272 | transact_descriptor = std::make_shared<Variable>("java.lang.String", "descriptor"); |
| 273 | transact_statements->Add(std::make_shared<VariableDeclaration>( |
| 274 | transact_descriptor, std::make_shared<LiteralExpression>("DESCRIPTOR"))); |
Andreas Gampe | 7fab0d1 | 2017-11-22 17:50:17 -0800 | [diff] [blame] | 275 | } |
| 276 | return transact_descriptor; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 277 | } |
| 278 | |
Jooyung Han | 35a3418 | 2021-10-07 18:31:03 +0900 | [diff] [blame] | 279 | void StubClass::MakeAsInterface(const AidlInterface* interfaceType) { |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 280 | auto obj = std::make_shared<Variable>("android.os.IBinder", "obj"); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 281 | |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 282 | auto m = std::make_shared<Method>(); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 283 | m->comment = "/**\n * Cast an IBinder object into an "; |
Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 284 | m->comment += interfaceType->GetCanonicalName(); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 285 | m->comment += " interface,\n"; |
| 286 | m->comment += " * generating a proxy if needed.\n */"; |
| 287 | m->modifiers = PUBLIC | STATIC; |
Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 288 | m->returnType = interfaceType->GetCanonicalName(); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 289 | m->name = "asInterface"; |
| 290 | m->parameters.push_back(obj); |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 291 | m->statements = std::make_shared<StatementBlock>(); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 292 | |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 293 | auto ifstatement = std::make_shared<IfStatement>(); |
| 294 | ifstatement->expression = std::make_shared<Comparison>(obj, "==", NULL_VALUE); |
| 295 | ifstatement->statements = std::make_shared<StatementBlock>(); |
| 296 | ifstatement->statements->Add(std::make_shared<ReturnStatement>(NULL_VALUE)); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 297 | m->statements->Add(ifstatement); |
| 298 | |
| 299 | // IInterface iin = obj.queryLocalInterface(DESCRIPTOR) |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 300 | auto queryLocalInterface = std::make_shared<MethodCall>(obj, "queryLocalInterface"); |
| 301 | queryLocalInterface->arguments.push_back(std::make_shared<LiteralExpression>("DESCRIPTOR")); |
| 302 | auto iin = std::make_shared<Variable>("android.os.IInterface", "iin"); |
| 303 | auto iinVd = std::make_shared<VariableDeclaration>(iin, queryLocalInterface); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 304 | m->statements->Add(iinVd); |
| 305 | |
| 306 | // Ensure the instance type of the local object is as expected. |
| 307 | // One scenario where this is needed is if another package (with a |
| 308 | // different class loader) runs in the same process as the service. |
| 309 | |
| 310 | // if (iin != null && iin instanceof <interfaceType>) return (<interfaceType>) |
| 311 | // iin; |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 312 | auto iinNotNull = std::make_shared<Comparison>(iin, "!=", NULL_VALUE); |
| 313 | auto instOfCheck = std::make_shared<Comparison>( |
| 314 | iin, " instanceof ", std::make_shared<LiteralExpression>(interfaceType->GetCanonicalName())); |
| 315 | auto instOfStatement = std::make_shared<IfStatement>(); |
| 316 | instOfStatement->expression = std::make_shared<Comparison>(iinNotNull, "&&", instOfCheck); |
| 317 | instOfStatement->statements = std::make_shared<StatementBlock>(); |
| 318 | instOfStatement->statements->Add(std::make_shared<ReturnStatement>( |
| 319 | std::make_shared<Cast>(interfaceType->GetCanonicalName(), iin))); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 320 | m->statements->Add(instOfStatement); |
| 321 | |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 322 | auto ne = std::make_shared<NewExpression>(interfaceType->GetCanonicalName() + ".Stub.Proxy"); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 323 | ne->arguments.push_back(obj); |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 324 | m->statements->Add(std::make_shared<ReturnStatement>(ne)); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 325 | |
| 326 | this->elements.push_back(m); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 327 | } |
| 328 | |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 329 | // ================================================= |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 330 | class ProxyClass : public Class { |
| 331 | public: |
Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 332 | ProxyClass(const AidlInterface* interfaceType, const Options& options); |
Yi Kong | de13891 | 2019-03-30 01:38:17 -0700 | [diff] [blame] | 333 | ~ProxyClass() override; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 334 | |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 335 | std::shared_ptr<Variable> mRemote; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 336 | }; |
| 337 | |
Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 338 | ProxyClass::ProxyClass(const AidlInterface* interfaceType, const Options& options) : Class() { |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 339 | this->modifiers = PRIVATE | STATIC; |
| 340 | this->what = Class::CLASS; |
Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 341 | this->type = interfaceType->GetCanonicalName() + ".Stub.Proxy"; |
| 342 | this->interfaces.push_back(interfaceType->GetCanonicalName()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 343 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 344 | // IBinder mRemote |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 345 | mRemote = std::make_shared<Variable>("android.os.IBinder", "mRemote"); |
| 346 | this->elements.push_back(std::make_shared<Field>(PRIVATE, mRemote)); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 347 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 348 | // Proxy() |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 349 | auto remote = std::make_shared<Variable>("android.os.IBinder", "remote"); |
| 350 | auto ctor = std::make_shared<Method>(); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 351 | ctor->name = "Proxy"; |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 352 | ctor->statements = std::make_shared<StatementBlock>(); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 353 | ctor->parameters.push_back(remote); |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 354 | ctor->statements->Add(std::make_shared<Assignment>(mRemote, remote)); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 355 | this->elements.push_back(ctor); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 356 | |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 357 | if (options.Version() > 0) { |
| 358 | std::ostringstream code; |
| 359 | code << "private int mCachedVersion = -1;\n"; |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 360 | this->elements.emplace_back(std::make_shared<LiteralClassElement>(code.str())); |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 361 | } |
Paul Trautrim | b77048c | 2020-01-21 16:39:32 +0900 | [diff] [blame] | 362 | if (!options.Hash().empty()) { |
| 363 | std::ostringstream code; |
| 364 | code << "private String mCachedHash = \"-1\";\n"; |
| 365 | this->elements.emplace_back(std::make_shared<LiteralClassElement>(code.str())); |
| 366 | } |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 367 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 368 | // IBinder asBinder() |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 369 | auto asBinder = std::make_shared<Method>(); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 370 | asBinder->modifiers = PUBLIC | OVERRIDE; |
Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 371 | asBinder->returnType = "android.os.IBinder"; |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 372 | asBinder->name = "asBinder"; |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 373 | asBinder->statements = std::make_shared<StatementBlock>(); |
| 374 | asBinder->statements->Add(std::make_shared<ReturnStatement>(mRemote)); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 375 | this->elements.push_back(asBinder); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 376 | } |
| 377 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 378 | ProxyClass::~ProxyClass() {} |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 379 | |
| 380 | // ================================================= |
Jooyung Han | 35a3418 | 2021-10-07 18:31:03 +0900 | [diff] [blame] | 381 | static void GenerateNewArray(const AidlTypeSpecifier& type, const AidlTypenames& typenames, |
| 382 | std::shared_ptr<StatementBlock> addTo, std::shared_ptr<Variable> v, |
| 383 | std::shared_ptr<Variable> parcel) { |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 384 | auto len = std::make_shared<Variable>("int", v->name + "_length"); |
| 385 | addTo->Add( |
| 386 | std::make_shared<VariableDeclaration>(len, std::make_shared<MethodCall>(parcel, "readInt"))); |
| 387 | auto lencheck = std::make_shared<IfStatement>(); |
| 388 | lencheck->expression = |
| 389 | std::make_shared<Comparison>(len, "<", std::make_shared<LiteralExpression>("0")); |
| 390 | lencheck->statements->Add(std::make_shared<Assignment>(v, NULL_VALUE)); |
| 391 | lencheck->elseif = std::make_shared<IfStatement>(); |
| 392 | lencheck->elseif->statements->Add(std::make_shared<Assignment>( |
Daniel Norman | 716d311 | 2019-09-10 13:11:56 -0700 | [diff] [blame] | 393 | v, std::make_shared<NewArrayExpression>(InstantiableJavaSignatureOf(type, typenames), len))); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 394 | addTo->Add(lencheck); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 395 | } |
| 396 | |
Jooyung Han | 35a3418 | 2021-10-07 18:31:03 +0900 | [diff] [blame] | 397 | static void GenerateWriteToParcel(const AidlTypeSpecifier& type, |
| 398 | std::shared_ptr<StatementBlock> addTo, |
| 399 | std::shared_ptr<Variable> v, std::shared_ptr<Variable> parcel, |
| 400 | bool is_return_value, const AidlTypenames& typenames) { |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 401 | string code; |
| 402 | CodeWriterPtr writer = CodeWriter::ForString(&code); |
| 403 | CodeGeneratorContext context{ |
| 404 | .writer = *(writer.get()), |
| 405 | .typenames = typenames, |
| 406 | .type = type, |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 407 | .parcel = parcel->name, |
Nick Desaulniers | 27e1ff6 | 2019-10-07 23:13:10 -0700 | [diff] [blame] | 408 | .var = v->name, |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 409 | .is_return_value = is_return_value, |
| 410 | }; |
| 411 | WriteToParcelFor(context); |
| 412 | writer->Close(); |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 413 | addTo->Add(std::make_shared<LiteralStatement>(code)); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 414 | } |
| 415 | |
Jooyung Han | 35a3418 | 2021-10-07 18:31:03 +0900 | [diff] [blame] | 416 | void GenerateConstantDeclarations(CodeWriter& out, const AidlDefinedType& type) { |
Jooyung Han | b43a61c | 2020-12-02 14:18:53 +0900 | [diff] [blame] | 417 | for (const auto& constant : type.GetConstantDeclarations()) { |
Jooyung Han | 3f347ca | 2020-12-01 12:41:50 +0900 | [diff] [blame] | 418 | const AidlTypeSpecifier& type = constant->GetType(); |
Jooyung Han | bd9db44 | 2021-01-14 01:45:55 +0900 | [diff] [blame] | 419 | out << GenerateComments(*constant); |
| 420 | out << GenerateAnnotations(*constant); |
Jooyung Han | 3f347ca | 2020-12-01 12:41:50 +0900 | [diff] [blame] | 421 | out << "public static final " << type.Signature() << " " << constant->GetName() << " = " |
| 422 | << constant->ValueString(ConstantValueDecorator) << ";\n"; |
| 423 | } |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 424 | } |
| 425 | |
Jooyung Han | 35a3418 | 2021-10-07 18:31:03 +0900 | [diff] [blame] | 426 | static std::shared_ptr<Method> GenerateInterfaceMethod(const AidlMethod& method, |
| 427 | const AidlTypenames& typenames) { |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 428 | auto decl = std::make_shared<Method>(); |
Jooyung Han | bd9db44 | 2021-01-14 01:45:55 +0900 | [diff] [blame] | 429 | decl->comment = GenerateComments(method); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 430 | decl->modifiers = PUBLIC; |
Daniel Norman | 716d311 | 2019-09-10 13:11:56 -0700 | [diff] [blame] | 431 | decl->returnType = JavaSignatureOf(method.GetType(), typenames); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 432 | decl->name = method.GetName(); |
Jooyung Han | 720253d | 2021-01-05 19:13:17 +0900 | [diff] [blame] | 433 | decl->annotations = JavaAnnotationsFor(method); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 434 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 435 | for (const std::unique_ptr<AidlArgument>& arg : method.GetArguments()) { |
Jiyong Park | bf5fd5c | 2020-06-05 19:48:05 +0900 | [diff] [blame] | 436 | auto var = std::make_shared<Variable>(JavaSignatureOf(arg->GetType(), typenames), arg->GetName()); |
Jooyung Han | 720253d | 2021-01-05 19:13:17 +0900 | [diff] [blame] | 437 | var->annotations = JavaAnnotationsFor(arg->GetType()); |
Jiyong Park | bf5fd5c | 2020-06-05 19:48:05 +0900 | [diff] [blame] | 438 | decl->parameters.push_back(var); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 439 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 440 | |
Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 441 | decl->exceptions.push_back("android.os.RemoteException"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 442 | |
Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 443 | return decl; |
| 444 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 445 | |
ThiƩbaud Weksteen | 5a4db21 | 2021-09-02 17:09:34 +0200 | [diff] [blame] | 446 | // Visitor for the permission declared in the @Enforce annotation. |
| 447 | // The visitor pattern evaluates one node at a time, recursive evaluations should be dispatched |
| 448 | // by creating a new instance of the visitor. Visit methods should use SetResult to return their |
| 449 | // current value. For example: |
| 450 | // |
| 451 | // Visit(const perm::Expression& permissionExpression) { |
| 452 | // std::shared_ptr<Expression> expr = Evaluate(permissionExpression.FirstChild()); |
| 453 | // ... |
| 454 | // SetResult(TRUE_VALUE); |
| 455 | // } |
| 456 | class PermissionVisitor : public perm::Visitor { |
| 457 | public: |
| 458 | // Converts a permission expression (e.g., "permission = CALL_PHONE") into an equivalent Java |
| 459 | // expression (e.g., "(checkPermission("CALL_PHONE", ...) == GRANTED"). |
| 460 | static std::shared_ptr<Expression> Evaluate(const perm::Expression& expr) { |
| 461 | PermissionVisitor visitor; |
| 462 | expr.DispatchVisit(visitor); |
| 463 | return visitor.GetResult(); |
| 464 | } |
| 465 | |
| 466 | private: |
| 467 | void Visit(const perm::AndQuantifier& quantifier) { |
| 468 | std::shared_ptr<Expression> result; |
| 469 | for (const auto& operand : quantifier.GetOperands()) { |
| 470 | auto expr = Evaluate(*operand); |
| 471 | if (result) { |
| 472 | result = std::make_shared<Comparison>(result, "&&", expr); |
| 473 | } else { |
| 474 | result = expr; |
| 475 | } |
| 476 | } |
| 477 | SetResult(result); |
| 478 | } |
| 479 | |
| 480 | void Visit(const perm::OrQuantifier& quantifier) { |
| 481 | std::shared_ptr<Expression> result; |
| 482 | for (const auto& operand : quantifier.GetOperands()) { |
| 483 | auto expr = Evaluate(*operand); |
| 484 | if (result) { |
| 485 | result = std::make_shared<Comparison>(result, "||", expr); |
| 486 | } else { |
| 487 | result = expr; |
| 488 | } |
| 489 | } |
| 490 | SetResult(result); |
| 491 | } |
| 492 | |
| 493 | void Visit(const perm::Predicate& p) { |
| 494 | switch (p.GetType()) { |
| 495 | case perm::Predicate::Type::kPermission: { |
| 496 | auto permissionGranted = std::make_shared<LiteralExpression>( |
| 497 | "android.content.pm.PackageManager.PERMISSION_GRANTED"); |
| 498 | auto checkPermission = std::make_shared<MethodCall>( |
| 499 | std::make_shared<LiteralExpression>("android.permission.PermissionManager"), |
| 500 | "checkPermission", |
| 501 | std::vector<std::shared_ptr<Expression>>{ |
| 502 | std::make_shared<LiteralExpression>("android.Manifest.permission." + p.GetValue()), |
| 503 | std::make_shared<MethodCall>(THIS_VALUE, "getCallingPid"), |
| 504 | std::make_shared<MethodCall>(THIS_VALUE, "getCallingUid")}); |
| 505 | SetResult(std::make_shared<Comparison>(checkPermission, "==", permissionGranted)); |
| 506 | break; |
| 507 | } |
| 508 | case perm::Predicate::Type::kUid: { |
| 509 | auto uid = std::make_shared<LiteralExpression>("android.os.Process." + p.GetValue()); |
| 510 | auto getCallingUid = std::make_shared<MethodCall>(THIS_VALUE, "getCallingUid"); |
| 511 | SetResult(std::make_shared<Comparison>(getCallingUid, "==", uid)); |
| 512 | break; |
| 513 | } |
| 514 | default: { |
| 515 | AIDL_FATAL(AIDL_LOCATION_HERE) << "Unsupported predicate: " << p.ToString(); |
| 516 | break; |
| 517 | } |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | std::shared_ptr<Expression> GetResult() { return result_; } |
| 522 | void SetResult(std::shared_ptr<Expression> expr) { result_ = expr; } |
| 523 | std::shared_ptr<Expression> result_; |
| 524 | }; |
| 525 | |
Jooyung Han | 35a3418 | 2021-10-07 18:31:03 +0900 | [diff] [blame] | 526 | static void GeneratePermissionChecks(const AidlMethod& method, |
| 527 | std::shared_ptr<StatementBlock> addTo) { |
ThiƩbaud Weksteen | 5a4db21 | 2021-09-02 17:09:34 +0200 | [diff] [blame] | 528 | auto expr = method.GetType().EnforceExpression(method); |
| 529 | if (expr) { |
| 530 | auto ifstatement = std::make_shared<IfStatement>(); |
| 531 | auto permissionExpression = PermissionVisitor::Evaluate(*expr.get()); |
| 532 | ifstatement->expression = std::make_shared<Comparison>(permissionExpression, "!=", TRUE_VALUE); |
| 533 | ifstatement->statements = std::make_shared<StatementBlock>(); |
| 534 | ifstatement->statements->Add(std::make_shared<LiteralStatement>(android::base::StringPrintf( |
| 535 | "throw new SecurityException(\"Access denied, requires: %s\");\n", |
| 536 | expr->ToString().c_str()))); |
| 537 | addTo->Add(ifstatement); |
| 538 | } |
| 539 | } |
| 540 | |
Jooyung Han | 35a3418 | 2021-10-07 18:31:03 +0900 | [diff] [blame] | 541 | static void GenerateStubCode(const AidlInterface& iface, const AidlMethod& method, bool oneway, |
| 542 | std::shared_ptr<Variable> transact_data, |
| 543 | std::shared_ptr<Variable> transact_reply, |
| 544 | const AidlTypenames& typenames, |
| 545 | std::shared_ptr<StatementBlock> statement_block, |
| 546 | const Options& options) { |
Devin Moore | e2ccf8a | 2020-05-13 14:28:20 -0700 | [diff] [blame] | 547 | // try and finally |
| 548 | auto tryStatement = std::make_shared<TryStatement>(); |
| 549 | auto finallyStatement = std::make_shared<FinallyStatement>(); |
| 550 | auto& statements = statement_block; |
| 551 | |
| 552 | if (options.GenTraces()) { |
| 553 | statements->Add(tryStatement); |
| 554 | statements->Add(finallyStatement); |
| 555 | statements = tryStatement->statements; |
| 556 | tryStatement->statements->Add(std::make_shared<MethodCall>( |
| 557 | std::make_shared<LiteralExpression>("android.os.Trace"), "traceBegin", |
| 558 | std::vector<std::shared_ptr<Expression>>{ |
| 559 | std::make_shared<LiteralExpression>("android.os.Trace.TRACE_TAG_AIDL"), |
| 560 | std::make_shared<StringLiteralExpression>("AIDL::java::" + iface.GetName() + |
| 561 | "::" + method.GetName() + "::server")})); |
| 562 | finallyStatement->statements->Add(std::make_shared<MethodCall>( |
| 563 | std::make_shared<LiteralExpression>("android.os.Trace"), "traceEnd", |
| 564 | std::vector<std::shared_ptr<Expression>>{ |
| 565 | std::make_shared<LiteralExpression>("android.os.Trace.TRACE_TAG_AIDL")})); |
| 566 | } |
| 567 | |
Jooyung Han | 35a3418 | 2021-10-07 18:31:03 +0900 | [diff] [blame] | 568 | GeneratePermissionChecks(method, statements); |
ThiƩbaud Weksteen | 5a4db21 | 2021-09-02 17:09:34 +0200 | [diff] [blame] | 569 | |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 570 | auto realCall = std::make_shared<MethodCall>(THIS_VALUE, method.GetName()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 571 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 572 | // args |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 573 | VariableFactory stubArgs("_arg"); |
Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 574 | { |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 575 | // keep this across different args in order to create the classloader |
| 576 | // at most once. |
| 577 | bool is_classloader_created = false; |
Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 578 | for (const std::unique_ptr<AidlArgument>& arg : method.GetArguments()) { |
Daniel Norman | 716d311 | 2019-09-10 13:11:56 -0700 | [diff] [blame] | 579 | std::shared_ptr<Variable> v = stubArgs.Get(arg->GetType(), typenames); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 580 | |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 581 | statements->Add(std::make_shared<VariableDeclaration>(v)); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 582 | |
Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 583 | if (arg->GetDirection() & AidlArgument::IN_DIR) { |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 584 | string code; |
| 585 | CodeWriterPtr writer = CodeWriter::ForString(&code); |
| 586 | CodeGeneratorContext context{.writer = *(writer.get()), |
Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 587 | .typenames = typenames, |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 588 | .type = arg->GetType(), |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 589 | .parcel = transact_data->name, |
Nick Desaulniers | 27e1ff6 | 2019-10-07 23:13:10 -0700 | [diff] [blame] | 590 | .var = v->name, |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 591 | .is_classloader_created = &is_classloader_created}; |
| 592 | CreateFromParcelFor(context); |
| 593 | writer->Close(); |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 594 | statements->Add(std::make_shared<LiteralStatement>(code)); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 595 | } else { |
Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 596 | if (!arg->GetType().IsArray()) { |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 597 | statements->Add(std::make_shared<Assignment>( |
Daniel Norman | 716d311 | 2019-09-10 13:11:56 -0700 | [diff] [blame] | 598 | v, std::make_shared<NewExpression>( |
| 599 | InstantiableJavaSignatureOf(arg->GetType(), typenames)))); |
Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 600 | } else { |
Jooyung Han | 35a3418 | 2021-10-07 18:31:03 +0900 | [diff] [blame] | 601 | GenerateNewArray(arg->GetType(), typenames, statements, v, transact_data); |
Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 602 | } |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 603 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 604 | |
Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 605 | realCall->arguments.push_back(v); |
| 606 | } |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 607 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 608 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 609 | // the real call |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 610 | if (method.GetType().GetName() == "void") { |
Devin Moore | e2ccf8a | 2020-05-13 14:28:20 -0700 | [diff] [blame] | 611 | statements->Add(realCall); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 612 | |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 613 | if (!oneway) { |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 614 | // report that there were no exceptions |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 615 | auto ex = std::make_shared<MethodCall>(transact_reply, "writeNoException"); |
Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 616 | statements->Add(ex); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 617 | } |
| 618 | } else { |
Daniel Norman | 716d311 | 2019-09-10 13:11:56 -0700 | [diff] [blame] | 619 | auto _result = |
| 620 | std::make_shared<Variable>(JavaSignatureOf(method.GetType(), typenames), "_result"); |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 621 | statements->Add(std::make_shared<VariableDeclaration>(_result, realCall)); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 622 | |
| 623 | if (!oneway) { |
| 624 | // report that there were no exceptions |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 625 | auto ex = std::make_shared<MethodCall>(transact_reply, "writeNoException"); |
Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 626 | statements->Add(ex); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 627 | } |
| 628 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 629 | // marshall the return value |
Jooyung Han | 35a3418 | 2021-10-07 18:31:03 +0900 | [diff] [blame] | 630 | GenerateWriteToParcel(method.GetType(), statements, _result, transact_reply, true, typenames); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | // out parameters |
Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 634 | int i = 0; |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 635 | for (const std::unique_ptr<AidlArgument>& arg : method.GetArguments()) { |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 636 | std::shared_ptr<Variable> v = stubArgs.Get(i++); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 637 | |
| 638 | if (arg->GetDirection() & AidlArgument::OUT_DIR) { |
Jooyung Han | 35a3418 | 2021-10-07 18:31:03 +0900 | [diff] [blame] | 639 | GenerateWriteToParcel(arg->GetType(), statements, v, transact_reply, true, typenames); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 640 | } |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 641 | } |
Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 642 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 643 | |
Jooyung Han | 35a3418 | 2021-10-07 18:31:03 +0900 | [diff] [blame] | 644 | static void GenerateStubCase(const AidlInterface& iface, const AidlMethod& method, |
| 645 | const std::string& transactCodeName, bool oneway, |
| 646 | std::shared_ptr<StubClass> stubClass, const AidlTypenames& typenames, |
| 647 | const Options& options) { |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 648 | auto c = std::make_shared<Case>(transactCodeName); |
Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 649 | |
Jooyung Han | 35a3418 | 2021-10-07 18:31:03 +0900 | [diff] [blame] | 650 | GenerateStubCode(iface, method, oneway, stubClass->transact_data, stubClass->transact_reply, |
| 651 | typenames, c->statements, options); |
Kevin Jeon | f2551d8 | 2021-07-27 16:06:07 +0000 | [diff] [blame] | 652 | c->statements->Add(std::make_shared<BreakStatement>()); |
Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 653 | |
Jiyong Park | a7ea8bf | 2021-01-05 10:36:18 +0900 | [diff] [blame] | 654 | stubClass->transact_switch_user->cases.push_back(c); |
Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 655 | } |
| 656 | |
Jooyung Han | 35a3418 | 2021-10-07 18:31:03 +0900 | [diff] [blame] | 657 | static void GenerateStubCaseOutline(const AidlInterface& iface, const AidlMethod& method, |
| 658 | const std::string& transactCodeName, bool oneway, |
| 659 | std::shared_ptr<StubClass> stubClass, |
| 660 | const AidlTypenames& typenames, const Options& options) { |
Andreas Gampe | 1b865af | 2017-11-22 11:31:47 -0800 | [diff] [blame] | 661 | std::string outline_name = "onTransact$" + method.GetName() + "$"; |
| 662 | // Generate an "outlined" method with the actual code. |
| 663 | { |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 664 | auto transact_data = std::make_shared<Variable>("android.os.Parcel", "data"); |
| 665 | auto transact_reply = std::make_shared<Variable>("android.os.Parcel", "reply"); |
| 666 | auto onTransact_case = std::make_shared<Method>(); |
Andreas Gampe | 1b865af | 2017-11-22 11:31:47 -0800 | [diff] [blame] | 667 | onTransact_case->modifiers = PRIVATE; |
Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 668 | onTransact_case->returnType = "boolean"; |
Andreas Gampe | 1b865af | 2017-11-22 11:31:47 -0800 | [diff] [blame] | 669 | onTransact_case->name = outline_name; |
| 670 | onTransact_case->parameters.push_back(transact_data); |
| 671 | onTransact_case->parameters.push_back(transact_reply); |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 672 | onTransact_case->statements = std::make_shared<StatementBlock>(); |
Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 673 | onTransact_case->exceptions.push_back("android.os.RemoteException"); |
Andreas Gampe | 1b865af | 2017-11-22 11:31:47 -0800 | [diff] [blame] | 674 | stubClass->elements.push_back(onTransact_case); |
| 675 | |
Jooyung Han | 35a3418 | 2021-10-07 18:31:03 +0900 | [diff] [blame] | 676 | GenerateStubCode(iface, method, oneway, transact_data, transact_reply, typenames, |
| 677 | onTransact_case->statements, options); |
Kevin Jeon | f2551d8 | 2021-07-27 16:06:07 +0000 | [diff] [blame] | 678 | onTransact_case->statements->Add(std::make_shared<ReturnStatement>(TRUE_VALUE)); |
Andreas Gampe | 1b865af | 2017-11-22 11:31:47 -0800 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | // Generate the case dispatch. |
| 682 | { |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 683 | auto c = std::make_shared<Case>(transactCodeName); |
Andreas Gampe | 1b865af | 2017-11-22 11:31:47 -0800 | [diff] [blame] | 684 | |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 685 | auto helper_call = |
| 686 | std::make_shared<MethodCall>(THIS_VALUE, outline_name, |
| 687 | std::vector<std::shared_ptr<Expression>>{ |
| 688 | stubClass->transact_data, stubClass->transact_reply}); |
| 689 | c->statements->Add(std::make_shared<ReturnStatement>(helper_call)); |
Andreas Gampe | 1b865af | 2017-11-22 11:31:47 -0800 | [diff] [blame] | 690 | |
Jiyong Park | a7ea8bf | 2021-01-05 10:36:18 +0900 | [diff] [blame] | 691 | stubClass->transact_switch_user->cases.push_back(c); |
Andreas Gampe | 1b865af | 2017-11-22 11:31:47 -0800 | [diff] [blame] | 692 | } |
| 693 | } |
| 694 | |
Jooyung Han | 35a3418 | 2021-10-07 18:31:03 +0900 | [diff] [blame] | 695 | static std::shared_ptr<Method> GenerateProxyMethod(const AidlInterface& iface, |
| 696 | const AidlMethod& method, |
| 697 | const std::string& transactCodeName, bool oneway, |
| 698 | std::shared_ptr<ProxyClass> proxyClass, |
| 699 | const AidlTypenames& typenames, |
| 700 | const Options& options) { |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 701 | auto proxy = std::make_shared<Method>(); |
Jooyung Han | bd9db44 | 2021-01-14 01:45:55 +0900 | [diff] [blame] | 702 | proxy->comment = GenerateComments(method); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 703 | proxy->modifiers = PUBLIC | OVERRIDE; |
Daniel Norman | 716d311 | 2019-09-10 13:11:56 -0700 | [diff] [blame] | 704 | proxy->returnType = JavaSignatureOf(method.GetType(), typenames); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 705 | proxy->name = method.GetName(); |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 706 | proxy->statements = std::make_shared<StatementBlock>(); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 707 | for (const std::unique_ptr<AidlArgument>& arg : method.GetArguments()) { |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 708 | proxy->parameters.push_back( |
Daniel Norman | 716d311 | 2019-09-10 13:11:56 -0700 | [diff] [blame] | 709 | std::make_shared<Variable>(JavaSignatureOf(arg->GetType(), typenames), arg->GetName())); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 710 | } |
Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 711 | proxy->exceptions.push_back("android.os.RemoteException"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 712 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 713 | // the parcels |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 714 | auto _data = std::make_shared<Variable>("android.os.Parcel", "_data"); |
Steven Moreland | a4b6b29 | 2021-09-01 12:35:28 -0700 | [diff] [blame] | 715 | if (options.GenRpc()) { |
Steven Moreland | 188a427 | 2021-09-29 12:05:47 -0700 | [diff] [blame] | 716 | proxy->statements->Add(std::make_shared<LiteralStatement>( |
| 717 | "android.os.Parcel _data = android.os.Parcel.obtain(asBinder());\n")); |
| 718 | } else { |
| 719 | proxy->statements->Add(std::make_shared<LiteralStatement>( |
| 720 | "android.os.Parcel _data = android.os.Parcel.obtain();\n")); |
Steven Moreland | a4b6b29 | 2021-09-01 12:35:28 -0700 | [diff] [blame] | 721 | } |
| 722 | |
Steven Moreland | a7764e5 | 2020-10-27 17:29:29 +0000 | [diff] [blame] | 723 | if (iface.IsSensitiveData()) { |
Steven Moreland | a4b6b29 | 2021-09-01 12:35:28 -0700 | [diff] [blame] | 724 | proxy->statements->Add(std::make_shared<LiteralStatement>("_data.markSensitive();\n")); |
Steven Moreland | a7764e5 | 2020-10-27 17:29:29 +0000 | [diff] [blame] | 725 | } |
| 726 | |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 727 | std::shared_ptr<Variable> _reply = nullptr; |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 728 | if (!oneway) { |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 729 | _reply = std::make_shared<Variable>("android.os.Parcel", "_reply"); |
| 730 | proxy->statements->Add(std::make_shared<VariableDeclaration>( |
| 731 | _reply, std::make_shared<MethodCall>("android.os.Parcel", "obtain"))); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | // the return value |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 735 | std::shared_ptr<Variable> _result = nullptr; |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 736 | if (method.GetType().GetName() != "void") { |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 737 | _result = std::make_shared<Variable>(*proxy->returnType, "_result"); |
| 738 | proxy->statements->Add(std::make_shared<VariableDeclaration>(_result)); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | // try and finally |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 742 | auto tryStatement = std::make_shared<TryStatement>(); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 743 | proxy->statements->Add(tryStatement); |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 744 | auto finallyStatement = std::make_shared<FinallyStatement>(); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 745 | proxy->statements->Add(finallyStatement); |
| 746 | |
Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 747 | if (options.GenTraces()) { |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 748 | tryStatement->statements->Add(std::make_shared<MethodCall>( |
| 749 | std::make_shared<LiteralExpression>("android.os.Trace"), "traceBegin", |
| 750 | std::vector<std::shared_ptr<Expression>>{ |
| 751 | std::make_shared<LiteralExpression>("android.os.Trace.TRACE_TAG_AIDL"), |
Devin Moore | e2ccf8a | 2020-05-13 14:28:20 -0700 | [diff] [blame] | 752 | std::make_shared<StringLiteralExpression>("AIDL::java::" + iface.GetName() + |
| 753 | "::" + method.GetName() + "::client")})); |
Martijn Coenen | f1b5078 | 2018-02-21 21:06:23 +0100 | [diff] [blame] | 754 | } |
| 755 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 756 | // the interface identifier token: the DESCRIPTOR constant, marshalled as a |
| 757 | // string |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 758 | tryStatement->statements->Add(std::make_shared<MethodCall>( |
| 759 | _data, "writeInterfaceToken", |
| 760 | std::vector<std::shared_ptr<Expression>>{std::make_shared<LiteralExpression>("DESCRIPTOR")})); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 761 | |
| 762 | // the parameters |
| 763 | for (const std::unique_ptr<AidlArgument>& arg : method.GetArguments()) { |
Daniel Norman | 716d311 | 2019-09-10 13:11:56 -0700 | [diff] [blame] | 764 | auto v = std::make_shared<Variable>(JavaSignatureOf(arg->GetType(), typenames), arg->GetName()); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 765 | AidlArgument::Direction dir = arg->GetDirection(); |
| 766 | if (dir == AidlArgument::OUT_DIR && arg->GetType().IsArray()) { |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 767 | auto checklen = std::make_shared<IfStatement>(); |
| 768 | checklen->expression = std::make_shared<Comparison>(v, "==", NULL_VALUE); |
| 769 | checklen->statements->Add(std::make_shared<MethodCall>( |
| 770 | _data, "writeInt", |
| 771 | std::vector<std::shared_ptr<Expression>>{std::make_shared<LiteralExpression>("-1")})); |
| 772 | checklen->elseif = std::make_shared<IfStatement>(); |
| 773 | checklen->elseif->statements->Add(std::make_shared<MethodCall>( |
| 774 | _data, "writeInt", |
| 775 | std::vector<std::shared_ptr<Expression>>{std::make_shared<FieldVariable>(v, "length")})); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 776 | tryStatement->statements->Add(checklen); |
| 777 | } else if (dir & AidlArgument::IN_DIR) { |
Jooyung Han | 35a3418 | 2021-10-07 18:31:03 +0900 | [diff] [blame] | 778 | GenerateWriteToParcel(arg->GetType(), tryStatement->statements, v, _data, false, typenames); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 779 | } |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 780 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 781 | |
Steven Moreland | a7764e5 | 2020-10-27 17:29:29 +0000 | [diff] [blame] | 782 | std::vector<std::string> flags; |
| 783 | if (oneway) flags.push_back("android.os.IBinder.FLAG_ONEWAY"); |
| 784 | if (iface.IsSensitiveData()) flags.push_back("android.os.IBinder.FLAG_CLEAR_BUF"); |
| 785 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 786 | // the transact call |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 787 | auto call = std::make_shared<MethodCall>( |
| 788 | proxyClass->mRemote, "transact", |
| 789 | std::vector<std::shared_ptr<Expression>>{ |
| 790 | std::make_shared<LiteralExpression>("Stub." + transactCodeName), _data, |
| 791 | _reply ? _reply : NULL_VALUE, |
Steven Moreland | a7764e5 | 2020-10-27 17:29:29 +0000 | [diff] [blame] | 792 | std::make_shared<LiteralExpression>(flags.empty() ? "0" : Join(flags, " | "))}); |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 793 | auto _status = std::make_shared<Variable>("boolean", "_status"); |
| 794 | tryStatement->statements->Add(std::make_shared<VariableDeclaration>(_status, call)); |
Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 795 | |
Jiyong Park | cf999d6 | 2020-09-15 11:57:04 +0900 | [diff] [blame] | 796 | // If the transaction returns false, which means UNKNOWN_TRANSACTION, fall back to the local |
| 797 | // method in the default impl, if set before. Otherwise, throw a RuntimeException if the interface |
| 798 | // is versioned. We can't throw the exception for unversioned interface because that would be an |
| 799 | // app breaking change. |
Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 800 | vector<string> arg_names; |
| 801 | for (const auto& arg : method.GetArguments()) { |
| 802 | arg_names.emplace_back(arg->GetName()); |
| 803 | } |
| 804 | bool has_return_type = method.GetType().GetName() != "void"; |
Jiyong Park | cf999d6 | 2020-09-15 11:57:04 +0900 | [diff] [blame] | 805 | |
| 806 | auto checkDefaultImpl = std::make_shared<IfStatement>(); |
| 807 | checkDefaultImpl->expression = std::make_shared<LiteralExpression>("getDefaultImpl() != null"); |
| 808 | if (has_return_type) { |
| 809 | checkDefaultImpl->statements->Add(std::make_shared<LiteralStatement>( |
| 810 | android::base::StringPrintf("return getDefaultImpl().%s(%s);\n", method.GetName().c_str(), |
| 811 | Join(arg_names, ", ").c_str()))); |
| 812 | } else { |
| 813 | checkDefaultImpl->statements->Add(std::make_shared<LiteralStatement>( |
| 814 | android::base::StringPrintf("getDefaultImpl().%s(%s);\n", method.GetName().c_str(), |
| 815 | Join(arg_names, ", ").c_str()))); |
| 816 | checkDefaultImpl->statements->Add(std::make_shared<LiteralStatement>("return;\n")); |
| 817 | } |
| 818 | if (options.Version() > 0) { |
| 819 | checkDefaultImpl->elseif = std::make_shared<IfStatement>(); |
| 820 | checkDefaultImpl->elseif->statements->Add( |
| 821 | std::make_shared<LiteralStatement>(android::base::StringPrintf( |
Jiyong Park | 1fd4078 | 2020-09-15 13:09:52 +0900 | [diff] [blame] | 822 | "throw new android.os.RemoteException(\"Method %s is unimplemented.\");\n", |
Jiyong Park | cf999d6 | 2020-09-15 11:57:04 +0900 | [diff] [blame] | 823 | method.GetName().c_str()))); |
| 824 | } |
| 825 | |
| 826 | auto checkTransactionError = std::make_shared<IfStatement>(); |
| 827 | checkTransactionError->expression = std::make_shared<LiteralExpression>("!_status"); |
| 828 | checkTransactionError->statements->Add(checkDefaultImpl); |
| 829 | |
| 830 | tryStatement->statements->Add(checkTransactionError); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 831 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 832 | // throw back exceptions. |
| 833 | if (_reply) { |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 834 | auto ex = std::make_shared<MethodCall>(_reply, "readException"); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 835 | tryStatement->statements->Add(ex); |
| 836 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 837 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 838 | // returning and cleanup |
Yi Kong | 894d6ba | 2018-07-24 11:27:38 -0700 | [diff] [blame] | 839 | if (_reply != nullptr) { |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 840 | // keep this across return value and arguments in order to create the |
| 841 | // classloader at most once. |
| 842 | bool is_classloader_created = false; |
Yi Kong | 894d6ba | 2018-07-24 11:27:38 -0700 | [diff] [blame] | 843 | if (_result != nullptr) { |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 844 | string code; |
| 845 | CodeWriterPtr writer = CodeWriter::ForString(&code); |
| 846 | CodeGeneratorContext context{.writer = *(writer.get()), |
Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 847 | .typenames = typenames, |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 848 | .type = method.GetType(), |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 849 | .parcel = _reply->name, |
Nick Desaulniers | 27e1ff6 | 2019-10-07 23:13:10 -0700 | [diff] [blame] | 850 | .var = _result->name, |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 851 | .is_classloader_created = &is_classloader_created}; |
| 852 | CreateFromParcelFor(context); |
| 853 | writer->Close(); |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 854 | tryStatement->statements->Add(std::make_shared<LiteralStatement>(code)); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 855 | } |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 856 | |
| 857 | // the out/inout parameters |
| 858 | for (const std::unique_ptr<AidlArgument>& arg : method.GetArguments()) { |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 859 | if (arg->GetDirection() & AidlArgument::OUT_DIR) { |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 860 | string code; |
| 861 | CodeWriterPtr writer = CodeWriter::ForString(&code); |
| 862 | CodeGeneratorContext context{.writer = *(writer.get()), |
Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 863 | .typenames = typenames, |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 864 | .type = arg->GetType(), |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 865 | .parcel = _reply->name, |
Nick Desaulniers | 27e1ff6 | 2019-10-07 23:13:10 -0700 | [diff] [blame] | 866 | .var = arg->GetName(), |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 867 | .is_classloader_created = &is_classloader_created}; |
| 868 | ReadFromParcelFor(context); |
| 869 | writer->Close(); |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 870 | tryStatement->statements->Add(std::make_shared<LiteralStatement>(code)); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 871 | } |
| 872 | } |
| 873 | |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 874 | finallyStatement->statements->Add(std::make_shared<MethodCall>(_reply, "recycle")); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 875 | } |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 876 | finallyStatement->statements->Add(std::make_shared<MethodCall>(_data, "recycle")); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 877 | |
Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 878 | if (options.GenTraces()) { |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 879 | finallyStatement->statements->Add(std::make_shared<MethodCall>( |
| 880 | std::make_shared<LiteralExpression>("android.os.Trace"), "traceEnd", |
| 881 | std::vector<std::shared_ptr<Expression>>{ |
| 882 | std::make_shared<LiteralExpression>("android.os.Trace.TRACE_TAG_AIDL")})); |
Martijn Coenen | f1b5078 | 2018-02-21 21:06:23 +0100 | [diff] [blame] | 883 | } |
| 884 | |
Yi Kong | 894d6ba | 2018-07-24 11:27:38 -0700 | [diff] [blame] | 885 | if (_result != nullptr) { |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 886 | proxy->statements->Add(std::make_shared<ReturnStatement>(_result)); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 887 | } |
Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 888 | |
| 889 | return proxy; |
| 890 | } |
| 891 | |
Jooyung Han | 35a3418 | 2021-10-07 18:31:03 +0900 | [diff] [blame] | 892 | static void GenerateMethods(const AidlInterface& iface, const AidlMethod& method, Class* interface, |
| 893 | std::shared_ptr<StubClass> stubClass, |
| 894 | std::shared_ptr<ProxyClass> proxyClass, int index, |
| 895 | const AidlTypenames& typenames, const Options& options) { |
Steven Moreland | acd5347 | 2018-12-14 10:17:26 -0800 | [diff] [blame] | 896 | const bool oneway = method.IsOneway(); |
Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 897 | |
| 898 | // == the TRANSACT_ constant ============================================= |
| 899 | string transactCodeName = "TRANSACTION_"; |
| 900 | transactCodeName += method.GetName(); |
| 901 | |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 902 | auto transactCode = |
| 903 | std::make_shared<Field>(STATIC | FINAL, std::make_shared<Variable>("int", transactCodeName)); |
Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 904 | transactCode->value = |
| 905 | StringPrintf("(android.os.IBinder.FIRST_CALL_TRANSACTION + %d)", index); |
| 906 | stubClass->elements.push_back(transactCode); |
| 907 | |
Olivier Gaillard | 1140140 | 2018-07-05 15:01:34 +0100 | [diff] [blame] | 908 | // getTransactionName |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 909 | if (options.GenTransactionNames()) { |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 910 | auto c = std::make_shared<Case>(transactCodeName); |
| 911 | c->statements->Add(std::make_shared<ReturnStatement>( |
| 912 | std::make_shared<StringLiteralExpression>(method.GetName()))); |
Olivier Gaillard | 1140140 | 2018-07-05 15:01:34 +0100 | [diff] [blame] | 913 | stubClass->code_to_method_name_switch->cases.push_back(c); |
| 914 | } |
| 915 | |
Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 916 | // == the declaration in the interface =================================== |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 917 | std::shared_ptr<ClassElement> decl; |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 918 | if (method.IsUserDefined()) { |
Jooyung Han | 35a3418 | 2021-10-07 18:31:03 +0900 | [diff] [blame] | 919 | decl = GenerateInterfaceMethod(method, typenames); |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 920 | } else { |
| 921 | if (method.GetName() == kGetInterfaceVersion && options.Version() > 0) { |
| 922 | std::ostringstream code; |
| 923 | code << "public int " << kGetInterfaceVersion << "() " |
| 924 | << "throws android.os.RemoteException;\n"; |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 925 | decl = std::make_shared<LiteralClassElement>(code.str()); |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 926 | } |
Paul Trautrim | b77048c | 2020-01-21 16:39:32 +0900 | [diff] [blame] | 927 | if (method.GetName() == kGetInterfaceHash && !options.Hash().empty()) { |
| 928 | std::ostringstream code; |
| 929 | code << "public String " << kGetInterfaceHash << "() " |
| 930 | << "throws android.os.RemoteException;\n"; |
| 931 | decl = std::make_shared<LiteralClassElement>(code.str()); |
| 932 | } |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 933 | } |
Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 934 | interface->elements.push_back(decl); |
| 935 | |
| 936 | // == the stub method ==================================================== |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 937 | if (method.IsUserDefined()) { |
| 938 | bool outline_stub = |
| 939 | stubClass->transact_outline && stubClass->outline_methods.count(&method) != 0; |
| 940 | if (outline_stub) { |
Jooyung Han | 35a3418 | 2021-10-07 18:31:03 +0900 | [diff] [blame] | 941 | GenerateStubCaseOutline(iface, method, transactCodeName, oneway, stubClass, typenames, |
| 942 | options); |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 943 | } else { |
Jooyung Han | 35a3418 | 2021-10-07 18:31:03 +0900 | [diff] [blame] | 944 | GenerateStubCase(iface, method, transactCodeName, oneway, stubClass, typenames, options); |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 945 | } |
Andreas Gampe | 1b865af | 2017-11-22 11:31:47 -0800 | [diff] [blame] | 946 | } else { |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 947 | if (method.GetName() == kGetInterfaceVersion && options.Version() > 0) { |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 948 | auto c = std::make_shared<Case>(transactCodeName); |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 949 | std::ostringstream code; |
Kevin Jeon | f2551d8 | 2021-07-27 16:06:07 +0000 | [diff] [blame] | 950 | code << "reply.writeNoException();\n" |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 951 | << "reply.writeInt(" << kGetInterfaceVersion << "());\n" |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 952 | << "return true;\n"; |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 953 | c->statements->Add(std::make_shared<LiteralStatement>(code.str())); |
Jiyong Park | a7ea8bf | 2021-01-05 10:36:18 +0900 | [diff] [blame] | 954 | stubClass->transact_switch_meta->cases.push_back(c); |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 955 | } |
Paul Trautrim | b77048c | 2020-01-21 16:39:32 +0900 | [diff] [blame] | 956 | if (method.GetName() == kGetInterfaceHash && !options.Hash().empty()) { |
| 957 | auto c = std::make_shared<Case>(transactCodeName); |
| 958 | std::ostringstream code; |
Kevin Jeon | f2551d8 | 2021-07-27 16:06:07 +0000 | [diff] [blame] | 959 | code << "reply.writeNoException();\n" |
Paul Trautrim | b77048c | 2020-01-21 16:39:32 +0900 | [diff] [blame] | 960 | << "reply.writeString(" << kGetInterfaceHash << "());\n" |
| 961 | << "return true;\n"; |
| 962 | c->statements->Add(std::make_shared<LiteralStatement>(code.str())); |
Jiyong Park | a7ea8bf | 2021-01-05 10:36:18 +0900 | [diff] [blame] | 963 | stubClass->transact_switch_meta->cases.push_back(c); |
Paul Trautrim | b77048c | 2020-01-21 16:39:32 +0900 | [diff] [blame] | 964 | } |
Andreas Gampe | 1b865af | 2017-11-22 11:31:47 -0800 | [diff] [blame] | 965 | } |
Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 966 | |
| 967 | // == the proxy method =================================================== |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 968 | std::shared_ptr<ClassElement> proxy = nullptr; |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 969 | if (method.IsUserDefined()) { |
Jooyung Han | 35a3418 | 2021-10-07 18:31:03 +0900 | [diff] [blame] | 970 | proxy = GenerateProxyMethod(iface, method, transactCodeName, oneway, proxyClass, typenames, |
| 971 | options); |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 972 | |
| 973 | } else { |
| 974 | if (method.GetName() == kGetInterfaceVersion && options.Version() > 0) { |
| 975 | std::ostringstream code; |
| 976 | code << "@Override\n" |
| 977 | << "public int " << kGetInterfaceVersion << "()" |
| 978 | << " throws " |
| 979 | << "android.os.RemoteException {\n" |
| 980 | << " if (mCachedVersion == -1) {\n" |
| 981 | << " android.os.Parcel data = android.os.Parcel.obtain();\n" |
Steven Moreland | a4b6b29 | 2021-09-01 12:35:28 -0700 | [diff] [blame] | 982 | << " android.os.Parcel reply = android.os.Parcel.obtain();\n"; |
| 983 | if (options.GenRpc()) { |
| 984 | code << " data.markForBinder(asBinder());\n"; |
| 985 | } |
| 986 | code << " try {\n" |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 987 | << " data.writeInterfaceToken(DESCRIPTOR);\n" |
Jiyong Park | 6b39de4 | 2019-08-27 13:04:57 +0900 | [diff] [blame] | 988 | << " boolean _status = mRemote.transact(Stub." << transactCodeName << ", " |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 989 | << "data, reply, 0);\n" |
Jiyong Park | 6b39de4 | 2019-08-27 13:04:57 +0900 | [diff] [blame] | 990 | << " if (!_status) {\n" |
| 991 | << " if (getDefaultImpl() != null) {\n" |
| 992 | << " return getDefaultImpl().getInterfaceVersion();\n" |
| 993 | << " }\n" |
| 994 | << " }\n" |
Jeongik Cha | f1470e2 | 2019-05-20 18:45:05 +0900 | [diff] [blame] | 995 | << " reply.readException();\n" |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 996 | << " mCachedVersion = reply.readInt();\n" |
| 997 | << " } finally {\n" |
| 998 | << " reply.recycle();\n" |
| 999 | << " data.recycle();\n" |
| 1000 | << " }\n" |
| 1001 | << " }\n" |
| 1002 | << " return mCachedVersion;\n" |
| 1003 | << "}\n"; |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 1004 | proxy = std::make_shared<LiteralClassElement>(code.str()); |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 1005 | } |
Paul Trautrim | b77048c | 2020-01-21 16:39:32 +0900 | [diff] [blame] | 1006 | if (method.GetName() == kGetInterfaceHash && !options.Hash().empty()) { |
| 1007 | std::ostringstream code; |
| 1008 | code << "@Override\n" |
| 1009 | << "public synchronized String " << kGetInterfaceHash << "()" |
| 1010 | << " throws " |
| 1011 | << "android.os.RemoteException {\n" |
Jeongik Cha | b142818 | 2020-05-13 19:49:30 +0900 | [diff] [blame] | 1012 | << " if (\"-1\".equals(mCachedHash)) {\n" |
Paul Trautrim | b77048c | 2020-01-21 16:39:32 +0900 | [diff] [blame] | 1013 | << " android.os.Parcel data = android.os.Parcel.obtain();\n" |
Steven Moreland | a4b6b29 | 2021-09-01 12:35:28 -0700 | [diff] [blame] | 1014 | << " android.os.Parcel reply = android.os.Parcel.obtain();\n"; |
| 1015 | if (options.GenRpc()) { |
| 1016 | code << " data.markForBinder(asBinder());\n"; |
| 1017 | } |
| 1018 | code << " try {\n" |
Paul Trautrim | b77048c | 2020-01-21 16:39:32 +0900 | [diff] [blame] | 1019 | << " data.writeInterfaceToken(DESCRIPTOR);\n" |
| 1020 | << " boolean _status = mRemote.transact(Stub." << transactCodeName << ", " |
| 1021 | << "data, reply, 0);\n" |
| 1022 | << " if (!_status) {\n" |
| 1023 | << " if (getDefaultImpl() != null) {\n" |
| 1024 | << " return getDefaultImpl().getInterfaceHash();\n" |
| 1025 | << " }\n" |
| 1026 | << " }\n" |
| 1027 | << " reply.readException();\n" |
| 1028 | << " mCachedHash = reply.readString();\n" |
| 1029 | << " } finally {\n" |
| 1030 | << " reply.recycle();\n" |
| 1031 | << " data.recycle();\n" |
| 1032 | << " }\n" |
| 1033 | << " }\n" |
| 1034 | << " return mCachedHash;\n" |
| 1035 | << "}\n"; |
| 1036 | proxy = std::make_shared<LiteralClassElement>(code.str()); |
| 1037 | } |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 1038 | } |
| 1039 | if (proxy != nullptr) { |
| 1040 | proxyClass->elements.push_back(proxy); |
| 1041 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1042 | } |
| 1043 | |
Jooyung Han | 35a3418 | 2021-10-07 18:31:03 +0900 | [diff] [blame] | 1044 | static void GenerateInterfaceDescriptors(const Options& options, const AidlInterface* iface, |
| 1045 | Class* interface, std::shared_ptr<StubClass> stub, |
| 1046 | std::shared_ptr<ProxyClass> proxy) { |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 1047 | // the interface descriptor transaction handler |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 1048 | auto c = std::make_shared<Case>("INTERFACE_TRANSACTION"); |
| 1049 | c->statements->Add(std::make_shared<MethodCall>( |
| 1050 | stub->transact_reply, "writeString", |
Jooyung Han | 35a3418 | 2021-10-07 18:31:03 +0900 | [diff] [blame] | 1051 | std::vector<std::shared_ptr<Expression>>{stub->GetTransactDescriptor(nullptr)})); |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 1052 | c->statements->Add(std::make_shared<ReturnStatement>(TRUE_VALUE)); |
Jiyong Park | a7ea8bf | 2021-01-05 10:36:18 +0900 | [diff] [blame] | 1053 | stub->transact_switch_meta->cases.push_back(c); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1054 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 1055 | // and the proxy-side method returning the descriptor directly |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 1056 | auto getDesc = std::make_shared<Method>(); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 1057 | getDesc->modifiers = PUBLIC; |
Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 1058 | getDesc->returnType = "java.lang.String"; |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 1059 | getDesc->name = "getInterfaceDescriptor"; |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 1060 | getDesc->statements = std::make_shared<StatementBlock>(); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 1061 | getDesc->statements->Add( |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 1062 | std::make_shared<ReturnStatement>(std::make_shared<LiteralExpression>("DESCRIPTOR"))); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 1063 | proxy->elements.push_back(getDesc); |
Jiyong Park | 5faba3e | 2020-04-24 17:08:37 +0900 | [diff] [blame] | 1064 | |
| 1065 | // add the DESCRIPTOR field to the interface class |
| 1066 | Class* classToAddDescriptor = interface; |
| 1067 | static std::set<std::string> greylist = { |
| 1068 | #include "hiddenapi-greylist" |
| 1069 | }; |
| 1070 | if (greylist.find(iface->GetCanonicalName()) != greylist.end()) { |
| 1071 | // For app compatibility, we keep DESCRIPTOR to the stub class for |
| 1072 | // the interfaces that are in the greylist. |
| 1073 | classToAddDescriptor = stub.get(); |
| 1074 | } |
| 1075 | auto descriptor = std::make_shared<Field>( |
| 1076 | STATIC | FINAL | PUBLIC, std::make_shared<Variable>("java.lang.String", "DESCRIPTOR")); |
Jiyong Park | 27fd7fd | 2020-08-27 16:25:09 +0900 | [diff] [blame] | 1077 | std::string name = iface->GetDescriptor(); |
Jiyong Park | 5faba3e | 2020-04-24 17:08:37 +0900 | [diff] [blame] | 1078 | if (options.IsStructured()) { |
| 1079 | // mangle the interface name at build time and demangle it at runtime, to avoid |
| 1080 | // being renamed by jarjar. See b/153843174 |
Jiyong Park | 5faba3e | 2020-04-24 17:08:37 +0900 | [diff] [blame] | 1081 | std::replace(name.begin(), name.end(), '.', '$'); |
| 1082 | descriptor->value = "\"" + name + "\".replace('$', '.')"; |
| 1083 | } else { |
Jiyong Park | 27fd7fd | 2020-08-27 16:25:09 +0900 | [diff] [blame] | 1084 | descriptor->value = "\"" + name + "\""; |
Jiyong Park | 5faba3e | 2020-04-24 17:08:37 +0900 | [diff] [blame] | 1085 | } |
| 1086 | classToAddDescriptor->elements.push_back(descriptor); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1087 | } |
| 1088 | |
Andreas Gampe | e9c816e | 2018-03-14 09:05:48 -0700 | [diff] [blame] | 1089 | // Check whether (some) methods in this interface should be "outlined," that |
| 1090 | // is, have specific onTransact methods for certain cases. Set up StubClass |
| 1091 | // metadata accordingly. |
| 1092 | // |
| 1093 | // Outlining will be enabled if the interface has more than outline_threshold |
| 1094 | // methods. In that case, the methods are sorted by number of arguments |
| 1095 | // (so that more "complex" methods come later), and the first non_outline_count |
| 1096 | // number of methods not outlined (are kept in the onTransact() method). |
| 1097 | // |
| 1098 | // Requirements: non_outline_count <= outline_threshold. |
Jooyung Han | 35a3418 | 2021-10-07 18:31:03 +0900 | [diff] [blame] | 1099 | static void ComputeOutlineMethods(const AidlInterface* iface, const std::shared_ptr<StubClass> stub, |
| 1100 | size_t outline_threshold, size_t non_outline_count) { |
Steven Moreland | 2178081 | 2020-09-11 01:29:45 +0000 | [diff] [blame] | 1101 | AIDL_FATAL_IF(non_outline_count > outline_threshold, iface); |
Andreas Gampe | e9c816e | 2018-03-14 09:05:48 -0700 | [diff] [blame] | 1102 | // We'll outline (create sub methods) if there are more than min_methods |
| 1103 | // cases. |
| 1104 | stub->transact_outline = iface->GetMethods().size() > outline_threshold; |
| 1105 | if (stub->transact_outline) { |
| 1106 | stub->all_method_count = iface->GetMethods().size(); |
| 1107 | std::vector<const AidlMethod*> methods; |
| 1108 | methods.reserve(iface->GetMethods().size()); |
Jooyung Han | 3f347ca | 2020-12-01 12:41:50 +0900 | [diff] [blame] | 1109 | for (const auto& ptr : iface->GetMethods()) { |
Andreas Gampe | e9c816e | 2018-03-14 09:05:48 -0700 | [diff] [blame] | 1110 | methods.push_back(ptr.get()); |
| 1111 | } |
| 1112 | |
| 1113 | std::stable_sort( |
| 1114 | methods.begin(), |
| 1115 | methods.end(), |
| 1116 | [](const AidlMethod* m1, const AidlMethod* m2) { |
| 1117 | return m1->GetArguments().size() < m2->GetArguments().size(); |
| 1118 | }); |
| 1119 | |
| 1120 | stub->outline_methods.insert(methods.begin() + non_outline_count, |
| 1121 | methods.end()); |
| 1122 | } |
| 1123 | } |
| 1124 | |
Jooyung Han | 35a3418 | 2021-10-07 18:31:03 +0900 | [diff] [blame] | 1125 | static shared_ptr<ClassElement> GenerateDefaultImplMethod(const AidlMethod& method, |
| 1126 | const AidlTypenames& typenames) { |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 1127 | auto default_method = std::make_shared<Method>(); |
Jooyung Han | bd9db44 | 2021-01-14 01:45:55 +0900 | [diff] [blame] | 1128 | default_method->comment = GenerateComments(method); |
Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 1129 | default_method->modifiers = PUBLIC | OVERRIDE; |
Daniel Norman | 716d311 | 2019-09-10 13:11:56 -0700 | [diff] [blame] | 1130 | default_method->returnType = JavaSignatureOf(method.GetType(), typenames); |
Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 1131 | default_method->name = method.GetName(); |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 1132 | default_method->statements = std::make_shared<StatementBlock>(); |
Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 1133 | for (const auto& arg : method.GetArguments()) { |
Steven Moreland | 3dc29d8 | 2019-08-21 17:23:11 -0700 | [diff] [blame] | 1134 | default_method->parameters.push_back( |
Daniel Norman | 716d311 | 2019-09-10 13:11:56 -0700 | [diff] [blame] | 1135 | std::make_shared<Variable>(JavaSignatureOf(arg->GetType(), typenames), arg->GetName())); |
Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 1136 | } |
Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 1137 | default_method->exceptions.push_back("android.os.RemoteException"); |
Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 1138 | |
| 1139 | if (method.GetType().GetName() != "void") { |
Daniel Norman | 716d311 | 2019-09-10 13:11:56 -0700 | [diff] [blame] | 1140 | const string& defaultValue = DefaultJavaValueOf(method.GetType(), typenames); |
Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 1141 | default_method->statements->Add( |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 1142 | std::make_shared<LiteralStatement>(StringPrintf("return %s;\n", defaultValue.c_str()))); |
Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 1143 | } |
| 1144 | return default_method; |
| 1145 | } |
| 1146 | |
Jooyung Han | 35a3418 | 2021-10-07 18:31:03 +0900 | [diff] [blame] | 1147 | static shared_ptr<Class> GenerateDefaultImplClass(const AidlInterface& iface, |
| 1148 | const AidlTypenames& typenames, |
| 1149 | const Options& options) { |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 1150 | auto default_class = std::make_shared<Class>(); |
Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 1151 | default_class->comment = "/** Default implementation for " + iface.GetName() + ". */"; |
| 1152 | default_class->modifiers = PUBLIC | STATIC; |
| 1153 | default_class->what = Class::CLASS; |
Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 1154 | default_class->type = iface.GetCanonicalName() + ".Default"; |
| 1155 | default_class->interfaces.emplace_back(iface.GetCanonicalName()); |
Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 1156 | |
| 1157 | for (const auto& m : iface.GetMethods()) { |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 1158 | if (m->IsUserDefined()) { |
Jooyung Han | 35a3418 | 2021-10-07 18:31:03 +0900 | [diff] [blame] | 1159 | default_class->elements.emplace_back(GenerateDefaultImplMethod(*m, typenames)); |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 1160 | } else { |
Paul Trautrim | b77048c | 2020-01-21 16:39:32 +0900 | [diff] [blame] | 1161 | // These are called only when the remote side does not implement these |
| 1162 | // methods, which is normally impossible, because these methods are |
| 1163 | // automatically declared in the interface class and not implementing |
| 1164 | // them on the remote side causes a compilation error. But if the remote |
| 1165 | // side somehow managed to not implement it, that's an error and we |
| 1166 | // report the case by returning an invalid value here. |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 1167 | if (m->GetName() == kGetInterfaceVersion && options.Version() > 0) { |
| 1168 | std::ostringstream code; |
| 1169 | code << "@Override\n" |
| 1170 | << "public int " << kGetInterfaceVersion << "() {\n" |
Paul Trautrim | b77048c | 2020-01-21 16:39:32 +0900 | [diff] [blame] | 1171 | << " return 0;\n" |
| 1172 | << "}\n"; |
| 1173 | default_class->elements.emplace_back(std::make_shared<LiteralClassElement>(code.str())); |
| 1174 | } |
| 1175 | if (m->GetName() == kGetInterfaceHash && !options.Hash().empty()) { |
| 1176 | std::ostringstream code; |
| 1177 | code << "@Override\n" |
| 1178 | << "public String " << kGetInterfaceHash << "() {\n" |
| 1179 | << " return \"\";\n" |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 1180 | << "}\n"; |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 1181 | default_class->elements.emplace_back(std::make_shared<LiteralClassElement>(code.str())); |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 1182 | } |
| 1183 | } |
Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 1184 | } |
| 1185 | |
| 1186 | default_class->elements.emplace_back( |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 1187 | std::make_shared<LiteralClassElement>("@Override\n" |
| 1188 | "public android.os.IBinder asBinder() {\n" |
| 1189 | " return null;\n" |
| 1190 | "}\n")); |
Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 1191 | |
| 1192 | return default_class; |
| 1193 | } |
| 1194 | |
Jooyung Han | 35a3418 | 2021-10-07 18:31:03 +0900 | [diff] [blame] | 1195 | std::unique_ptr<Class> GenerateInterfaceClass(const AidlInterface* iface, |
| 1196 | const AidlTypenames& typenames, |
| 1197 | const Options& options) { |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 1198 | // the interface class |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 1199 | auto interface = std::make_unique<Class>(); |
Jooyung Han | bd9db44 | 2021-01-14 01:45:55 +0900 | [diff] [blame] | 1200 | interface->comment = GenerateComments(*iface); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 1201 | interface->modifiers = PUBLIC; |
| 1202 | interface->what = Class::INTERFACE; |
Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 1203 | interface->type = iface->GetCanonicalName(); |
| 1204 | interface->interfaces.push_back("android.os.IInterface"); |
Jooyung Han | 720253d | 2021-01-05 19:13:17 +0900 | [diff] [blame] | 1205 | interface->annotations = JavaAnnotationsFor(*iface); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1206 | |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 1207 | if (options.Version()) { |
| 1208 | std::ostringstream code; |
| 1209 | code << "/**\n" |
| 1210 | << " * The version of this interface that the caller is built against.\n" |
| 1211 | << " * This might be different from what {@link #getInterfaceVersion()\n" |
| 1212 | << " * getInterfaceVersion} returns as that is the version of the interface\n" |
| 1213 | << " * that the remote object is implementing.\n" |
| 1214 | << " */\n" |
| 1215 | << "public static final int VERSION = " << options.Version() << ";\n"; |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 1216 | interface->elements.emplace_back(std::make_shared<LiteralClassElement>(code.str())); |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 1217 | } |
Paul Trautrim | b77048c | 2020-01-21 16:39:32 +0900 | [diff] [blame] | 1218 | if (!options.Hash().empty()) { |
| 1219 | std::ostringstream code; |
| 1220 | code << "public static final String HASH = \"" << options.Hash() << "\";\n"; |
| 1221 | interface->elements.emplace_back(std::make_shared<LiteralClassElement>(code.str())); |
| 1222 | } |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 1223 | |
Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 1224 | // the default impl class |
Jooyung Han | 35a3418 | 2021-10-07 18:31:03 +0900 | [diff] [blame] | 1225 | auto default_impl = GenerateDefaultImplClass(*iface, typenames, options); |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 1226 | interface->elements.emplace_back(default_impl); |
Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 1227 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 1228 | // the stub inner class |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 1229 | auto stub = std::make_shared<StubClass>(iface, options); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 1230 | interface->elements.push_back(stub); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1231 | |
Jooyung Han | 35a3418 | 2021-10-07 18:31:03 +0900 | [diff] [blame] | 1232 | ComputeOutlineMethods(iface, stub, options.onTransact_outline_threshold_, |
| 1233 | options.onTransact_non_outline_count_); |
Andreas Gampe | 1b865af | 2017-11-22 11:31:47 -0800 | [diff] [blame] | 1234 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 1235 | // the proxy inner class |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 1236 | auto proxy = std::make_shared<ProxyClass>(iface, options); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 1237 | stub->elements.push_back(proxy); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1238 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 1239 | // stub and proxy support for getInterfaceDescriptor() |
Jooyung Han | 35a3418 | 2021-10-07 18:31:03 +0900 | [diff] [blame] | 1240 | GenerateInterfaceDescriptors(options, iface, interface.get(), stub, proxy); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1241 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 1242 | // all the declared constants of the interface |
Jooyung Han | b43a61c | 2020-12-02 14:18:53 +0900 | [diff] [blame] | 1243 | string constants; |
Jooyung Han | 35a3418 | 2021-10-07 18:31:03 +0900 | [diff] [blame] | 1244 | GenerateConstantDeclarations(*CodeWriter::ForString(&constants), *iface); |
Jooyung Han | 3f347ca | 2020-12-01 12:41:50 +0900 | [diff] [blame] | 1245 | interface->elements.push_back(std::make_shared<LiteralClassElement>(constants)); |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 1246 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 1247 | // all the declared methods of the interface |
Andreas Gampe | 1b865af | 2017-11-22 11:31:47 -0800 | [diff] [blame] | 1248 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 1249 | for (const auto& item : iface->GetMethods()) { |
Jooyung Han | 35a3418 | 2021-10-07 18:31:03 +0900 | [diff] [blame] | 1250 | GenerateMethods(*iface, *item, interface.get(), stub, proxy, item->GetId(), typenames, options); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 1251 | } |
Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 1252 | |
Jooyung Han | 9983413 | 2021-10-07 20:40:31 +0900 | [diff] [blame] | 1253 | // all the nested types |
| 1254 | for (const auto& nested : iface->GetNestedTypes()) { |
| 1255 | string code; |
| 1256 | auto writer = CodeWriter::ForString(&code); |
| 1257 | GenerateClass(*writer, *nested, typenames, options); |
| 1258 | writer->Close(); |
| 1259 | interface->elements.push_back(std::make_shared<LiteralClassElement>(code)); |
| 1260 | } |
| 1261 | |
Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 1262 | // additional static methods for the default impl set/get to the |
| 1263 | // stub class. Can't add them to the interface as the generated java files |
| 1264 | // may be compiled with Java < 1.7 where static interface method isn't |
| 1265 | // supported. |
| 1266 | // TODO(b/111417145) make this conditional depending on the Java language |
| 1267 | // version requested |
Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 1268 | const string i_name = iface->GetCanonicalName(); |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 1269 | stub->elements.emplace_back(std::make_shared<LiteralClassElement>( |
Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 1270 | StringPrintf("public static boolean setDefaultImpl(%s impl) {\n" |
Jooyung Han | 4a04466 | 2020-05-13 17:17:07 +0900 | [diff] [blame] | 1271 | " // Only one user of this interface can use this function\n" |
| 1272 | " // at a time. This is a heuristic to detect if two different\n" |
| 1273 | " // users in the same process use this function.\n" |
| 1274 | " if (Stub.Proxy.sDefaultImpl != null) {\n" |
| 1275 | " throw new IllegalStateException(\"setDefaultImpl() called twice\");\n" |
| 1276 | " }\n" |
| 1277 | " if (impl != null) {\n" |
Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 1278 | " Stub.Proxy.sDefaultImpl = impl;\n" |
| 1279 | " return true;\n" |
| 1280 | " }\n" |
| 1281 | " return false;\n" |
| 1282 | "}\n", |
| 1283 | i_name.c_str()))); |
| 1284 | stub->elements.emplace_back( |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 1285 | std::make_shared<LiteralClassElement>(StringPrintf("public static %s getDefaultImpl() {\n" |
| 1286 | " return Stub.Proxy.sDefaultImpl;\n" |
| 1287 | "}\n", |
| 1288 | i_name.c_str()))); |
Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 1289 | |
| 1290 | // the static field is defined in the proxy class, not in the interface class |
| 1291 | // because all fields in an interface class are by default final. |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 1292 | proxy->elements.emplace_back(std::make_shared<LiteralClassElement>( |
Jiyong Park | 47fb0d6 | 2018-11-17 10:12:15 +0900 | [diff] [blame] | 1293 | StringPrintf("public static %s sDefaultImpl;\n", i_name.c_str()))); |
Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 1294 | |
Jooyung Han | 35a3418 | 2021-10-07 18:31:03 +0900 | [diff] [blame] | 1295 | stub->Finish(); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1296 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 1297 | return interface; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1298 | } |
| 1299 | |
Christopher Wiley | db154a5 | 2015-09-28 16:32:25 -0700 | [diff] [blame] | 1300 | } // namespace java |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 1301 | } // namespace aidl |
Steven Moreland | f4c64df | 2019-07-29 19:54:04 -0700 | [diff] [blame] | 1302 | } // namespace android |