| 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), history_service_observer_(this) { |
| 116 } | 117 } |
| 117 | 118 |
| 118 LastDownloadFinder::LastDownloadFinder(const std::vector<Profile*>& profiles, | 119 LastDownloadFinder::LastDownloadFinder(const std::vector<Profile*>& profiles, |
| 119 const LastDownloadCallback& callback) | 120 const LastDownloadCallback& callback) |
| 120 : callback_(callback), weak_ptr_factory_(this) { | 121 : callback_(callback), |
| 122 weak_ptr_factory_(this), |
| 123 history_service_observer_(this) { |
| 121 // Observe profile lifecycle events so that the finder can begin or abandon | 124 // Observe profile lifecycle events so that the finder can begin or abandon |
| 122 // the search in profiles while it is running. | 125 // the search in profiles while it is running. |
| 123 notification_registrar_.Add(this, | 126 notification_registrar_.Add(this, |
| 124 chrome::NOTIFICATION_PROFILE_ADDED, | 127 chrome::NOTIFICATION_PROFILE_ADDED, |
| 125 content::NotificationService::AllSources()); | 128 content::NotificationService::AllSources()); |
| 126 notification_registrar_.Add(this, | 129 notification_registrar_.Add(this, |
| 127 chrome::NOTIFICATION_HISTORY_LOADED, | |
| 128 content::NotificationService::AllSources()); | |
| 129 notification_registrar_.Add(this, | |
| 130 chrome::NOTIFICATION_PROFILE_DESTROYED, | 130 chrome::NOTIFICATION_PROFILE_DESTROYED, |
| 131 content::NotificationService::AllSources()); | 131 content::NotificationService::AllSources()); |
| 132 | 132 |
| 133 // Begin the seach for all given profiles. | 133 // Begin the seach for all given profiles. |
| 134 std::for_each( | 134 std::for_each( |
| 135 profiles.begin(), | 135 profiles.begin(), |
| 136 profiles.end(), | 136 profiles.end(), |
| 137 std::bind1st(std::mem_fun(&LastDownloadFinder::SearchInProfile), this)); | 137 std::bind1st(std::mem_fun(&LastDownloadFinder::SearchInProfile), this)); |
| 138 } | 138 } |
| 139 | 139 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 158 // No history service is returned for profiles that do not save history. | 158 // No history service is returned for profiles that do not save history. |
| 159 if (!history_service) | 159 if (!history_service) |
| 160 return; | 160 return; |
| 161 | 161 |
| 162 profiles_.push_back(profile); | 162 profiles_.push_back(profile); |
| 163 if (history_service->BackendLoaded()) { | 163 if (history_service->BackendLoaded()) { |
| 164 history_service->QueryDownloads( | 164 history_service->QueryDownloads( |
| 165 base::Bind(&LastDownloadFinder::OnDownloadQuery, | 165 base::Bind(&LastDownloadFinder::OnDownloadQuery, |
| 166 weak_ptr_factory_.GetWeakPtr(), | 166 weak_ptr_factory_.GetWeakPtr(), |
| 167 profile)); | 167 profile)); |
| 168 } // else wait until history is loaded. | 168 } else { |
| 169 // else wait until history is loaded. |
| 170 history_service_observer_.Add(history_service); |
| 171 } |
| 169 } | 172 } |
| 170 | 173 |
| 171 void LastDownloadFinder::OnProfileHistoryLoaded( | 174 void LastDownloadFinder::OnProfileHistoryLoaded( |
| 172 Profile* profile, | 175 Profile* profile, |
| 173 HistoryService* history_service) { | 176 HistoryService* history_service) { |
| 174 if (std::find(profiles_.begin(), profiles_.end(), profile) != | 177 if (std::find(profiles_.begin(), profiles_.end(), profile) != |
| 175 profiles_.end()) { | 178 profiles_.end()) { |
| 176 history_service->QueryDownloads( | 179 history_service->QueryDownloads( |
| 177 base::Bind(&LastDownloadFinder::OnDownloadQuery, | 180 base::Bind(&LastDownloadFinder::OnDownloadQuery, |
| 178 weak_ptr_factory_.GetWeakPtr(), | 181 weak_ptr_factory_.GetWeakPtr(), |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 } | 238 } |
| 236 } | 239 } |
| 237 | 240 |
| 238 void LastDownloadFinder::Observe(int type, | 241 void LastDownloadFinder::Observe(int type, |
| 239 const content::NotificationSource& source, | 242 const content::NotificationSource& source, |
| 240 const content::NotificationDetails& details) { | 243 const content::NotificationDetails& details) { |
| 241 switch (type) { | 244 switch (type) { |
| 242 case chrome::NOTIFICATION_PROFILE_ADDED: | 245 case chrome::NOTIFICATION_PROFILE_ADDED: |
| 243 SearchInProfile(content::Source<Profile>(source).ptr()); | 246 SearchInProfile(content::Source<Profile>(source).ptr()); |
| 244 break; | 247 break; |
| 245 case chrome::NOTIFICATION_HISTORY_LOADED: | 248 case chrome::NOTIFICATION_PROFILE_DESTROYED: { |
| 246 OnProfileHistoryLoaded(content::Source<Profile>(source).ptr(), | 249 AbandonSearchInProfile(content::Source<Profile>(source).ptr()); |
| 247 content::Details<HistoryService>(details).ptr()); | 250 HistoryService* history_service = |
| 251 HistoryServiceFactory::GetForProfileIfExists( |
| 252 content::Source<Profile>(source).ptr(), Profile::IMPLICIT_ACCESS); |
| 253 if (history_service) |
| 254 history_service_observer_.Remove(history_service); |
| 248 break; | 255 break; |
| 249 case chrome::NOTIFICATION_PROFILE_DESTROYED: | 256 } |
| 250 AbandonSearchInProfile(content::Source<Profile>(source).ptr()); | |
| 251 break; | |
| 252 default: | 257 default: |
| 253 break; | 258 break; |
| 254 } | 259 } |
| 255 } | 260 } |
| 256 | 261 |
| 262 void LastDownloadFinder::OnHistoryServiceLoaded( |
| 263 HistoryService* history_service) { |
| 264 OnProfileHistoryLoaded(history_service->profile(), history_service); |
| 265 history_service_observer_.Remove(history_service); |
| 266 } |
| 267 |
| 257 } // namespace safe_browsing | 268 } // namespace safe_browsing |
| OLD | NEW |