Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(64)

Side by Side Diff: chrome/browser/safe_browsing/threat_details_history.cc

Issue 2796123003: Componentize safe_browsing: factor out chrome/ deps in ThreatDetailsRedirectsCollector. (Closed)
Patch Set: fix crashes Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 // Implementation of the ThreatDetailsRedirectsCollector class. 5 // Implementation of the ThreatDetailsRedirectsCollector class.
6 6
7 #include "chrome/browser/safe_browsing/threat_details_history.h" 7 #include "chrome/browser/safe_browsing/threat_details_history.h"
8 8
9 #include <stddef.h> 9 #include <stddef.h>
10 10
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/bind_helpers.h" 12 #include "base/bind_helpers.h"
13 #include "chrome/browser/chrome_notification_types.h"
14 #include "chrome/browser/history/history_service_factory.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/safe_browsing/threat_details.h" 13 #include "chrome/browser/safe_browsing/threat_details.h"
14 #include "components/history/core/browser/history_service.h"
17 #include "content/public/browser/browser_thread.h" 15 #include "content/public/browser/browser_thread.h"
18 #include "content/public/browser/notification_details.h" 16 #include "content/public/browser/notification_details.h"
19 #include "content/public/browser/notification_source.h" 17 #include "content/public/browser/notification_source.h"
20 18
21 using content::BrowserThread; 19 using content::BrowserThread;
22 20
23 namespace safe_browsing { 21 namespace safe_browsing {
24 22
25 ThreatDetailsRedirectsCollector::ThreatDetailsRedirectsCollector( 23 ThreatDetailsRedirectsCollector::ThreatDetailsRedirectsCollector(
26 Profile* profile) 24 const base::WeakPtr<history::HistoryService>& history_service)
27 : profile_(profile), has_started_(false) { 25 : has_started_(false),
26 history_service_(history_service),
27 history_service_observer_(this) {
28 DCHECK_CURRENTLY_ON(BrowserThread::UI); 28 DCHECK_CURRENTLY_ON(BrowserThread::UI);
29 if (profile) { 29
30 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED, 30 if (history_service) {
31 content::Source<Profile>(profile)); 31 history_service_observer_.Add(history_service.get());
32 } 32 }
33 } 33 }
34 34
35 void ThreatDetailsRedirectsCollector::StartHistoryCollection( 35 void ThreatDetailsRedirectsCollector::StartHistoryCollection(
36 const std::vector<GURL>& urls, 36 const std::vector<GURL>& urls,
37 const base::Closure& callback) { 37 const base::Closure& callback) {
38 DVLOG(1) << "Num of urls to check in history service: " << urls.size(); 38 DVLOG(1) << "Num of urls to check in history service: " << urls.size();
39 has_started_ = true; 39 has_started_ = true;
40 callback_ = callback; 40 callback_ = callback;
41 41
(...skipping 10 matching lines...) Expand all
52 52
53 bool ThreatDetailsRedirectsCollector::HasStarted() const { 53 bool ThreatDetailsRedirectsCollector::HasStarted() const {
54 return has_started_; 54 return has_started_;
55 } 55 }
56 56
57 const std::vector<RedirectChain>& 57 const std::vector<RedirectChain>&
58 ThreatDetailsRedirectsCollector::GetCollectedUrls() const { 58 ThreatDetailsRedirectsCollector::GetCollectedUrls() const {
59 return redirects_urls_; 59 return redirects_urls_;
60 } 60 }
61 61
62 void ThreatDetailsRedirectsCollector::Observe(
63 int type,
64 const content::NotificationSource& source,
65 const content::NotificationDetails& details) {
66 DCHECK_CURRENTLY_ON(BrowserThread::UI);
67 DCHECK_EQ(type, chrome::NOTIFICATION_PROFILE_DESTROYED);
68 DVLOG(1) << "Profile gone.";
69 profile_ = NULL;
70 }
71
72 ThreatDetailsRedirectsCollector::~ThreatDetailsRedirectsCollector() {} 62 ThreatDetailsRedirectsCollector::~ThreatDetailsRedirectsCollector() {}
73 63
74 void ThreatDetailsRedirectsCollector::StartGetRedirects( 64 void ThreatDetailsRedirectsCollector::StartGetRedirects(
75 const std::vector<GURL>& urls) { 65 const std::vector<GURL>& urls) {
76 // History access from profile needs to happen in UI thread 66 // History access from profile needs to happen in UI thread
77 DCHECK_CURRENTLY_ON(BrowserThread::UI); 67 DCHECK_CURRENTLY_ON(BrowserThread::UI);
78 for (size_t i = 0; i < urls.size(); ++i) { 68 for (size_t i = 0; i < urls.size(); ++i) {
79 urls_.push_back(urls[i]); 69 urls_.push_back(urls[i]);
80 } 70 }
81 urls_it_ = urls_.begin(); 71 urls_it_ = urls_.begin();
82 GetRedirects(*urls_it_); 72 GetRedirects(*urls_it_);
83 } 73 }
84 74
85 void ThreatDetailsRedirectsCollector::GetRedirects(const GURL& url) { 75 void ThreatDetailsRedirectsCollector::GetRedirects(const GURL& url) {
86 DCHECK_CURRENTLY_ON(BrowserThread::UI); 76 DCHECK_CURRENTLY_ON(BrowserThread::UI);
87 if (!profile_) { 77
78 if (!history_service_) {
88 AllDone(); 79 AllDone();
89 return; 80 return;
90 } 81 }
91 82
92 history::HistoryService* history = HistoryServiceFactory::GetForProfile( 83 history_service_->QueryRedirectsTo(
93 profile_, ServiceAccessType::EXPLICIT_ACCESS); 84 url,
94 if (!history) { 85 base::Bind(&ThreatDetailsRedirectsCollector::OnGotQueryRedirectsTo,
95 AllDone(); 86 base::Unretained(this), url),
96 return;
97 }
98
99 history->QueryRedirectsTo(
100 url, base::Bind(&ThreatDetailsRedirectsCollector::OnGotQueryRedirectsTo,
101 base::Unretained(this), url),
102 &request_tracker_); 87 &request_tracker_);
103 } 88 }
104 89
105 void ThreatDetailsRedirectsCollector::OnGotQueryRedirectsTo( 90 void ThreatDetailsRedirectsCollector::OnGotQueryRedirectsTo(
106 const GURL& url, 91 const GURL& url,
107 const history::RedirectList* redirect_list) { 92 const history::RedirectList* redirect_list) {
108 if (!redirect_list->empty()) { 93 if (!redirect_list->empty()) {
109 std::vector<GURL> urllist; 94 std::vector<GURL> urllist;
110 urllist.push_back(url); 95 urllist.push_back(url);
111 urllist.insert(urllist.end(), redirect_list->begin(), redirect_list->end()); 96 urllist.insert(urllist.end(), redirect_list->begin(), redirect_list->end());
(...skipping 10 matching lines...) Expand all
122 107
123 GetRedirects(*urls_it_); 108 GetRedirects(*urls_it_);
124 } 109 }
125 110
126 void ThreatDetailsRedirectsCollector::AllDone() { 111 void ThreatDetailsRedirectsCollector::AllDone() {
127 DVLOG(1) << "AllDone"; 112 DVLOG(1) << "AllDone";
128 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, callback_); 113 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, callback_);
129 callback_.Reset(); 114 callback_.Reset();
130 } 115 }
131 116
117 void ThreatDetailsRedirectsCollector::HistoryServiceBeingDeleted(
118 history::HistoryService* history_service) {
119 history_service_observer_.Remove(history_service);
120 history_service_.reset();
121 }
122
132 } // namespace safe_browsing 123 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698