[email protected] | c2316134 | 2010-08-18 20:34:04 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | // |
| 5 | |
| 6 | #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
| 7 | |
[email protected] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 8 | #include "base/callback.h" |
[email protected] | 894c4e8 | 2010-06-29 21:53:18 | [diff] [blame] | 9 | #include "base/command_line.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 10 | #include "base/path_service.h" |
| 11 | #include "base/string_util.h" |
| 12 | #include "chrome/browser/browser_process.h" |
[email protected] | 017a7a11 | 2010-10-12 16:38:27 | [diff] [blame] | 13 | #include "chrome/browser/browser_thread.h" |
[email protected] | 975bead | 2009-11-30 21:59:53 | [diff] [blame] | 14 | #include "chrome/browser/metrics/metrics_service.h" |
[email protected] | 37858e5 | 2010-08-26 00:22:02 | [diff] [blame] | 15 | #include "chrome/browser/prefs/pref_service.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 16 | #include "chrome/browser/profile_manager.h" |
[email protected] | 51065cb | 2009-02-19 00:25:23 | [diff] [blame] | 17 | #include "chrome/browser/safe_browsing/protocol_manager.h" |
[email protected] | 5b7c7d3c | 2009-02-19 00:06:22 | [diff] [blame] | 18 | #include "chrome/browser/safe_browsing/safe_browsing_blocking_page.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 19 | #include "chrome/browser/safe_browsing/safe_browsing_database.h" |
[email protected] | f3ec774 | 2009-01-15 00:59:16 | [diff] [blame] | 20 | #include "chrome/browser/tab_contents/tab_util.h" |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 21 | #include "chrome/browser/tab_contents/tab_contents.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 22 | #include "chrome/common/chrome_constants.h" |
| 23 | #include "chrome/common/chrome_paths.h" |
[email protected] | 894c4e8 | 2010-06-29 21:53:18 | [diff] [blame] | 24 | #include "chrome/common/chrome_switches.h" |
[email protected] | 68d2a05f | 2010-05-07 21:39:55 | [diff] [blame] | 25 | #include "chrome/common/net/url_request_context_getter.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 26 | #include "chrome/common/pref_names.h" |
[email protected] | dcf7d35 | 2009-02-26 01:56:02 | [diff] [blame] | 27 | #include "chrome/common/url_constants.h" |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 28 | #include "net/base/registry_controlled_domain.h" |
| 29 | |
[email protected] | 1a87151 | 2009-11-06 06:11:18 | [diff] [blame] | 30 | #if defined(OS_WIN) |
| 31 | #include "chrome/installer/util/browser_distribution.h" |
| 32 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 33 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 34 | using base::Time; |
| 35 | using base::TimeDelta; |
| 36 | |
[email protected] | 894c4e8 | 2010-06-29 21:53:18 | [diff] [blame] | 37 | // The default URL prefix where browser fetches chunk updates, hashes, |
| 38 | // and reports malware. |
| 39 | static const char* const kSbDefaultInfoURLPrefix = |
| 40 | "http://safebrowsing.clients.google.com/safebrowsing"; |
| 41 | |
| 42 | // The default URL prefix where browser fetches MAC client key. |
| 43 | static const char* const kSbDefaultMacKeyURLPrefix = |
| 44 | "https://sb-ssl.google.com/safebrowsing"; |
| 45 | |
[email protected] | d11f566 | 2009-11-12 20:52:56 | [diff] [blame] | 46 | static Profile* GetDefaultProfile() { |
| 47 | FilePath user_data_dir; |
| 48 | PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); |
| 49 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 50 | return profile_manager->GetDefaultProfile(user_data_dir); |
| 51 | } |
| 52 | |
[email protected] | 93aa89c7 | 2010-10-20 21:32:04 | [diff] [blame^] | 53 | struct SafeBrowsingService::WhiteListedEntry { |
| 54 | int render_process_host_id; |
| 55 | int render_view_id; |
| 56 | std::string domain; |
| 57 | UrlCheckResult result; |
| 58 | }; |
| 59 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 60 | SafeBrowsingService::SafeBrowsingService() |
[email protected] | d83d03aa | 2009-11-02 21:44:37 | [diff] [blame] | 61 | : database_(NULL), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 62 | protocol_manager_(NULL), |
| 63 | enabled_(false), |
[email protected] | cb2a67e | 2009-11-17 00:48:53 | [diff] [blame] | 64 | update_in_progress_(false), |
[email protected] | ef5b482 | 2010-09-09 16:45:49 | [diff] [blame] | 65 | database_update_in_progress_(false), |
[email protected] | cb2a67e | 2009-11-17 00:48:53 | [diff] [blame] | 66 | closing_database_(false) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 67 | } |
| 68 | |
[email protected] | d83d03aa | 2009-11-02 21:44:37 | [diff] [blame] | 69 | void SafeBrowsingService::Initialize() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 70 | // Get the profile's preference for SafeBrowsing. |
[email protected] | d11f566 | 2009-11-12 20:52:56 | [diff] [blame] | 71 | PrefService* pref_service = GetDefaultProfile()->GetPrefs(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 72 | if (pref_service->GetBoolean(prefs::kSafeBrowsingEnabled)) |
| 73 | Start(); |
| 74 | } |
| 75 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 76 | void SafeBrowsingService::ShutDown() { |
[email protected] | 4d3b42a | 2010-10-07 05:00:29 | [diff] [blame] | 77 | BrowserThread::PostTask( |
| 78 | BrowserThread::IO, FROM_HERE, |
[email protected] | d83d03aa | 2009-11-02 21:44:37 | [diff] [blame] | 79 | NewRunnableMethod(this, &SafeBrowsingService::OnIOShutdown)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 80 | } |
| 81 | |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 82 | bool SafeBrowsingService::CanCheckUrl(const GURL& url) const { |
[email protected] | 14aab33 | 2010-06-02 14:35:48 | [diff] [blame] | 83 | return url.SchemeIs(chrome::kFtpScheme) || |
| 84 | url.SchemeIs(chrome::kHttpScheme) || |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 85 | url.SchemeIs(chrome::kHttpsScheme); |
| 86 | } |
| 87 | |
| 88 | bool SafeBrowsingService::CheckUrl(const GURL& url, Client* client) { |
[email protected] | 4d3b42a | 2010-10-07 05:00:29 | [diff] [blame] | 89 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | dec173b | 2009-11-13 21:53:26 | [diff] [blame] | 90 | if (!enabled_) |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 91 | return true; |
| 92 | |
[email protected] | 14aab33 | 2010-06-02 14:35:48 | [diff] [blame] | 93 | if (!CanCheckUrl(url)) |
| 94 | return true; |
| 95 | |
[email protected] | cb2a67e | 2009-11-17 00:48:53 | [diff] [blame] | 96 | if (!MakeDatabaseAvailable()) { |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 97 | QueuedCheck check; |
| 98 | check.client = client; |
| 99 | check.url = url; |
| 100 | queued_checks_.push_back(check); |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | std::string list; |
| 105 | std::vector<SBPrefix> prefix_hits; |
| 106 | std::vector<SBFullHashResult> full_hits; |
| 107 | base::Time check_start = base::Time::Now(); |
| 108 | bool prefix_match = database_->ContainsUrl(url, &list, &prefix_hits, |
| 109 | &full_hits, |
| 110 | protocol_manager_->last_update()); |
| 111 | |
| 112 | UMA_HISTOGRAM_TIMES("SB2.FilterCheck", base::Time::Now() - check_start); |
| 113 | |
| 114 | if (!prefix_match) |
| 115 | return true; // URL is okay. |
| 116 | |
| 117 | // Needs to be asynchronous, since we could be in the constructor of a |
| 118 | // ResourceDispatcherHost event handler which can't pause there. |
| 119 | SafeBrowsingCheck* check = new SafeBrowsingCheck(); |
| 120 | check->url = url; |
| 121 | check->client = client; |
| 122 | check->result = URL_SAFE; |
| 123 | check->need_get_hash = full_hits.empty(); |
| 124 | check->prefix_hits.swap(prefix_hits); |
| 125 | check->full_hits.swap(full_hits); |
| 126 | checks_.insert(check); |
| 127 | |
[email protected] | 4d3b42a | 2010-10-07 05:00:29 | [diff] [blame] | 128 | BrowserThread::PostTask( |
| 129 | BrowserThread::IO, FROM_HERE, |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 130 | NewRunnableMethod(this, &SafeBrowsingService::OnCheckDone, check)); |
| 131 | |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | void SafeBrowsingService::CancelCheck(Client* client) { |
[email protected] | 4d3b42a | 2010-10-07 05:00:29 | [diff] [blame] | 136 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 137 | for (CurrentChecks::iterator i = checks_.begin(); i != checks_.end(); ++i) { |
[email protected] | 03addd9 | 2009-11-17 21:38:10 | [diff] [blame] | 138 | // We can't delete matching checks here because the db thread has a copy of |
| 139 | // the pointer. Instead, we simply NULL out the client, and when the db |
| 140 | // thread calls us back, we'll clean up the check. |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 141 | if ((*i)->client == client) |
| 142 | (*i)->client = NULL; |
| 143 | } |
| 144 | |
| 145 | // Scan the queued clients store. Clients may be here if they requested a URL |
[email protected] | fd220e60 | 2009-11-14 01:02:37 | [diff] [blame] | 146 | // check before the database has finished loading. |
[email protected] | dec173b | 2009-11-13 21:53:26 | [diff] [blame] | 147 | for (std::deque<QueuedCheck>::iterator it(queued_checks_.begin()); |
[email protected] | 03addd9 | 2009-11-17 21:38:10 | [diff] [blame] | 148 | it != queued_checks_.end(); ) { |
| 149 | // In this case it's safe to delete matches entirely since nothing has a |
| 150 | // pointer to them. |
[email protected] | dec173b | 2009-11-13 21:53:26 | [diff] [blame] | 151 | if (it->client == client) |
[email protected] | 03addd9 | 2009-11-17 21:38:10 | [diff] [blame] | 152 | it = queued_checks_.erase(it); |
| 153 | else |
| 154 | ++it; |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
| 158 | void SafeBrowsingService::DisplayBlockingPage(const GURL& url, |
[email protected] | 293f1925 | 2010-08-18 20:01:18 | [diff] [blame] | 159 | const GURL& original_url, |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 160 | ResourceType::Type resource_type, |
| 161 | UrlCheckResult result, |
| 162 | Client* client, |
| 163 | int render_process_host_id, |
| 164 | int render_view_id) { |
[email protected] | 4d3b42a | 2010-10-07 05:00:29 | [diff] [blame] | 165 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | cb2a67e | 2009-11-17 00:48:53 | [diff] [blame] | 166 | |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 167 | // Check if the user has already ignored our warning for this render_view |
| 168 | // and domain. |
| 169 | for (size_t i = 0; i < white_listed_entries_.size(); ++i) { |
| 170 | const WhiteListedEntry& entry = white_listed_entries_[i]; |
| 171 | if (entry.render_process_host_id == render_process_host_id && |
| 172 | entry.render_view_id == render_view_id && |
| 173 | entry.result == result && |
| 174 | entry.domain == |
| 175 | net::RegistryControlledDomainService::GetDomainAndRegistry(url)) { |
| 176 | MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
| 177 | this, &SafeBrowsingService::NotifyClientBlockingComplete, |
| 178 | client, true)); |
| 179 | return; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | UnsafeResource resource; |
| 184 | resource.url = url; |
[email protected] | 293f1925 | 2010-08-18 20:01:18 | [diff] [blame] | 185 | resource.original_url = original_url; |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 186 | resource.resource_type = resource_type; |
| 187 | resource.threat_type= result; |
| 188 | resource.client = client; |
| 189 | resource.render_process_host_id = render_process_host_id; |
| 190 | resource.render_view_id = render_view_id; |
| 191 | |
| 192 | // The blocking page must be created from the UI thread. |
[email protected] | 4d3b42a | 2010-10-07 05:00:29 | [diff] [blame] | 193 | BrowserThread::PostTask( |
| 194 | BrowserThread::UI, FROM_HERE, |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 195 | NewRunnableMethod( |
| 196 | this, &SafeBrowsingService::DoDisplayBlockingPage, resource)); |
| 197 | } |
| 198 | |
| 199 | void SafeBrowsingService::HandleGetHashResults( |
| 200 | SafeBrowsingCheck* check, |
| 201 | const std::vector<SBFullHashResult>& full_hashes, |
| 202 | bool can_cache) { |
[email protected] | 4d3b42a | 2010-10-07 05:00:29 | [diff] [blame] | 203 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 204 | if (checks_.find(check) == checks_.end()) |
| 205 | return; |
| 206 | |
| 207 | DCHECK(enabled_); |
| 208 | |
| 209 | UMA_HISTOGRAM_LONG_TIMES("SB2.Network", Time::Now() - check->start); |
| 210 | |
| 211 | std::vector<SBPrefix> prefixes = check->prefix_hits; |
| 212 | OnHandleGetHashResults(check, full_hashes); // 'check' is deleted here. |
| 213 | |
[email protected] | cb2a67e | 2009-11-17 00:48:53 | [diff] [blame] | 214 | if (can_cache && MakeDatabaseAvailable()) { |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 215 | // Cache the GetHash results in memory: |
| 216 | database_->CacheHashResults(prefixes, full_hashes); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | void SafeBrowsingService::HandleChunk(const std::string& list, |
[email protected] | 7b1e3710 | 2010-03-08 21:43:16 | [diff] [blame] | 221 | SBChunkList* chunks) { |
[email protected] | 4d3b42a | 2010-10-07 05:00:29 | [diff] [blame] | 222 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 223 | DCHECK(enabled_); |
| 224 | safe_browsing_thread_->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( |
| 225 | this, &SafeBrowsingService::HandleChunkForDatabase, list, chunks)); |
| 226 | } |
| 227 | |
| 228 | void SafeBrowsingService::HandleChunkDelete( |
| 229 | std::vector<SBChunkDelete>* chunk_deletes) { |
[email protected] | 4d3b42a | 2010-10-07 05:00:29 | [diff] [blame] | 230 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 231 | DCHECK(enabled_); |
| 232 | safe_browsing_thread_->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( |
| 233 | this, &SafeBrowsingService::DeleteChunks, chunk_deletes)); |
| 234 | } |
| 235 | |
| 236 | void SafeBrowsingService::UpdateStarted() { |
[email protected] | 4d3b42a | 2010-10-07 05:00:29 | [diff] [blame] | 237 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 238 | DCHECK(enabled_); |
| 239 | DCHECK(!update_in_progress_); |
| 240 | update_in_progress_ = true; |
| 241 | safe_browsing_thread_->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( |
| 242 | this, &SafeBrowsingService::GetAllChunksFromDatabase)); |
| 243 | } |
| 244 | |
| 245 | void SafeBrowsingService::UpdateFinished(bool update_succeeded) { |
[email protected] | 4d3b42a | 2010-10-07 05:00:29 | [diff] [blame] | 246 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 247 | DCHECK(enabled_); |
| 248 | if (update_in_progress_) { |
| 249 | update_in_progress_ = false; |
| 250 | safe_browsing_thread_->message_loop()->PostTask(FROM_HERE, |
| 251 | NewRunnableMethod(this, |
| 252 | &SafeBrowsingService::DatabaseUpdateFinished, |
| 253 | update_succeeded)); |
| 254 | } |
| 255 | } |
| 256 | |
[email protected] | 894c4e8 | 2010-06-29 21:53:18 | [diff] [blame] | 257 | bool SafeBrowsingService::IsUpdateInProgress() const { |
[email protected] | 4d3b42a | 2010-10-07 05:00:29 | [diff] [blame] | 258 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | 894c4e8 | 2010-06-29 21:53:18 | [diff] [blame] | 259 | return update_in_progress_; |
| 260 | } |
| 261 | |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 262 | void SafeBrowsingService::OnBlockingPageDone( |
| 263 | const std::vector<UnsafeResource>& resources, |
| 264 | bool proceed) { |
| 265 | for (std::vector<UnsafeResource>::const_iterator iter = resources.begin(); |
| 266 | iter != resources.end(); ++iter) { |
| 267 | const UnsafeResource& resource = *iter; |
| 268 | NotifyClientBlockingComplete(resource.client, proceed); |
| 269 | |
| 270 | if (proceed) { |
| 271 | // Whitelist this domain and warning type for the given tab. |
| 272 | WhiteListedEntry entry; |
| 273 | entry.render_process_host_id = resource.render_process_host_id; |
| 274 | entry.render_view_id = resource.render_view_id; |
| 275 | entry.domain = net::RegistryControlledDomainService::GetDomainAndRegistry( |
| 276 | resource.url); |
| 277 | entry.result = resource.threat_type; |
| 278 | white_listed_entries_.push_back(entry); |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | void SafeBrowsingService::OnNewMacKeys(const std::string& client_key, |
| 284 | const std::string& wrapped_key) { |
| 285 | PrefService* prefs = g_browser_process->local_state(); |
| 286 | if (prefs) { |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 287 | prefs->SetString(prefs::kSafeBrowsingClientKey, client_key); |
| 288 | prefs->SetString(prefs::kSafeBrowsingWrappedKey, wrapped_key); |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | |
| 292 | void SafeBrowsingService::OnEnable(bool enabled) { |
| 293 | if (enabled) |
| 294 | Start(); |
| 295 | else |
| 296 | ShutDown(); |
| 297 | } |
| 298 | |
| 299 | // static |
| 300 | void SafeBrowsingService::RegisterPrefs(PrefService* prefs) { |
[email protected] | 20ce516d | 2010-06-18 02:20:04 | [diff] [blame] | 301 | prefs->RegisterStringPref(prefs::kSafeBrowsingClientKey, ""); |
| 302 | prefs->RegisterStringPref(prefs::kSafeBrowsingWrappedKey, ""); |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 303 | } |
| 304 | |
[email protected] | cb2a67e | 2009-11-17 00:48:53 | [diff] [blame] | 305 | void SafeBrowsingService::CloseDatabase() { |
[email protected] | 4d3b42a | 2010-10-07 05:00:29 | [diff] [blame] | 306 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | cb2a67e | 2009-11-17 00:48:53 | [diff] [blame] | 307 | |
[email protected] | 03addd9 | 2009-11-17 21:38:10 | [diff] [blame] | 308 | // Cases to avoid: |
| 309 | // * If |closing_database_| is true, continuing will queue up a second |
| 310 | // request, |closing_database_| will be reset after handling the first |
| 311 | // request, and if any functions on the db thread recreate the database, we |
| 312 | // could start using it on the IO thread and then have the second request |
| 313 | // handler delete it out from under us. |
| 314 | // * If |database_| is NULL, then either no creation request is in flight, in |
| 315 | // which case we don't need to do anything, or one is in flight, in which |
| 316 | // case the database will be recreated before our deletion request is |
| 317 | // handled, and could be used on the IO thread in that time period, leading |
| 318 | // to the same problem as above. |
| 319 | // * If |queued_checks_| is non-empty and |database_| is non-NULL, we're |
| 320 | // about to be called back (in DatabaseLoadComplete()). This will call |
| 321 | // CheckUrl(), which will want the database. Closing the database here |
| 322 | // would lead to an infinite loop in DatabaseLoadComplete(), and even if it |
| 323 | // didn't, it would be pointless since we'd just want to recreate. |
| 324 | // |
[email protected] | 3409431 | 2009-12-03 21:40:26 | [diff] [blame] | 325 | // The first two cases above are handled by checking DatabaseAvailable(). |
| 326 | if (!DatabaseAvailable() || !queued_checks_.empty()) |
[email protected] | cb2a67e | 2009-11-17 00:48:53 | [diff] [blame] | 327 | return; |
| 328 | |
| 329 | closing_database_ = true; |
| 330 | if (safe_browsing_thread_.get()) { |
| 331 | safe_browsing_thread_->message_loop()->PostTask(FROM_HERE, |
| 332 | NewRunnableMethod(this, &SafeBrowsingService::OnCloseDatabase)); |
| 333 | } |
| 334 | } |
| 335 | |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 336 | void SafeBrowsingService::ResetDatabase() { |
[email protected] | 4d3b42a | 2010-10-07 05:00:29 | [diff] [blame] | 337 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | cb2a67e | 2009-11-17 00:48:53 | [diff] [blame] | 338 | DCHECK(enabled_); |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 339 | safe_browsing_thread_->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( |
| 340 | this, &SafeBrowsingService::OnResetDatabase)); |
| 341 | } |
| 342 | |
| 343 | void SafeBrowsingService::LogPauseDelay(TimeDelta time) { |
| 344 | UMA_HISTOGRAM_LONG_TIMES("SB2.Delay", time); |
| 345 | } |
| 346 | |
| 347 | SafeBrowsingService::~SafeBrowsingService() { |
[email protected] | cb2a67e | 2009-11-17 00:48:53 | [diff] [blame] | 348 | // We should have already been shut down. If we're still enabled, then the |
| 349 | // database isn't going to be closed properly, which could lead to corruption. |
| 350 | DCHECK(!enabled_); |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 351 | } |
| 352 | |
[email protected] | d11f566 | 2009-11-12 20:52:56 | [diff] [blame] | 353 | void SafeBrowsingService::OnIOInitialize( |
| 354 | const std::string& client_key, |
| 355 | const std::string& wrapped_key, |
| 356 | URLRequestContextGetter* request_context_getter) { |
[email protected] | 4d3b42a | 2010-10-07 05:00:29 | [diff] [blame] | 357 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 358 | enabled_ = true; |
[email protected] | cb2a67e | 2009-11-17 00:48:53 | [diff] [blame] | 359 | MakeDatabaseAvailable(); |
[email protected] | 1a87151 | 2009-11-06 06:11:18 | [diff] [blame] | 360 | |
| 361 | // On Windows, get the safe browsing client name from the browser |
| 362 | // distribution classes in installer util. These classes don't yet have |
| 363 | // an analog on non-Windows builds so just keep the name specified here. |
| 364 | #if defined(OS_WIN) |
| 365 | BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
| 366 | std::string client_name(dist->GetSafeBrowsingName()); |
| 367 | #else |
| 368 | #if defined(GOOGLE_CHROME_BUILD) |
| 369 | std::string client_name("googlechrome"); |
| 370 | #else |
| 371 | std::string client_name("chromium"); |
| 372 | #endif |
| 373 | #endif |
[email protected] | 894c4e8 | 2010-06-29 21:53:18 | [diff] [blame] | 374 | CommandLine* cmdline = CommandLine::ForCurrentProcess(); |
[email protected] | 6142deb | 2010-09-08 23:21:18 | [diff] [blame] | 375 | bool disable_auto_update = |
| 376 | cmdline->HasSwitch(switches::kSbDisableAutoUpdate) || |
| 377 | cmdline->HasSwitch(switches::kDisableBackgroundNetworking); |
[email protected] | 894c4e8 | 2010-06-29 21:53:18 | [diff] [blame] | 378 | std::string info_url_prefix = |
| 379 | cmdline->HasSwitch(switches::kSbInfoURLPrefix) ? |
| 380 | cmdline->GetSwitchValueASCII(switches::kSbInfoURLPrefix) : |
| 381 | kSbDefaultInfoURLPrefix; |
| 382 | std::string mackey_url_prefix = |
| 383 | cmdline->HasSwitch(switches::kSbMacKeyURLPrefix) ? |
| 384 | cmdline->GetSwitchValueASCII(switches::kSbMacKeyURLPrefix) : |
| 385 | kSbDefaultMacKeyURLPrefix; |
[email protected] | 1a87151 | 2009-11-06 06:11:18 | [diff] [blame] | 386 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 387 | protocol_manager_ = new SafeBrowsingProtocolManager(this, |
[email protected] | 1a87151 | 2009-11-06 06:11:18 | [diff] [blame] | 388 | client_name, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 389 | client_key, |
[email protected] | d11f566 | 2009-11-12 20:52:56 | [diff] [blame] | 390 | wrapped_key, |
[email protected] | 894c4e8 | 2010-06-29 21:53:18 | [diff] [blame] | 391 | request_context_getter, |
| 392 | info_url_prefix, |
| 393 | mackey_url_prefix, |
| 394 | disable_auto_update); |
[email protected] | d11f566 | 2009-11-12 20:52:56 | [diff] [blame] | 395 | |
[email protected] | dec173b | 2009-11-13 21:53:26 | [diff] [blame] | 396 | protocol_manager_->Initialize(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 397 | } |
| 398 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 399 | void SafeBrowsingService::OnIOShutdown() { |
[email protected] | 4d3b42a | 2010-10-07 05:00:29 | [diff] [blame] | 400 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 401 | if (!enabled_) |
| 402 | return; |
| 403 | |
| 404 | enabled_ = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 405 | |
| 406 | // This cancels all in-flight GetHash requests. |
| 407 | delete protocol_manager_; |
[email protected] | fbb2b7a | 2008-11-18 22:54:04 | [diff] [blame] | 408 | protocol_manager_ = NULL; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 409 | |
[email protected] | 03addd9 | 2009-11-17 21:38:10 | [diff] [blame] | 410 | // Delete queued checks, calling back any clients with 'URL_SAFE'. |
| 411 | // If we don't do this here we may fail to close the database below. |
| 412 | while (!queued_checks_.empty()) { |
| 413 | QueuedCheck check = queued_checks_.front(); |
| 414 | if (check.client) |
| 415 | check.client->OnUrlCheckResult(check.url, URL_SAFE); |
| 416 | queued_checks_.pop_front(); |
| 417 | } |
| 418 | |
[email protected] | cb2a67e | 2009-11-17 00:48:53 | [diff] [blame] | 419 | // Close the database. We don't simply DeleteSoon() because if a close is |
| 420 | // already pending, we'll double-free, and we don't set |database_| to NULL |
| 421 | // because if there is still anything running on the db thread, it could |
| 422 | // create a new database object (via GetDatabase()) that would then leak. |
| 423 | CloseDatabase(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 424 | |
| 425 | // Flush the database thread. Any in-progress database check results will be |
| 426 | // ignored and cleaned up below. |
[email protected] | cb2a67e | 2009-11-17 00:48:53 | [diff] [blame] | 427 | // |
| 428 | // Note that to avoid leaking the database, we rely on the fact that no new |
| 429 | // tasks will be added to the db thread between the call above and this one. |
| 430 | // See comments on the declaration of |safe_browsing_thread_|. |
| 431 | safe_browsing_thread_.reset(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 432 | |
[email protected] | 03addd9 | 2009-11-17 21:38:10 | [diff] [blame] | 433 | // Delete pending checks, calling back any clients with 'URL_SAFE'. We have |
| 434 | // to do this after the db thread returns because methods on it can have |
| 435 | // copies of these pointers, so deleting them might lead to accessing garbage. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 436 | for (CurrentChecks::iterator it = checks_.begin(); |
| 437 | it != checks_.end(); ++it) { |
| 438 | if ((*it)->client) |
| 439 | (*it)->client->OnUrlCheckResult((*it)->url, URL_SAFE); |
| 440 | delete *it; |
| 441 | } |
| 442 | checks_.clear(); |
| 443 | |
| 444 | gethash_requests_.clear(); |
| 445 | } |
| 446 | |
[email protected] | 3409431 | 2009-12-03 21:40:26 | [diff] [blame] | 447 | bool SafeBrowsingService::DatabaseAvailable() const { |
| 448 | AutoLock lock(database_lock_); |
| 449 | return !closing_database_ && (database_ != NULL); |
| 450 | } |
| 451 | |
[email protected] | cb2a67e | 2009-11-17 00:48:53 | [diff] [blame] | 452 | bool SafeBrowsingService::MakeDatabaseAvailable() { |
[email protected] | 4d3b42a | 2010-10-07 05:00:29 | [diff] [blame] | 453 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | cb2a67e | 2009-11-17 00:48:53 | [diff] [blame] | 454 | DCHECK(enabled_); |
[email protected] | 3409431 | 2009-12-03 21:40:26 | [diff] [blame] | 455 | if (DatabaseAvailable()) |
| 456 | return true; |
| 457 | safe_browsing_thread_->message_loop()->PostTask(FROM_HERE, |
| 458 | NewRunnableMethod(this, &SafeBrowsingService::GetDatabase)); |
| 459 | return false; |
[email protected] | cb2a67e | 2009-11-17 00:48:53 | [diff] [blame] | 460 | } |
| 461 | |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 462 | SafeBrowsingDatabase* SafeBrowsingService::GetDatabase() { |
[email protected] | ef5b482 | 2010-09-09 16:45:49 | [diff] [blame] | 463 | DCHECK_EQ(MessageLoop::current(), safe_browsing_thread_->message_loop()); |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 464 | if (database_) |
| 465 | return database_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 466 | |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 467 | FilePath path; |
| 468 | bool result = PathService::Get(chrome::DIR_USER_DATA, &path); |
| 469 | DCHECK(result); |
| 470 | path = path.Append(chrome::kSafeBrowsingFilename); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 471 | |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 472 | Time before = Time::Now(); |
| 473 | SafeBrowsingDatabase* database = SafeBrowsingDatabase::Create(); |
[email protected] | 7b1e3710 | 2010-03-08 21:43:16 | [diff] [blame] | 474 | database->Init(path); |
[email protected] | 3409431 | 2009-12-03 21:40:26 | [diff] [blame] | 475 | { |
| 476 | // Acquiring the lock here guarantees correct ordering between the writes to |
| 477 | // the new database object above, and the setting of |databse_| below. |
| 478 | AutoLock lock(database_lock_); |
| 479 | database_ = database; |
| 480 | } |
[email protected] | 613a03b | 2008-10-24 23:02:00 | [diff] [blame] | 481 | |
[email protected] | 4d3b42a | 2010-10-07 05:00:29 | [diff] [blame] | 482 | BrowserThread::PostTask( |
| 483 | BrowserThread::IO, FROM_HERE, |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 484 | NewRunnableMethod(this, &SafeBrowsingService::DatabaseLoadComplete)); |
[email protected] | 613a03b | 2008-10-24 23:02:00 | [diff] [blame] | 485 | |
[email protected] | cb2a67e | 2009-11-17 00:48:53 | [diff] [blame] | 486 | UMA_HISTOGRAM_TIMES("SB2.DatabaseOpen", Time::Now() - before); |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 487 | return database_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 488 | } |
| 489 | |
[email protected] | 613a03b | 2008-10-24 23:02:00 | [diff] [blame] | 490 | void SafeBrowsingService::OnCheckDone(SafeBrowsingCheck* check) { |
[email protected] | 4d3b42a | 2010-10-07 05:00:29 | [diff] [blame] | 491 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 492 | |
| 493 | // If we've been shutdown during the database lookup, this check will already |
| 494 | // have been deleted (in OnIOShutdown). |
[email protected] | 613a03b | 2008-10-24 23:02:00 | [diff] [blame] | 495 | if (!enabled_ || checks_.find(check) == checks_.end()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 496 | return; |
| 497 | |
[email protected] | 613a03b | 2008-10-24 23:02:00 | [diff] [blame] | 498 | if (check->client && check->need_get_hash) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 499 | // We have a partial match so we need to query Google for the full hash. |
| 500 | // Clean up will happen in HandleGetHashResults. |
| 501 | |
| 502 | // See if we have a GetHash request already in progress for this particular |
| 503 | // prefix. If so, we just append ourselves to the list of interested parties |
| 504 | // when the results arrive. We only do this for checks involving one prefix, |
| 505 | // since that is the common case (multiple prefixes will issue the request |
| 506 | // as normal). |
[email protected] | 613a03b | 2008-10-24 23:02:00 | [diff] [blame] | 507 | if (check->prefix_hits.size() == 1) { |
| 508 | SBPrefix prefix = check->prefix_hits[0]; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 509 | GetHashRequests::iterator it = gethash_requests_.find(prefix); |
| 510 | if (it != gethash_requests_.end()) { |
| 511 | // There's already a request in progress. |
[email protected] | 613a03b | 2008-10-24 23:02:00 | [diff] [blame] | 512 | it->second.push_back(check); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 513 | return; |
| 514 | } |
| 515 | |
| 516 | // No request in progress, so we're the first for this prefix. |
| 517 | GetHashRequestors requestors; |
[email protected] | 613a03b | 2008-10-24 23:02:00 | [diff] [blame] | 518 | requestors.push_back(check); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 519 | gethash_requests_[prefix] = requestors; |
| 520 | } |
| 521 | |
| 522 | // Reset the start time so that we can measure the network time without the |
| 523 | // database time. |
[email protected] | 613a03b | 2008-10-24 23:02:00 | [diff] [blame] | 524 | check->start = Time::Now(); |
| 525 | protocol_manager_->GetFullHash(check, check->prefix_hits); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 526 | } else { |
| 527 | // We may have cached results for previous GetHash queries. |
[email protected] | 613a03b | 2008-10-24 23:02:00 | [diff] [blame] | 528 | HandleOneCheck(check, check->full_hits); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 529 | } |
| 530 | } |
| 531 | |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 532 | void SafeBrowsingService::GetAllChunksFromDatabase() { |
[email protected] | ef5b482 | 2010-09-09 16:45:49 | [diff] [blame] | 533 | DCHECK_EQ(MessageLoop::current(), safe_browsing_thread_->message_loop()); |
| 534 | |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 535 | bool database_error = true; |
| 536 | std::vector<SBListChunkRanges> lists; |
[email protected] | ef5b482 | 2010-09-09 16:45:49 | [diff] [blame] | 537 | DCHECK(!database_update_in_progress_); |
| 538 | database_update_in_progress_ = true; |
[email protected] | cb2a67e | 2009-11-17 00:48:53 | [diff] [blame] | 539 | GetDatabase(); // This guarantees that |database_| is non-NULL. |
[email protected] | c2316134 | 2010-08-18 20:34:04 | [diff] [blame] | 540 | if (database_->UpdateStarted(&lists)) { |
[email protected] | cb2a67e | 2009-11-17 00:48:53 | [diff] [blame] | 541 | database_error = false; |
| 542 | } else { |
| 543 | database_->UpdateFinished(false); |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 544 | } |
[email protected] | 613a03b | 2008-10-24 23:02:00 | [diff] [blame] | 545 | |
[email protected] | 4d3b42a | 2010-10-07 05:00:29 | [diff] [blame] | 546 | BrowserThread::PostTask( |
| 547 | BrowserThread::IO, FROM_HERE, |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 548 | NewRunnableMethod( |
| 549 | this, &SafeBrowsingService::OnGetAllChunksFromDatabase, lists, |
| 550 | database_error)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 551 | } |
| 552 | |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 553 | void SafeBrowsingService::OnGetAllChunksFromDatabase( |
| 554 | const std::vector<SBListChunkRanges>& lists, bool database_error) { |
[email protected] | 4d3b42a | 2010-10-07 05:00:29 | [diff] [blame] | 555 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 556 | if (enabled_) |
| 557 | protocol_manager_->OnGetChunksComplete(lists, database_error); |
| 558 | } |
| 559 | |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 560 | void SafeBrowsingService::OnChunkInserted() { |
[email protected] | 4d3b42a | 2010-10-07 05:00:29 | [diff] [blame] | 561 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 562 | if (enabled_) |
| 563 | protocol_manager_->OnChunkInserted(); |
| 564 | } |
| 565 | |
| 566 | void SafeBrowsingService::DatabaseLoadComplete() { |
[email protected] | 4d3b42a | 2010-10-07 05:00:29 | [diff] [blame] | 567 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 568 | if (!enabled_) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 569 | return; |
| 570 | |
[email protected] | 03addd9 | 2009-11-17 21:38:10 | [diff] [blame] | 571 | HISTOGRAM_COUNTS("SB.QueueDepth", queued_checks_.size()); |
| 572 | if (queued_checks_.empty()) |
| 573 | return; |
| 574 | |
| 575 | // If the database isn't already available, calling CheckUrl() in the loop |
| 576 | // below will add the check back to the queue, and we'll infinite-loop. |
[email protected] | 3409431 | 2009-12-03 21:40:26 | [diff] [blame] | 577 | DCHECK(DatabaseAvailable()); |
[email protected] | 03addd9 | 2009-11-17 21:38:10 | [diff] [blame] | 578 | while (!queued_checks_.empty()) { |
| 579 | QueuedCheck check = queued_checks_.front(); |
| 580 | HISTOGRAM_TIMES("SB.QueueDelay", Time::Now() - check.start); |
| 581 | // If CheckUrl() determines the URL is safe immediately, it doesn't call the |
| 582 | // client's handler function (because normally it's being directly called by |
| 583 | // the client). Since we're not the client, we have to convey this result. |
| 584 | if (check.client && CheckUrl(check.url, check.client)) |
| 585 | check.client->OnUrlCheckResult(check.url, URL_SAFE); |
| 586 | queued_checks_.pop_front(); |
| 587 | } |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 588 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 589 | |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 590 | void SafeBrowsingService::HandleChunkForDatabase( |
| 591 | const std::string& list_name, |
[email protected] | 7b1e3710 | 2010-03-08 21:43:16 | [diff] [blame] | 592 | SBChunkList* chunks) { |
[email protected] | ef5b482 | 2010-09-09 16:45:49 | [diff] [blame] | 593 | DCHECK_EQ(MessageLoop::current(), safe_browsing_thread_->message_loop()); |
[email protected] | 7b1e3710 | 2010-03-08 21:43:16 | [diff] [blame] | 594 | if (chunks) { |
| 595 | GetDatabase()->InsertChunks(list_name, *chunks); |
| 596 | delete chunks; |
| 597 | } |
[email protected] | 4d3b42a | 2010-10-07 05:00:29 | [diff] [blame] | 598 | BrowserThread::PostTask( |
| 599 | BrowserThread::IO, FROM_HERE, |
[email protected] | 7b1e3710 | 2010-03-08 21:43:16 | [diff] [blame] | 600 | NewRunnableMethod(this, &SafeBrowsingService::OnChunkInserted)); |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | void SafeBrowsingService::DeleteChunks( |
| 604 | std::vector<SBChunkDelete>* chunk_deletes) { |
[email protected] | ef5b482 | 2010-09-09 16:45:49 | [diff] [blame] | 605 | DCHECK_EQ(MessageLoop::current(), safe_browsing_thread_->message_loop()); |
[email protected] | 7b1e3710 | 2010-03-08 21:43:16 | [diff] [blame] | 606 | if (chunk_deletes) { |
| 607 | GetDatabase()->DeleteChunks(*chunk_deletes); |
| 608 | delete chunk_deletes; |
| 609 | } |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | SafeBrowsingService::UrlCheckResult SafeBrowsingService::GetResultFromListname( |
| 613 | const std::string& list_name) { |
| 614 | if (safe_browsing_util::IsPhishingList(list_name)) { |
| 615 | return URL_PHISHING; |
[email protected] | 613a03b | 2008-10-24 23:02:00 | [diff] [blame] | 616 | } |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 617 | |
| 618 | if (safe_browsing_util::IsMalwareList(list_name)) { |
| 619 | return URL_MALWARE; |
| 620 | } |
| 621 | |
| 622 | SB_DLOG(INFO) << "Unknown safe browsing list " << list_name; |
| 623 | return URL_SAFE; |
| 624 | } |
| 625 | |
| 626 | void SafeBrowsingService::NotifyClientBlockingComplete(Client* client, |
| 627 | bool proceed) { |
| 628 | client->OnBlockingPageComplete(proceed); |
| 629 | } |
| 630 | |
| 631 | void SafeBrowsingService::DatabaseUpdateFinished(bool update_succeeded) { |
[email protected] | ef5b482 | 2010-09-09 16:45:49 | [diff] [blame] | 632 | DCHECK_EQ(MessageLoop::current(), safe_browsing_thread_->message_loop()); |
[email protected] | cb2a67e | 2009-11-17 00:48:53 | [diff] [blame] | 633 | GetDatabase()->UpdateFinished(update_succeeded); |
[email protected] | ef5b482 | 2010-09-09 16:45:49 | [diff] [blame] | 634 | DCHECK(database_update_in_progress_); |
| 635 | database_update_in_progress_ = false; |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | void SafeBrowsingService::Start() { |
| 639 | DCHECK(!safe_browsing_thread_.get()); |
| 640 | safe_browsing_thread_.reset(new base::Thread("Chrome_SafeBrowsingThread")); |
| 641 | if (!safe_browsing_thread_->Start()) |
| 642 | return; |
| 643 | |
| 644 | // Retrieve client MAC keys. |
| 645 | PrefService* local_state = g_browser_process->local_state(); |
| 646 | std::string client_key, wrapped_key; |
| 647 | if (local_state) { |
| 648 | client_key = |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 649 | local_state->GetString(prefs::kSafeBrowsingClientKey); |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 650 | wrapped_key = |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 651 | local_state->GetString(prefs::kSafeBrowsingWrappedKey); |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | // We will issue network fetches using the default profile's request context. |
[email protected] | 41a95c4 | 2010-10-20 15:49:15 | [diff] [blame] | 655 | scoped_refptr<URLRequestContextGetter> request_context_getter = |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 656 | GetDefaultProfile()->GetRequestContext(); |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 657 | |
[email protected] | 4d3b42a | 2010-10-07 05:00:29 | [diff] [blame] | 658 | BrowserThread::PostTask( |
| 659 | BrowserThread::IO, FROM_HERE, |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 660 | NewRunnableMethod( |
[email protected] | 0d7e79fa | 2010-10-08 23:35:47 | [diff] [blame] | 661 | this, &SafeBrowsingService::OnIOInitialize, client_key, wrapped_key, |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 662 | request_context_getter)); |
[email protected] | cb2a67e | 2009-11-17 00:48:53 | [diff] [blame] | 663 | } |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 664 | |
[email protected] | cb2a67e | 2009-11-17 00:48:53 | [diff] [blame] | 665 | void SafeBrowsingService::OnCloseDatabase() { |
[email protected] | ef5b482 | 2010-09-09 16:45:49 | [diff] [blame] | 666 | DCHECK_EQ(MessageLoop::current(), safe_browsing_thread_->message_loop()); |
[email protected] | a3aaf3d | 2009-11-17 22:12:39 | [diff] [blame] | 667 | DCHECK(closing_database_); |
[email protected] | cb2a67e | 2009-11-17 00:48:53 | [diff] [blame] | 668 | |
| 669 | // Because |closing_database_| is true, nothing on the IO thread will be |
| 670 | // accessing the database, so it's safe to delete and then NULL the pointer. |
| 671 | delete database_; |
| 672 | database_ = NULL; |
[email protected] | 3409431 | 2009-12-03 21:40:26 | [diff] [blame] | 673 | |
| 674 | // Acquiring the lock here guarantees correct ordering between the resetting |
| 675 | // of |database_| above and of |closing_database_| below, which ensures there |
| 676 | // won't be a window during which the IO thread falsely believes the database |
| 677 | // is available. |
| 678 | AutoLock lock(database_lock_); |
[email protected] | cb2a67e | 2009-11-17 00:48:53 | [diff] [blame] | 679 | closing_database_ = false; |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | void SafeBrowsingService::OnResetDatabase() { |
[email protected] | ef5b482 | 2010-09-09 16:45:49 | [diff] [blame] | 683 | DCHECK_EQ(MessageLoop::current(), safe_browsing_thread_->message_loop()); |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 684 | GetDatabase()->ResetDatabase(); |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | void SafeBrowsingService::CacheHashResults( |
| 688 | const std::vector<SBPrefix>& prefixes, |
| 689 | const std::vector<SBFullHashResult>& full_hashes) { |
[email protected] | ef5b482 | 2010-09-09 16:45:49 | [diff] [blame] | 690 | DCHECK_EQ(MessageLoop::current(), safe_browsing_thread_->message_loop()); |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 691 | GetDatabase()->CacheHashResults(prefixes, full_hashes); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | void SafeBrowsingService::OnHandleGetHashResults( |
| 695 | SafeBrowsingCheck* check, |
| 696 | const std::vector<SBFullHashResult>& full_hashes) { |
[email protected] | 4d3b42a | 2010-10-07 05:00:29 | [diff] [blame] | 697 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 698 | SBPrefix prefix = check->prefix_hits[0]; |
| 699 | GetHashRequests::iterator it = gethash_requests_.find(prefix); |
| 700 | if (check->prefix_hits.size() > 1 || it == gethash_requests_.end()) { |
| 701 | HandleOneCheck(check, full_hashes); |
| 702 | return; |
| 703 | } |
| 704 | |
| 705 | // Call back all interested parties. |
| 706 | GetHashRequestors& requestors = it->second; |
| 707 | for (GetHashRequestors::iterator r = requestors.begin(); |
| 708 | r != requestors.end(); ++r) { |
| 709 | HandleOneCheck(*r, full_hashes); |
| 710 | } |
| 711 | |
| 712 | gethash_requests_.erase(it); |
| 713 | } |
| 714 | |
| 715 | void SafeBrowsingService::HandleOneCheck( |
| 716 | SafeBrowsingCheck* check, |
| 717 | const std::vector<SBFullHashResult>& full_hashes) { |
[email protected] | 4d3b42a | 2010-10-07 05:00:29 | [diff] [blame] | 718 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 719 | if (check->client) { |
| 720 | UrlCheckResult result = URL_SAFE; |
| 721 | int index = safe_browsing_util::CompareFullHashes(check->url, full_hashes); |
[email protected] | 09d985f | 2009-05-11 21:59:58 | [diff] [blame] | 722 | if (index != -1) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 723 | result = GetResultFromListname(full_hashes[index].list_name); |
[email protected] | 09d985f | 2009-05-11 21:59:58 | [diff] [blame] | 724 | } else { |
| 725 | // Log the case where the SafeBrowsing servers return full hashes in the |
| 726 | // GetHash response that match the prefix we're looking up, but don't |
| 727 | // match the full hash of the URL. |
| 728 | if (!full_hashes.empty()) |
| 729 | UMA_HISTOGRAM_COUNTS("SB2.GetHashServerMiss", 1); |
| 730 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 731 | |
| 732 | // Let the client continue handling the original request. |
| 733 | check->client->OnUrlCheckResult(check->url, result); |
| 734 | } |
| 735 | |
| 736 | checks_.erase(check); |
| 737 | delete check; |
| 738 | } |
| 739 | |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 740 | void SafeBrowsingService::DoDisplayBlockingPage( |
| 741 | const UnsafeResource& resource) { |
| 742 | // The tab might have been closed. |
| 743 | TabContents* wc = |
| 744 | tab_util::GetTabContentsByID(resource.render_process_host_id, |
| 745 | resource.render_view_id); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 746 | |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 747 | if (!wc) { |
| 748 | // The tab is gone and we did not have a chance at showing the interstitial. |
| 749 | // Just act as "Don't Proceed" was chosen. |
| 750 | std::vector<UnsafeResource> resources; |
| 751 | resources.push_back(resource); |
[email protected] | 4d3b42a | 2010-10-07 05:00:29 | [diff] [blame] | 752 | BrowserThread::PostTask( |
| 753 | BrowserThread::IO, FROM_HERE, |
[email protected] | d83d03aa | 2009-11-02 21:44:37 | [diff] [blame] | 754 | NewRunnableMethod( |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 755 | this, &SafeBrowsingService::OnBlockingPageDone, resources, false)); |
| 756 | return; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 757 | } |
| 758 | |
[email protected] | 036237d4 | 2010-10-07 22:41:58 | [diff] [blame] | 759 | // Report the malicious resource to the SafeBrowsing servers if the user has |
| 760 | // opted in to reporting statistics. |
[email protected] | 975bead | 2009-11-30 21:59:53 | [diff] [blame] | 761 | const MetricsService* metrics = g_browser_process->metrics_service(); |
| 762 | DCHECK(metrics); |
| 763 | if (metrics && metrics->reporting_active() && |
[email protected] | 036237d4 | 2010-10-07 22:41:58 | [diff] [blame] | 764 | (resource.threat_type == SafeBrowsingService::URL_MALWARE || |
| 765 | resource.threat_type == SafeBrowsingService::URL_PHISHING)) { |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 766 | GURL page_url = wc->GetURL(); |
| 767 | GURL referrer_url; |
| 768 | NavigationEntry* entry = wc->controller().GetActiveEntry(); |
| 769 | if (entry) |
| 770 | referrer_url = entry->referrer(); |
[email protected] | 293f1925 | 2010-08-18 20:01:18 | [diff] [blame] | 771 | bool is_subresource = resource.resource_type != ResourceType::MAIN_FRAME; |
[email protected] | ee4d3e80 | 2010-08-12 23:59:51 | [diff] [blame] | 772 | |
[email protected] | 293f1925 | 2010-08-18 20:01:18 | [diff] [blame] | 773 | // When the malicious url is on the main frame, and resource.original_url |
| 774 | // is not the same as the resource.url, that means we have a redirect from |
| 775 | // resource.original_url to resource.url. |
| 776 | // Also, at this point, page_url points to the _previous_ page that we |
| 777 | // were on. We replace page_url with resource.original_url and referrer |
| 778 | // with page_url. |
| 779 | if (!is_subresource && |
| 780 | !resource.original_url.is_empty() && |
| 781 | resource.original_url != resource.url) { |
| 782 | referrer_url = page_url; |
| 783 | page_url = resource.original_url; |
| 784 | } |
| 785 | |
[email protected] | 036237d4 | 2010-10-07 22:41:58 | [diff] [blame] | 786 | BrowserThread::PostTask( |
| 787 | BrowserThread::IO, FROM_HERE, |
| 788 | NewRunnableMethod( |
| 789 | this, |
| 790 | &SafeBrowsingService::ReportSafeBrowsingHit, |
| 791 | resource.url, |
| 792 | page_url, |
| 793 | referrer_url, |
| 794 | is_subresource, |
| 795 | resource.threat_type)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 796 | } |
| 797 | |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 798 | SafeBrowsingBlockingPage::ShowBlockingPage(this, resource); |
[email protected] | 613a03b | 2008-10-24 23:02:00 | [diff] [blame] | 799 | } |
| 800 | |
[email protected] | 036237d4 | 2010-10-07 22:41:58 | [diff] [blame] | 801 | void SafeBrowsingService::ReportSafeBrowsingHit( |
| 802 | const GURL& malicious_url, |
| 803 | const GURL& page_url, |
| 804 | const GURL& referrer_url, |
| 805 | bool is_subresource, |
| 806 | SafeBrowsingService::UrlCheckResult threat_type) { |
[email protected] | 4d3b42a | 2010-10-07 05:00:29 | [diff] [blame] | 807 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | fd220e60 | 2009-11-14 01:02:37 | [diff] [blame] | 808 | if (!enabled_) |
[email protected] | dfdb0de7 | 2009-02-19 21:58:14 | [diff] [blame] | 809 | return; |
| 810 | |
[email protected] | 036237d4 | 2010-10-07 22:41:58 | [diff] [blame] | 811 | DLOG(INFO) << "ReportSafeBrowsingHit: " << malicious_url << " " << page_url |
| 812 | << " " << referrer_url << " " << is_subresource |
| 813 | << " " << threat_type; |
| 814 | protocol_manager_->ReportSafeBrowsingHit(malicious_url, page_url, |
| 815 | referrer_url, is_subresource, |
| 816 | threat_type); |
[email protected] | dfdb0de7 | 2009-02-19 21:58:14 | [diff] [blame] | 817 | } |