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

Side by Side Diff: chrome/browser/safe_browsing/incident_reporting/last_download_finder.cc

Issue 573553004: Eliminate NOTIFICATION_HISTORY_LOADED notification (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add ScopedObserver to InMemoryHistoryBackend,PrerenderLocalPredictor,ChromeTemplateURLServiceClient Created 6 years 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/browser/safe_browsing/incident_reporting/last_download_finder.h " 5 #include "chrome/browser/safe_browsing/incident_reporting/last_download_finder.h "
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <functional> 8 #include <functional>
9 #include <utility> 9 #include <utility>
10 10
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 scoped_ptr<LastDownloadFinder> finder(make_scoped_ptr(new LastDownloadFinder( 152 scoped_ptr<LastDownloadFinder> finder(make_scoped_ptr(new LastDownloadFinder(
153 download_details_getter, 153 download_details_getter,
154 g_browser_process->profile_manager()->GetLoadedProfiles(), 154 g_browser_process->profile_manager()->GetLoadedProfiles(),
155 callback))); 155 callback)));
156 // Return NULL if there is no work to do. 156 // Return NULL if there is no work to do.
157 if (finder->profile_states_.empty()) 157 if (finder->profile_states_.empty())
158 return scoped_ptr<LastDownloadFinder>(); 158 return scoped_ptr<LastDownloadFinder>();
159 return finder.Pass(); 159 return finder.Pass();
160 } 160 }
161 161
162 LastDownloadFinder::LastDownloadFinder() : weak_ptr_factory_(this) { 162 LastDownloadFinder::LastDownloadFinder()
163 : history_service_observer_(this), weak_ptr_factory_(this) {
163 } 164 }
164 165
165 LastDownloadFinder::LastDownloadFinder( 166 LastDownloadFinder::LastDownloadFinder(
166 const DownloadDetailsGetter& download_details_getter, 167 const DownloadDetailsGetter& download_details_getter,
167 const std::vector<Profile*>& profiles, 168 const std::vector<Profile*>& profiles,
168 const LastDownloadCallback& callback) 169 const LastDownloadCallback& callback)
169 : download_details_getter_(download_details_getter), 170 : download_details_getter_(download_details_getter),
170 callback_(callback), 171 callback_(callback),
172 history_service_observer_(this),
171 weak_ptr_factory_(this) { 173 weak_ptr_factory_(this) {
172 // Observe profile lifecycle events so that the finder can begin or abandon 174 // Observe profile lifecycle events so that the finder can begin or abandon
173 // the search in profiles while it is running. 175 // the search in profiles while it is running.
174 notification_registrar_.Add(this, 176 notification_registrar_.Add(this,
175 chrome::NOTIFICATION_PROFILE_ADDED, 177 chrome::NOTIFICATION_PROFILE_ADDED,
176 content::NotificationService::AllSources()); 178 content::NotificationService::AllSources());
177 notification_registrar_.Add(this, 179 notification_registrar_.Add(this,
178 chrome::NOTIFICATION_HISTORY_LOADED,
179 content::NotificationService::AllSources());
180 notification_registrar_.Add(this,
181 chrome::NOTIFICATION_PROFILE_DESTROYED, 180 chrome::NOTIFICATION_PROFILE_DESTROYED,
182 content::NotificationService::AllSources()); 181 content::NotificationService::AllSources());
183 182
184 // Begin the seach for all given profiles. 183 // Begin the seach for all given profiles.
185 std::for_each( 184 std::for_each(
186 profiles.begin(), 185 profiles.begin(),
187 profiles.end(), 186 profiles.end(),
188 std::bind1st(std::mem_fun(&LastDownloadFinder::SearchInProfile), this)); 187 std::bind1st(std::mem_fun(&LastDownloadFinder::SearchInProfile), this));
189 } 188 }
190 189
191 void LastDownloadFinder::SearchInProfile(Profile* profile) { 190 void LastDownloadFinder::SearchInProfile(Profile* profile) {
192 // Do not look in OTR profiles or in profiles that do not participate in 191 // Do not look in OTR profiles or in profiles that do not participate in
193 // safe browsing. 192 // safe browsing.
194 if (profile->IsOffTheRecord() || 193 if (profile->IsOffTheRecord() ||
195 !profile->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled)) { 194 !profile->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled)) {
196 return; 195 return;
197 } 196 }
198 197
199 // Exit early if already processing this profile. This could happen if, for 198 // Exit early if already processing this profile. This could happen if, for
200 // example, NOTIFICATION_PROFILE_ADDED arrives after construction while 199 // example, NOTIFICATION_PROFILE_ADDED arrives after construction while
201 // waiting for NOTIFICATION_HISTORY_LOADED. 200 // waiting for OnHistoryServiceLoaded.
202 if (profile_states_.count(profile)) 201 if (profile_states_.count(profile))
203 return; 202 return;
204 203
205 // Initiate a metadata search. 204 // Initiate a metadata search.
206 profile_states_[profile] = WAITING_FOR_METADATA; 205 profile_states_[profile] = WAITING_FOR_METADATA;
207 download_details_getter_.Run(profile, 206 download_details_getter_.Run(profile,
208 base::Bind(&LastDownloadFinder::OnMetadataQuery, 207 base::Bind(&LastDownloadFinder::OnMetadataQuery,
209 weak_ptr_factory_.GetWeakPtr(), 208 weak_ptr_factory_.GetWeakPtr(),
210 profile)); 209 profile));
211 } 210 }
(...skipping 21 matching lines...) Expand all
233 // No history service is returned for profiles that do not save history. 232 // No history service is returned for profiles that do not save history.
234 if (!history_service) { 233 if (!history_service) {
235 RemoveProfileAndReportIfDone(iter); 234 RemoveProfileAndReportIfDone(iter);
236 return; 235 return;
237 } 236 }
238 if (history_service->BackendLoaded()) { 237 if (history_service->BackendLoaded()) {
239 history_service->QueryDownloads( 238 history_service->QueryDownloads(
240 base::Bind(&LastDownloadFinder::OnDownloadQuery, 239 base::Bind(&LastDownloadFinder::OnDownloadQuery,
241 weak_ptr_factory_.GetWeakPtr(), 240 weak_ptr_factory_.GetWeakPtr(),
242 profile)); 241 profile));
243 } // else wait until history is loaded. 242 } else {
243 // else wait until history is loaded.
244 history_service_observer_.Add(history_service);
245 }
244 } 246 }
245 } 247 }
246 248
247 void LastDownloadFinder::OnProfileHistoryLoaded( 249 void LastDownloadFinder::OnProfileHistoryLoaded(
248 Profile* profile, 250 Profile* profile,
249 HistoryService* history_service) { 251 HistoryService* history_service) {
250 auto iter = profile_states_.find(profile); 252 auto iter = profile_states_.find(profile);
251 if (iter == profile_states_.end()) 253 if (iter == profile_states_.end())
252 return; 254 return;
253 255
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
320 } 322 }
321 } 323 }
322 324
323 void LastDownloadFinder::Observe(int type, 325 void LastDownloadFinder::Observe(int type,
324 const content::NotificationSource& source, 326 const content::NotificationSource& source,
325 const content::NotificationDetails& details) { 327 const content::NotificationDetails& details) {
326 switch (type) { 328 switch (type) {
327 case chrome::NOTIFICATION_PROFILE_ADDED: 329 case chrome::NOTIFICATION_PROFILE_ADDED:
328 SearchInProfile(content::Source<Profile>(source).ptr()); 330 SearchInProfile(content::Source<Profile>(source).ptr());
329 break; 331 break;
330 case chrome::NOTIFICATION_HISTORY_LOADED:
331 OnProfileHistoryLoaded(content::Source<Profile>(source).ptr(),
332 content::Details<HistoryService>(details).ptr());
333 break;
334 case chrome::NOTIFICATION_PROFILE_DESTROYED: 332 case chrome::NOTIFICATION_PROFILE_DESTROYED:
335 AbandonSearchInProfile(content::Source<Profile>(source).ptr()); 333 AbandonSearchInProfile(content::Source<Profile>(source).ptr());
336 break; 334 break;
337 default: 335 default:
338 break; 336 break;
339 } 337 }
340 } 338 }
341 339
340 void LastDownloadFinder::OnHistoryServiceLoaded(
341 HistoryService* history_service) {
342 OnProfileHistoryLoaded(history_service->profile(), history_service);
343 }
344
345 void LastDownloadFinder::HistoryServiceBeingDeleted(
346 HistoryService* history_service) {
347 history_service_observer_.Remove(history_service);
348 }
349
342 } // namespace safe_browsing 350 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698