Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 105 scoped_ptr<LastDownloadFinder> LastDownloadFinder::Create( | 105 scoped_ptr<LastDownloadFinder> LastDownloadFinder::Create( |
| 106 const LastDownloadCallback& callback) { | 106 const LastDownloadCallback& callback) { |
| 107 scoped_ptr<LastDownloadFinder> finder(make_scoped_ptr(new LastDownloadFinder( | 107 scoped_ptr<LastDownloadFinder> finder(make_scoped_ptr(new LastDownloadFinder( |
| 108 g_browser_process->profile_manager()->GetLoadedProfiles(), callback))); | 108 g_browser_process->profile_manager()->GetLoadedProfiles(), callback))); |
| 109 // Return NULL if there is no work to do. | 109 // Return NULL if there is no work to do. |
| 110 if (finder->profiles_.empty()) | 110 if (finder->profiles_.empty()) |
| 111 return scoped_ptr<LastDownloadFinder>(); | 111 return scoped_ptr<LastDownloadFinder>(); |
| 112 return finder.Pass(); | 112 return finder.Pass(); |
| 113 } | 113 } |
| 114 | 114 |
| 115 LastDownloadFinder::LastDownloadFinder() : weak_ptr_factory_(this) { | 115 LastDownloadFinder::LastDownloadFinder() |
| 116 : weak_ptr_factory_(this), | |
| 117 history_service_observer_(this) { | |
| 116 } | 118 } |
| 117 | 119 |
| 118 LastDownloadFinder::LastDownloadFinder(const std::vector<Profile*>& profiles, | 120 LastDownloadFinder::LastDownloadFinder(const std::vector<Profile*>& profiles, |
| 119 const LastDownloadCallback& callback) | 121 const LastDownloadCallback& callback) |
| 120 : callback_(callback), weak_ptr_factory_(this) { | 122 : callback_(callback), |
| 123 weak_ptr_factory_(this), | |
| 124 history_service_observer_(this) { | |
| 121 // Observe profile lifecycle events so that the finder can begin or abandon | 125 // Observe profile lifecycle events so that the finder can begin or abandon |
| 122 // the search in profiles while it is running. | 126 // the search in profiles while it is running. |
| 123 notification_registrar_.Add(this, | 127 notification_registrar_.Add(this, |
| 124 chrome::NOTIFICATION_PROFILE_ADDED, | 128 chrome::NOTIFICATION_PROFILE_ADDED, |
| 125 content::NotificationService::AllSources()); | 129 content::NotificationService::AllSources()); |
| 126 notification_registrar_.Add(this, | 130 notification_registrar_.Add(this, |
| 127 chrome::NOTIFICATION_HISTORY_LOADED, | |
| 128 content::NotificationService::AllSources()); | |
| 129 notification_registrar_.Add(this, | |
| 130 chrome::NOTIFICATION_PROFILE_DESTROYED, | 131 chrome::NOTIFICATION_PROFILE_DESTROYED, |
| 131 content::NotificationService::AllSources()); | 132 content::NotificationService::AllSources()); |
| 132 | 133 |
| 133 // Begin the seach for all given profiles. | 134 // Begin the seach for all given profiles. |
| 134 std::for_each( | 135 std::for_each( |
| 135 profiles.begin(), | 136 profiles.begin(), |
| 136 profiles.end(), | 137 profiles.end(), |
| 137 std::bind1st(std::mem_fun(&LastDownloadFinder::SearchInProfile), this)); | 138 std::bind1st(std::mem_fun(&LastDownloadFinder::SearchInProfile), this)); |
| 138 } | 139 } |
| 139 | 140 |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 158 // No history service is returned for profiles that do not save history. | 159 // No history service is returned for profiles that do not save history. |
| 159 if (!history_service) | 160 if (!history_service) |
| 160 return; | 161 return; |
| 161 | 162 |
| 162 profiles_.push_back(profile); | 163 profiles_.push_back(profile); |
| 163 if (history_service->BackendLoaded()) { | 164 if (history_service->BackendLoaded()) { |
| 164 history_service->QueryDownloads( | 165 history_service->QueryDownloads( |
| 165 base::Bind(&LastDownloadFinder::OnDownloadQuery, | 166 base::Bind(&LastDownloadFinder::OnDownloadQuery, |
| 166 weak_ptr_factory_.GetWeakPtr(), | 167 weak_ptr_factory_.GetWeakPtr(), |
| 167 profile)); | 168 profile)); |
| 168 } // else wait until history is loaded. | 169 } else { |
| 170 // else wait until history is loaded. | |
| 171 history_service_observer_.Add(history_service); | |
| 172 } | |
| 169 } | 173 } |
| 170 | 174 |
| 171 void LastDownloadFinder::OnProfileHistoryLoaded( | 175 void LastDownloadFinder::OnProfileHistoryLoaded( |
| 172 Profile* profile, | 176 Profile* profile, |
| 173 HistoryService* history_service) { | 177 HistoryService* history_service) { |
| 174 if (std::find(profiles_.begin(), profiles_.end(), profile) != | 178 if (std::find(profiles_.begin(), profiles_.end(), profile) != |
| 175 profiles_.end()) { | 179 profiles_.end()) { |
| 176 history_service->QueryDownloads( | 180 history_service->QueryDownloads( |
| 177 base::Bind(&LastDownloadFinder::OnDownloadQuery, | 181 base::Bind(&LastDownloadFinder::OnDownloadQuery, |
| 178 weak_ptr_factory_.GetWeakPtr(), | 182 weak_ptr_factory_.GetWeakPtr(), |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 235 } | 239 } |
| 236 } | 240 } |
| 237 | 241 |
| 238 void LastDownloadFinder::Observe(int type, | 242 void LastDownloadFinder::Observe(int type, |
| 239 const content::NotificationSource& source, | 243 const content::NotificationSource& source, |
| 240 const content::NotificationDetails& details) { | 244 const content::NotificationDetails& details) { |
| 241 switch (type) { | 245 switch (type) { |
| 242 case chrome::NOTIFICATION_PROFILE_ADDED: | 246 case chrome::NOTIFICATION_PROFILE_ADDED: |
| 243 SearchInProfile(content::Source<Profile>(source).ptr()); | 247 SearchInProfile(content::Source<Profile>(source).ptr()); |
| 244 break; | 248 break; |
| 245 case chrome::NOTIFICATION_HISTORY_LOADED: | |
| 246 OnProfileHistoryLoaded(content::Source<Profile>(source).ptr(), | |
| 247 content::Details<HistoryService>(details).ptr()); | |
| 248 break; | |
| 249 case chrome::NOTIFICATION_PROFILE_DESTROYED: | 249 case chrome::NOTIFICATION_PROFILE_DESTROYED: |
| 250 AbandonSearchInProfile(content::Source<Profile>(source).ptr()); | 250 AbandonSearchInProfile(content::Source<Profile>(source).ptr()); |
|
sdefresne
2014/10/30 17:37:04
You need to stop observing the HistoryService asso
nshaik
2014/10/30 21:48:36
Fixed it.
| |
| 251 break; | 251 break; |
| 252 default: | 252 default: |
| 253 break; | 253 break; |
| 254 } | 254 } |
| 255 } | 255 } |
| 256 | 256 |
| 257 void LastDownloadFinder::OnHistoryServiceLoaded( | |
| 258 HistoryService* history_service) { | |
| 259 OnProfileHistoryLoaded(history_service->profile(), history_service); | |
| 260 history_service_observer_.Remove(history_service); | |
| 261 } | |
| 262 | |
| 257 } // namespace safe_browsing | 263 } // namespace safe_browsing |
| OLD | NEW |