managed_network_configuration_handler.h revision b2df76ea8fec9e32f6f3718986dba0d95315b29c
1// Copyright (c) 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_MANAGED_NETWORK_CONFIGURATION_HANDLER_H_ 6#define CHROMEOS_NETWORK_MANAGED_NETWORK_CONFIGURATION_HANDLER_H_ 7 8#include <map> 9#include <string> 10 11#include "base/basictypes.h" 12#include "base/callback.h" 13#include "base/gtest_prod_util.h" 14#include "base/memory/weak_ptr.h" 15#include "chromeos/chromeos_export.h" 16#include "chromeos/network/network_handler_callbacks.h" 17#include "chromeos/network/network_profile_observer.h" 18#include "chromeos/network/onc/onc_constants.h" 19 20namespace base { 21class DictionaryValue; 22class ListValue; 23} 24 25namespace chromeos { 26 27class NetworkProfileHandler; 28 29// The ManagedNetworkConfigurationHandler class is used to create and configure 30// networks in ChromeOS using ONC and takes care of network policies. 31// 32// Its interface exposes only ONC and should decouple users from Shill. 33// Internally it translates ONC to Shill dictionaries and calls through to the 34// NetworkConfigurationHandler. 35// 36// For accessing lists of visible networks, and other state information, see the 37// class NetworkStateHandler. 38// 39// This is a singleton and its lifetime is managed by the Chrome startup code. 40// 41// Network configurations are referred to by Shill's service path. These 42// identifiers should at most be used to also access network state using the 43// NetworkStateHandler, but dependencies to Shill should be avoided. In the 44// future, we may switch to other identifiers. 45// 46// Note on callbacks: Because all the functions here are meant to be 47// asynchronous, they all take a |callback| of some type, and an 48// |error_callback|. When the operation succeeds, |callback| will be called, and 49// when it doesn't, |error_callback| will be called with information about the 50// error, including a symbolic name for the error and often some error message 51// that is suitable for logging. None of the error message text is meant for 52// user consumption. 53 54class CHROMEOS_EXPORT ManagedNetworkConfigurationHandler 55 : public NetworkProfileObserver { 56 public: 57 typedef std::map<std::string, const base::DictionaryValue*> GuidToPolicyMap; 58 typedef std::map<std::string, GuidToPolicyMap> UserToPoliciesMap; 59 60 // Initializes the singleton. 61 static void Initialize(NetworkProfileHandler* profile_handler); 62 63 // Returns if the singleton is initialized. 64 static bool IsInitialized(); 65 66 // Destroys the singleton. 67 static void Shutdown(); 68 69 // Initialize() must be called before this. 70 static ManagedNetworkConfigurationHandler* Get(); 71 72 // Provides the properties of the network with |service_path| to |callback|. 73 void GetProperties( 74 const std::string& service_path, 75 const network_handler::DictionaryResultCallback& callback, 76 const network_handler::ErrorCallback& error_callback) const; 77 78 // Provides the managed properties of the network with |service_path| to 79 // |callback|. |userhash| is only used to ensure that the user's policy is 80 // already applied. 81 void GetManagedProperties( 82 const std::string& userhash, 83 const std::string& service_path, 84 const network_handler::DictionaryResultCallback& callback, 85 const network_handler::ErrorCallback& error_callback); 86 87 // Sets the user's settings of an already configured network with 88 // |service_path|. A network can be initially configured by calling 89 // CreateConfiguration or if it is managed by a policy. The given properties 90 // will be merged with the existing settings, and it won't clear any existing 91 // properties. 92 void SetProperties( 93 const std::string& service_path, 94 const base::DictionaryValue& user_settings, 95 const base::Closure& callback, 96 const network_handler::ErrorCallback& error_callback) const; 97 98 // Initiates a connection with network that has |service_path|. |callback| is 99 // called if the connection request was successfully handled. That doesn't 100 // mean that the connection was successfully established. 101 void Connect(const std::string& service_path, 102 const base::Closure& callback, 103 const network_handler::ErrorCallback& error_callback) const; 104 105 // Initiates a disconnect with the network at |service_path|. |callback| is 106 // called if the diconnect request was successfully handled. That doesn't mean 107 // that the network is already diconnected. 108 void Disconnect(const std::string& service_path, 109 const base::Closure& callback, 110 const network_handler::ErrorCallback& error_callback) const; 111 112 // Initially configures an unconfigured network with the given user settings 113 // and returns the new identifier to |callback| if successful. Fails if the 114 // network was already configured by a call to this function or because of a 115 // policy. The new configuration will be owned by user |userhash|. If 116 // |userhash| is empty, the new configuration will be shared. 117 void CreateConfiguration( 118 const std::string& userhash, 119 const base::DictionaryValue& properties, 120 const network_handler::StringResultCallback& callback, 121 const network_handler::ErrorCallback& error_callback) const; 122 123 // Removes the user's configuration from the network with |service_path|. The 124 // network may still show up in the visible networks after this, but no user 125 // configuration will remain. If it was managed, it will still be configured. 126 void RemoveConfiguration( 127 const std::string& service_path, 128 const base::Closure& callback, 129 const network_handler::ErrorCallback& error_callback) const; 130 131 // Only to be called by NetworkConfigurationUpdater or from tests. Sets 132 // |network_configs_onc| as the current policy of |onc_source|. The network 133 // configurations of the policy will be applied (not necessarily immediately) 134 // to Shill's profiles and enforced in future configurations until the policy 135 // associated with |onc_source| is changed again with this function. For 136 // device policies, |userhash| must be empty. 137 void SetPolicy(onc::ONCSource onc_source, 138 const std::string& userhash, 139 const base::ListValue& network_configs_onc); 140 141 // NetworkProfileObserver overrides 142 virtual void OnProfileAdded(const NetworkProfile& profile) OVERRIDE; 143 virtual void OnProfileRemoved(const NetworkProfile& profile) OVERRIDE; 144 145 private: 146 class PolicyApplicator; 147 148 explicit ManagedNetworkConfigurationHandler( 149 NetworkProfileHandler* profile_handler); 150 virtual ~ManagedNetworkConfigurationHandler(); 151 152 void GetManagedPropertiesCallback( 153 const network_handler::DictionaryResultCallback& callback, 154 const network_handler::ErrorCallback& error_callback, 155 const std::string& service_path, 156 const base::DictionaryValue& shill_properties); 157 158 const GuidToPolicyMap* GetPoliciesForUser(const std::string& userhash) const; 159 const GuidToPolicyMap* GetPoliciesForProfile( 160 const NetworkProfile& profile) const; 161 162 // The DictionaryValues of the nested maps are owned by this class and are 163 // explicitly deleted where necessary. If present, the empty string maps to 164 // the device policy. 165 UserToPoliciesMap policies_by_user_; 166 167 // A local reference to the policy handler singleton. 168 NetworkProfileHandler* profile_handler_; 169 170 // For Shill client callbacks 171 base::WeakPtrFactory<ManagedNetworkConfigurationHandler> weak_ptr_factory_; 172 173 DISALLOW_COPY_AND_ASSIGN(ManagedNetworkConfigurationHandler); 174}; 175 176} // namespace chromeos 177 178#endif // CHROMEOS_NETWORK_MANAGED_NETWORK_CONFIGURATION_HANDLER_H_ 179