crl_set_distributor.cc 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "services/network/crl_set_distributor.h"
  5. #include <string>
  6. #include "base/bind.h"
  7. #include "base/containers/span.h"
  8. #include "base/feature_list.h"
  9. #include "base/location.h"
  10. #include "base/observer_list.h"
  11. #include "base/strings/string_piece.h"
  12. #include "base/task/thread_pool.h"
  13. #include "services/network/public/cpp/features.h"
  14. namespace network {
  15. namespace {
  16. // Attempts to parse |crl_set|, returning nullptr on error or the parsed
  17. // CRLSet.
  18. scoped_refptr<net::CRLSet> ParseCRLSet(std::string crl_set) {
  19. scoped_refptr<net::CRLSet> result;
  20. if (!net::CRLSet::ParseAndStoreUnparsedData(std::move(crl_set), &result))
  21. return nullptr;
  22. return result;
  23. }
  24. // Helper to guarantee |notify_callback| is run, even if |process_callback|
  25. // no-ops due to the worker pool doing the parsing outliving the
  26. // CRLSetDistributor.
  27. void ProcessParsedCRLSet(
  28. base::OnceCallback<void(scoped_refptr<net::CRLSet>)> process_callback,
  29. base::OnceClosure notify_callback,
  30. scoped_refptr<net::CRLSet> crl_set) {
  31. std::move(process_callback).Run(std::move(crl_set));
  32. std::move(notify_callback).Run();
  33. }
  34. } // namespace
  35. CRLSetDistributor::CRLSetDistributor() {}
  36. CRLSetDistributor::~CRLSetDistributor() = default;
  37. void CRLSetDistributor::AddObserver(Observer* observer) {
  38. observers_.AddObserver(observer);
  39. }
  40. void CRLSetDistributor::RemoveObserver(Observer* observer) {
  41. observers_.RemoveObserver(observer);
  42. }
  43. void CRLSetDistributor::OnNewCRLSet(base::span<const uint8_t> crl_set,
  44. base::OnceClosure callback) {
  45. // Make a copy for the background task, since the underlying storage for
  46. // the span will go away.
  47. std::string crl_set_string(reinterpret_cast<const char*>(crl_set.data()),
  48. crl_set.size());
  49. base::ThreadPool::PostTaskAndReplyWithResult(
  50. FROM_HERE, {base::TaskPriority::BEST_EFFORT},
  51. base::BindOnce(&ParseCRLSet, std::move(crl_set_string)),
  52. base::BindOnce(&ProcessParsedCRLSet,
  53. base::BindOnce(&CRLSetDistributor::OnCRLSetParsed,
  54. weak_factory_.GetWeakPtr()),
  55. std::move(callback)));
  56. }
  57. void CRLSetDistributor::OnCRLSetParsed(scoped_refptr<net::CRLSet> crl_set) {
  58. if (!crl_set)
  59. return; // Error parsing
  60. if (crl_set_ && crl_set_->sequence() >= crl_set->sequence()) {
  61. // Don't allow downgrades, and don't refresh CRLSets that are identical
  62. // (the sequence is globally unique for all CRLSets).
  63. return;
  64. }
  65. crl_set_ = std::move(crl_set);
  66. for (auto& observer : observers_) {
  67. observer.OnNewCRLSet(crl_set_);
  68. }
  69. }
  70. } // namespace network