| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 | 5 |
| 6 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 6 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
| 7 | 7 |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/histogram.h" | 9 #include "base/histogram.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
| 12 #include "base/path_service.h" | 12 #include "base/path_service.h" |
| 13 #include "base/string_util.h" | 13 #include "base/string_util.h" |
| 14 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
| 15 #include "chrome/browser/chrome_thread.h" | 15 #include "chrome/browser/chrome_thread.h" |
| 16 #include "chrome/browser/net/url_request_context_getter.h" |
| 16 #include "chrome/browser/profile_manager.h" | 17 #include "chrome/browser/profile_manager.h" |
| 17 #include "chrome/browser/safe_browsing/protocol_manager.h" | 18 #include "chrome/browser/safe_browsing/protocol_manager.h" |
| 18 #include "chrome/browser/safe_browsing/safe_browsing_blocking_page.h" | 19 #include "chrome/browser/safe_browsing/safe_browsing_blocking_page.h" |
| 19 #include "chrome/browser/safe_browsing/safe_browsing_database.h" | 20 #include "chrome/browser/safe_browsing/safe_browsing_database.h" |
| 20 #include "chrome/browser/tab_contents/navigation_entry.h" | 21 #include "chrome/browser/tab_contents/navigation_entry.h" |
| 21 #include "chrome/browser/tab_contents/tab_util.h" | 22 #include "chrome/browser/tab_contents/tab_util.h" |
| 22 #include "chrome/browser/tab_contents/tab_contents.h" | 23 #include "chrome/browser/tab_contents/tab_contents.h" |
| 23 #include "chrome/common/chrome_constants.h" | 24 #include "chrome/common/chrome_constants.h" |
| 24 #include "chrome/common/chrome_paths.h" | 25 #include "chrome/common/chrome_paths.h" |
| 25 #include "chrome/common/pref_names.h" | 26 #include "chrome/common/pref_names.h" |
| 26 #include "chrome/common/pref_service.h" | 27 #include "chrome/common/pref_service.h" |
| 27 #include "chrome/common/url_constants.h" | 28 #include "chrome/common/url_constants.h" |
| 28 #if defined(OS_WIN) | 29 #if defined(OS_WIN) |
| 29 #include "chrome/installer/util/browser_distribution.h" | 30 #include "chrome/installer/util/browser_distribution.h" |
| 30 #endif | 31 #endif |
| 31 #include "net/base/registry_controlled_domain.h" | 32 #include "net/base/registry_controlled_domain.h" |
| 32 | 33 |
| 33 using base::Time; | 34 using base::Time; |
| 34 using base::TimeDelta; | 35 using base::TimeDelta; |
| 35 | 36 |
| 37 static Profile* GetDefaultProfile() { |
| 38 FilePath user_data_dir; |
| 39 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); |
| 40 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 41 return profile_manager->GetDefaultProfile(user_data_dir); |
| 42 } |
| 43 |
| 36 SafeBrowsingService::SafeBrowsingService() | 44 SafeBrowsingService::SafeBrowsingService() |
| 37 : database_(NULL), | 45 : database_(NULL), |
| 38 protocol_manager_(NULL), | 46 protocol_manager_(NULL), |
| 39 enabled_(false), | 47 enabled_(false), |
| 40 resetting_(false), | 48 resetting_(false), |
| 41 database_loaded_(false), | 49 database_loaded_(false), |
| 42 update_in_progress_(false) { | 50 update_in_progress_(false) { |
| 43 } | 51 } |
| 44 | 52 |
| 45 SafeBrowsingService::~SafeBrowsingService() { | 53 SafeBrowsingService::~SafeBrowsingService() { |
| 46 } | 54 } |
| 47 | 55 |
| 48 // Only called on the UI thread. | 56 // Only called on the UI thread. |
| 49 void SafeBrowsingService::Initialize() { | 57 void SafeBrowsingService::Initialize() { |
| 50 // Get the profile's preference for SafeBrowsing. | 58 // Get the profile's preference for SafeBrowsing. |
| 51 FilePath user_data_dir; | 59 PrefService* pref_service = GetDefaultProfile()->GetPrefs(); |
| 52 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); | |
| 53 ProfileManager* profile_manager = g_browser_process->profile_manager(); | |
| 54 Profile* profile = profile_manager->GetDefaultProfile(user_data_dir); | |
| 55 PrefService* pref_service = profile->GetPrefs(); | |
| 56 if (pref_service->GetBoolean(prefs::kSafeBrowsingEnabled)) | 60 if (pref_service->GetBoolean(prefs::kSafeBrowsingEnabled)) |
| 57 Start(); | 61 Start(); |
| 58 } | 62 } |
| 59 | 63 |
| 60 // Start up SafeBrowsing objects. This can be called at browser start, or when | 64 // Start up SafeBrowsing objects. This can be called at browser start, or when |
| 61 // the user checks the "Enable SafeBrowsing" option in the Advanced options UI. | 65 // the user checks the "Enable SafeBrowsing" option in the Advanced options UI. |
| 62 void SafeBrowsingService::Start() { | 66 void SafeBrowsingService::Start() { |
| 63 DCHECK(!safe_browsing_thread_.get()); | 67 DCHECK(!safe_browsing_thread_.get()); |
| 64 safe_browsing_thread_.reset(new base::Thread("Chrome_SafeBrowsingThread")); | 68 safe_browsing_thread_.reset(new base::Thread("Chrome_SafeBrowsingThread")); |
| 65 if (!safe_browsing_thread_->Start()) | 69 if (!safe_browsing_thread_->Start()) |
| 66 return; | 70 return; |
| 67 | 71 |
| 68 // Retrieve client MAC keys. | 72 // Retrieve client MAC keys. |
| 69 PrefService* local_state = g_browser_process->local_state(); | 73 PrefService* local_state = g_browser_process->local_state(); |
| 70 std::string client_key, wrapped_key; | 74 std::string client_key, wrapped_key; |
| 71 if (local_state) { | 75 if (local_state) { |
| 72 client_key = | 76 client_key = |
| 73 WideToASCII(local_state->GetString(prefs::kSafeBrowsingClientKey)); | 77 WideToASCII(local_state->GetString(prefs::kSafeBrowsingClientKey)); |
| 74 wrapped_key = | 78 wrapped_key = |
| 75 WideToASCII(local_state->GetString(prefs::kSafeBrowsingWrappedKey)); | 79 WideToASCII(local_state->GetString(prefs::kSafeBrowsingWrappedKey)); |
| 76 } | 80 } |
| 77 | 81 |
| 82 // We will issue network fetches using the default profile's request context. |
| 83 URLRequestContextGetter* request_context_getter = |
| 84 GetDefaultProfile()->GetRequestContext(); |
| 85 request_context_getter->AddRef(); // Balanced in OnIOInitialize. |
| 86 |
| 78 ChromeThread::PostTask( | 87 ChromeThread::PostTask( |
| 79 ChromeThread::IO, FROM_HERE, | 88 ChromeThread::IO, FROM_HERE, |
| 80 NewRunnableMethod( | 89 NewRunnableMethod( |
| 81 this, &SafeBrowsingService::OnIOInitialize, client_key, wrapped_key)); | 90 this, &SafeBrowsingService::OnIOInitialize, client_key, wrapped_key, |
| 91 request_context_getter)); |
| 82 | 92 |
| 83 safe_browsing_thread_->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( | 93 safe_browsing_thread_->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( |
| 84 this, &SafeBrowsingService::OnDBInitialize)); | 94 this, &SafeBrowsingService::OnDBInitialize)); |
| 85 } | 95 } |
| 86 | 96 |
| 87 void SafeBrowsingService::ShutDown() { | 97 void SafeBrowsingService::ShutDown() { |
| 88 ChromeThread::PostTask( | 98 ChromeThread::PostTask( |
| 89 ChromeThread::IO, FROM_HERE, | 99 ChromeThread::IO, FROM_HERE, |
| 90 NewRunnableMethod(this, &SafeBrowsingService::OnIOShutdown)); | 100 NewRunnableMethod(this, &SafeBrowsingService::OnIOShutdown)); |
| 91 } | 101 } |
| 92 | 102 |
| 93 void SafeBrowsingService::OnIOInitialize(const std::string& client_key, | 103 void SafeBrowsingService::OnIOInitialize( |
| 94 const std::string& wrapped_key) { | 104 const std::string& client_key, |
| 105 const std::string& wrapped_key, |
| 106 URLRequestContextGetter* request_context_getter) { |
| 95 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); | 107 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); |
| 96 enabled_ = true; | 108 enabled_ = true; |
| 97 | 109 |
| 98 // On Windows, get the safe browsing client name from the browser | 110 // On Windows, get the safe browsing client name from the browser |
| 99 // distribution classes in installer util. These classes don't yet have | 111 // distribution classes in installer util. These classes don't yet have |
| 100 // an analog on non-Windows builds so just keep the name specified here. | 112 // an analog on non-Windows builds so just keep the name specified here. |
| 101 #if defined(OS_WIN) | 113 #if defined(OS_WIN) |
| 102 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); | 114 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
| 103 std::string client_name(dist->GetSafeBrowsingName()); | 115 std::string client_name(dist->GetSafeBrowsingName()); |
| 104 #else | 116 #else |
| 105 #if defined(GOOGLE_CHROME_BUILD) | 117 #if defined(GOOGLE_CHROME_BUILD) |
| 106 std::string client_name("googlechrome"); | 118 std::string client_name("googlechrome"); |
| 107 #else | 119 #else |
| 108 std::string client_name("chromium"); | 120 std::string client_name("chromium"); |
| 109 #endif | 121 #endif |
| 110 #endif | 122 #endif |
| 111 | 123 |
| 112 protocol_manager_ = new SafeBrowsingProtocolManager(this, | 124 protocol_manager_ = new SafeBrowsingProtocolManager(this, |
| 113 client_name, | 125 client_name, |
| 114 client_key, | 126 client_key, |
| 115 wrapped_key); | 127 wrapped_key, |
| 128 request_context_getter); |
| 129 |
| 130 // Balance the reference added by Start(). |
| 131 request_context_getter->Release(); |
| 132 |
| 116 // We want to initialize the protocol manager only after the database has | 133 // We want to initialize the protocol manager only after the database has |
| 117 // loaded, which we'll receive asynchronously (DatabaseLoadComplete). If | 134 // loaded, which we'll receive asynchronously (DatabaseLoadComplete). If |
| 118 // database_loaded_ isn't true, we'll wait for that notification to do the | 135 // database_loaded_ isn't true, we'll wait for that notification to do the |
| 119 // init. | 136 // init. |
| 120 if (database_loaded_) | 137 if (database_loaded_) |
| 121 protocol_manager_->Initialize(); | 138 protocol_manager_->Initialize(); |
| 122 } | 139 } |
| 123 | 140 |
| 124 void SafeBrowsingService::OnDBInitialize() { | 141 void SafeBrowsingService::OnDBInitialize() { |
| 125 DCHECK(MessageLoop::current() == safe_browsing_thread_->message_loop()); | 142 DCHECK(MessageLoop::current() == safe_browsing_thread_->message_loop()); |
| (...skipping 566 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 692 // report if it's not there. | 709 // report if it's not there. |
| 693 std::string list; | 710 std::string list; |
| 694 std::vector<SBPrefix> prefix_hits; | 711 std::vector<SBPrefix> prefix_hits; |
| 695 std::vector<SBFullHashResult> full_hits; | 712 std::vector<SBFullHashResult> full_hits; |
| 696 database_->ContainsUrl(page_url, &list, &prefix_hits, &full_hits, | 713 database_->ContainsUrl(page_url, &list, &prefix_hits, &full_hits, |
| 697 protocol_manager_->last_update()); | 714 protocol_manager_->last_update()); |
| 698 | 715 |
| 699 if (full_hits.empty()) | 716 if (full_hits.empty()) |
| 700 protocol_manager_->ReportMalware(malware_url, page_url, referrer_url); | 717 protocol_manager_->ReportMalware(malware_url, page_url, referrer_url); |
| 701 } | 718 } |
| OLD | NEW |