client_cert_resolver.h revision f8ee788a64d60abd8f2d742a5fdedde054ecd910
1// Copyright 2013 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#ifndef CHROMEOS_NETWORK_CLIENT_CERT_RESOLVER_H_
6#define CHROMEOS_NETWORK_CLIENT_CERT_RESOLVER_H_
7
8#include <string>
9#include <vector>
10
11#include "base/basictypes.h"
12#include "base/memory/ref_counted.h"
13#include "base/memory/scoped_ptr.h"
14#include "base/memory/weak_ptr.h"
15#include "chromeos/cert_loader.h"
16#include "chromeos/chromeos_export.h"
17#include "chromeos/network/network_policy_observer.h"
18#include "chromeos/network/network_state_handler.h"
19#include "chromeos/network/network_state_handler_observer.h"
20
21namespace base {
22class TaskRunner;
23}
24
25namespace chromeos {
26
27class NetworkState;
28class ManagedNetworkConfigurationHandler;
29
30// Observes the known networks. If a network is configured with a client
31// certificate pattern, this class searches for a matching client certificate.
32// Each time it finds a match, it configures the network accordingly.
33class CHROMEOS_EXPORT ClientCertResolver : public NetworkStateHandlerObserver,
34                                           public CertLoader::Observer,
35                                           public NetworkPolicyObserver {
36 public:
37  struct NetworkAndMatchingCert;
38
39  ClientCertResolver();
40  virtual ~ClientCertResolver();
41
42  void Init(NetworkStateHandler* network_state_handler,
43            ManagedNetworkConfigurationHandler* managed_network_config_handler);
44
45  // Sets the task runner that any slow calls will be made from, e.g. calls
46  // to the NSS database. If not set, uses base::WorkerPool.
47  void SetSlowTaskRunnerForTest(
48      const scoped_refptr<base::TaskRunner>& task_runner);
49
50 private:
51   // NetworkStateHandlerObserver overrides
52  virtual void NetworkListChanged() OVERRIDE;
53
54  // CertLoader::Observer overrides
55  virtual void OnCertificatesLoaded(const net::CertificateList& cert_list,
56                                    bool initial_load) OVERRIDE;
57
58  // NetworkPolicyObserver overrides
59  virtual void PolicyApplied(const std::string& service_path) OVERRIDE;
60
61  // Check which networks of |networks| are configured with a client certificate
62  // pattern. Search for certificates, on the worker thread, and configure the
63  // networks for which a matching cert is found (see ConfigureCertificates).
64  void ResolveNetworks(const NetworkStateHandler::NetworkStateList& networks);
65
66  // |matches| contains networks for which a matching certificate was found.
67  // Configures these networks.
68  void ConfigureCertificates(std::vector<NetworkAndMatchingCert>* matches);
69
70  // The set of networks that were checked/resolved in previous passes. These
71  // networks are skipped in the NetworkListChanged notification.
72  std::set<std::string> resolved_networks_;
73
74  // Unowned associated (global or test) instance.
75  NetworkStateHandler* network_state_handler_;
76
77  // Unowned associated (global or test) instance.
78  ManagedNetworkConfigurationHandler* managed_network_config_handler_;
79
80  // TaskRunner for slow tasks.
81  scoped_refptr<base::TaskRunner> slow_task_runner_for_test_;
82
83  base::WeakPtrFactory<ClientCertResolver> weak_ptr_factory_;
84
85  DISALLOW_COPY_AND_ASSIGN(ClientCertResolver);
86};
87
88}  // namespace chromeos
89
90#endif  // CHROMEOS_NETWORK_CLIENT_CERT_RESOLVER_H_
91