OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 |
| 5 #include "components/safe_browsing/renderer/websocket_sb_handshake_throttle.h" |
| 6 |
| 7 #include <utility> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/command_line.h" |
| 11 #include "base/logging.h" |
| 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/strings/stringprintf.h" |
| 14 #include "content/public/common/content_switches.h" |
| 15 #include "content/public/common/resource_type.h" |
| 16 #include "content/public/common/service_names.mojom.h" |
| 17 #include "content/public/renderer/render_frame.h" |
| 18 #include "content/public/renderer/render_thread.h" |
| 19 #include "ipc/ipc_message.h" |
| 20 #include "mojo/public/cpp/bindings/interface_request.h" |
| 21 #include "services/service_manager/public/cpp/connector.h" |
| 22 #include "third_party/WebKit/public/platform/WebString.h" |
| 23 #include "third_party/WebKit/public/platform/WebURL.h" |
| 24 |
| 25 namespace safe_browsing { |
| 26 |
| 27 // static |
| 28 std::unique_ptr<WebSocketSBHandshakeThrottle> |
| 29 WebSocketSBHandshakeThrottle::MaybeCreate( |
| 30 mojom::SafeBrowsingPtr* safe_browsing_service) { |
| 31 // TODO(ricea): Is there some way to avoid doing a string lookup every time |
| 32 // here? |
| 33 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 34 switches::kEnableNetworkService)) { |
| 35 return nullptr; |
| 36 } |
| 37 if (!*safe_browsing_service) { |
| 38 content::RenderThread::Get()->GetConnector()->BindInterface( |
| 39 content::mojom::kBrowserServiceName, safe_browsing_service); |
| 40 } |
| 41 return base::MakeUnique<WebSocketSBHandshakeThrottle>( |
| 42 safe_browsing_service->get()); |
| 43 } |
| 44 |
| 45 WebSocketSBHandshakeThrottle::WebSocketSBHandshakeThrottle( |
| 46 mojom::SafeBrowsing* safe_browsing) |
| 47 : callbacks_(nullptr), safe_browsing_(safe_browsing), weak_factory_(this) {} |
| 48 |
| 49 WebSocketSBHandshakeThrottle::~WebSocketSBHandshakeThrottle() {} |
| 50 |
| 51 void WebSocketSBHandshakeThrottle::ThrottleHandshake( |
| 52 const blink::WebURL& url, |
| 53 blink::WebLocalFrame* web_local_frame, |
| 54 blink::WebCallbacks<void, const blink::WebString&>* callbacks) { |
| 55 DCHECK(!callbacks_); |
| 56 DCHECK(!url_checker_); |
| 57 callbacks_ = callbacks; |
| 58 url_ = url; |
| 59 int render_frame_id = MSG_ROUTING_NONE; |
| 60 if (web_local_frame) { |
| 61 render_frame_id = |
| 62 content::RenderFrame::FromWebFrame(web_local_frame)->GetRoutingID(); |
| 63 } |
| 64 int load_flags = 0; |
| 65 safe_browsing_->CreateCheckerAndCheck( |
| 66 render_frame_id, mojo::MakeRequest(&url_checker_), url, load_flags, |
| 67 content::RESOURCE_TYPE_SUB_RESOURCE, |
| 68 base::BindOnce(&WebSocketSBHandshakeThrottle::OnCheckResult, |
| 69 weak_factory_.GetWeakPtr())); |
| 70 } |
| 71 |
| 72 void WebSocketSBHandshakeThrottle::OnCheckResult(bool safe) { |
| 73 if (safe) { |
| 74 callbacks_->OnSuccess(); |
| 75 } else { |
| 76 callbacks_->OnError(blink::WebString::FromUTF8(base::StringPrintf( |
| 77 "WebSocket connection to %s failed safe browsing check", |
| 78 url_.spec().c_str()))); |
| 79 } |
| 80 // |this| is destroyed here. |
| 81 } |
| 82 |
| 83 } // namespace safe_browsing |
OLD | NEW |