Home | History | Annotate | Download | only in policy
      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 #include "chrome/browser/chromeos/policy/user_network_configuration_updater.h"
      6 
      7 #include "base/bind.h"
      8 #include "base/bind_helpers.h"
      9 #include "base/logging.h"
     10 #include "chrome/browser/chromeos/login/user.h"
     11 #include "chrome/browser/chromeos/net/onc_utils.h"
     12 #include "chromeos/network/managed_network_configuration_handler.h"
     13 #include "chromeos/network/onc/onc_certificate_importer.h"
     14 #include "content/public/browser/browser_thread.h"
     15 #include "net/cert/x509_certificate.h"
     16 #include "policy/policy_constants.h"
     17 
     18 namespace policy {
     19 
     20 UserNetworkConfigurationUpdater::~UserNetworkConfigurationUpdater() {}
     21 
     22 // static
     23 scoped_ptr<UserNetworkConfigurationUpdater>
     24 UserNetworkConfigurationUpdater::CreateForUserPolicy(
     25     bool allow_trusted_certs_from_policy,
     26     const chromeos::User& user,
     27     scoped_ptr<chromeos::onc::CertificateImporter> certificate_importer,
     28     PolicyService* policy_service,
     29     chromeos::ManagedNetworkConfigurationHandler* network_config_handler) {
     30   scoped_ptr<UserNetworkConfigurationUpdater> updater(
     31       new UserNetworkConfigurationUpdater(allow_trusted_certs_from_policy,
     32                                           user,
     33                                           certificate_importer.Pass(),
     34                                           policy_service,
     35                                           network_config_handler));
     36   updater->Init();
     37   return updater.Pass();
     38 }
     39 
     40 void UserNetworkConfigurationUpdater::AddTrustedCertsObserver(
     41     WebTrustedCertsObserver* observer) {
     42   observer_list_.AddObserver(observer);
     43 }
     44 
     45 void UserNetworkConfigurationUpdater::RemoveTrustedCertsObserver(
     46     WebTrustedCertsObserver* observer) {
     47   observer_list_.RemoveObserver(observer);
     48 }
     49 
     50 UserNetworkConfigurationUpdater::UserNetworkConfigurationUpdater(
     51     bool allow_trusted_certs_from_policy,
     52     const chromeos::User& user,
     53     scoped_ptr<chromeos::onc::CertificateImporter> certificate_importer,
     54     PolicyService* policy_service,
     55     chromeos::ManagedNetworkConfigurationHandler* network_config_handler)
     56     : NetworkConfigurationUpdater(onc::ONC_SOURCE_USER_POLICY,
     57                                   key::kOpenNetworkConfiguration,
     58                                   certificate_importer.Pass(),
     59                                   policy_service,
     60                                   network_config_handler),
     61       allow_trusted_certificates_from_policy_(allow_trusted_certs_from_policy),
     62       user_(&user) {}
     63 
     64 void UserNetworkConfigurationUpdater::GetWebTrustedCertificates(
     65     net::CertificateList* certs) const {
     66   *certs = web_trust_certs_;
     67 }
     68 
     69 void UserNetworkConfigurationUpdater::ImportCertificates(
     70     const base::ListValue& certificates_onc) {
     71   web_trust_certs_.clear();
     72   certificate_importer_->ImportCertificates(
     73       certificates_onc,
     74       onc_source_,
     75       allow_trusted_certificates_from_policy_ ? &web_trust_certs_ : NULL);
     76 
     77   NotifyTrustAnchorsChanged();
     78 }
     79 
     80 void UserNetworkConfigurationUpdater::ApplyNetworkPolicy(
     81     base::ListValue* network_configs_onc,
     82     base::DictionaryValue* global_network_config) {
     83   DCHECK(user_);
     84   chromeos::onc::ExpandStringPlaceholdersInNetworksForUser(user_,
     85                                                            network_configs_onc);
     86   network_config_handler_->SetPolicy(onc_source_,
     87                                      user_->username_hash(),
     88                                      *network_configs_onc,
     89                                      *global_network_config);
     90 }
     91 
     92 void UserNetworkConfigurationUpdater::NotifyTrustAnchorsChanged() {
     93   FOR_EACH_OBSERVER(WebTrustedCertsObserver,
     94                     observer_list_,
     95                     OnTrustAnchorsChanged(web_trust_certs_));
     96 }
     97 
     98 }  // namespace policy
     99