OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 // Implementation of the ThreatDetailsRedirectsCollector class. | |
6 | |
7 #include "chrome/browser/safe_browsing/threat_details_history.h" | |
8 | |
9 #include <stddef.h> | |
10 | |
11 #include "base/bind.h" | |
12 #include "base/bind_helpers.h" | |
13 #include "chrome/browser/safe_browsing/threat_details.h" | |
14 #include "components/history/core/browser/history_service.h" | |
15 #include "content/public/browser/browser_thread.h" | |
16 #include "content/public/browser/notification_details.h" | |
17 #include "content/public/browser/notification_source.h" | |
18 | |
19 using content::BrowserThread; | |
20 | |
21 namespace safe_browsing { | |
22 | |
23 ThreatDetailsRedirectsCollector::ThreatDetailsRedirectsCollector( | |
24 const base::WeakPtr<history::HistoryService>& history_service) | |
25 : has_started_(false), | |
26 history_service_(history_service), | |
27 history_service_observer_(this) { | |
28 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
29 | |
30 if (history_service) { | |
31 history_service_observer_.Add(history_service.get()); | |
32 } | |
33 } | |
34 | |
35 void ThreatDetailsRedirectsCollector::StartHistoryCollection( | |
36 const std::vector<GURL>& urls, | |
37 const base::Closure& callback) { | |
38 DVLOG(1) << "Num of urls to check in history service: " << urls.size(); | |
39 has_started_ = true; | |
40 callback_ = callback; | |
41 | |
42 if (urls.size() == 0) { | |
43 AllDone(); | |
44 return; | |
45 } | |
46 | |
47 BrowserThread::PostTask( | |
48 BrowserThread::UI, FROM_HERE, | |
49 base::BindOnce(&ThreatDetailsRedirectsCollector::StartGetRedirects, this, | |
50 urls)); | |
51 } | |
52 | |
53 bool ThreatDetailsRedirectsCollector::HasStarted() const { | |
54 return has_started_; | |
55 } | |
56 | |
57 const std::vector<RedirectChain>& | |
58 ThreatDetailsRedirectsCollector::GetCollectedUrls() const { | |
59 return redirects_urls_; | |
60 } | |
61 | |
62 ThreatDetailsRedirectsCollector::~ThreatDetailsRedirectsCollector() {} | |
63 | |
64 void ThreatDetailsRedirectsCollector::StartGetRedirects( | |
65 const std::vector<GURL>& urls) { | |
66 // History access from profile needs to happen in UI thread | |
67 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
68 for (size_t i = 0; i < urls.size(); ++i) { | |
69 urls_.push_back(urls[i]); | |
70 } | |
71 urls_it_ = urls_.begin(); | |
72 GetRedirects(*urls_it_); | |
73 } | |
74 | |
75 void ThreatDetailsRedirectsCollector::GetRedirects(const GURL& url) { | |
76 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
77 | |
78 if (!history_service_) { | |
79 AllDone(); | |
80 return; | |
81 } | |
82 | |
83 history_service_->QueryRedirectsTo( | |
84 url, | |
85 base::Bind(&ThreatDetailsRedirectsCollector::OnGotQueryRedirectsTo, | |
86 base::Unretained(this), url), | |
87 &request_tracker_); | |
88 } | |
89 | |
90 void ThreatDetailsRedirectsCollector::OnGotQueryRedirectsTo( | |
91 const GURL& url, | |
92 const history::RedirectList* redirect_list) { | |
93 if (!redirect_list->empty()) { | |
94 std::vector<GURL> urllist; | |
95 urllist.push_back(url); | |
96 urllist.insert(urllist.end(), redirect_list->begin(), redirect_list->end()); | |
97 redirects_urls_.push_back(urllist); | |
98 } | |
99 | |
100 // Proceed to next url | |
101 ++urls_it_; | |
102 | |
103 if (urls_it_ == urls_.end()) { | |
104 AllDone(); | |
105 return; | |
106 } | |
107 | |
108 GetRedirects(*urls_it_); | |
109 } | |
110 | |
111 void ThreatDetailsRedirectsCollector::AllDone() { | |
112 DVLOG(1) << "AllDone"; | |
113 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, callback_); | |
114 callback_.Reset(); | |
115 } | |
116 | |
117 void ThreatDetailsRedirectsCollector::HistoryServiceBeingDeleted( | |
118 history::HistoryService* history_service) { | |
119 history_service_observer_.Remove(history_service); | |
120 history_service_.reset(); | |
121 } | |
122 | |
123 } // namespace safe_browsing | |
OLD | NEW |