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 "chrome/renderer/safe_browsing/safe_browsing_url_loader_throttle.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "mojo/public/cpp/bindings/interface_request.h" | |
9 #include "net/url_request/redirect_info.h" | |
10 | |
11 namespace safe_browsing { | |
12 | |
13 SafeBrowsingURLLoaderThrottle::SafeBrowsingURLLoaderThrottle( | |
14 mojom::SafeBrowsing* safe_browsing, | |
15 int render_frame_id) | |
16 : safe_browsing_(safe_browsing), | |
17 render_frame_id_(render_frame_id), | |
18 weak_factory_(this) {} | |
19 | |
20 SafeBrowsingURLLoaderThrottle::~SafeBrowsingURLLoaderThrottle() = default; | |
21 | |
22 void SafeBrowsingURLLoaderThrottle::WillStartRequest( | |
23 const GURL& url, | |
24 int load_flags, | |
25 content::ResourceType resource_type, | |
26 bool* defer) { | |
27 DCHECK_EQ(0u, pending_checks_); | |
28 DCHECK(!blocked_); | |
29 DCHECK(!url_checker_); | |
30 | |
31 pending_checks_++; | |
32 // Use a weak pointer to self because |safe_browsing_| is not owned by this | |
33 // object. | |
34 safe_browsing_->CreateCheckerAndCheck( | |
35 render_frame_id_, mojo::MakeRequest(&url_checker_), url, load_flags, | |
36 resource_type, | |
37 base::BindOnce(&SafeBrowsingURLLoaderThrottle::OnCheckUrlResult, | |
38 weak_factory_.GetWeakPtr())); | |
39 safe_browsing_ = nullptr; | |
40 | |
41 url_checker_.set_connection_error_handler( | |
42 base::Bind(&SafeBrowsingURLLoaderThrottle::OnConnectionError, | |
43 base::Unretained(this))); | |
44 } | |
45 | |
46 void SafeBrowsingURLLoaderThrottle::WillRedirectRequest( | |
47 const net::RedirectInfo& redirect_info, | |
48 bool* defer) { | |
49 // If |blocked_| is true, the resource load has been canceled and there | |
50 // shouldn't be such a notification. | |
51 DCHECK(!blocked_); | |
52 | |
53 if (!url_checker_) { | |
54 DCHECK_EQ(0u, pending_checks_); | |
55 return; | |
56 } | |
57 | |
58 pending_checks_++; | |
59 url_checker_->CheckUrl( | |
60 redirect_info.new_url, | |
61 base::BindOnce(&SafeBrowsingURLLoaderThrottle::OnCheckUrlResult, | |
62 base::Unretained(this))); | |
63 } | |
64 | |
65 void SafeBrowsingURLLoaderThrottle::WillProcessResponse(bool* defer) { | |
66 // If |blocked_| is true, the resource load has been canceled and there | |
67 // shouldn't be such a notification. | |
68 DCHECK(!blocked_); | |
69 | |
70 if (pending_checks_ > 0) | |
71 *defer = true; | |
72 } | |
73 | |
74 void SafeBrowsingURLLoaderThrottle::OnCheckUrlResult(bool safe) { | |
75 if (blocked_ || !url_checker_) | |
76 return; | |
77 | |
78 DCHECK_LT(0u, pending_checks_); | |
79 pending_checks_--; | |
80 | |
81 if (safe) { | |
82 if (pending_checks_ == 0) { | |
83 // The resource load is not necessarily deferred, in that case Resume() is | |
84 // a no-op. | |
85 delegate_->Resume(); | |
86 } | |
87 } else { | |
88 url_checker_.reset(); | |
89 blocked_ = true; | |
90 pending_checks_ = 0; | |
91 delegate_->CancelWithError(net::ERR_ABORTED); | |
92 } | |
93 } | |
94 | |
95 void SafeBrowsingURLLoaderThrottle::OnConnectionError() { | |
96 DCHECK(!blocked_); | |
97 | |
98 // If a service-side disconnect happens, treat all URLs as if they are safe. | |
99 url_checker_.reset(); | |
100 pending_checks_ = 0; | |
101 delegate_->Resume(); | |
102 } | |
103 | |
104 } // namespace safe_browsing | |
OLD | NEW |