| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/safe_browsing_service.h" | 5 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
| 6 | 6 |
| 7 #include "base/callback.h" | 7 #include "base/callback.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| 11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
| 12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
| 13 #include "base/threading/thread_restrictions.h" | 13 #include "base/threading/thread_restrictions.h" |
| 14 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
| 15 #include "chrome/browser/metrics/metrics_service.h" | 15 #include "chrome/browser/metrics/metrics_service.h" |
| 16 #include "chrome/browser/prefs/pref_change_registrar.h" | 16 #include "chrome/browser/prefs/pref_change_registrar.h" |
| 17 #include "chrome/browser/prefs/pref_service.h" | 17 #include "chrome/browser/prefs/pref_service.h" |
| 18 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
| 19 #include "chrome/browser/profiles/profile_manager.h" | 19 #include "chrome/browser/profiles/profile_manager.h" |
| 20 #include "chrome/browser/safe_browsing/client_side_detection_service.h" |
| 20 #include "chrome/browser/safe_browsing/malware_details.h" | 21 #include "chrome/browser/safe_browsing/malware_details.h" |
| 21 #include "chrome/browser/safe_browsing/protocol_manager.h" | 22 #include "chrome/browser/safe_browsing/protocol_manager.h" |
| 22 #include "chrome/browser/safe_browsing/safe_browsing_blocking_page.h" | 23 #include "chrome/browser/safe_browsing/safe_browsing_blocking_page.h" |
| 23 #include "chrome/browser/safe_browsing/safe_browsing_database.h" | 24 #include "chrome/browser/safe_browsing/safe_browsing_database.h" |
| 24 #include "chrome/browser/tab_contents/tab_util.h" | 25 #include "chrome/browser/tab_contents/tab_util.h" |
| 25 #include "chrome/common/chrome_constants.h" | 26 #include "chrome/common/chrome_constants.h" |
| 26 #include "chrome/common/chrome_notification_types.h" | 27 #include "chrome/common/chrome_notification_types.h" |
| 27 #include "chrome/common/chrome_paths.h" | 28 #include "chrome/common/chrome_paths.h" |
| 28 #include "chrome/common/chrome_switches.h" | 29 #include "chrome/common/chrome_switches.h" |
| 29 #include "chrome/common/pref_names.h" | 30 #include "chrome/common/pref_names.h" |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 : database_(NULL), | 159 : database_(NULL), |
| 159 protocol_manager_(NULL), | 160 protocol_manager_(NULL), |
| 160 enabled_(false), | 161 enabled_(false), |
| 161 enable_download_protection_(false), | 162 enable_download_protection_(false), |
| 162 enable_csd_whitelist_(false), | 163 enable_csd_whitelist_(false), |
| 163 update_in_progress_(false), | 164 update_in_progress_(false), |
| 164 database_update_in_progress_(false), | 165 database_update_in_progress_(false), |
| 165 closing_database_(false), | 166 closing_database_(false), |
| 166 download_urlcheck_timeout_ms_(kDownloadUrlCheckTimeoutMs), | 167 download_urlcheck_timeout_ms_(kDownloadUrlCheckTimeoutMs), |
| 167 download_hashcheck_timeout_ms_(kDownloadHashCheckTimeoutMs) { | 168 download_hashcheck_timeout_ms_(kDownloadHashCheckTimeoutMs) { |
| 169 #if !defined(OS_CHROMEOS) |
| 170 if (!CommandLine::ForCurrentProcess()->HasSwitch( |
| 171 switches::kDisableClientSidePhishingDetection) && |
| 172 CanReportStats()) { |
| 173 csd_service_.reset( |
| 174 safe_browsing::ClientSideDetectionService::Create( |
| 175 g_browser_process->system_request_context())); |
| 176 } |
| 177 #endif |
| 178 } |
| 179 |
| 180 SafeBrowsingService::~SafeBrowsingService() { |
| 181 // Deletes the PrefChangeRegistrars, whose dtors also unregister |this| as an |
| 182 // observer of the preferences. |
| 183 STLDeleteValues(&prefs_map_); |
| 184 |
| 185 // We should have already been shut down. If we're still enabled, then the |
| 186 // database isn't going to be closed properly, which could lead to corruption. |
| 187 DCHECK(!enabled_); |
| 168 } | 188 } |
| 169 | 189 |
| 170 void SafeBrowsingService::Initialize() { | 190 void SafeBrowsingService::Initialize() { |
| 171 // Track the safe browsing preference of existing profiles. | 191 // Track the safe browsing preference of existing profiles. |
| 172 // The SafeBrowsingService will be started if any existing profile has the | 192 // The SafeBrowsingService will be started if any existing profile has the |
| 173 // preference enabled. It will also listen for updates to the preferences. | 193 // preference enabled. It will also listen for updates to the preferences. |
| 174 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 194 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 175 if (profile_manager) { | 195 if (profile_manager) { |
| 176 std::vector<Profile*> profiles = profile_manager->GetLoadedProfiles(); | 196 std::vector<Profile*> profiles = profile_manager->GetLoadedProfiles(); |
| 177 for (size_t i = 0; i < profiles.size(); ++i) { | 197 for (size_t i = 0; i < profiles.size(); ++i) { |
| 178 if (profiles[i]->IsOffTheRecord()) | 198 if (profiles[i]->IsOffTheRecord()) |
| 179 continue; | 199 continue; |
| 180 AddPrefService(profiles[i]->GetPrefs()); | 200 AddPrefService(profiles[i]->GetPrefs()); |
| 181 } | 201 } |
| 182 } | 202 } |
| 183 | 203 |
| 184 // Track profile creation and destruction. | 204 // Track profile creation and destruction. |
| 185 prefs_registrar_.Add(this, chrome::NOTIFICATION_PROFILE_CREATED, | 205 prefs_registrar_.Add(this, chrome::NOTIFICATION_PROFILE_CREATED, |
| 186 NotificationService::AllSources()); | 206 NotificationService::AllSources()); |
| 187 prefs_registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED, | 207 prefs_registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED, |
| 188 NotificationService::AllSources()); | 208 NotificationService::AllSources()); |
| 189 } | 209 } |
| 190 | 210 |
| 191 void SafeBrowsingService::ShutDown() { | 211 void SafeBrowsingService::ShutDown() { |
| 192 BrowserThread::PostTask( | 212 Stop(); |
| 193 BrowserThread::IO, FROM_HERE, | 213 // The IO thread is going away, so make sure the ClientSideDetectionService |
| 194 NewRunnableMethod(this, &SafeBrowsingService::OnIOShutdown)); | 214 // dtor executes now since it may call the dtor of URLFetcher which relies |
| 215 // on it. |
| 216 csd_service_.reset(); |
| 195 } | 217 } |
| 196 | 218 |
| 197 bool SafeBrowsingService::CanCheckUrl(const GURL& url) const { | 219 bool SafeBrowsingService::CanCheckUrl(const GURL& url) const { |
| 198 return url.SchemeIs(chrome::kFtpScheme) || | 220 return url.SchemeIs(chrome::kFtpScheme) || |
| 199 url.SchemeIs(chrome::kHttpScheme) || | 221 url.SchemeIs(chrome::kHttpScheme) || |
| 200 url.SchemeIs(chrome::kHttpsScheme); | 222 url.SchemeIs(chrome::kHttpsScheme); |
| 201 } | 223 } |
| 202 | 224 |
| 203 // Only report SafeBrowsing related stats when UMA is enabled. User must also | 225 // Only report SafeBrowsing related stats when UMA is enabled. User must also |
| 204 // ensure that safe browsing is enabled from the calling profile. | 226 // ensure that safe browsing is enabled from the calling profile. |
| (...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 474 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 496 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 475 DCHECK(enabled_); | 497 DCHECK(enabled_); |
| 476 safe_browsing_thread_->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( | 498 safe_browsing_thread_->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( |
| 477 this, &SafeBrowsingService::OnResetDatabase)); | 499 this, &SafeBrowsingService::OnResetDatabase)); |
| 478 } | 500 } |
| 479 | 501 |
| 480 void SafeBrowsingService::LogPauseDelay(base::TimeDelta time) { | 502 void SafeBrowsingService::LogPauseDelay(base::TimeDelta time) { |
| 481 UMA_HISTOGRAM_LONG_TIMES("SB2.Delay", time); | 503 UMA_HISTOGRAM_LONG_TIMES("SB2.Delay", time); |
| 482 } | 504 } |
| 483 | 505 |
| 484 SafeBrowsingService::~SafeBrowsingService() { | |
| 485 // Deletes the PrefChangeRegistrars, whose dtors also unregister |this| as an | |
| 486 // observer of the preferences. | |
| 487 STLDeleteValues(&prefs_map_); | |
| 488 | |
| 489 // We should have already been shut down. If we're still enabled, then the | |
| 490 // database isn't going to be closed properly, which could lead to corruption. | |
| 491 DCHECK(!enabled_); | |
| 492 } | |
| 493 | |
| 494 void SafeBrowsingService::OnIOInitialize( | 506 void SafeBrowsingService::OnIOInitialize( |
| 495 const std::string& client_key, | 507 const std::string& client_key, |
| 496 const std::string& wrapped_key, | 508 const std::string& wrapped_key, |
| 497 net::URLRequestContextGetter* request_context_getter) { | 509 net::URLRequestContextGetter* request_context_getter) { |
| 498 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 510 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 499 if (enabled_) | 511 if (enabled_) |
| 500 return; | 512 return; |
| 501 DCHECK(!safe_browsing_thread_.get()); | 513 DCHECK(!safe_browsing_thread_.get()); |
| 502 safe_browsing_thread_.reset(new base::Thread("Chrome_SafeBrowsingThread")); | 514 safe_browsing_thread_.reset(new base::Thread("Chrome_SafeBrowsingThread")); |
| 503 if (!safe_browsing_thread_->Start()) | 515 if (!safe_browsing_thread_->Start()) |
| (...skipping 386 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 890 local_state && local_state->GetBoolean(prefs::kMetricsReportingEnabled)); | 902 local_state && local_state->GetBoolean(prefs::kMetricsReportingEnabled)); |
| 891 #endif | 903 #endif |
| 892 | 904 |
| 893 BrowserThread::PostTask( | 905 BrowserThread::PostTask( |
| 894 BrowserThread::IO, FROM_HERE, | 906 BrowserThread::IO, FROM_HERE, |
| 895 NewRunnableMethod( | 907 NewRunnableMethod( |
| 896 this, &SafeBrowsingService::OnIOInitialize, client_key, wrapped_key, | 908 this, &SafeBrowsingService::OnIOInitialize, client_key, wrapped_key, |
| 897 request_context_getter)); | 909 request_context_getter)); |
| 898 } | 910 } |
| 899 | 911 |
| 912 void SafeBrowsingService::Stop() { |
| 913 BrowserThread::PostTask( |
| 914 BrowserThread::IO, FROM_HERE, |
| 915 NewRunnableMethod(this, &SafeBrowsingService::OnIOShutdown)); |
| 916 } |
| 917 |
| 900 void SafeBrowsingService::OnCloseDatabase() { | 918 void SafeBrowsingService::OnCloseDatabase() { |
| 901 DCHECK_EQ(MessageLoop::current(), safe_browsing_thread_->message_loop()); | 919 DCHECK_EQ(MessageLoop::current(), safe_browsing_thread_->message_loop()); |
| 902 DCHECK(closing_database_); | 920 DCHECK(closing_database_); |
| 903 | 921 |
| 904 // Because |closing_database_| is true, nothing on the IO thread will be | 922 // Because |closing_database_| is true, nothing on the IO thread will be |
| 905 // accessing the database, so it's safe to delete and then NULL the pointer. | 923 // accessing the database, so it's safe to delete and then NULL the pointer. |
| 906 delete database_; | 924 delete database_; |
| 907 database_ = NULL; | 925 database_ = NULL; |
| 908 | 926 |
| 909 // Acquiring the lock here guarantees correct ordering between the resetting | 927 // Acquiring the lock here guarantees correct ordering between the resetting |
| (...skipping 404 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1314 for (iter = prefs_map_.begin(); iter != prefs_map_.end(); ++iter) { | 1332 for (iter = prefs_map_.begin(); iter != prefs_map_.end(); ++iter) { |
| 1315 if (iter->first->GetBoolean(prefs::kSafeBrowsingEnabled)) { | 1333 if (iter->first->GetBoolean(prefs::kSafeBrowsingEnabled)) { |
| 1316 enable = true; | 1334 enable = true; |
| 1317 break; | 1335 break; |
| 1318 } | 1336 } |
| 1319 } | 1337 } |
| 1320 | 1338 |
| 1321 if (enable) | 1339 if (enable) |
| 1322 Start(); | 1340 Start(); |
| 1323 else | 1341 else |
| 1324 ShutDown(); | 1342 Stop(); |
| 1343 |
| 1344 if (csd_service_.get()) |
| 1345 csd_service_->SetEnabled(enable); |
| 1325 } | 1346 } |
| OLD | NEW |