websocket_interceptor.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright 2021 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 SERVICES_NETWORK_WEBSOCKET_INTERCEPTOR_H_
  5. #define SERVICES_NETWORK_WEBSOCKET_INTERCEPTOR_H_
  6. #include "base/callback.h"
  7. #include "base/component_export.h"
  8. #include "base/unguessable_token.h"
  9. #include "services/network/throttling/scoped_throttling_token.h"
  10. #include "services/network/throttling/throttling_network_interceptor.h"
  11. #include "third_party/abseil-cpp/absl/types/optional.h"
  12. namespace network {
  13. // WebSocket interceptor is meant to abstract away the details on
  14. // network emulation from the WebSockets code. At the same time it adapts the
  15. // ThrottlingNetworkInterceptor, which was designed with HTTP in mind, to the
  16. // specifics of WebSockets.
  17. class COMPONENT_EXPORT(NETWORK_SERVICE) WebSocketInterceptor {
  18. public:
  19. enum FrameDirection {
  20. kIncoming,
  21. kOutgoing,
  22. };
  23. WebSocketInterceptor(
  24. uint32_t net_log_source_id,
  25. const absl::optional<base::UnguessableToken>& throttling_profile_id);
  26. virtual ~WebSocketInterceptor();
  27. enum InterceptResult {
  28. kContinue,
  29. kShouldWait,
  30. };
  31. // This method is meant to be called before WebSocket starts processing each
  32. // incoming or outgoing frame. 'size' represents frame length in bytes.
  33. //
  34. // The return value has the following meaning:
  35. // * kContinue: the interceptor does not want to delay frame processing,
  36. // `retry_callback` is ignored.
  37. // * kShouldWait: frame processing should be paused until `retry_callback`
  38. // is invoked.
  39. // Calling Intercept again before the callback runs is not allowed.
  40. InterceptResult Intercept(FrameDirection direction,
  41. size_t size,
  42. base::OnceClosure retry_callback);
  43. private:
  44. void ThrottleCallback(FrameDirection direction, int result, int64_t bytes);
  45. ThrottlingNetworkInterceptor::ThrottleCallback throttle_callbacks_[2];
  46. const uint32_t net_log_source_id_;
  47. const std::unique_ptr<ScopedThrottlingToken> throttling_token_;
  48. base::OnceClosure pending_callbacks_[2];
  49. };
  50. } // namespace network
  51. #endif // SERVICES_NETWORK_WEBSOCKET_INTERCEPTOR_H_