chromium/mojo/public/tools/bindings/generators/cpp_templates/module.cc.tmpl

// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

{%- from 'cpp_macros.tmpl' import
    namespace_begin, namespace_end, variant_path %}

#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-private-field"
#endif

#include "{{variant_path(module.path, variant)}}.h"

#include <math.h>
#include <stdint.h>
#include <utility>

#include "base/debug/alias.h"
#include "base/hash/md5_constexpr.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/task/thread_pool/thread_pool_instance.h"
#include "base/trace_event/trace_event.h"
#include "base/trace_event/typed_macros.h"
#include "mojo/public/cpp/bindings/features.h"
#include "mojo/public/cpp/bindings/lib/default_construct_tag_internal.h"
#include "mojo/public/cpp/bindings/lib/generated_code_util.h"
#include "mojo/public/cpp/bindings/lib/message_internal.h"
#include "mojo/public/cpp/bindings/lib/proxy_to_responder.h"
#include "mojo/public/cpp/bindings/lib/send_message_helper.h"
#include "mojo/public/cpp/bindings/lib/serialization_util.h"
#include "mojo/public/cpp/bindings/lib/unserialized_message_context.h"
#include "mojo/public/cpp/bindings/lib/validate_params.h"
#include "mojo/public/cpp/bindings/lib/validation_errors.h"
#include "mojo/public/cpp/bindings/mojo_buildflags.h"
#include "mojo/public/cpp/bindings/urgent_message_scope.h"
#include "mojo/public/interfaces/bindings/interface_control_messages.mojom.h"
#include "third_party/perfetto/include/perfetto/tracing/traced_value.h"

#include "{{module.path}}-params-data.h"
#include "{{module.path}}-shared-message-ids.h"

#include "{{variant_path(module.path, variant)}}-import-headers.h"
#include "{{variant_path(module.path, variant)}}-test-utils.h"

{%- if for_blink %}
#include "mojo/public/cpp/bindings/lib/wtf_serialization.h"
{%- endif %}

{%- for header in extra_traits_headers %}
#include "{{header}}"
{%- endfor %}

{{namespace_begin(namespaces_as_array, variant)}}

{#--- Constants #}
{%- for constant in module.constants %}
{%-   if constant.kind|is_string_kind %}
const char {{constant.name}}[] = {{constant|constant_value}};
{%-   endif %}
{%- endfor %}

{#--- Struct Constants #}
{%- for struct in structs %}
{%-   for constant in struct.constants %}
{%-     if constant.kind|is_string_kind %}
const char {{struct.name}}::{{constant.name}}[] = {{constant|constant_value}};
{%-     else %}
constexpr {{constant.kind|cpp_pod_type}} {{struct.name}}::{{constant.name}};
{%-     endif %}
{%-   endfor %}
{%- endfor %}

{#--- Struct builder definitions #}
{%- for struct in structs %}
{%-   if not struct|is_native_only_kind %}
{%-     include "wrapper_class_definition.tmpl" %}
{%-   endif %}
{%- endfor %}

{#--- Union builder definitions #}
{%- for union in unions %}
{%-   include "wrapper_union_class_definition.tmpl" %}
{%- endfor %}

{#--- Interface definitions #}
{%- for interface in interfaces %}
{%-   include "interface_definition.tmpl" %}
{%- endfor %}

{{namespace_end(namespaces_as_array, variant)}}


namespace mojo {

{#--- Struct Serialization Helpers -#}
{%  for struct in structs %}
{%-   if not struct|is_native_only_kind %}
{%      include "struct_traits_definition.tmpl" %}
{%-   endif %}
{%- endfor %}

{#--- Union Serialization Helpers #}
{%- for union in unions %}
{%-   include "union_traits_definition.tmpl" %}
{%- endfor %}

}  // namespace mojo


// Symbols declared in the -test-utils.h header are defined here instead of a
// separate .cc file to save compile time.

{{namespace_begin(namespaces_as_array, variant)}}

{%- import "interface_macros.tmpl" as interface_macros %}

{%  for interface in interfaces %}

{#--- Testing interceptor #}
{%- for method in interface.methods %}
void {{interface.name}}InterceptorForTesting::{{method.name}}({{interface_macros.declare_request_params("", method)}}) {
  GetForwardingInterface()->{{method.name}}(
    {%- for param in method.parameters -%}
    {%- if param.kind|is_non_const_ref_kind %}
    {{param.name}}
    {%- else %}
    std::move({{param.name}})
    {% endif %}
    {%- if not loop.last %}, {% endif %}
    {%- endfor %}
    {%-   if method.response_parameters != None -%}
    {%-     if method.parameters %}, {% endif -%}
    std::move(callback)
    {%-   endif -%}
  );
}
{%- endfor %}

{#--- Async wait helper for testing #}
{{interface.name}}AsyncWaiter::{{interface.name}}AsyncWaiter(
    {{interface.name}}* proxy) : proxy_(proxy) {}

{{interface.name}}AsyncWaiter::~{{interface.name}}AsyncWaiter() = default;

{%  for method in interface.methods if method.response_parameters != None -%}
void {{interface.name}}AsyncWaiter::{{method.name}}(
    {{interface_macros.declare_sync_method_params("", method)}}) {
  base::RunLoop loop;
  proxy_->{{method.name}}(
{%-   for param in method.parameters -%}
{%- if param.kind|is_non_const_ref_kind %}
      {{param.name}},
{%- else %}
      std::move({{param.name}}),
{%- endif %}
{%-   endfor %}
      base::BindOnce(
          [](base::RunLoop* loop
{%-   for param in method.response_parameters -%},
             {{param.kind|cpp_wrapper_call_type}}* out_{{param.name}}
{%    endfor -%}
{%-   for param in method.response_parameters -%},
             {{param.kind|cpp_wrapper_param_type}} {{param.name}}
{%-   endfor %}) {
{%-   for param in method.response_parameters -%}
            *out_{{param.name}} = std::move({{param.name}});
{%-   endfor %}
            loop->Quit();
          },
          &loop
{%-   for param in method.response_parameters -%},
          out_{{param.name}}
{%-   endfor %}));
  loop.Run();
}

{%   if method.response_parameters|length == 1 -%}
{%-     set response_kind = method.response_parameters[0].kind -%}
{%-     set response_type = response_kind|cpp_wrapper_call_type -%}
{{response_type}} {{interface.name}}AsyncWaiter::{{method.name}}(
    {{interface_macros.declare_params("", method.parameters)}}) {
  {{response_type}} async_wait_result{{ '' if response_kind|is_default_constructible else '{mojo::internal::DefaultConstructTag()}' }};
  {{method.name}}(
{%-   for param in method.parameters -%}
        std::move({{param.name}}),
{%-   endfor -%}
        &async_wait_result);
  return async_wait_result;
}
{%-   endif %}

{% endfor %}

{% endfor %}

{{namespace_end(namespaces_as_array, variant)}}


#if defined(__clang__)
#pragma clang diagnostic pop
#endif