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