Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(136)

Side by Side Diff: mojo/public/tools/bindings/generators/cpp_templates/interface_definition.tmpl

Issue 1701423002: Mojo C++ bindings: support sync methods - part 1. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 {%- import "interface_macros.tmpl" as interface_macros %} 1 {%- import "interface_macros.tmpl" as interface_macros %}
2 {%- import "struct_macros.tmpl" as struct_macros %} 2 {%- import "struct_macros.tmpl" as struct_macros %}
3 3
4 {%- set class_name = interface.name %} 4 {%- set class_name = interface.name %}
5 {%- set proxy_name = interface.name ~ "Proxy" %} 5 {%- set proxy_name = interface.name ~ "Proxy" %}
6 {%- set namespace_as_string = "%s"|format(namespace|replace(".","::")) %} 6 {%- set namespace_as_string = "%s"|format(namespace|replace(".","::")) %}
7 7
8 {%- macro alloc_params(struct, serialization_context) %} 8 {%- macro alloc_params(struct, serialization_context) %}
9 bool success = true; 9 bool success = true;
10 {%- for param in struct.packed.packed_fields_in_ordinal_order %} 10 {%- for param in struct.packed.packed_fields_in_ordinal_order %}
11 {{param.field.kind|cpp_wrapper_type}} p_{{param.field.name}}{}; 11 {{param.field.kind|cpp_wrapper_type}} p_{{param.field.name}}{};
12 {%- endfor %} 12 {%- endfor %}
13 {{struct_macros.deserialize(struct, "params", "p_%s", serialization_context, " success")}} 13 {{struct_macros.deserialize(struct, "params", "p_%s", serialization_context, " success")}}
14 if (!success) 14 if (!success)
15 return false; 15 return false;
16 {%- endmacro %} 16 {%- endmacro %}
17 17
18 {%- macro pass_params(parameters) %} 18 {%- macro pass_params(parameters) %}
19 {%- for param in parameters %} 19 {%- for param in parameters %}
20 {%- if param.kind|is_move_only_kind -%} 20 {%- if param.kind|is_move_only_kind -%}
21 std::move(p_{{param.name}}) 21 std::move(p_{{param.name}})
22 {%- else -%} 22 {%- else -%}
23 p_{{param.name}} 23 p_{{param.name}}
24 {%- endif -%} 24 {%- endif -%}
25 {%- if not loop.last %}, {% endif %} 25 {%- if not loop.last %}, {% endif %}
26 {%- endfor %} 26 {%- endfor %}
27 {%- endmacro %} 27 {%- endmacro %}
28 28
29 {%- macro build_message(struct, struct_display_name) -%} 29 {%- macro build_message(struct, input_pattern, struct_display_name) -%}
30 {{struct_macros.serialize(struct, struct_display_name, "in_%s", "params", "bui lder.buffer()")}} 30 {{struct_macros.serialize(struct, struct_display_name, input_pattern, "params" , "builder.buffer()")}}
31 params->EncodePointersAndHandles(builder.message()->mutable_handles()); 31 params->EncodePointersAndHandles(builder.message()->mutable_handles());
32 {%- endmacro %} 32 {%- endmacro %}
33 33
34 {#--- Begin #} 34 {#--- Begin #}
35 MOJO_STATIC_CONST_MEMBER_DEFINITION const char {{class_name}}::Name_[] = "{{name space_as_string}}::{{class_name}}"; 35 MOJO_STATIC_CONST_MEMBER_DEFINITION const char {{class_name}}::Name_[] = "{{name space_as_string}}::{{class_name}}";
36 MOJO_STATIC_CONST_MEMBER_DEFINITION const uint32_t {{class_name}}::Version_; 36 MOJO_STATIC_CONST_MEMBER_DEFINITION const uint32_t {{class_name}}::Version_;
37 37
38 {#--- Constants #} 38 {#--- Constants #}
39 {%- for constant in interface.constants %} 39 {%- for constant in interface.constants %}
40 {%- if constant.kind|is_integral_kind %} 40 {%- if constant.kind|is_integral_kind %}
41 MOJO_STATIC_CONST_MEMBER_DEFINITION const {{constant.kind|cpp_pod_type}} {{inter face.name}}::{{constant.name}}; 41 MOJO_STATIC_CONST_MEMBER_DEFINITION const {{constant.kind|cpp_pod_type}} {{inter face.name}}::{{constant.name}};
42 {%- else %} 42 {%- else %}
43 MOJO_STATIC_CONST_MEMBER_DEFINITION const {{constant.kind|cpp_pod_type}} {{inter face.name}}::{{constant.name}} = {{constant|constant_value}}; 43 MOJO_STATIC_CONST_MEMBER_DEFINITION const {{constant.kind|cpp_pod_type}} {{inter face.name}}::{{constant.name}} = {{constant|constant_value}};
44 {%- endif %} 44 {%- endif %}
45 {%- endfor %} 45 {%- endfor %}
46 46
47 {#--- ForwardToCallback definition #} 47 {#--- ForwardToCallback definition #}
48 {%- for method in interface.methods -%} 48 {%- for method in interface.methods -%}
49 {%- if method.response_parameters != None %} 49 {%- if method.response_parameters != None %}
50 {%- if method.sync %}
51 class {{class_name}}_{{method.name}}_HandleSyncResponse
52 : public mojo::MessageReceiver {
53 public:
54 {{class_name}}_{{method.name}}_HandleSyncResponse(
55 scoped_refptr<mojo::internal::MultiplexRouter> router, bool* result
56 {%- for param in method.response_parameters -%}
57 , {{param.kind|cpp_wrapper_type}}* out_{{param.name}}
58 {%- endfor %})
59 : serialization_context_(std::move(router)), result_(result)
60 {%- for param in method.response_parameters -%}
61 , out_{{param.name}}_(out_{{param.name}})
62 {%- endfor %} {
63 DCHECK(!*result_);
64 }
65 bool Accept(mojo::Message* message) override;
66 private:
67 mojo::internal::SerializationContext serialization_context_;
68 bool* result_;
69 {%- for param in method.response_parameters %}
70 {{param.kind|cpp_wrapper_type}}* out_{{param.name}}_;
71 {%- endfor -%}
72 DISALLOW_COPY_AND_ASSIGN({{class_name}}_{{method.name}}_HandleSyncResponse);
73 };
74 bool {{class_name}}_{{method.name}}_HandleSyncResponse::Accept(
75 mojo::Message* message) {
76 internal::{{class_name}}_{{method.name}}_ResponseParams_Data* params =
77 reinterpret_cast<internal::{{class_name}}_{{method.name}}_ResponseParams_D ata*>(
78 message->mutable_payload());
79
80 params->DecodePointersAndHandles(message->mutable_handles());
81 {{alloc_params(method.response_param_struct, "&serialization_context_")}}
82
83 {%- for param in method.response_parameters %}
84 {%- if param.kind|is_move_only_kind %}
85 *out_{{param.name}}_ = std::move(p_{{param.name}});
86 {%- else %}
87 *out_{{param.name}}_ = p_{{param.name}};
88 {%- endif %}
89 {%- endfor %}
90 *result_ = true;
91 return true;
92 }
93 {%- endif %}
94
50 class {{class_name}}_{{method.name}}_ForwardToCallback 95 class {{class_name}}_{{method.name}}_ForwardToCallback
51 : public mojo::MessageReceiver { 96 : public mojo::MessageReceiver {
52 public: 97 public:
53 {{class_name}}_{{method.name}}_ForwardToCallback( 98 {{class_name}}_{{method.name}}_ForwardToCallback(
54 const {{class_name}}::{{method.name}}Callback& callback, 99 const {{class_name}}::{{method.name}}Callback& callback,
55 scoped_refptr<mojo::internal::MultiplexRouter> router) 100 scoped_refptr<mojo::internal::MultiplexRouter> router)
56 : callback_(callback), serialization_context_(std::move(router)) { 101 : callback_(callback), serialization_context_(std::move(router)) {
57 } 102 }
58 bool Accept(mojo::Message* message) override; 103 bool Accept(mojo::Message* message) override;
59 private: 104 private:
60 {{class_name}}::{{method.name}}Callback callback_; 105 {{class_name}}::{{method.name}}Callback callback_;
61 mojo::internal::SerializationContext serialization_context_; 106 mojo::internal::SerializationContext serialization_context_;
62 MOJO_DISALLOW_COPY_AND_ASSIGN({{class_name}}_{{method.name}}_ForwardToCallback ); 107 DISALLOW_COPY_AND_ASSIGN({{class_name}}_{{method.name}}_ForwardToCallback);
63 }; 108 };
64 bool {{class_name}}_{{method.name}}_ForwardToCallback::Accept( 109 bool {{class_name}}_{{method.name}}_ForwardToCallback::Accept(
65 mojo::Message* message) { 110 mojo::Message* message) {
66 internal::{{class_name}}_{{method.name}}_ResponseParams_Data* params = 111 internal::{{class_name}}_{{method.name}}_ResponseParams_Data* params =
67 reinterpret_cast<internal::{{class_name}}_{{method.name}}_ResponseParams_D ata*>( 112 reinterpret_cast<internal::{{class_name}}_{{method.name}}_ResponseParams_D ata*>(
68 message->mutable_payload()); 113 message->mutable_payload());
69 114
70 params->DecodePointersAndHandles(message->mutable_handles()); 115 params->DecodePointersAndHandles(message->mutable_handles());
71 {{alloc_params(method.response_param_struct, "&serialization_context_")}} 116 {{alloc_params(method.response_param_struct, "&serialization_context_")}}
72 callback_.Run({{pass_params(method.response_parameters)}}); 117 callback_.Run({{pass_params(method.response_parameters)}});
73 return true; 118 return true;
74 } 119 }
75 {%- endif %} 120 {%- endif %}
76 {%- endfor %} 121 {%- endfor %}
77 122
78 {{proxy_name}}::{{proxy_name}}(mojo::MessageReceiverWithResponder* receiver) 123 {{proxy_name}}::{{proxy_name}}(mojo::MessageReceiverWithResponder* receiver)
79 : ControlMessageProxy(receiver) { 124 : ControlMessageProxy(receiver) {
80 } 125 }
81 126
82 {#--- Proxy definitions #} 127 {#--- Proxy definitions #}
83 128
84 {%- for method in interface.methods %} 129 {%- for method in interface.methods %}
85 {%- set message_name = 130 {%- set message_name =
86 "internal::k%s_%s_Name"|format(interface.name, method.name) %} 131 "internal::k%s_%s_Name"|format(interface.name, method.name) %}
87 {%- set params_struct = method.param_struct %} 132 {%- set params_struct = method.param_struct %}
88 {%- set params_description = 133 {%- set params_description =
89 "%s.%s request"|format(interface.name, method.name) %} 134 "%s.%s request"|format(interface.name, method.name) %}
135 {%- if method.sync %}
136 bool {{proxy_name}}::{{method.name}}(
137 {{interface_macros.declare_sync_method_params("param_", method)}}) {
138 {{struct_macros.get_serialized_size(params_struct, "param_%s")}}
139
140 mojo::internal::RequestMessageBuilder builder({{message_name}}, size,
141 mojo::internal::kMessageIsSync);
142
143 {{build_message(params_struct, "param_%s", params_description)}}
144
145 bool result = false;
146 mojo::MessageReceiver* responder =
147 new {{class_name}}_{{method.name}}_HandleSyncResponse(
148 serialization_context_.router, &result
149 {%- for param in method.response_parameters -%}
150 , param_{{param.name}}
151 {%- endfor %});
152 if (!receiver_->AcceptWithResponder(builder.message(), responder))
153 delete responder;
154 return result;
155 }
156 {%- endif %}
157
90 void {{proxy_name}}::{{method.name}}( 158 void {{proxy_name}}::{{method.name}}(
91 {{interface_macros.declare_request_params("in_", method)}}) { 159 {{interface_macros.declare_request_params("in_", method)}}) {
92 {{struct_macros.get_serialized_size(params_struct, "in_%s")}} 160 {{struct_macros.get_serialized_size(params_struct, "in_%s")}}
93 161
94 {%- if method.response_parameters != None %} 162 {%- if method.response_parameters != None %}
95 mojo::internal::RequestMessageBuilder builder({{message_name}}, size); 163 mojo::internal::RequestMessageBuilder builder({{message_name}}, size);
96 {%- else %} 164 {%- else %}
97 mojo::internal::MessageBuilder builder({{message_name}}, size); 165 mojo::internal::MessageBuilder builder({{message_name}}, size);
98 {%- endif %} 166 {%- endif %}
99 167
100 {{build_message(params_struct, params_description)}} 168 {{build_message(params_struct, "in_%s", params_description)}}
101 169
102 {%- if method.response_parameters != None %} 170 {%- if method.response_parameters != None %}
103 mojo::MessageReceiver* responder = 171 mojo::MessageReceiver* responder =
104 new {{class_name}}_{{method.name}}_ForwardToCallback(callback, serializati on_context_.router); 172 new {{class_name}}_{{method.name}}_ForwardToCallback(callback, serializati on_context_.router);
105 if (!receiver_->AcceptWithResponder(builder.message(), responder)) 173 if (!receiver_->AcceptWithResponder(builder.message(), responder))
106 delete responder; 174 delete responder;
107 {%- else %} 175 {%- else %}
108 bool ok = receiver_->Accept(builder.message()); 176 bool ok = receiver_->Accept(builder.message());
109 // This return value may be ignored as !ok implies the Connector has 177 // This return value may be ignored as !ok implies the Connector has
110 // encountered an error, which will be visible through other means. 178 // encountered an error, which will be visible through other means.
(...skipping 20 matching lines...) Expand all
131 // If the Callback was dropped then deleting the responder will close 199 // If the Callback was dropped then deleting the responder will close
132 // the pipe so the calling application knows to stop waiting for a reply. 200 // the pipe so the calling application knows to stop waiting for a reply.
133 delete responder_; 201 delete responder_;
134 DCHECK(!callback_was_dropped) << "The callback passed to " 202 DCHECK(!callback_was_dropped) << "The callback passed to "
135 "{{class_name}}::{{method.name}}({%- if method.parameters -%}{{pass_para ms(method.parameters)}}, {% endif -%}callback) " 203 "{{class_name}}::{{method.name}}({%- if method.parameters -%}{{pass_para ms(method.parameters)}}, {% endif -%}callback) "
136 "was never run."; 204 "was never run.";
137 } 205 }
138 206
139 {{class_name}}_{{method.name}}_ProxyToResponder( 207 {{class_name}}_{{method.name}}_ProxyToResponder(
140 uint64_t request_id, 208 uint64_t request_id,
209 bool is_sync,
141 mojo::MessageReceiverWithStatus* responder) 210 mojo::MessageReceiverWithStatus* responder)
142 : request_id_(request_id), 211 : request_id_(request_id),
212 is_sync_(is_sync),
143 responder_(responder) { 213 responder_(responder) {
144 } 214 }
145 215
146 void Run({{interface_macros.declare_params("in_", method.response_parameters)} }) const override; 216 void Run({{interface_macros.declare_params("in_", method.response_parameters)} }) const override;
147 217
148 private: 218 private:
149 uint64_t request_id_; 219 uint64_t request_id_;
220 bool is_sync_;
150 mutable mojo::MessageReceiverWithStatus* responder_; 221 mutable mojo::MessageReceiverWithStatus* responder_;
151 MOJO_DISALLOW_COPY_AND_ASSIGN({{class_name}}_{{method.name}}_ProxyToResponder) ; 222 DISALLOW_COPY_AND_ASSIGN({{class_name}}_{{method.name}}_ProxyToResponder);
152 }; 223 };
153 void {{class_name}}_{{method.name}}_ProxyToResponder::Run( 224 void {{class_name}}_{{method.name}}_ProxyToResponder::Run(
154 {{interface_macros.declare_params("in_", method.response_parameters)}}) cons t { 225 {{interface_macros.declare_params("in_", method.response_parameters)}}) cons t {
155 {{struct_macros.get_serialized_size(response_params_struct, "in_%s")}} 226 {{struct_macros.get_serialized_size(response_params_struct, "in_%s")}}
156 mojo::internal::ResponseMessageBuilder builder( 227 mojo::internal::ResponseMessageBuilder builder(
157 {{message_name}}, size, request_id_); 228 {{message_name}}, size, request_id_,
158 {{build_message(response_params_struct, params_description)}} 229 is_sync_ ? mojo::internal::kMessageIsSync : 0);
230 {{build_message(response_params_struct, "in_%s", params_description)}}
159 bool ok = responder_->Accept(builder.message()); 231 bool ok = responder_->Accept(builder.message());
160 MOJO_ALLOW_UNUSED_LOCAL(ok); 232 MOJO_ALLOW_UNUSED_LOCAL(ok);
161 // TODO(darin): !ok returned here indicates a malformed message, and that may 233 // TODO(darin): !ok returned here indicates a malformed message, and that may
162 // be good reason to close the connection. However, we don't have a way to do 234 // be good reason to close the connection. However, we don't have a way to do
163 // that from here. We should add a way. 235 // that from here. We should add a way.
164 delete responder_; 236 delete responder_;
165 responder_ = nullptr; 237 responder_ = nullptr;
166 } 238 }
167 {%- endif -%} 239 {%- endif -%}
168 {%- endfor %} 240 {%- endfor %}
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
215 case internal::k{{class_name}}_{{method.name}}_Name: { 287 case internal::k{{class_name}}_{{method.name}}_Name: {
216 {%- if method.response_parameters != None %} 288 {%- if method.response_parameters != None %}
217 internal::{{class_name}}_{{method.name}}_Params_Data* params = 289 internal::{{class_name}}_{{method.name}}_Params_Data* params =
218 reinterpret_cast<internal::{{class_name}}_{{method.name}}_Params_Data* >( 290 reinterpret_cast<internal::{{class_name}}_{{method.name}}_Params_Data* >(
219 message->mutable_payload()); 291 message->mutable_payload());
220 292
221 params->DecodePointersAndHandles(message->mutable_handles()); 293 params->DecodePointersAndHandles(message->mutable_handles());
222 {{alloc_params(method.param_struct, "&serialization_context_")|indent(4)}} 294 {{alloc_params(method.param_struct, "&serialization_context_")|indent(4)}}
223 {{class_name}}::{{method.name}}Callback::Runnable* runnable = 295 {{class_name}}::{{method.name}}Callback::Runnable* runnable =
224 new {{class_name}}_{{method.name}}_ProxyToResponder( 296 new {{class_name}}_{{method.name}}_ProxyToResponder(
225 message->request_id(), responder); 297 message->request_id(),
298 message->has_flag(mojo::internal::kMessageIsSync),
299 responder);
226 {{class_name}}::{{method.name}}Callback callback(runnable); 300 {{class_name}}::{{method.name}}Callback callback(runnable);
227 // A null |sink_| means no implementation was bound. 301 // A null |sink_| means no implementation was bound.
228 assert(sink_); 302 assert(sink_);
229 TRACE_EVENT0("mojom", "{{class_name}}::{{method.name}}"); 303 TRACE_EVENT0("mojom", "{{class_name}}::{{method.name}}");
230 sink_->{{method.name}}( 304 sink_->{{method.name}}(
231 {%- if method.parameters -%}{{pass_params(method.parameters)}}, {% endif -%}call back); 305 {%- if method.parameters -%}{{pass_params(method.parameters)}}, {% endif -%}call back);
232 return true; 306 return true;
233 {%- else %} 307 {%- else %}
234 break; 308 break;
235 {%- endif %} 309 {%- endif %}
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
312 default: 386 default:
313 break; 387 break;
314 } 388 }
315 389
316 // Unrecognized message. 390 // Unrecognized message.
317 ReportValidationError( 391 ReportValidationError(
318 mojo::internal::VALIDATION_ERROR_MESSAGE_HEADER_UNKNOWN_METHOD); 392 mojo::internal::VALIDATION_ERROR_MESSAGE_HEADER_UNKNOWN_METHOD);
319 return false; 393 return false;
320 } 394 }
321 {%- endif -%} 395 {%- endif -%}
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698