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.
5 // Implementation of the MalwareDetailsRedirectsCollector class.
7 #include "chrome/browser/safe_browsing/malware_details_history.h"
10 #include "base/bind_helpers.h"
11 #include "chrome/browser/chrome_notification_types.h"
12 #include "chrome/browser/history/history_service_factory.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/safe_browsing/malware_details.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 #include "content/public/browser/render_view_host.h"
19 #include "content/public/browser/web_contents.h"
21 using content::BrowserThread
;
23 MalwareDetailsRedirectsCollector::MalwareDetailsRedirectsCollector(
27 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
29 registrar_
.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED
,
30 content::Source
<Profile
>(profile
));
34 void MalwareDetailsRedirectsCollector::StartHistoryCollection(
35 const std::vector
<GURL
>& urls
,
36 const base::Closure
& callback
) {
37 DVLOG(1) << "Num of urls to check in history service: " << urls
.size();
41 if (urls
.size() == 0) {
46 BrowserThread::PostTask(
47 BrowserThread::UI
, FROM_HERE
,
48 base::Bind(&MalwareDetailsRedirectsCollector::StartGetRedirects
,
52 bool MalwareDetailsRedirectsCollector::HasStarted() const {
56 const std::vector
<safe_browsing::RedirectChain
>&
57 MalwareDetailsRedirectsCollector::GetCollectedUrls() const {
58 return redirects_urls_
;
61 void MalwareDetailsRedirectsCollector::Observe(
63 const content::NotificationSource
& source
,
64 const content::NotificationDetails
& details
) {
65 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
66 DCHECK_EQ(type
, chrome::NOTIFICATION_PROFILE_DESTROYED
);
67 DVLOG(1) << "Profile gone.";
71 MalwareDetailsRedirectsCollector::~MalwareDetailsRedirectsCollector() {}
73 void MalwareDetailsRedirectsCollector::StartGetRedirects(
74 const std::vector
<GURL
>& urls
) {
75 // History access from profile needs to happen in UI thread
76 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
77 for (size_t i
= 0; i
< urls
.size(); ++i
) {
78 urls_
.push_back(urls
[i
]);
80 urls_it_
= urls_
.begin();
81 GetRedirects(*urls_it_
);
84 void MalwareDetailsRedirectsCollector::GetRedirects(const GURL
& url
) {
85 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
91 HistoryService
* history
= HistoryServiceFactory::GetForProfile(
92 profile_
, Profile::EXPLICIT_ACCESS
);
98 history
->QueryRedirectsTo(
101 base::Bind(&MalwareDetailsRedirectsCollector::OnGotQueryRedirectsTo
,
102 base::Unretained(this)));
105 void MalwareDetailsRedirectsCollector::OnGotQueryRedirectsTo(
106 HistoryService::Handle handle
,
109 history::RedirectList
* redirect_list
) {
111 if (success
&& redirect_list
->size() > 0) {
112 std::vector
<GURL
> urllist
;
113 urllist
.push_back(url
);
114 for (size_t i
= 0; i
< redirect_list
->size(); i
++) {
115 urllist
.push_back(redirect_list
->at(i
));
117 redirects_urls_
.push_back(urllist
);
120 // Proceed to next url
123 if (urls_it_
== urls_
.end()) {
128 GetRedirects(*urls_it_
);
131 void MalwareDetailsRedirectsCollector::AllDone() {
132 DVLOG(1) << "AllDone";
133 BrowserThread::PostTask(BrowserThread::IO
, FROM_HERE
, callback_
);