action_message_handler.cc 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "remoting/host/action_message_handler.h"
  5. #include <utility>
  6. #include "base/callback_helpers.h"
  7. #include "remoting/base/compound_buffer.h"
  8. #include "remoting/host/action_executor.h"
  9. #include "remoting/proto/action.pb.h"
  10. #include "remoting/protocol/message_serialization.h"
  11. namespace remoting {
  12. using protocol::ActionResponse;
  13. ActionMessageHandler::ActionMessageHandler(
  14. const std::string& name,
  15. const std::vector<protocol::ActionRequest::Action>& actions,
  16. std::unique_ptr<protocol::MessagePipe> pipe,
  17. std::unique_ptr<ActionExecutor> action_executor)
  18. : protocol::NamedMessagePipeHandler(name, std::move(pipe)),
  19. action_executor_(std::move(action_executor)),
  20. supported_actions_(actions) {
  21. DCHECK(action_executor_);
  22. }
  23. ActionMessageHandler::~ActionMessageHandler() = default;
  24. void ActionMessageHandler::OnIncomingMessage(
  25. std::unique_ptr<CompoundBuffer> message) {
  26. DCHECK(message);
  27. std::unique_ptr<protocol::ActionRequest> request =
  28. protocol::ParseMessage<protocol::ActionRequest>(message.get());
  29. ActionResponse response;
  30. response.set_request_id(request ? request->request_id() : 0);
  31. if (!request) {
  32. response.set_code(ActionResponse::PROTOCOL_ERROR);
  33. response.set_protocol_error_type(ActionResponse::INVALID_MESSAGE_ERROR);
  34. } else if (!request->has_action()) {
  35. // |has_action()| will return false if either the field is not set or the
  36. // value is out of range. Unfortunately we can't distinguish between these
  37. // two conditions so we return the same error for both.
  38. response.set_code(ActionResponse::PROTOCOL_ERROR);
  39. response.set_protocol_error_type(ActionResponse::INVALID_ACTION_ERROR);
  40. } else if (supported_actions_.count(request->action()) == 0) {
  41. // We received an action which is valid, but not supported by this platform
  42. // or connection mode.
  43. response.set_code(ActionResponse::PROTOCOL_ERROR);
  44. response.set_protocol_error_type(ActionResponse::UNSUPPORTED_ACTION_ERROR);
  45. } else {
  46. // Valid action request received. None of the supported actions at this
  47. // time support return codes, if we add actions in the future which could
  48. // fail in an observable way, we should consider returning that info to the
  49. // client.
  50. action_executor_->ExecuteAction(*request);
  51. response.set_code(ActionResponse::ACTION_SUCCESS);
  52. }
  53. Send(response, base::DoNothing());
  54. }
  55. } // namespace remoting