| // Copyright 2013 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #if defined(__clang__) |
| #pragma clang diagnostic push |
| #pragma clang diagnostic ignored "-Wunused-private-field" |
| #elif defined(_MSC_VER) |
| #pragma warning(push) |
| #pragma warning(disable:4056) |
| #pragma warning(disable:4065) |
| #pragma warning(disable:4756) |
| #endif |
| |
| #include "services/device/public/interfaces/vibration_manager.mojom.h" |
| |
| #include <math.h> |
| #include <stdint.h> |
| #include <utility> |
| |
| #include "base/logging.h" |
| #include "base/run_loop.h" |
| #include "mojo/public/cpp/bindings/lib/message_internal.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_context.h" |
| #include "mojo/public/cpp/bindings/lib/validation_errors.h" |
| #include "mojo/public/interfaces/bindings/interface_control_messages.mojom.h" |
| |
| #include "services/device/public/interfaces/vibration_manager.mojom-shared-message-ids.h" |
| namespace device { |
| namespace mojom { |
| const char VibrationManager::Name_[] = "device::mojom::VibrationManager"; |
| |
| class VibrationManager_Vibrate_ForwardToCallback |
| : public mojo::MessageReceiver { |
| public: |
| VibrationManager_Vibrate_ForwardToCallback( |
| VibrationManager::VibrateCallback callback |
| ) : callback_(std::move(callback)) { |
| } |
| bool Accept(mojo::Message* message) override; |
| private: |
| VibrationManager::VibrateCallback callback_; |
| DISALLOW_COPY_AND_ASSIGN(VibrationManager_Vibrate_ForwardToCallback); |
| }; |
| |
| class VibrationManager_Cancel_ForwardToCallback |
| : public mojo::MessageReceiver { |
| public: |
| VibrationManager_Cancel_ForwardToCallback( |
| VibrationManager::CancelCallback callback |
| ) : callback_(std::move(callback)) { |
| } |
| bool Accept(mojo::Message* message) override; |
| private: |
| VibrationManager::CancelCallback callback_; |
| DISALLOW_COPY_AND_ASSIGN(VibrationManager_Cancel_ForwardToCallback); |
| }; |
| |
| VibrationManagerProxy::VibrationManagerProxy(mojo::MessageReceiverWithResponder* receiver) |
| : receiver_(receiver) { |
| } |
| |
| void VibrationManagerProxy::Vibrate( |
| int64_t in_milliseconds, VibrateCallback callback) { |
| #if BUILDFLAG(MOJO_TRACE_ENABLED) |
| TRACE_EVENT0("mojom", "device::mojom::VibrationManager::Vibrate"); |
| #endif |
| const bool kExpectsResponse = true; |
| const bool kIsSync = false; |
| |
| const uint32_t kFlags = |
| ((kExpectsResponse) ? mojo::Message::kFlagExpectsResponse : 0) | |
| ((kIsSync) ? mojo::Message::kFlagIsSync : 0); |
| |
| mojo::Message message( |
| internal::kVibrationManager_Vibrate_Name, kFlags, 0, 0, nullptr); |
| auto* buffer = message.payload_buffer(); |
| ::device::mojom::internal::VibrationManager_Vibrate_Params_Data::BufferWriter |
| params; |
| mojo::internal::SerializationContext serialization_context; |
| params.Allocate(buffer); |
| params->milliseconds = in_milliseconds; |
| message.AttachHandlesFromSerializationContext( |
| &serialization_context); |
| std::unique_ptr<mojo::MessageReceiver> responder( |
| new VibrationManager_Vibrate_ForwardToCallback( |
| std::move(callback))); |
| ignore_result(receiver_->AcceptWithResponder(&message, std::move(responder))); |
| } |
| |
| void VibrationManagerProxy::Cancel( |
| CancelCallback callback) { |
| #if BUILDFLAG(MOJO_TRACE_ENABLED) |
| TRACE_EVENT0("mojom", "device::mojom::VibrationManager::Cancel"); |
| #endif |
| const bool kExpectsResponse = true; |
| const bool kIsSync = false; |
| |
| const uint32_t kFlags = |
| ((kExpectsResponse) ? mojo::Message::kFlagExpectsResponse : 0) | |
| ((kIsSync) ? mojo::Message::kFlagIsSync : 0); |
| |
| mojo::Message message( |
| internal::kVibrationManager_Cancel_Name, kFlags, 0, 0, nullptr); |
| auto* buffer = message.payload_buffer(); |
| ::device::mojom::internal::VibrationManager_Cancel_Params_Data::BufferWriter |
| params; |
| mojo::internal::SerializationContext serialization_context; |
| params.Allocate(buffer); |
| message.AttachHandlesFromSerializationContext( |
| &serialization_context); |
| std::unique_ptr<mojo::MessageReceiver> responder( |
| new VibrationManager_Cancel_ForwardToCallback( |
| std::move(callback))); |
| ignore_result(receiver_->AcceptWithResponder(&message, std::move(responder))); |
| } |
| class VibrationManager_Vibrate_ProxyToResponder { |
| public: |
| static VibrationManager::VibrateCallback CreateCallback( |
| uint64_t request_id, |
| bool is_sync, |
| std::unique_ptr<mojo::MessageReceiverWithStatus> responder) { |
| std::unique_ptr<VibrationManager_Vibrate_ProxyToResponder> proxy( |
| new VibrationManager_Vibrate_ProxyToResponder( |
| request_id, is_sync, std::move(responder))); |
| return base::Bind(&VibrationManager_Vibrate_ProxyToResponder::Run, |
| base::Passed(&proxy)); |
| } |
| |
| ~VibrationManager_Vibrate_ProxyToResponder() { |
| #if DCHECK_IS_ON() |
| if (responder_) { |
| // If we're being destroyed without being run, we want to ensure the |
| // binding endpoint has been closed. This checks for that asynchronously. |
| // We pass a bound generated callback to handle the response so that any |
| // resulting DCHECK stack will have useful interface type information. |
| responder_->IsConnectedAsync(base::BindOnce(&OnIsConnectedComplete)); |
| } |
| #endif |
| // If the Callback was dropped then deleting the responder will close |
| // the pipe so the calling application knows to stop waiting for a reply. |
| responder_ = nullptr; |
| } |
| |
| private: |
| VibrationManager_Vibrate_ProxyToResponder( |
| uint64_t request_id, |
| bool is_sync, |
| std::unique_ptr<mojo::MessageReceiverWithStatus> responder) |
| : request_id_(request_id), |
| is_sync_(is_sync), |
| responder_(std::move(responder)) { |
| } |
| |
| #if DCHECK_IS_ON() |
| static void OnIsConnectedComplete(bool connected) { |
| DCHECK(!connected) |
| << "VibrationManager::VibrateCallback was destroyed without " |
| << "first either being run or its corresponding binding being closed. " |
| << "It is an error to drop response callbacks which still correspond " |
| << "to an open interface pipe."; |
| } |
| #endif |
| |
| void Run( |
| ); |
| |
| uint64_t request_id_; |
| bool is_sync_; |
| std::unique_ptr<mojo::MessageReceiverWithStatus> responder_; |
| |
| DISALLOW_COPY_AND_ASSIGN(VibrationManager_Vibrate_ProxyToResponder); |
| }; |
| |
| bool VibrationManager_Vibrate_ForwardToCallback::Accept( |
| mojo::Message* message) { |
| #if BUILDFLAG(MOJO_TRACE_ENABLED) |
| TRACE_EVENT1("mojom", "device::mojom::VibrationManager::VibrateCallback", |
| "message", message->name()); |
| #endif |
| mojo::internal::MessageDispatchContext dispatch_context(message); |
| |
| DCHECK(message->is_serialized()); |
| internal::VibrationManager_Vibrate_ResponseParams_Data* params = |
| reinterpret_cast< |
| internal::VibrationManager_Vibrate_ResponseParams_Data*>( |
| message->mutable_payload()); |
| |
| mojo::internal::SerializationContext serialization_context; |
| serialization_context.TakeHandlesFromMessage(message); |
| bool success = true; |
| VibrationManager_Vibrate_ResponseParamsDataView input_data_view(params, &serialization_context); |
| |
| if (!success) { |
| ReportValidationErrorForMessage( |
| message, |
| mojo::internal::VALIDATION_ERROR_DESERIALIZATION_FAILED, |
| "VibrationManager::Vibrate response deserializer"); |
| return false; |
| } |
| if (!callback_.is_null()) |
| std::move(callback_).Run(); |
| return true; |
| } |
| |
| void VibrationManager_Vibrate_ProxyToResponder::Run( |
| ) { |
| |
| const uint32_t kFlags = mojo::Message::kFlagIsResponse | |
| ((is_sync_) ? mojo::Message::kFlagIsSync : 0); |
| |
| mojo::Message message( |
| internal::kVibrationManager_Vibrate_Name, kFlags, 0, 0, nullptr); |
| auto* buffer = message.payload_buffer(); |
| ::device::mojom::internal::VibrationManager_Vibrate_ResponseParams_Data::BufferWriter |
| params; |
| mojo::internal::SerializationContext serialization_context; |
| params.Allocate(buffer); |
| message.AttachHandlesFromSerializationContext( |
| &serialization_context); |
| |
| #if BUILDFLAG(MOJO_TRACE_ENABLED) |
| TRACE_EVENT1("mojom", "(Impl)device::mojom::VibrationManager::VibrateCallback", |
| "message", message.name()); |
| #endif |
| message.set_request_id(request_id_); |
| ignore_result(responder_->Accept(&message)); |
| // TODO(darin): Accept() returning false indicates a malformed message, and |
| // that may be good reason to close the connection. However, we don't have a |
| // way to do that from here. We should add a way. |
| responder_ = nullptr; |
| } |
| class VibrationManager_Cancel_ProxyToResponder { |
| public: |
| static VibrationManager::CancelCallback CreateCallback( |
| uint64_t request_id, |
| bool is_sync, |
| std::unique_ptr<mojo::MessageReceiverWithStatus> responder) { |
| std::unique_ptr<VibrationManager_Cancel_ProxyToResponder> proxy( |
| new VibrationManager_Cancel_ProxyToResponder( |
| request_id, is_sync, std::move(responder))); |
| return base::Bind(&VibrationManager_Cancel_ProxyToResponder::Run, |
| base::Passed(&proxy)); |
| } |
| |
| ~VibrationManager_Cancel_ProxyToResponder() { |
| #if DCHECK_IS_ON() |
| if (responder_) { |
| // If we're being destroyed without being run, we want to ensure the |
| // binding endpoint has been closed. This checks for that asynchronously. |
| // We pass a bound generated callback to handle the response so that any |
| // resulting DCHECK stack will have useful interface type information. |
| responder_->IsConnectedAsync(base::BindOnce(&OnIsConnectedComplete)); |
| } |
| #endif |
| // If the Callback was dropped then deleting the responder will close |
| // the pipe so the calling application knows to stop waiting for a reply. |
| responder_ = nullptr; |
| } |
| |
| private: |
| VibrationManager_Cancel_ProxyToResponder( |
| uint64_t request_id, |
| bool is_sync, |
| std::unique_ptr<mojo::MessageReceiverWithStatus> responder) |
| : request_id_(request_id), |
| is_sync_(is_sync), |
| responder_(std::move(responder)) { |
| } |
| |
| #if DCHECK_IS_ON() |
| static void OnIsConnectedComplete(bool connected) { |
| DCHECK(!connected) |
| << "VibrationManager::CancelCallback was destroyed without " |
| << "first either being run or its corresponding binding being closed. " |
| << "It is an error to drop response callbacks which still correspond " |
| << "to an open interface pipe."; |
| } |
| #endif |
| |
| void Run( |
| ); |
| |
| uint64_t request_id_; |
| bool is_sync_; |
| std::unique_ptr<mojo::MessageReceiverWithStatus> responder_; |
| |
| DISALLOW_COPY_AND_ASSIGN(VibrationManager_Cancel_ProxyToResponder); |
| }; |
| |
| bool VibrationManager_Cancel_ForwardToCallback::Accept( |
| mojo::Message* message) { |
| #if BUILDFLAG(MOJO_TRACE_ENABLED) |
| TRACE_EVENT1("mojom", "device::mojom::VibrationManager::CancelCallback", |
| "message", message->name()); |
| #endif |
| mojo::internal::MessageDispatchContext dispatch_context(message); |
| |
| DCHECK(message->is_serialized()); |
| internal::VibrationManager_Cancel_ResponseParams_Data* params = |
| reinterpret_cast< |
| internal::VibrationManager_Cancel_ResponseParams_Data*>( |
| message->mutable_payload()); |
| |
| mojo::internal::SerializationContext serialization_context; |
| serialization_context.TakeHandlesFromMessage(message); |
| bool success = true; |
| VibrationManager_Cancel_ResponseParamsDataView input_data_view(params, &serialization_context); |
| |
| if (!success) { |
| ReportValidationErrorForMessage( |
| message, |
| mojo::internal::VALIDATION_ERROR_DESERIALIZATION_FAILED, |
| "VibrationManager::Cancel response deserializer"); |
| return false; |
| } |
| if (!callback_.is_null()) |
| std::move(callback_).Run(); |
| return true; |
| } |
| |
| void VibrationManager_Cancel_ProxyToResponder::Run( |
| ) { |
| |
| const uint32_t kFlags = mojo::Message::kFlagIsResponse | |
| ((is_sync_) ? mojo::Message::kFlagIsSync : 0); |
| |
| mojo::Message message( |
| internal::kVibrationManager_Cancel_Name, kFlags, 0, 0, nullptr); |
| auto* buffer = message.payload_buffer(); |
| ::device::mojom::internal::VibrationManager_Cancel_ResponseParams_Data::BufferWriter |
| params; |
| mojo::internal::SerializationContext serialization_context; |
| params.Allocate(buffer); |
| message.AttachHandlesFromSerializationContext( |
| &serialization_context); |
| |
| #if BUILDFLAG(MOJO_TRACE_ENABLED) |
| TRACE_EVENT1("mojom", "(Impl)device::mojom::VibrationManager::CancelCallback", |
| "message", message.name()); |
| #endif |
| message.set_request_id(request_id_); |
| ignore_result(responder_->Accept(&message)); |
| // TODO(darin): Accept() returning false indicates a malformed message, and |
| // that may be good reason to close the connection. However, we don't have a |
| // way to do that from here. We should add a way. |
| responder_ = nullptr; |
| } |
| |
| // static |
| bool VibrationManagerStubDispatch::Accept( |
| VibrationManager* impl, |
| mojo::Message* message) { |
| switch (message->header()->name) { |
| case internal::kVibrationManager_Vibrate_Name: { |
| #if BUILDFLAG(MOJO_TRACE_ENABLED) |
| TRACE_EVENT1("mojom", "(Impl)device::mojom::VibrationManager::Vibrate", |
| "message", message->name()); |
| #endif |
| break; |
| } |
| case internal::kVibrationManager_Cancel_Name: { |
| #if BUILDFLAG(MOJO_TRACE_ENABLED) |
| TRACE_EVENT1("mojom", "(Impl)device::mojom::VibrationManager::Cancel", |
| "message", message->name()); |
| #endif |
| break; |
| } |
| } |
| return false; |
| } |
| |
| // static |
| bool VibrationManagerStubDispatch::AcceptWithResponder( |
| VibrationManager* impl, |
| mojo::Message* message, |
| std::unique_ptr<mojo::MessageReceiverWithStatus> responder) { |
| switch (message->header()->name) { |
| case internal::kVibrationManager_Vibrate_Name: { |
| #if BUILDFLAG(MOJO_TRACE_ENABLED) |
| TRACE_EVENT1("mojom", "(Impl)device::mojom::VibrationManager::Vibrate", |
| "message", message->name()); |
| #endif |
| mojo::internal::MessageDispatchContext context(message); |
| |
| internal::VibrationManager_Vibrate_Params_Data* params = |
| reinterpret_cast< |
| internal::VibrationManager_Vibrate_Params_Data*>( |
| message->mutable_payload()); |
| |
| mojo::internal::SerializationContext serialization_context; |
| serialization_context.TakeHandlesFromMessage(message); |
| bool success = true; |
| int64_t p_milliseconds{}; |
| VibrationManager_Vibrate_ParamsDataView input_data_view(params, &serialization_context); |
| |
| p_milliseconds = input_data_view.milliseconds(); |
| if (!success) { |
| ReportValidationErrorForMessage( |
| message, |
| mojo::internal::VALIDATION_ERROR_DESERIALIZATION_FAILED, |
| "VibrationManager::Vibrate deserializer"); |
| return false; |
| } |
| VibrationManager::VibrateCallback callback = |
| VibrationManager_Vibrate_ProxyToResponder::CreateCallback( |
| message->request_id(), |
| message->has_flag(mojo::Message::kFlagIsSync), |
| std::move(responder)); |
| // A null |impl| means no implementation was bound. |
| assert(impl); |
| impl->Vibrate( |
| std::move(p_milliseconds), std::move(callback)); |
| return true; |
| } |
| case internal::kVibrationManager_Cancel_Name: { |
| #if BUILDFLAG(MOJO_TRACE_ENABLED) |
| TRACE_EVENT1("mojom", "(Impl)device::mojom::VibrationManager::Cancel", |
| "message", message->name()); |
| #endif |
| mojo::internal::MessageDispatchContext context(message); |
| |
| internal::VibrationManager_Cancel_Params_Data* params = |
| reinterpret_cast< |
| internal::VibrationManager_Cancel_Params_Data*>( |
| message->mutable_payload()); |
| |
| mojo::internal::SerializationContext serialization_context; |
| serialization_context.TakeHandlesFromMessage(message); |
| bool success = true; |
| VibrationManager_Cancel_ParamsDataView input_data_view(params, &serialization_context); |
| |
| if (!success) { |
| ReportValidationErrorForMessage( |
| message, |
| mojo::internal::VALIDATION_ERROR_DESERIALIZATION_FAILED, |
| "VibrationManager::Cancel deserializer"); |
| return false; |
| } |
| VibrationManager::CancelCallback callback = |
| VibrationManager_Cancel_ProxyToResponder::CreateCallback( |
| message->request_id(), |
| message->has_flag(mojo::Message::kFlagIsSync), |
| std::move(responder)); |
| // A null |impl| means no implementation was bound. |
| assert(impl); |
| impl->Cancel(std::move(callback)); |
| return true; |
| } |
| } |
| return false; |
| } |
| |
| bool VibrationManagerRequestValidator::Accept(mojo::Message* message) { |
| if (!message->is_serialized() || |
| mojo::internal::ControlMessageHandler::IsControlMessage(message)) { |
| return true; |
| } |
| |
| mojo::internal::ValidationContext validation_context( |
| message->payload(), message->payload_num_bytes(), |
| message->handles()->size(), message->payload_num_interface_ids(), message, |
| "VibrationManager RequestValidator"); |
| |
| switch (message->header()->name) { |
| case internal::kVibrationManager_Vibrate_Name: { |
| if (!mojo::internal::ValidateMessageIsRequestExpectingResponse( |
| message, &validation_context)) { |
| return false; |
| } |
| if (!mojo::internal::ValidateMessagePayload< |
| internal::VibrationManager_Vibrate_Params_Data>( |
| message, &validation_context)) { |
| return false; |
| } |
| return true; |
| } |
| case internal::kVibrationManager_Cancel_Name: { |
| if (!mojo::internal::ValidateMessageIsRequestExpectingResponse( |
| message, &validation_context)) { |
| return false; |
| } |
| if (!mojo::internal::ValidateMessagePayload< |
| internal::VibrationManager_Cancel_Params_Data>( |
| message, &validation_context)) { |
| return false; |
| } |
| return true; |
| } |
| default: |
| break; |
| } |
| |
| // Unrecognized message. |
| ReportValidationError( |
| &validation_context, |
| mojo::internal::VALIDATION_ERROR_MESSAGE_HEADER_UNKNOWN_METHOD); |
| return false; |
| } |
| |
| bool VibrationManagerResponseValidator::Accept(mojo::Message* message) { |
| if (!message->is_serialized() || |
| mojo::internal::ControlMessageHandler::IsControlMessage(message)) { |
| return true; |
| } |
| |
| mojo::internal::ValidationContext validation_context( |
| message->payload(), message->payload_num_bytes(), |
| message->handles()->size(), message->payload_num_interface_ids(), message, |
| "VibrationManager ResponseValidator"); |
| |
| if (!mojo::internal::ValidateMessageIsResponse(message, &validation_context)) |
| return false; |
| switch (message->header()->name) { |
| case internal::kVibrationManager_Vibrate_Name: { |
| if (!mojo::internal::ValidateMessagePayload< |
| internal::VibrationManager_Vibrate_ResponseParams_Data>( |
| message, &validation_context)) { |
| return false; |
| } |
| return true; |
| } |
| case internal::kVibrationManager_Cancel_Name: { |
| if (!mojo::internal::ValidateMessagePayload< |
| internal::VibrationManager_Cancel_ResponseParams_Data>( |
| message, &validation_context)) { |
| return false; |
| } |
| return true; |
| } |
| default: |
| break; |
| } |
| |
| // Unrecognized message. |
| ReportValidationError( |
| &validation_context, |
| mojo::internal::VALIDATION_ERROR_MESSAGE_HEADER_UNKNOWN_METHOD); |
| return false; |
| } |
| void VibrationManagerInterceptorForTesting::Vibrate(int64_t milliseconds, VibrateCallback callback) { |
| GetForwardingInterface()->Vibrate(std::move(milliseconds), std::move(callback)); |
| } |
| void VibrationManagerInterceptorForTesting::Cancel(CancelCallback callback) { |
| GetForwardingInterface()->Cancel(std::move(callback)); |
| } |
| VibrationManagerAsyncWaiter::VibrationManagerAsyncWaiter( |
| VibrationManager* proxy) : proxy_(proxy) {} |
| |
| VibrationManagerAsyncWaiter::~VibrationManagerAsyncWaiter() = default; |
| |
| void VibrationManagerAsyncWaiter::Vibrate( |
| int64_t milliseconds) { |
| base::RunLoop loop; |
| proxy_->Vibrate(std::move(milliseconds), |
| base::Bind( |
| [](base::RunLoop* loop) { |
| loop->Quit(); |
| }, |
| &loop)); |
| loop.Run(); |
| } |
| void VibrationManagerAsyncWaiter::Cancel( |
| ) { |
| base::RunLoop loop; |
| proxy_->Cancel( |
| base::Bind( |
| [](base::RunLoop* loop) { |
| loop->Quit(); |
| }, |
| &loop)); |
| loop.Run(); |
| } |
| |
| } // namespace mojom |
| } // namespace device |
| |
| namespace mojo { |
| |
| } // namespace mojo |
| |
| #if defined(__clang__) |
| #pragma clang diagnostic pop |
| #elif defined(_MSC_VER) |
| #pragma warning(pop) |
| #endif |