| OLD | NEW | 
|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "net/ssl/default_channel_id_store.h" | 5 #include "net/ssl/default_channel_id_store.h" | 
| 6 | 6 | 
| 7 #include <utility> | 7 #include <utility> | 
| 8 | 8 | 
| 9 #include "base/bind.h" | 9 #include "base/bind.h" | 
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" | 
| (...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 220     PersistentStore* store) | 220     PersistentStore* store) | 
| 221     : initialized_(false), | 221     : initialized_(false), | 
| 222       loaded_(false), | 222       loaded_(false), | 
| 223       store_(store), | 223       store_(store), | 
| 224       weak_ptr_factory_(this) {} | 224       weak_ptr_factory_(this) {} | 
| 225 | 225 | 
| 226 int DefaultChannelIDStore::GetChannelID( | 226 int DefaultChannelIDStore::GetChannelID( | 
| 227     const std::string& server_identifier, | 227     const std::string& server_identifier, | 
| 228     std::unique_ptr<crypto::ECPrivateKey>* key_result, | 228     std::unique_ptr<crypto::ECPrivateKey>* key_result, | 
| 229     const GetChannelIDCallback& callback) { | 229     const GetChannelIDCallback& callback) { | 
| 230   DCHECK(CalledOnValidThread()); | 230   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | 
| 231   InitIfNecessary(); | 231   InitIfNecessary(); | 
| 232 | 232 | 
| 233   if (!loaded_) { | 233   if (!loaded_) { | 
| 234     EnqueueTask(std::unique_ptr<Task>( | 234     EnqueueTask(std::unique_ptr<Task>( | 
| 235         new GetChannelIDTask(server_identifier, callback))); | 235         new GetChannelIDTask(server_identifier, callback))); | 
| 236     return ERR_IO_PENDING; | 236     return ERR_IO_PENDING; | 
| 237   } | 237   } | 
| 238 | 238 | 
| 239   ChannelIDMap::iterator it = channel_ids_.find(server_identifier); | 239   ChannelIDMap::iterator it = channel_ids_.find(server_identifier); | 
| 240 | 240 | 
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 278 void DefaultChannelIDStore::GetAllChannelIDs( | 278 void DefaultChannelIDStore::GetAllChannelIDs( | 
| 279     const GetChannelIDListCallback& callback) { | 279     const GetChannelIDListCallback& callback) { | 
| 280   RunOrEnqueueTask(std::unique_ptr<Task>(new GetAllChannelIDsTask(callback))); | 280   RunOrEnqueueTask(std::unique_ptr<Task>(new GetAllChannelIDsTask(callback))); | 
| 281 } | 281 } | 
| 282 | 282 | 
| 283 void DefaultChannelIDStore::Flush() { | 283 void DefaultChannelIDStore::Flush() { | 
| 284   store_->Flush(); | 284   store_->Flush(); | 
| 285 } | 285 } | 
| 286 | 286 | 
| 287 int DefaultChannelIDStore::GetChannelIDCount() { | 287 int DefaultChannelIDStore::GetChannelIDCount() { | 
| 288   DCHECK(CalledOnValidThread()); | 288   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | 
| 289 | 289 | 
| 290   return channel_ids_.size(); | 290   return channel_ids_.size(); | 
| 291 } | 291 } | 
| 292 | 292 | 
| 293 void DefaultChannelIDStore::SetForceKeepSessionState() { | 293 void DefaultChannelIDStore::SetForceKeepSessionState() { | 
| 294   DCHECK(CalledOnValidThread()); | 294   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | 
| 295   InitIfNecessary(); | 295   InitIfNecessary(); | 
| 296 | 296 | 
| 297   if (store_) | 297   if (store_) | 
| 298     store_->SetForceKeepSessionState(); | 298     store_->SetForceKeepSessionState(); | 
| 299 } | 299 } | 
| 300 | 300 | 
| 301 DefaultChannelIDStore::~DefaultChannelIDStore() { | 301 DefaultChannelIDStore::~DefaultChannelIDStore() { | 
| 302   DeleteAllInMemory(); | 302   DeleteAllInMemory(); | 
| 303 } | 303 } | 
| 304 | 304 | 
| 305 void DefaultChannelIDStore::DeleteAllInMemory() { | 305 void DefaultChannelIDStore::DeleteAllInMemory() { | 
| 306   DCHECK(CalledOnValidThread()); | 306   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | 
| 307 | 307 | 
| 308   for (ChannelIDMap::iterator it = channel_ids_.begin(); | 308   for (ChannelIDMap::iterator it = channel_ids_.begin(); | 
| 309        it != channel_ids_.end(); ++it) { | 309        it != channel_ids_.end(); ++it) { | 
| 310     delete it->second; | 310     delete it->second; | 
| 311   } | 311   } | 
| 312   channel_ids_.clear(); | 312   channel_ids_.clear(); | 
| 313 } | 313 } | 
| 314 | 314 | 
| 315 void DefaultChannelIDStore::InitStore() { | 315 void DefaultChannelIDStore::InitStore() { | 
| 316   DCHECK(CalledOnValidThread()); | 316   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | 
| 317   DCHECK(store_) << "Store must exist to initialize"; | 317   DCHECK(store_) << "Store must exist to initialize"; | 
| 318   DCHECK(!loaded_); | 318   DCHECK(!loaded_); | 
| 319 | 319 | 
| 320   store_->Load(base::Bind(&DefaultChannelIDStore::OnLoaded, | 320   store_->Load(base::Bind(&DefaultChannelIDStore::OnLoaded, | 
| 321                           weak_ptr_factory_.GetWeakPtr())); | 321                           weak_ptr_factory_.GetWeakPtr())); | 
| 322 } | 322 } | 
| 323 | 323 | 
| 324 void DefaultChannelIDStore::OnLoaded( | 324 void DefaultChannelIDStore::OnLoaded( | 
| 325     std::unique_ptr<std::vector<std::unique_ptr<ChannelID>>> channel_ids) { | 325     std::unique_ptr<std::vector<std::unique_ptr<ChannelID>>> channel_ids) { | 
| 326   DCHECK(CalledOnValidThread()); | 326   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | 
| 327   for (std::vector<std::unique_ptr<ChannelID>>::iterator it = | 327   for (std::vector<std::unique_ptr<ChannelID>>::iterator it = | 
| 328            channel_ids->begin(); | 328            channel_ids->begin(); | 
| 329        it != channel_ids->end(); ++it) { | 329        it != channel_ids->end(); ++it) { | 
| 330     DCHECK(channel_ids_.find((*it)->server_identifier()) == | 330     DCHECK(channel_ids_.find((*it)->server_identifier()) == | 
| 331            channel_ids_.end()); | 331            channel_ids_.end()); | 
| 332     std::string ident = (*it)->server_identifier(); | 332     std::string ident = (*it)->server_identifier(); | 
| 333     channel_ids_[ident] = it->release(); | 333     channel_ids_[ident] = it->release(); | 
| 334   } | 334   } | 
| 335   channel_ids->clear(); | 335   channel_ids->clear(); | 
| 336 | 336 | 
| 337   loaded_ = true; | 337   loaded_ = true; | 
| 338 | 338 | 
| 339   for (std::unique_ptr<Task>& i : waiting_tasks_) | 339   for (std::unique_ptr<Task>& i : waiting_tasks_) | 
| 340     i->Run(this); | 340     i->Run(this); | 
| 341   waiting_tasks_.clear(); | 341   waiting_tasks_.clear(); | 
| 342 } | 342 } | 
| 343 | 343 | 
| 344 void DefaultChannelIDStore::SyncSetChannelID( | 344 void DefaultChannelIDStore::SyncSetChannelID( | 
| 345     std::unique_ptr<ChannelID> channel_id) { | 345     std::unique_ptr<ChannelID> channel_id) { | 
| 346   DCHECK(CalledOnValidThread()); | 346   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | 
| 347   DCHECK(loaded_); | 347   DCHECK(loaded_); | 
| 348 | 348 | 
| 349   InternalDeleteChannelID(channel_id->server_identifier()); | 349   InternalDeleteChannelID(channel_id->server_identifier()); | 
| 350   InternalInsertChannelID(std::move(channel_id)); | 350   InternalInsertChannelID(std::move(channel_id)); | 
| 351 } | 351 } | 
| 352 | 352 | 
| 353 void DefaultChannelIDStore::SyncDeleteChannelID( | 353 void DefaultChannelIDStore::SyncDeleteChannelID( | 
| 354     const std::string& server_identifier) { | 354     const std::string& server_identifier) { | 
| 355   DCHECK(CalledOnValidThread()); | 355   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | 
| 356   DCHECK(loaded_); | 356   DCHECK(loaded_); | 
| 357   InternalDeleteChannelID(server_identifier); | 357   InternalDeleteChannelID(server_identifier); | 
| 358 } | 358 } | 
| 359 | 359 | 
| 360 void DefaultChannelIDStore::SyncDeleteForDomainsCreatedBetween( | 360 void DefaultChannelIDStore::SyncDeleteForDomainsCreatedBetween( | 
| 361     const base::Callback<bool(const std::string&)>& domain_predicate, | 361     const base::Callback<bool(const std::string&)>& domain_predicate, | 
| 362     base::Time delete_begin, | 362     base::Time delete_begin, | 
| 363     base::Time delete_end) { | 363     base::Time delete_end) { | 
| 364   DCHECK(CalledOnValidThread()); | 364   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | 
| 365   DCHECK(loaded_); | 365   DCHECK(loaded_); | 
| 366   for (ChannelIDMap::iterator it = channel_ids_.begin(); | 366   for (ChannelIDMap::iterator it = channel_ids_.begin(); | 
| 367        it != channel_ids_.end();) { | 367        it != channel_ids_.end();) { | 
| 368     ChannelIDMap::iterator cur = it; | 368     ChannelIDMap::iterator cur = it; | 
| 369     ++it; | 369     ++it; | 
| 370     ChannelID* channel_id = cur->second; | 370     ChannelID* channel_id = cur->second; | 
| 371 | 371 | 
| 372     if ((delete_begin.is_null() || | 372     if ((delete_begin.is_null() || | 
| 373          channel_id->creation_time() >= delete_begin) && | 373          channel_id->creation_time() >= delete_begin) && | 
| 374         (delete_end.is_null() || channel_id->creation_time() < delete_end) && | 374         (delete_end.is_null() || channel_id->creation_time() < delete_end) && | 
| 375         domain_predicate.Run(channel_id->server_identifier())) { | 375         domain_predicate.Run(channel_id->server_identifier())) { | 
| 376       if (store_) | 376       if (store_) | 
| 377         store_->DeleteChannelID(*channel_id); | 377         store_->DeleteChannelID(*channel_id); | 
| 378       delete channel_id; | 378       delete channel_id; | 
| 379       channel_ids_.erase(cur); | 379       channel_ids_.erase(cur); | 
| 380     } | 380     } | 
| 381   } | 381   } | 
| 382 } | 382 } | 
| 383 | 383 | 
| 384 void DefaultChannelIDStore::SyncGetAllChannelIDs( | 384 void DefaultChannelIDStore::SyncGetAllChannelIDs( | 
| 385     ChannelIDList* channel_id_list) { | 385     ChannelIDList* channel_id_list) { | 
| 386   DCHECK(CalledOnValidThread()); | 386   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | 
| 387   DCHECK(loaded_); | 387   DCHECK(loaded_); | 
| 388   for (ChannelIDMap::iterator it = channel_ids_.begin(); | 388   for (ChannelIDMap::iterator it = channel_ids_.begin(); | 
| 389        it != channel_ids_.end(); ++it) | 389        it != channel_ids_.end(); ++it) | 
| 390     channel_id_list->push_back(*it->second); | 390     channel_id_list->push_back(*it->second); | 
| 391 } | 391 } | 
| 392 | 392 | 
| 393 void DefaultChannelIDStore::EnqueueTask(std::unique_ptr<Task> task) { | 393 void DefaultChannelIDStore::EnqueueTask(std::unique_ptr<Task> task) { | 
| 394   DCHECK(CalledOnValidThread()); | 394   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | 
| 395   DCHECK(!loaded_); | 395   DCHECK(!loaded_); | 
| 396   waiting_tasks_.push_back(std::move(task)); | 396   waiting_tasks_.push_back(std::move(task)); | 
| 397 } | 397 } | 
| 398 | 398 | 
| 399 void DefaultChannelIDStore::RunOrEnqueueTask(std::unique_ptr<Task> task) { | 399 void DefaultChannelIDStore::RunOrEnqueueTask(std::unique_ptr<Task> task) { | 
| 400   DCHECK(CalledOnValidThread()); | 400   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | 
| 401   InitIfNecessary(); | 401   InitIfNecessary(); | 
| 402 | 402 | 
| 403   if (!loaded_) { | 403   if (!loaded_) { | 
| 404     EnqueueTask(std::move(task)); | 404     EnqueueTask(std::move(task)); | 
| 405     return; | 405     return; | 
| 406   } | 406   } | 
| 407 | 407 | 
| 408   task->Run(this); | 408   task->Run(this); | 
| 409 } | 409 } | 
| 410 | 410 | 
| 411 void DefaultChannelIDStore::InternalDeleteChannelID( | 411 void DefaultChannelIDStore::InternalDeleteChannelID( | 
| 412     const std::string& server_identifier) { | 412     const std::string& server_identifier) { | 
| 413   DCHECK(CalledOnValidThread()); | 413   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | 
| 414   DCHECK(loaded_); | 414   DCHECK(loaded_); | 
| 415 | 415 | 
| 416   ChannelIDMap::iterator it = channel_ids_.find(server_identifier); | 416   ChannelIDMap::iterator it = channel_ids_.find(server_identifier); | 
| 417   if (it == channel_ids_.end()) | 417   if (it == channel_ids_.end()) | 
| 418     return;  // There is nothing to delete. | 418     return;  // There is nothing to delete. | 
| 419 | 419 | 
| 420   ChannelID* channel_id = it->second; | 420   ChannelID* channel_id = it->second; | 
| 421   if (store_) | 421   if (store_) | 
| 422     store_->DeleteChannelID(*channel_id); | 422     store_->DeleteChannelID(*channel_id); | 
| 423   channel_ids_.erase(it); | 423   channel_ids_.erase(it); | 
| 424   delete channel_id; | 424   delete channel_id; | 
| 425 } | 425 } | 
| 426 | 426 | 
| 427 void DefaultChannelIDStore::InternalInsertChannelID( | 427 void DefaultChannelIDStore::InternalInsertChannelID( | 
| 428     std::unique_ptr<ChannelID> channel_id) { | 428     std::unique_ptr<ChannelID> channel_id) { | 
| 429   DCHECK(CalledOnValidThread()); | 429   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | 
| 430   DCHECK(loaded_); | 430   DCHECK(loaded_); | 
| 431 | 431 | 
| 432   if (store_) | 432   if (store_) | 
| 433     store_->AddChannelID(*channel_id); | 433     store_->AddChannelID(*channel_id); | 
| 434   const std::string& server_identifier = channel_id->server_identifier(); | 434   const std::string& server_identifier = channel_id->server_identifier(); | 
| 435   channel_ids_[server_identifier] = channel_id.release(); | 435   channel_ids_[server_identifier] = channel_id.release(); | 
| 436 } | 436 } | 
| 437 | 437 | 
| 438 bool DefaultChannelIDStore::IsEphemeral() { | 438 bool DefaultChannelIDStore::IsEphemeral() { | 
| 439   return !store_; | 439   return !store_; | 
| 440 } | 440 } | 
| 441 | 441 | 
| 442 DefaultChannelIDStore::PersistentStore::PersistentStore() {} | 442 DefaultChannelIDStore::PersistentStore::PersistentStore() {} | 
| 443 | 443 | 
| 444 DefaultChannelIDStore::PersistentStore::~PersistentStore() {} | 444 DefaultChannelIDStore::PersistentStore::~PersistentStore() {} | 
| 445 | 445 | 
| 446 }  // namespace net | 446 }  // namespace net | 
| OLD | NEW | 
|---|