chromoting_session.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright 2013 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. #ifndef REMOTING_CLIENT_CHROMOTING_SESSION_H_
  5. #define REMOTING_CLIENT_CHROMOTING_SESSION_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/memory/ref_counted.h"
  10. #include "base/memory/weak_ptr.h"
  11. #include "remoting/client/chromoting_client.h"
  12. #include "remoting/client/client_context.h"
  13. #include "remoting/client/client_telemetry_logger.h"
  14. #include "remoting/client/client_user_interface.h"
  15. #include "remoting/client/connect_to_host_info.h"
  16. #include "remoting/client/feedback_data.h"
  17. #include "remoting/client/input/client_input_injector.h"
  18. #include "remoting/proto/control.pb.h"
  19. #include "remoting/proto/event.pb.h"
  20. #include "remoting/protocol/clipboard_stub.h"
  21. #include "remoting/protocol/cursor_shape_stub.h"
  22. #include "remoting/signaling/ftl_device_id_provider.h"
  23. #include "remoting/signaling/signal_strategy.h"
  24. namespace remoting {
  25. namespace protocol {
  26. class AudioStub;
  27. class VideoRenderer;
  28. } // namespace protocol
  29. class ChromotingClientRuntime;
  30. // ChromotingSession is scoped to the session.
  31. // Construction, destruction, and all method calls must occur on the UI Thread.
  32. // All callbacks will be posted to the UI Thread.
  33. // A ChromotingSession instance can be used for at most one connection attempt.
  34. // If you need to reconnect an ended session, you will need to create a new
  35. // session instance.
  36. class ChromotingSession : public ClientInputInjector {
  37. public:
  38. // All methods of the delegate are called on the UI thread. Callbacks should
  39. // also be invoked on the UI thread too.
  40. class Delegate {
  41. public:
  42. virtual ~Delegate() {}
  43. // Notifies the delegate of the current connection status. The delegate
  44. // should destroy the ChromotingSession instance when the connection state
  45. // is an end state.
  46. virtual void OnConnectionState(protocol::ConnectionToHost::State state,
  47. protocol::ErrorCode error) = 0;
  48. // Saves new pairing credentials to permanent storage.
  49. virtual void CommitPairingCredentials(const std::string& host,
  50. const std::string& id,
  51. const std::string& secret) = 0;
  52. // Notifies the user interface that the user needs to enter a PIN. The
  53. // current authentication attempt is put on hold until |callback| is
  54. // invoked.
  55. virtual void FetchSecret(
  56. bool pairing_supported,
  57. const protocol::SecretFetchedCallback& secret_fetched_callback) = 0;
  58. // Pops up a third party login page to fetch token required for
  59. // authentication.
  60. virtual void FetchThirdPartyToken(
  61. const std::string& token_url,
  62. const std::string& client_id,
  63. const std::string& scopes,
  64. const protocol::ThirdPartyTokenFetchedCallback&
  65. token_fetched_callback) = 0;
  66. // Pass on the set of negotiated capabilities to the client.
  67. virtual void SetCapabilities(const std::string& capabilities) = 0;
  68. // Passes on the deconstructed ExtensionMessage to the client to handle
  69. // appropriately.
  70. virtual void HandleExtensionMessage(const std::string& type,
  71. const std::string& message) = 0;
  72. };
  73. using GetFeedbackDataCallback =
  74. base::OnceCallback<void(std::unique_ptr<FeedbackData>)>;
  75. // Initiates a connection with the specified host. This will start the
  76. // connection immediately.
  77. ChromotingSession(base::WeakPtr<ChromotingSession::Delegate> delegate,
  78. std::unique_ptr<protocol::CursorShapeStub> cursor_stub,
  79. std::unique_ptr<protocol::VideoRenderer> video_renderer,
  80. std::unique_ptr<protocol::AudioStub> audio_player,
  81. const ConnectToHostInfo& info);
  82. ChromotingSession(const ChromotingSession&) = delete;
  83. ChromotingSession& operator=(const ChromotingSession&) = delete;
  84. ~ChromotingSession() override;
  85. // Gets the current feedback data and returns it to the callback on the
  86. // UI thread.
  87. void GetFeedbackData(GetFeedbackDataCallback callback) const;
  88. // Requests pairing between the host and client for PIN-less authentication.
  89. void RequestPairing(const std::string& device_name);
  90. // Moves the host's cursor to the specified coordinates, optionally with some
  91. // mouse button depressed. If |button| is BUTTON_UNDEFINED, no click is made.
  92. void SendMouseEvent(int x,
  93. int y,
  94. protocol::MouseEvent_MouseButton button,
  95. bool button_down);
  96. void SendMouseWheelEvent(int delta_x, int delta_y);
  97. // ClientInputInjector implementation.
  98. bool SendKeyEvent(int scan_code, int key_code, bool key_down) override;
  99. void SendTextEvent(const std::string& text) override;
  100. // Sends the provided touch event payload to the host.
  101. void SendTouchEvent(const protocol::TouchEvent& touch_event);
  102. void SendClientResolution(int dips_width, int dips_height, float scale);
  103. // Enables or disables the video channel.
  104. void EnableVideoChannel(bool enable);
  105. void SendClientMessage(const std::string& type, const std::string& data);
  106. private:
  107. class Core;
  108. template <typename Functor, typename... Args>
  109. void RunCoreTaskOnNetworkThread(const base::Location& from_here,
  110. Functor&& core_functor,
  111. Args&&... args);
  112. // Used to obtain task runner references.
  113. const raw_ptr<ChromotingClientRuntime> runtime_;
  114. // Created when the session is connected, then used, and destroyed on the
  115. // network thread when the instance is destroyed.
  116. std::unique_ptr<Core> core_;
  117. };
  118. } // namespace remoting
  119. #endif // REMOTING_CLIENT_CHROMOTING_SESSION_H_