remoting_log_to_server.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright 2019 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_SIGNALING_REMOTING_LOG_TO_SERVER_H_
  5. #define REMOTING_SIGNALING_REMOTING_LOG_TO_SERVER_H_
  6. #include <memory>
  7. #include "base/callback.h"
  8. #include "base/memory/scoped_refptr.h"
  9. #include "base/sequence_checker.h"
  10. #include "base/timer/timer.h"
  11. #include "net/base/backoff_entry.h"
  12. #include "remoting/signaling/log_to_server.h"
  13. namespace network {
  14. class SharedURLLoaderFactory;
  15. } // namespace network
  16. namespace remoting {
  17. namespace apis {
  18. namespace v1 {
  19. class CreateLogEntryRequest;
  20. class CreateLogEntryResponse;
  21. } // namespace v1
  22. } // namespace apis
  23. class OAuthTokenGetter;
  24. class ProtobufHttpStatus;
  25. // RemotingLogToServer sends log entries to to the remoting telemetry server.
  26. class RemotingLogToServer : public LogToServer {
  27. public:
  28. RemotingLogToServer(
  29. ServerLogEntry::Mode mode,
  30. std::unique_ptr<OAuthTokenGetter> token_getter,
  31. scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory);
  32. RemotingLogToServer(const RemotingLogToServer&) = delete;
  33. RemotingLogToServer& operator=(const RemotingLogToServer&) = delete;
  34. ~RemotingLogToServer() override;
  35. // LogToServer interface.
  36. void Log(const ServerLogEntry& entry) override;
  37. ServerLogEntry::Mode mode() const override;
  38. private:
  39. static constexpr int kMaxSendLogAttempts = 5;
  40. using CreateLogEntryResponseCallback = base::OnceCallback<void(
  41. const ProtobufHttpStatus&,
  42. std::unique_ptr<apis::v1::CreateLogEntryResponse>)>;
  43. using CreateLogEntryCallback =
  44. base::RepeatingCallback<void(const apis::v1::CreateLogEntryRequest&,
  45. CreateLogEntryResponseCallback callback)>;
  46. friend class RemotingLogToServerTest;
  47. void SendLogRequest(const apis::v1::CreateLogEntryRequest& request,
  48. int attempts_left);
  49. void SendLogRequestWithBackoff(const apis::v1::CreateLogEntryRequest& request,
  50. int attempts_left);
  51. void OnSendLogRequestResult(
  52. const apis::v1::CreateLogEntryRequest& request,
  53. int attempts_left,
  54. const ProtobufHttpStatus& status,
  55. std::unique_ptr<apis::v1::CreateLogEntryResponse> response);
  56. ServerLogEntry::Mode mode_;
  57. std::unique_ptr<OAuthTokenGetter> token_getter_;
  58. net::BackoffEntry backoff_;
  59. base::OneShotTimer backoff_timer_;
  60. // Callback used to send the log entry to the server. Replaceable for
  61. // unittest.
  62. CreateLogEntryCallback create_log_entry_;
  63. SEQUENCE_CHECKER(sequence_checker_);
  64. };
  65. } // namespace remoting
  66. #endif // REMOTING_SIGNALING_REMOTING_LOG_TO_SERVER_H_