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), 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 |
| 140 void LastDownloadFinder::SearchInProfile(Profile* profile) { | 140 void LastDownloadFinder::SearchInProfile(Profile* profile) { |
| 141 // Do not look in OTR profiles or in profiles that do not participate in | 141 // Do not look in OTR profiles or in profiles that do not participate in |
| 142 // safe browsing. | 142 // safe browsing. |
| 143 if (profile->IsOffTheRecord() || | 143 if (profile->IsOffTheRecord() || |
| 144 !profile->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled)) { | 144 !profile->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled)) { |
| 145 return; | 145 return; |
| 146 } | 146 } |
| 147 | 147 |
| 148 // Exit early if already processing this profile. This could happen if, for | 148 // Exit early if already processing this profile. This could happen if, for |
| 149 // example, NOTIFICATION_PROFILE_ADDED arrives after construction while | 149 // example, NOTIFICATION_PROFILE_ADDED arrives after construction while |
| 150 // waiting for NOTIFICATION_HISTORY_LOADED. | 150 // waiting for OnHistoryServiceLoaded. |
| 151 if (std::find(profiles_.begin(), profiles_.end(), profile) != | 151 if (std::find(profiles_.begin(), profiles_.end(), profile) != |
| 152 profiles_.end()) { | 152 profiles_.end()) { |
| 153 return; | 153 return; |
| 154 } | 154 } |
| 155 | 155 |
| 156 HistoryService* history_service = | 156 HistoryService* history_service = |
| 157 HistoryServiceFactory::GetForProfile(profile, Profile::IMPLICIT_ACCESS); | 157 HistoryServiceFactory::GetForProfile(profile, Profile::IMPLICIT_ACCESS); |
| 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: { |
|
brettw
2014/11/14 21:13:06
I think this change is unnecessary.
nshaik
2014/11/15 07:04:15
Done.
| |
| 246 OnProfileHistoryLoaded(content::Source<Profile>(source).ptr(), | |
| 247 content::Details<HistoryService>(details).ptr()); | |
| 248 break; | |
| 249 case chrome::NOTIFICATION_PROFILE_DESTROYED: | |
| 250 AbandonSearchInProfile(content::Source<Profile>(source).ptr()); | 249 AbandonSearchInProfile(content::Source<Profile>(source).ptr()); |
| 251 break; | 250 break; |
| 251 } | |
| 252 default: | 252 default: |
| 253 break; | 253 break; |
| 254 } | 254 } |
| 255 } | 255 } |
| 256 | 256 |
| 257 void LastDownloadFinder::OnHistoryServiceLoaded( | |
|
brettw
2014/11/14 21:13:07
Order should match.
nshaik
2014/11/15 07:04:15
Done.
| |
| 258 HistoryService* history_service) { | |
| 259 OnProfileHistoryLoaded(history_service->profile(), history_service); | |
| 260 history_service_observer_.Remove(history_service); | |
| 261 } | |
| 262 | |
| 263 void LastDownloadFinder::HistoryServiceBeingDeleted( | |
| 264 HistoryService* history_service) { | |
| 265 history_service_observer_.Remove(history_service); | |
| 266 } | |
| 267 | |
| 257 } // namespace safe_browsing | 268 } // namespace safe_browsing |
| OLD | NEW |