activity_filtering_websocket_handshake_throttle.cc 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. // Copyright 2020 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 "chromecast/renderer/activity_filtering_websocket_handshake_throttle.h"
  5. #include "base/strings/stringprintf.h"
  6. #include "third_party/blink/public/platform/web_string.h"
  7. #include "url/gurl.h"
  8. namespace chromecast {
  9. ActivityFilteringWebSocketHandshakeThrottle::
  10. ActivityFilteringWebSocketHandshakeThrottle(ActivityUrlFilter* filter)
  11. : url_filter_(filter) {}
  12. ActivityFilteringWebSocketHandshakeThrottle::
  13. ~ActivityFilteringWebSocketHandshakeThrottle() = default;
  14. void ActivityFilteringWebSocketHandshakeThrottle::ThrottleHandshake(
  15. const blink::WebURL& url,
  16. blink::WebSocketHandshakeThrottle::OnCompletion completion_callback) {
  17. GURL gurl = GURL(url);
  18. // Pass through allowed URLs, block otherwise.
  19. if (url_filter_->UrlMatchesWhitelist(gurl)) {
  20. std::move(completion_callback).Run(absl::nullopt);
  21. return;
  22. }
  23. std::move(completion_callback)
  24. .Run(blink::WebString::FromUTF8(base::StringPrintf(
  25. "WebSocket connection to %s is blocked", gurl.spec().c_str())));
  26. }
  27. } // namespace chromecast