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), | |
48 safe_browsing_(std::move(safe_browsing)), | |
kinuko
2017/06/21 06:42:41
nit: this std::move seems no-op
Adam Rice
2017/06/21 07:43:20
Thanks, removed.
| |
49 weak_factory_(this) {} | |
50 | |
51 WebSocketSBHandshakeThrottle::~WebSocketSBHandshakeThrottle() {} | |
52 | |
53 void WebSocketSBHandshakeThrottle::ThrottleHandshake( | |
54 const blink::WebURL& url, | |
55 blink::WebLocalFrame* web_local_frame, | |
56 blink::WebCallbacks<void, const blink::WebString&>* callbacks) { | |
57 DCHECK(!callbacks_); | |
58 DCHECK(!url_checker_); | |
59 callbacks_ = callbacks; | |
60 url_ = url; | |
61 int render_frame_id = MSG_ROUTING_NONE; | |
62 if (web_local_frame) { | |
63 render_frame_id = | |
64 content::RenderFrame::FromWebFrame(web_local_frame)->GetRoutingID(); | |
65 } | |
66 int load_flags = 0; | |
67 safe_browsing_->CreateCheckerAndCheck( | |
68 render_frame_id, mojo::MakeRequest(&url_checker_), url, load_flags, | |
69 content::RESOURCE_TYPE_SUB_RESOURCE, | |
70 base::BindOnce(&WebSocketSBHandshakeThrottle::OnCheckResult, | |
71 weak_factory_.GetWeakPtr())); | |
72 } | |
73 | |
74 void WebSocketSBHandshakeThrottle::OnCheckResult(bool safe) { | |
75 if (safe) { | |
76 callbacks_->OnSuccess(); | |
77 } else { | |
78 callbacks_->OnError(blink::WebString::FromUTF8(base::StringPrintf( | |
79 "WebSocket connection to %s failed safe browsing check", | |
80 url_.spec().c_str()))); | |
81 } | |
82 // |this| is destroyed here. | |
83 } | |
84 | |
85 } // namespace safe_browsing | |
OLD | NEW |