Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "components/safe_browsing_db/v4_local_database_manager.h" | 5 #include "components/safe_browsing_db/v4_local_database_manager.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/callback.h" | |
| 9 #include "components/safe_browsing_db/safebrowsing.pb.h" | 10 #include "components/safe_browsing_db/safebrowsing.pb.h" |
| 10 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
| 11 | 12 |
| 12 using content::BrowserThread; | 13 using content::BrowserThread; |
| 13 | 14 |
| 14 namespace safe_browsing { | 15 namespace safe_browsing { |
| 15 | 16 |
| 16 V4LocalDatabaseManager::V4LocalDatabaseManager() : enabled_(false) {} | 17 V4LocalDatabaseManager::V4LocalDatabaseManager(const base::FilePath& base_path) |
| 18 : base_path_(base_path), enabled_(false) { | |
| 19 DCHECK(!base_path_.empty()); | |
| 20 DVLOG(1) << "V4LocalDatabaseManager::V4LocalDatabaseManager: " | |
| 21 << "base_path_: " << base_path_.AsUTF8Unsafe(); | |
| 22 } | |
| 17 | 23 |
| 18 V4LocalDatabaseManager::~V4LocalDatabaseManager() { | 24 V4LocalDatabaseManager::~V4LocalDatabaseManager() { |
| 19 DCHECK(!enabled_); | 25 DCHECK(!enabled_); |
| 20 } | 26 } |
| 21 | 27 |
| 22 bool V4LocalDatabaseManager::IsSupported() const { | 28 bool V4LocalDatabaseManager::IsSupported() const { |
| 23 return true; | 29 return true; |
| 24 } | 30 } |
| 25 | 31 |
| 26 safe_browsing::ThreatSource V4LocalDatabaseManager::GetThreatSource() const { | 32 safe_browsing::ThreatSource V4LocalDatabaseManager::GetThreatSource() const { |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 116 | 122 |
| 117 bool V4LocalDatabaseManager::IsCsdWhitelistKillSwitchOn() { | 123 bool V4LocalDatabaseManager::IsCsdWhitelistKillSwitchOn() { |
| 118 // TODO(vakh): Implement this skeleton. | 124 // TODO(vakh): Implement this skeleton. |
| 119 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 125 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 120 return true; | 126 return true; |
| 121 } | 127 } |
| 122 | 128 |
| 123 bool V4LocalDatabaseManager::CheckBrowseUrl(const GURL& url, Client* client) { | 129 bool V4LocalDatabaseManager::CheckBrowseUrl(const GURL& url, Client* client) { |
| 124 // TODO(vakh): Implement this skeleton. | 130 // TODO(vakh): Implement this skeleton. |
| 125 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 131 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 126 if (!enabled_) | 132 if (!enabled_) { |
| 127 return true; | 133 return true; |
| 134 } | |
| 128 | 135 |
| 129 // Don't defer the resource load. | 136 // Don't defer the resource load. |
| 130 return true; | 137 return true; |
| 131 } | 138 } |
| 132 | 139 |
| 133 void V4LocalDatabaseManager::CancelCheck(Client* client) { | 140 void V4LocalDatabaseManager::CancelCheck(Client* client) { |
| 134 // TODO(vakh): Implement this skeleton. | 141 // TODO(vakh): Implement this skeleton. |
| 135 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 142 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 136 DCHECK(enabled_); | 143 DCHECK(enabled_); |
| 137 } | 144 } |
| 138 | 145 |
| 139 void V4LocalDatabaseManager::StartOnIOThread( | 146 void V4LocalDatabaseManager::StartOnIOThread( |
| 140 net::URLRequestContextGetter* request_context_getter, | 147 net::URLRequestContextGetter* request_context_getter, |
| 141 const V4ProtocolConfig& config) { | 148 const V4ProtocolConfig& config) { |
| 142 SafeBrowsingDatabaseManager::StartOnIOThread(request_context_getter, config); | 149 SafeBrowsingDatabaseManager::StartOnIOThread(request_context_getter, config); |
| 143 | 150 |
| 144 #if defined(OS_WIN) || defined (OS_LINUX) || defined (OS_MACOSX) | 151 SetupUpdateProtocolManager(request_context_getter, config); |
| 152 | |
| 153 SetupDatabase(); | |
| 154 | |
| 155 enabled_ = true; | |
| 156 } | |
| 157 | |
| 158 void V4LocalDatabaseManager::SetupUpdateProtocolManager( | |
| 159 net::URLRequestContextGetter* request_context_getter, | |
| 160 const V4ProtocolConfig& config) { | |
| 161 #if defined(OS_WIN) || defined(OS_LINUX) || defined(OS_MACOSX) | |
| 145 // TODO(vakh): Remove this if/endif block when the V4Database is implemented. | 162 // TODO(vakh): Remove this if/endif block when the V4Database is implemented. |
| 146 // Filed as http://crbug.com/608075 | 163 // Filed as http://crbug.com/608075 |
| 147 UpdateListIdentifier update_list_identifier; | 164 UpdateListIdentifier update_list_identifier; |
| 148 #if defined(OS_WIN) | 165 #if defined(OS_WIN) |
| 149 update_list_identifier.platform_type = WINDOWS_PLATFORM; | 166 update_list_identifier.platform_type = WINDOWS_PLATFORM; |
| 150 #elif defined (OS_LINUX) | 167 #elif defined(OS_LINUX) |
| 151 update_list_identifier.platform_type = LINUX_PLATFORM; | 168 update_list_identifier.platform_type = LINUX_PLATFORM; |
| 152 #else | 169 #else |
| 153 update_list_identifier.platform_type = OSX_PLATFORM; | 170 update_list_identifier.platform_type = OSX_PLATFORM; |
| 154 #endif | 171 #endif |
| 155 update_list_identifier.threat_entry_type = URL_EXPRESSION; | 172 update_list_identifier.threat_entry_type = URL_EXPRESSION; |
| 156 update_list_identifier.threat_type = MALWARE_THREAT; | 173 update_list_identifier.threat_type = MALWARE_THREAT; |
| 157 current_list_states_[update_list_identifier] = ""; | 174 current_list_states_[update_list_identifier] = ""; |
| 158 #endif | 175 #endif |
| 159 | 176 |
| 160 V4UpdateCallback callback = base::Bind( | 177 V4UpdateCallback callback = base::Bind( |
| 161 &V4LocalDatabaseManager::UpdateRequestCompleted, base::Unretained(this)); | 178 &V4LocalDatabaseManager::UpdateRequestCompleted, base::Unretained(this)); |
| 179 | |
| 162 v4_update_protocol_manager_ = V4UpdateProtocolManager::Create( | 180 v4_update_protocol_manager_ = V4UpdateProtocolManager::Create( |
| 163 request_context_getter, config, current_list_states_, callback); | 181 request_context_getter, config, current_list_states_, callback); |
| 182 } | |
| 164 | 183 |
| 165 enabled_ = true; | 184 void V4LocalDatabaseManager::SetupDatabase() { |
| 185 DCHECK(!base_path_.empty()); | |
| 186 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 187 | |
| 188 // Only get a new task runner if there isn't one already. If the service has | |
| 189 // previously been started and stopped, a task runner could already exist. | |
| 190 if (!task_runner_) { | |
| 191 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); | |
| 192 task_runner_ = pool->GetSequencedTaskRunnerWithShutdownBehavior( | |
| 193 pool->GetSequenceToken(), base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); | |
| 194 } | |
| 195 | |
| 196 // TODO(vakh): list_info_map should probably be a hard-coded map. | |
| 197 ListInfoMap list_info_map; | |
| 198 | |
| 199 // Do not create the database on the IO thread since this may be an expensive | |
| 200 // operation. Instead, do that on the task_runner and when the new database | |
| 201 // has been created, swap it out on the IO thread. | |
| 202 NewDatabaseReadyCallback db_ready_callback = base::Bind( | |
| 203 &V4LocalDatabaseManager::DatabaseReady, base::Unretained(this)); | |
| 204 V4Database::Create(task_runner_, base_path_, list_info_map, | |
| 205 db_ready_callback); | |
| 206 } | |
| 207 | |
| 208 void V4LocalDatabaseManager::DatabaseReady( | |
| 209 std::unique_ptr<V4Database> v4_database) { | |
| 210 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 211 | |
| 212 v4_database_ = std::move(v4_database); | |
| 213 | |
| 214 // The following check is needed because it is possible that by the time the | |
| 215 // database is ready, the user decides to turn off safebrowsing checks and we | |
| 216 // destroy v4_update_protocol_manager_ | |
|
Scott Hess - ex-Googler
2016/05/16 07:55:25
Does this mean that StopOnIOThread() was called?
vakh (use Gerrit instead)
2016/05/16 18:36:15
Done. It's cleaner.
| |
| 217 if (v4_update_protocol_manager_.get()) { | |
| 218 // The database is in place. Start fetching updates now. | |
| 219 v4_update_protocol_manager_->ScheduleNextUpdate(); | |
| 220 } | |
| 166 } | 221 } |
| 167 | 222 |
| 168 void V4LocalDatabaseManager::StopOnIOThread(bool shutdown) { | 223 void V4LocalDatabaseManager::StopOnIOThread(bool shutdown) { |
| 169 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 224 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 170 | 225 |
| 226 enabled_ = false; | |
| 227 | |
| 228 // Delete the V4Database. | |
| 229 // Any pending writes to disk are completed. | |
| 230 v4_database_.reset(); | |
|
Scott Hess - ex-Googler
2016/05/16 07:55:25
Actually, thinking of the above made me wonder - i
vakh (use Gerrit instead)
2016/05/16 18:36:15
[I may have misunderstood you here.]
The database
Scott Hess - ex-Googler
2016/05/16 19:49:52
The variable manipulation is fine, but the reset()
Scott Hess - ex-Googler
2016/05/16 20:20:09
Seriously - my expectation is you'll think "Oh, ye
vakh (use Gerrit instead)
2016/05/16 20:50:21
Done. Good catch!
Or: Oh, yeah, I see what you mea
| |
| 231 | |
| 171 // Delete the V4UpdateProtocolManager. | 232 // Delete the V4UpdateProtocolManager. |
| 172 // This cancels any in-flight update request. | 233 // This cancels any in-flight update request. |
| 173 if (v4_update_protocol_manager_.get()) { | 234 v4_update_protocol_manager_.reset(); |
| 174 v4_update_protocol_manager_.reset(); | |
| 175 } | |
| 176 | 235 |
| 177 enabled_ = false; | |
| 178 SafeBrowsingDatabaseManager::StopOnIOThread(shutdown); | 236 SafeBrowsingDatabaseManager::StopOnIOThread(shutdown); |
| 179 } | 237 } |
| 180 | 238 |
| 181 void V4LocalDatabaseManager::UpdateRequestCompleted( | 239 void V4LocalDatabaseManager::UpdateRequestCompleted( |
| 182 const std::vector<ListUpdateResponse>& responses) { | 240 const std::vector<ListUpdateResponse>& responses) { |
| 241 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 242 | |
| 183 // TODO(vakh): Updates downloaded. Store them on disk and record new state. | 243 // TODO(vakh): Updates downloaded. Store them on disk and record new state. |
| 244 v4_update_protocol_manager_->ScheduleNextUpdate(); | |
| 184 } | 245 } |
| 185 | 246 |
| 186 } // namespace safe_browsing | 247 } // namespace safe_browsing |
| OLD | NEW |