summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/policy/network_configuration_updater.cc
blob: db2dd931f491ad066f72579d6a27e6d9f3831dd2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/chromeos/policy/network_configuration_updater.h"
#include "chromeos/network/onc/onc_constants.h"
#include "content/public/browser/browser_thread.h"
#include "net/cert/cert_trust_anchor_provider.h"

using content::BrowserThread;

namespace policy {

namespace {

// A simple implementation of net::CertTrustAnchorProvider that returns a list
// of certificates that can be set by the owner of this object.
class CrosTrustAnchorProvider : public net::CertTrustAnchorProvider {
 public:
  CrosTrustAnchorProvider()
      : trust_anchors_(new net::CertificateList) {
  }

  virtual ~CrosTrustAnchorProvider() {
  }

  // CertTrustAnchorProvider overrides.
  virtual const net::CertificateList& GetAdditionalTrustAnchors() OVERRIDE {
    DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
    return *trust_anchors_;
  }

  void SetTrustAnchors(scoped_ptr<net::CertificateList> trust_anchors) {
    DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
    trust_anchors_ = trust_anchors.Pass();
  }

 private:
  scoped_ptr<net::CertificateList> trust_anchors_;

  DISALLOW_COPY_AND_ASSIGN(CrosTrustAnchorProvider);
};

}  // namespace

NetworkConfigurationUpdater::NetworkConfigurationUpdater()
    : allow_trusted_certificates_from_policy_(false),
      cert_trust_provider_(new CrosTrustAnchorProvider()) {
}

NetworkConfigurationUpdater::~NetworkConfigurationUpdater() {
  bool posted = BrowserThread::DeleteSoon(
      BrowserThread::IO, FROM_HERE, cert_trust_provider_);
  if (!posted)
    delete cert_trust_provider_;
}

net::CertTrustAnchorProvider*
NetworkConfigurationUpdater::GetCertTrustAnchorProvider() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  return cert_trust_provider_;
}

void NetworkConfigurationUpdater::SetAllowTrustedCertsFromPolicy() {
  allow_trusted_certificates_from_policy_ = true;
}

void NetworkConfigurationUpdater::SetTrustAnchors(
    scoped_ptr<net::CertificateList> web_trust_certs) {
  if (allow_trusted_certificates_from_policy_) {
    BrowserThread::PostTask(
        BrowserThread::IO, FROM_HERE,
        base::Bind(&CrosTrustAnchorProvider::SetTrustAnchors,
                   base::Unretained(static_cast<CrosTrustAnchorProvider*>(
                       cert_trust_provider_)),
                   base::Passed(&web_trust_certs)));
  }
}

}  // namespace policy