• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1{%- import "struct_macros.tmpl" as struct_macros %}
2
3{%- macro declare_params(prefix, parameters) %}
4{%-   for param in parameters -%}
5{{param.kind|cpp_wrapper_param_type}} {{prefix}}{{param.name}}
6{%- if not loop.last %}, {% endif %}
7{%-   endfor %}
8{%- endmacro %}
9
10{%- macro declare_callback(method, for_blink, use_once_callback) -%}
11{%-   if use_once_callback -%}
12base::OnceCallback<void(
13{%-   else -%}
14base::Callback<void(
15{%-   endif -%}
16{%-   for param in method.response_parameters -%}
17{{param.kind|cpp_wrapper_param_type}}
18{%-     if not loop.last %}, {% endif %}
19{%-   endfor -%}
20)>
21{%- endmacro -%}
22
23{%- macro declare_request_params(prefix, method, use_once_callback) -%}
24{{declare_params(prefix, method.parameters)}}
25{%-   if method.response_parameters != None -%}
26{%-     if method.parameters %}, {% endif -%}
27{%-     if use_once_callback -%}
28{{method.name}}Callback callback
29{%-     else -%}
30const {{method.name}}Callback& callback
31{%-     endif -%}
32{%-   endif -%}
33{%- endmacro -%}
34
35{%- macro declare_sync_method_params(prefix, method) -%}
36{{declare_params(prefix, method.parameters)}}
37{%-   if method.response_parameters %}
38{%-     if method.parameters %}, {% endif %}
39{%-     for param in method.response_parameters -%}
40{{param.kind|cpp_wrapper_call_type}}* out_{{prefix}}{{param.name}}
41{%-       if not loop.last %}, {% endif %}
42{%-     endfor %}
43{%-   endif -%}
44{%- endmacro -%}
45
46{%- macro build_message_flags(is_response, is_sync_text, expects_response_text,
47                              flags_name) %}
48{%-   if is_response %}
49  const uint32_t kFlags = mojo::Message::kFlagIsResponse |
50      (({{is_sync_text}}) ? mojo::Message::kFlagIsSync : 0);
51{%-   else %}
52  const uint32_t kFlags =
53      (({{expects_response_text}}) ? mojo::Message::kFlagExpectsResponse : 0) |
54      (({{is_sync_text}}) ? mojo::Message::kFlagIsSync : 0);
55{%-   endif %}
56{%- endmacro %}
57
58{%- macro build_serialized_message(message_name, param_name_prefix,
59                                   params_struct, params_description,
60                                   flags_text, message_object_name) %}
61  mojo::Message {{message_object_name}}(
62      {{message_name}}, {{flags_text}}, 0, 0, nullptr);
63  auto* buffer = {{message_object_name}}.payload_buffer();
64  {{params_struct|get_qualified_name_for_kind(internal=True)}}::BufferWriter
65      params;
66  mojo::internal::SerializationContext serialization_context;
67  {{struct_macros.serialize(params_struct, params_description,
68                            param_name_prefix, "params", "buffer",
69                            "&serialization_context")}}
70  {{message_object_name}}.AttachHandlesFromSerializationContext(
71      &serialization_context);
72{%- endmacro %}
73
74{%- macro define_message_type(interface, message_typename, message_name,
75                              is_response, method, parameters, params_struct,
76                              params_description, use_once_callback) -%}
77class {{message_typename}}
78    : public mojo::internal::UnserializedMessageContext {
79 public:
80  static const mojo::internal::UnserializedMessageContext::Tag kMessageTag;
81
82  explicit {{message_typename}}(
83      uint32_t message_flags
84{%-     for param in parameters %}
85      , {{param.kind|cpp_wrapper_param_type}} param_{{param.name}}
86{%-     endfor %}
87  )
88      : mojo::internal::UnserializedMessageContext(
89          &kMessageTag,
90          {{message_name}},
91          message_flags)
92{%-     for param in parameters -%}
93{%-       if param.kind|is_interface_kind %}
94      , param_{{param.name}}_(param_{{param.name}}.PassInterface())
95{%-       else %}
96      , param_{{param.name}}_(std::move(param_{{param.name}}))
97{%-       endif %}
98{%-     endfor -%} {}
99  ~{{message_typename}}() override = default;
100
101  static mojo::Message Build(
102      bool serialize,
103{%-   if not is_response %}
104      bool expects_response,
105{%-   endif %}
106      bool is_sync
107{%-   if parameters -%}
108      ,
109      {{declare_params("param_", parameters)}}
110{%-   endif %}) {
111
112    {{build_message_flags(is_response, "is_sync", "expects_response",
113                          "kFlags")}}
114
115    if (!serialize) {
116      return mojo::Message(std::make_unique<{{message_typename}}>(
117          kFlags
118{%-     for param in parameters %}
119          , std::move(param_{{param.name}})
120{%-     endfor %}
121          ));
122    }
123
124    DCHECK(serialize);
125    {{build_serialized_message(message_name, "param_%s", params_struct,
126                               params_description, "kFlags", "message")}}
127    return message;
128  }
129
130{%      if not is_response %}
131  void Dispatch({{interface.name}}* impl
132{%-       if method.response_parameters != None -%}
133{%-         if use_once_callback -%}
134      , {{interface.name}}::{{method.name}}Callback callback
135{%-         else -%}
136      , const {{interface.name}}::{{method.name}}Callback& callback
137{%-         endif -%}
138{%-       endif -%}) {
139    impl->{{method.name}}(
140{%-       for param in parameters -%}
141{%-         if param.kind|is_interface_kind %}
142        {{param.kind|get_name_for_kind}}Ptr(std::move(param_{{param.name}}_))
143{%-         else %}
144        std::move(param_{{param.name}}_)
145{%-         endif %}
146        {%- if not loop.last -%}, {%- endif %}
147{%-       endfor %}
148{%-       if method.response_parameters != None %}
149        {%- if parameters -%}, {% endif -%}std::move(callback)
150{%-       endif -%});
151  }
152{%-     else %}
153  void Dispatch({{interface.name}}::{{method.name}}Callback* callback) {
154    std::move(*callback).Run(
155{%-       for param in parameters -%}
156{%-         if param.kind|is_interface_kind %}
157        {{param.kind|get_name_for_kind}}Ptr(std::move(param_{{param.name}}_))
158{%-         else %}
159        std::move(param_{{param.name}}_)
160{%-         endif %}
161        {%- if not loop.last -%}, {% endif -%}
162{%-       endfor -%});
163  }
164
165{%        if method.sync %}
166  void HandleSyncResponse(
167{%          for param in parameters %}
168      {{param.kind|cpp_wrapper_call_type}}* out_{{param.name}}
169      {%- if not loop.last -%}, {% endif -%}
170{%-         endfor -%}) {
171{%          for param in parameters -%}
172{%-           if param.kind|is_interface_kind %}
173    out_{{param.name}}->Bind(std::move(param_{{param.name}}_));
174{%-           else %}
175    *out_{{param.name}} = std::move(param_{{param.name}}_);
176{%-           endif %}
177{%          endfor %}
178  }
179{%-       endif -%}
180{%-     endif %}
181
182 private:
183  // mojo::internal::UnserializedMessageContext:
184  void Serialize(mojo::internal::SerializationContext* serialization_context,
185                 mojo::internal::Buffer* buffer) override {
186    {{params_struct|get_qualified_name_for_kind(internal=True)}}::BufferWriter
187        params;
188    {{struct_macros.serialize(params_struct, params_description, "param_%s_",
189                              "params", "buffer", "serialization_context")}}
190  }
191
192{%-     for param in parameters %}
193  {{param.kind|cpp_wrapper_type}} param_{{param.name}}_;
194{%-     endfor %}
195
196  DISALLOW_COPY_AND_ASSIGN({{message_typename}});
197};
198
199const mojo::internal::UnserializedMessageContext::Tag
200{{message_typename}}::kMessageTag = {};
201{%- endmacro -%}
202