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_) { |
Nathan Parker
2016/05/14 00:43:41
I assume that if v4_database_ == null, we'll queue
vakh (use Gerrit instead)
2016/05/16 22:04:12
Yes, will add that logic later.
| |
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 database is in place. Start fetching updates now. | |
215 v4_update_protocol_manager_->ScheduleNextUpdate(); | |
166 } | 216 } |
167 | 217 |
168 void V4LocalDatabaseManager::StopOnIOThread(bool shutdown) { | 218 void V4LocalDatabaseManager::StopOnIOThread(bool shutdown) { |
169 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 219 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
170 | 220 |
221 enabled_ = false; | |
222 | |
223 // Delete the V4Database. | |
224 // Any pending writes to disk are completed. | |
225 v4_database_.reset(); | |
226 | |
171 // Delete the V4UpdateProtocolManager. | 227 // Delete the V4UpdateProtocolManager. |
172 // This cancels any in-flight update request. | 228 // This cancels any in-flight update request. |
173 if (v4_update_protocol_manager_.get()) { | 229 v4_update_protocol_manager_.reset(); |
174 v4_update_protocol_manager_.reset(); | |
175 } | |
176 | 230 |
177 enabled_ = false; | |
178 SafeBrowsingDatabaseManager::StopOnIOThread(shutdown); | 231 SafeBrowsingDatabaseManager::StopOnIOThread(shutdown); |
179 } | 232 } |
180 | 233 |
181 void V4LocalDatabaseManager::UpdateRequestCompleted( | 234 void V4LocalDatabaseManager::UpdateRequestCompleted( |
182 const std::vector<ListUpdateResponse>& responses) { | 235 const std::vector<ListUpdateResponse>& responses) { |
236 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
237 | |
183 // TODO(vakh): Updates downloaded. Store them on disk and record new state. | 238 // TODO(vakh): Updates downloaded. Store them on disk and record new state. |
239 v4_update_protocol_manager_->ScheduleNextUpdate(); | |
184 } | 240 } |
185 | 241 |
186 } // namespace safe_browsing | 242 } // namespace safe_browsing |
OLD | NEW |