Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(319)

Side by Side Diff: chrome/browser/safe_browsing/safe_browsing_service.cc

Issue 7134017: Make safe browsing work in a multi-profile environment. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: respond to mattm's comment Created 9 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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-inl.h" 11 #include "base/stl_util-inl.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_service.h" 16 #include "chrome/browser/prefs/pref_service.h"
17 #include "chrome/browser/profiles/profile_manager.h" 17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/safe_browsing/malware_details.h" 18 #include "chrome/browser/safe_browsing/malware_details.h"
19 #include "chrome/browser/safe_browsing/protocol_manager.h" 19 #include "chrome/browser/safe_browsing/protocol_manager.h"
20 #include "chrome/browser/safe_browsing/safe_browsing_blocking_page.h" 20 #include "chrome/browser/safe_browsing/safe_browsing_blocking_page.h"
21 #include "chrome/browser/safe_browsing/safe_browsing_database.h" 21 #include "chrome/browser/safe_browsing/safe_browsing_database.h"
22 #include "chrome/browser/tab_contents/tab_util.h" 22 #include "chrome/browser/tab_contents/tab_util.h"
23 #include "chrome/common/chrome_constants.h" 23 #include "chrome/common/chrome_constants.h"
24 #include "chrome/common/chrome_paths.h" 24 #include "chrome/common/chrome_paths.h"
25 #include "chrome/common/chrome_switches.h" 25 #include "chrome/common/chrome_switches.h"
26 #include "chrome/common/pref_names.h" 26 #include "chrome/common/pref_names.h"
27 #include "chrome/common/url_constants.h" 27 #include "chrome/common/url_constants.h"
(...skipping 18 matching lines...) Expand all
46 const char* const kSbDefaultMacKeyURLPrefix = 46 const char* const kSbDefaultMacKeyURLPrefix =
47 "https://sb-ssl.google.com/safebrowsing"; 47 "https://sb-ssl.google.com/safebrowsing";
48 48
49 // When download url check takes this long, client's callback will be called 49 // When download url check takes this long, client's callback will be called
50 // without waiting for the result. 50 // without waiting for the result.
51 const int64 kDownloadUrlCheckTimeoutMs = 10000; 51 const int64 kDownloadUrlCheckTimeoutMs = 10000;
52 52
53 // Similar to kDownloadUrlCheckTimeoutMs, but for download hash checks. 53 // Similar to kDownloadUrlCheckTimeoutMs, but for download hash checks.
54 const int64 kDownloadHashCheckTimeoutMs = 10000; 54 const int64 kDownloadHashCheckTimeoutMs = 10000;
55 55
56 // TODO(lzheng): Replace this with Profile* ProfileManager::GetDefaultProfile().
57 Profile* GetDefaultProfile() {
58 FilePath user_data_dir;
59 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
60 ProfileManager* profile_manager = g_browser_process->profile_manager();
61 return profile_manager->GetDefaultProfile(user_data_dir);
62 }
63
64 // Records disposition information about the check. |hit| should be 56 // Records disposition information about the check. |hit| should be
65 // |true| if there were any prefix hits in |full_hashes|. 57 // |true| if there were any prefix hits in |full_hashes|.
66 void RecordGetHashCheckStatus( 58 void RecordGetHashCheckStatus(
67 bool hit, 59 bool hit,
68 bool is_download, 60 bool is_download,
69 const std::vector<SBFullHashResult>& full_hashes) { 61 const std::vector<SBFullHashResult>& full_hashes) {
70 SafeBrowsingProtocolManager::ResultType result; 62 SafeBrowsingProtocolManager::ResultType result;
71 if (full_hashes.empty()) { 63 if (full_hashes.empty()) {
72 result = SafeBrowsingProtocolManager::GET_HASH_FULL_HASH_EMPTY; 64 result = SafeBrowsingProtocolManager::GET_HASH_FULL_HASH_EMPTY;
73 } else if (hit) { 65 } else if (hit) {
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
164 enable_download_protection_(false), 156 enable_download_protection_(false),
165 enable_csd_whitelist_(false), 157 enable_csd_whitelist_(false),
166 update_in_progress_(false), 158 update_in_progress_(false),
167 database_update_in_progress_(false), 159 database_update_in_progress_(false),
168 closing_database_(false), 160 closing_database_(false),
169 download_urlcheck_timeout_ms_(kDownloadUrlCheckTimeoutMs), 161 download_urlcheck_timeout_ms_(kDownloadUrlCheckTimeoutMs),
170 download_hashcheck_timeout_ms_(kDownloadHashCheckTimeoutMs) { 162 download_hashcheck_timeout_ms_(kDownloadHashCheckTimeoutMs) {
171 } 163 }
172 164
173 void SafeBrowsingService::Initialize() { 165 void SafeBrowsingService::Initialize() {
174 // Get the profile's preference for SafeBrowsing. 166 // Always initialize the safe browsing service. Each profile will decide
175 PrefService* pref_service = GetDefaultProfile()->GetPrefs(); 167 // whether to use it based on per-user preferences.
176 if (pref_service->GetBoolean(prefs::kSafeBrowsingEnabled)) 168 Start();
177 Start();
178 } 169 }
179 170
180 void SafeBrowsingService::ShutDown() { 171 void SafeBrowsingService::ShutDown() {
181 BrowserThread::PostTask( 172 BrowserThread::PostTask(
182 BrowserThread::IO, FROM_HERE, 173 BrowserThread::IO, FROM_HERE,
183 NewRunnableMethod(this, &SafeBrowsingService::OnIOShutdown)); 174 NewRunnableMethod(this, &SafeBrowsingService::OnIOShutdown));
184 } 175 }
185 176
186 bool SafeBrowsingService::CanCheckUrl(const GURL& url) const { 177 bool SafeBrowsingService::CanCheckUrl(const GURL& url) const {
187 return url.SchemeIs(chrome::kFtpScheme) || 178 return url.SchemeIs(chrome::kFtpScheme) ||
188 url.SchemeIs(chrome::kHttpScheme) || 179 url.SchemeIs(chrome::kHttpScheme) ||
189 url.SchemeIs(chrome::kHttpsScheme); 180 url.SchemeIs(chrome::kHttpsScheme);
190 } 181 }
191 182
192 // Only report SafeBrowsing related stats when UMA is enabled and 183 // Only report SafeBrowsing related stats when UMA is enabled. User must also
193 // safe browsing is enabled. 184 // ensure that safe browsing is enabled from the calling profile.
194 bool SafeBrowsingService::CanReportStats() const { 185 bool SafeBrowsingService::CanReportStats() const {
195 const MetricsService* metrics = g_browser_process->metrics_service(); 186 const MetricsService* metrics = g_browser_process->metrics_service();
196 const PrefService* pref_service = GetDefaultProfile()->GetPrefs(); 187 return metrics && metrics->reporting_active();
197 return metrics && metrics->reporting_active() &&
198 pref_service && pref_service->GetBoolean(prefs::kSafeBrowsingEnabled);
199 } 188 }
200 189
201 // Binhash verification is only enabled for UMA users for now. 190 // Binhash verification is only enabled for UMA users for now.
202 bool SafeBrowsingService::DownloadBinHashNeeded() const { 191 bool SafeBrowsingService::DownloadBinHashNeeded() const {
203 return enable_download_protection_ && CanReportStats(); 192 return enable_download_protection_ && CanReportStats();
204 } 193 }
205 194
206 bool SafeBrowsingService::CheckDownloadUrl(const std::vector<GURL>& url_chain, 195 bool SafeBrowsingService::CheckDownloadUrl(const std::vector<GURL>& url_chain,
207 Client* client) { 196 Client* client) {
208 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 197 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
(...skipping 639 matching lines...) Expand 10 before | Expand all | Expand 10 after
848 PrefService* local_state = g_browser_process->local_state(); 837 PrefService* local_state = g_browser_process->local_state();
849 DCHECK(local_state); 838 DCHECK(local_state);
850 std::string client_key, wrapped_key; 839 std::string client_key, wrapped_key;
851 if (local_state) { 840 if (local_state) {
852 client_key = 841 client_key =
853 local_state->GetString(prefs::kSafeBrowsingClientKey); 842 local_state->GetString(prefs::kSafeBrowsingClientKey);
854 wrapped_key = 843 wrapped_key =
855 local_state->GetString(prefs::kSafeBrowsingWrappedKey); 844 local_state->GetString(prefs::kSafeBrowsingWrappedKey);
856 } 845 }
857 846
858 // We will issue network fetches using the default profile's request context. 847 // We will issue network fetches using the system request context.
859 scoped_refptr<net::URLRequestContextGetter> request_context_getter( 848 scoped_refptr<net::URLRequestContextGetter> request_context_getter(
860 GetDefaultProfile()->GetRequestContext()); 849 g_browser_process->system_request_context());
861 850
862 CommandLine* cmdline = CommandLine::ForCurrentProcess(); 851 CommandLine* cmdline = CommandLine::ForCurrentProcess();
863 enable_download_protection_ = 852 enable_download_protection_ =
864 !cmdline->HasSwitch(switches::kSbDisableDownloadProtection); 853 !cmdline->HasSwitch(switches::kSbDisableDownloadProtection);
865 854
866 // We only download the csd-whitelist if client-side phishing detection is 855 // We only download the csd-whitelist if client-side phishing detection is
867 // enabled and if the user has opted in with stats collection. Note: we 856 // enabled and if the user has opted in with stats collection. Note: we
868 // cannot check whether the metrics_service() object is created because it 857 // cannot check whether the metrics_service() object is created because it
869 // may be initialized after this method is called. 858 // may be initialized after this method is called.
870 #ifdef OS_CHROMEOS 859 #ifdef OS_CHROMEOS
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
975 if (IsWhitelisted(resource)) { 964 if (IsWhitelisted(resource)) {
976 BrowserThread::PostTask( 965 BrowserThread::PostTask(
977 BrowserThread::IO, FROM_HERE, 966 BrowserThread::IO, FROM_HERE,
978 NewRunnableMethod(this, 967 NewRunnableMethod(this,
979 &SafeBrowsingService::NotifyClientBlockingComplete, 968 &SafeBrowsingService::NotifyClientBlockingComplete,
980 resource.client, true)); 969 resource.client, true));
981 return; 970 return;
982 } 971 }
983 972
984 // The tab might have been closed. 973 // The tab might have been closed.
985 TabContents* wc = 974 TabContents* tab_contents =
986 tab_util::GetTabContentsByID(resource.render_process_host_id, 975 tab_util::GetTabContentsByID(resource.render_process_host_id,
987 resource.render_view_id); 976 resource.render_view_id);
988 977
989 if (!wc) { 978 if (!tab_contents) {
990 // The tab is gone and we did not have a chance at showing the interstitial. 979 // The tab is gone and we did not have a chance at showing the interstitial.
991 // Just act as "Don't Proceed" was chosen. 980 // Just act as if "Don't Proceed" were chosen.
992 std::vector<UnsafeResource> resources; 981 std::vector<UnsafeResource> resources;
993 resources.push_back(resource); 982 resources.push_back(resource);
994 BrowserThread::PostTask( 983 BrowserThread::PostTask(
995 BrowserThread::IO, FROM_HERE, 984 BrowserThread::IO, FROM_HERE,
996 NewRunnableMethod( 985 NewRunnableMethod(
997 this, &SafeBrowsingService::OnBlockingPageDone, resources, false)); 986 this, &SafeBrowsingService::OnBlockingPageDone, resources, false));
998 return; 987 return;
999 } 988 }
1000 989
1001 if (resource.threat_type != SafeBrowsingService::SAFE && CanReportStats()) { 990 if (resource.threat_type != SafeBrowsingService::SAFE &&
1002 GURL page_url = wc->GetURL(); 991 CanReportStats()) {
992 GURL page_url = tab_contents->GetURL();
1003 GURL referrer_url; 993 GURL referrer_url;
1004 NavigationEntry* entry = wc->controller().GetActiveEntry(); 994 NavigationEntry* entry = tab_contents->controller().GetActiveEntry();
1005 if (entry) 995 if (entry)
1006 referrer_url = entry->referrer(); 996 referrer_url = entry->referrer();
1007 997
1008 // When the malicious url is on the main frame, and resource.original_url 998 // When the malicious url is on the main frame, and resource.original_url
1009 // is not the same as the resource.url, that means we have a redirect from 999 // is not the same as the resource.url, that means we have a redirect from
1010 // resource.original_url to resource.url. 1000 // resource.original_url to resource.url.
1011 // Also, at this point, page_url points to the _previous_ page that we 1001 // Also, at this point, page_url points to the _previous_ page that we
1012 // were on. We replace page_url with resource.original_url and referrer 1002 // were on. We replace page_url with resource.original_url and referrer
1013 // with page_url. 1003 // with page_url.
1014 if (!resource.is_subresource && 1004 if (!resource.is_subresource &&
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after
1220 (entry.result == CLIENT_SIDE_PHISHING_URL && 1210 (entry.result == CLIENT_SIDE_PHISHING_URL &&
1221 resource.threat_type == URL_PHISHING)) && 1211 resource.threat_type == URL_PHISHING)) &&
1222 entry.domain == 1212 entry.domain ==
1223 net::RegistryControlledDomainService::GetDomainAndRegistry( 1213 net::RegistryControlledDomainService::GetDomainAndRegistry(
1224 resource.url)) { 1214 resource.url)) {
1225 return true; 1215 return true;
1226 } 1216 }
1227 } 1217 }
1228 return false; 1218 return false;
1229 } 1219 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698