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