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 
5 #include "services/network/crl_set_distributor.h"
6 
7 #include <string>
8 
9 #include "base/bind.h"
10 #include "base/containers/span.h"
11 #include "base/location.h"
12 #include "base/strings/string_piece.h"
13 #include "base/task/post_task.h"
14 #include "base/task/thread_pool.h"
15 
16 namespace network {
17 
18 namespace {
19 
20 // Attempts to parse |crl_set|, returning nullptr on error or the parsed
21 // CRLSet.
ParseCRLSet(std::string crl_set)22 scoped_refptr<net::CRLSet> ParseCRLSet(std::string crl_set) {
23   scoped_refptr<net::CRLSet> result;
24   if (!net::CRLSet::Parse(crl_set, &result))
25     return nullptr;
26   return result;
27 }
28 
29 // Helper to guarantee |notify_callback| is run, even if |process_callback|
30 // no-ops due to the worker pool doing the parsing outliving the
31 // CRLSetDistributor.
ProcessParsedCRLSet(base::OnceCallback<void (scoped_refptr<net::CRLSet>)> process_callback,base::OnceClosure notify_callback,scoped_refptr<net::CRLSet> crl_set)32 void ProcessParsedCRLSet(
33     base::OnceCallback<void(scoped_refptr<net::CRLSet>)> process_callback,
34     base::OnceClosure notify_callback,
35     scoped_refptr<net::CRLSet> crl_set) {
36   std::move(process_callback).Run(std::move(crl_set));
37   std::move(notify_callback).Run();
38 }
39 
40 }  // namespace
41 
CRLSetDistributor()42 CRLSetDistributor::CRLSetDistributor() {}
43 
44 CRLSetDistributor::~CRLSetDistributor() = default;
45 
AddObserver(Observer * observer)46 void CRLSetDistributor::AddObserver(Observer* observer) {
47   observers_.AddObserver(observer);
48 }
49 
RemoveObserver(Observer * observer)50 void CRLSetDistributor::RemoveObserver(Observer* observer) {
51   observers_.RemoveObserver(observer);
52 }
53 
OnNewCRLSet(base::span<const uint8_t> crl_set,base::OnceClosure callback)54 void CRLSetDistributor::OnNewCRLSet(base::span<const uint8_t> crl_set,
55                                     base::OnceClosure callback) {
56   // Make a copy for the background task, since the underlying storage for
57   // the span will go away.
58   std::string crl_set_string(reinterpret_cast<const char*>(crl_set.data()),
59                              crl_set.size());
60 
61   base::ThreadPool::PostTaskAndReplyWithResult(
62       FROM_HERE, {base::TaskPriority::BEST_EFFORT},
63       base::BindOnce(&ParseCRLSet, std::move(crl_set_string)),
64       base::BindOnce(&ProcessParsedCRLSet,
65                      base::BindOnce(&CRLSetDistributor::OnCRLSetParsed,
66                                     weak_factory_.GetWeakPtr()),
67                      std::move(callback)));
68 }
69 
OnCRLSetParsed(scoped_refptr<net::CRLSet> crl_set)70 void CRLSetDistributor::OnCRLSetParsed(scoped_refptr<net::CRLSet> crl_set) {
71   if (!crl_set)
72     return;  // Error parsing
73 
74   if (crl_set_ && crl_set_->sequence() >= crl_set->sequence()) {
75     // Don't allow downgrades, and don't refresh CRLSets that are identical
76     // (the sequence is globally unique for all CRLSets).
77     return;
78   }
79 
80   crl_set_ = std::move(crl_set);
81 
82   for (auto& observer : observers_) {
83     observer.OnNewCRLSet(crl_set_);
84   }
85 }
86 
87 }  // namespace network
88