[Cronet] Delay StartNetLog and StopNetLog until native request context is initialized
[chromium-blink-merge.git] / chromeos / network / managed_network_configuration_handler_impl.cc
blob9411f4b1b983b942dca6ab51c0756b8b6bb1c260
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.
5 #include "chromeos/network/managed_network_configuration_handler_impl.h"
7 #include <vector>
9 #include "base/bind.h"
10 #include "base/guid.h"
11 #include "base/location.h"
12 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/message_loop/message_loop.h"
15 #include "base/stl_util.h"
16 #include "base/values.h"
17 #include "chromeos/dbus/shill_manager_client.h"
18 #include "chromeos/dbus/shill_profile_client.h"
19 #include "chromeos/dbus/shill_service_client.h"
20 #include "chromeos/network/device_state.h"
21 #include "chromeos/network/network_configuration_handler.h"
22 #include "chromeos/network/network_device_handler.h"
23 #include "chromeos/network/network_event_log.h"
24 #include "chromeos/network/network_policy_observer.h"
25 #include "chromeos/network/network_profile.h"
26 #include "chromeos/network/network_profile_handler.h"
27 #include "chromeos/network/network_state.h"
28 #include "chromeos/network/network_state_handler.h"
29 #include "chromeos/network/network_ui_data.h"
30 #include "chromeos/network/network_util.h"
31 #include "chromeos/network/onc/onc_merger.h"
32 #include "chromeos/network/onc/onc_signature.h"
33 #include "chromeos/network/onc/onc_translator.h"
34 #include "chromeos/network/onc/onc_utils.h"
35 #include "chromeos/network/onc/onc_validator.h"
36 #include "chromeos/network/policy_util.h"
37 #include "chromeos/network/shill_property_util.h"
38 #include "components/onc/onc_constants.h"
39 #include "third_party/cros_system_api/dbus/service_constants.h"
41 namespace chromeos {
43 namespace {
45 using GuidToPolicyMap = ManagedNetworkConfigurationHandler::GuidToPolicyMap;
47 // These are error strings used for error callbacks. None of these error
48 // messages are user-facing: they should only appear in logs.
49 const char kInvalidUserSettings[] = "InvalidUserSettings";
50 const char kNetworkAlreadyConfigured[] = "NetworkAlreadyConfigured";
51 const char kPoliciesNotInitialized[] = "PoliciesNotInitialized";
52 const char kProfileNotInitialized[] = "ProfileNotInitialized";
53 const char kUnconfiguredNetwork[] = "UnconfiguredNetwork";
54 const char kUnknownNetwork[] = "UnknownNetwork";
56 std::string ToDebugString(::onc::ONCSource source,
57 const std::string& userhash) {
58 return source == ::onc::ONC_SOURCE_USER_POLICY ?
59 ("user policy of " + userhash) : "device policy";
62 void InvokeErrorCallback(const std::string& service_path,
63 const network_handler::ErrorCallback& error_callback,
64 const std::string& error_name) {
65 std::string error_msg = "ManagedConfig Error: " + error_name;
66 NET_LOG_ERROR(error_msg, service_path);
67 network_handler::RunErrorCallback(
68 error_callback, service_path, error_name, error_msg);
71 void LogErrorWithDict(const tracked_objects::Location& from_where,
72 const std::string& error_name,
73 scoped_ptr<base::DictionaryValue> error_data) {
74 device_event_log::AddEntry(from_where.file_name(), from_where.line_number(),
75 device_event_log::LOG_TYPE_NETWORK,
76 device_event_log::LOG_LEVEL_ERROR, error_name);
79 const base::DictionaryValue* GetByGUID(const GuidToPolicyMap& policies,
80 const std::string& guid) {
81 GuidToPolicyMap::const_iterator it = policies.find(guid);
82 if (it == policies.end())
83 return NULL;
84 return it->second;
87 } // namespace
89 struct ManagedNetworkConfigurationHandlerImpl::Policies {
90 ~Policies();
92 GuidToPolicyMap per_network_config;
93 base::DictionaryValue global_network_config;
96 ManagedNetworkConfigurationHandlerImpl::Policies::~Policies() {
97 STLDeleteValues(&per_network_config);
100 void ManagedNetworkConfigurationHandlerImpl::AddObserver(
101 NetworkPolicyObserver* observer) {
102 observers_.AddObserver(observer);
105 void ManagedNetworkConfigurationHandlerImpl::RemoveObserver(
106 NetworkPolicyObserver* observer) {
107 observers_.RemoveObserver(observer);
110 // GetManagedProperties
112 void ManagedNetworkConfigurationHandlerImpl::GetManagedProperties(
113 const std::string& userhash,
114 const std::string& service_path,
115 const network_handler::DictionaryResultCallback& callback,
116 const network_handler::ErrorCallback& error_callback) {
117 if (!GetPoliciesForUser(userhash) || !GetPoliciesForUser(std::string())) {
118 InvokeErrorCallback(service_path, error_callback, kPoliciesNotInitialized);
119 return;
121 NET_LOG_USER("GetManagedProperties", service_path);
122 network_configuration_handler_->GetShillProperties(
123 service_path,
124 base::Bind(
125 &ManagedNetworkConfigurationHandlerImpl::GetPropertiesCallback,
126 weak_ptr_factory_.GetWeakPtr(),
127 base::Bind(
128 &ManagedNetworkConfigurationHandlerImpl::SendManagedProperties,
129 weak_ptr_factory_.GetWeakPtr(), userhash, callback,
130 error_callback)),
131 error_callback);
134 void ManagedNetworkConfigurationHandlerImpl::SendManagedProperties(
135 const std::string& userhash,
136 const network_handler::DictionaryResultCallback& callback,
137 const network_handler::ErrorCallback& error_callback,
138 const std::string& service_path,
139 scoped_ptr<base::DictionaryValue> shill_properties) {
140 std::string profile_path;
141 shill_properties->GetStringWithoutPathExpansion(shill::kProfileProperty,
142 &profile_path);
143 const NetworkProfile* profile =
144 network_profile_handler_->GetProfileForPath(profile_path);
145 if (!profile)
146 NET_LOG_ERROR("No profile for service: " + profile_path, service_path);
148 scoped_ptr<NetworkUIData> ui_data =
149 shill_property_util::GetUIDataFromProperties(*shill_properties);
151 const base::DictionaryValue* user_settings = NULL;
153 if (ui_data && profile) {
154 user_settings = ui_data->user_settings();
155 } else if (profile) {
156 NET_LOG_ERROR("Service contains empty or invalid UIData", service_path);
157 // TODO(pneubeck): add a conversion of user configured entries of old
158 // ChromeOS versions. We will have to use a heuristic to determine which
159 // properties _might_ be user configured.
162 std::string guid;
163 shill_properties->GetStringWithoutPathExpansion(shill::kGuidProperty, &guid);
165 ::onc::ONCSource onc_source;
166 FindPolicyByGUID(userhash, guid, &onc_source);
167 scoped_ptr<base::DictionaryValue> active_settings(
168 onc::TranslateShillServiceToONCPart(
169 *shill_properties, onc_source, &onc::kNetworkWithStateSignature));
171 const base::DictionaryValue* network_policy = NULL;
172 const base::DictionaryValue* global_policy = NULL;
173 if (profile) {
174 const Policies* policies = GetPoliciesForProfile(*profile);
175 if (!policies) {
176 InvokeErrorCallback(
177 service_path, error_callback, kPoliciesNotInitialized);
178 return;
180 if (!guid.empty())
181 network_policy = GetByGUID(policies->per_network_config, guid);
182 global_policy = &policies->global_network_config;
185 scoped_ptr<base::DictionaryValue> augmented_properties(
186 policy_util::CreateManagedONC(global_policy,
187 network_policy,
188 user_settings,
189 active_settings.get(),
190 profile));
191 callback.Run(service_path, *augmented_properties);
194 // GetProperties
196 void ManagedNetworkConfigurationHandlerImpl::GetProperties(
197 const std::string& service_path,
198 const network_handler::DictionaryResultCallback& callback,
199 const network_handler::ErrorCallback& error_callback) {
200 NET_LOG_USER("GetProperties", service_path);
201 network_configuration_handler_->GetShillProperties(
202 service_path,
203 base::Bind(
204 &ManagedNetworkConfigurationHandlerImpl::GetPropertiesCallback,
205 weak_ptr_factory_.GetWeakPtr(),
206 base::Bind(&ManagedNetworkConfigurationHandlerImpl::SendProperties,
207 weak_ptr_factory_.GetWeakPtr(), callback, error_callback)),
208 error_callback);
211 void ManagedNetworkConfigurationHandlerImpl::SendProperties(
212 const network_handler::DictionaryResultCallback& callback,
213 const network_handler::ErrorCallback& error_callback,
214 const std::string& service_path,
215 scoped_ptr<base::DictionaryValue> shill_properties) {
216 scoped_ptr<base::DictionaryValue> onc_network(
217 onc::TranslateShillServiceToONCPart(
218 *shill_properties, ::onc::ONC_SOURCE_UNKNOWN,
219 &onc::kNetworkWithStateSignature));
220 callback.Run(service_path, *onc_network);
223 // SetProperties
225 void ManagedNetworkConfigurationHandlerImpl::SetProperties(
226 const std::string& service_path,
227 const base::DictionaryValue& user_settings,
228 const base::Closure& callback,
229 const network_handler::ErrorCallback& error_callback) const {
230 const NetworkState* state =
231 network_state_handler_->GetNetworkStateFromServicePath(
232 service_path, true /* configured_only */);
233 if (!state) {
234 InvokeErrorCallback(service_path, error_callback, kUnknownNetwork);
235 return;
238 std::string guid = state->guid();
239 DCHECK(!guid.empty());
241 const std::string& profile_path = state->profile_path();
242 const NetworkProfile *profile =
243 network_profile_handler_->GetProfileForPath(profile_path);
244 if (!profile) {
245 // TODO(pneubeck): create an initial configuration in this case. As for
246 // CreateConfiguration, user settings from older ChromeOS versions have to
247 // be determined here.
248 InvokeErrorCallback(service_path, error_callback, kUnconfiguredNetwork);
249 return;
252 VLOG(2) << "SetProperties: Found GUID " << guid << " and profile "
253 << profile->ToDebugString();
255 const Policies* policies = GetPoliciesForProfile(*profile);
256 if (!policies) {
257 InvokeErrorCallback(service_path, error_callback, kPoliciesNotInitialized);
258 return;
261 // We need to ensure that required configuration properties (e.g. Type) are
262 // included for ONC validation and translation to Shill properties.
263 scoped_ptr<base::DictionaryValue> user_settings_copy(
264 user_settings.DeepCopy());
265 user_settings_copy->SetStringWithoutPathExpansion(
266 ::onc::network_config::kType,
267 network_util::TranslateShillTypeToONC(state->type()));
268 user_settings_copy->MergeDictionary(&user_settings);
270 // Validate the ONC dictionary. We are liberal and ignore unknown field
271 // names. User settings are only partial ONC, thus we ignore missing fields.
272 onc::Validator validator(false, // Ignore unknown fields.
273 false, // Ignore invalid recommended field names.
274 false, // Ignore missing fields.
275 false); // This ONC does not come from policy.
277 onc::Validator::Result validation_result;
278 scoped_ptr<base::DictionaryValue> validated_user_settings =
279 validator.ValidateAndRepairObject(
280 &onc::kNetworkConfigurationSignature,
281 *user_settings_copy,
282 &validation_result);
283 if (validation_result == onc::Validator::INVALID) {
284 InvokeErrorCallback(service_path, error_callback, kInvalidUserSettings);
285 return;
287 if (validation_result == onc::Validator::VALID_WITH_WARNINGS)
288 LOG(WARNING) << "Validation of ONC user settings produced warnings.";
290 // Fill in HexSSID field from contents of SSID field if not set already.
291 if (user_settings_copy) {
292 onc::FillInHexSSIDFieldsInOncObject(onc::kNetworkConfigurationSignature,
293 validated_user_settings.get());
296 const base::DictionaryValue* network_policy =
297 GetByGUID(policies->per_network_config, guid);
298 VLOG(2) << "This configuration is " << (network_policy ? "" : "not ")
299 << "managed.";
301 scoped_ptr<base::DictionaryValue> shill_dictionary(
302 policy_util::CreateShillConfiguration(*profile,
303 guid,
304 &policies->global_network_config,
305 network_policy,
306 validated_user_settings.get()));
308 network_configuration_handler_->SetShillProperties(
309 service_path, *shill_dictionary,
310 NetworkConfigurationObserver::SOURCE_USER_ACTION, callback,
311 error_callback);
314 void ManagedNetworkConfigurationHandlerImpl::CreateConfiguration(
315 const std::string& userhash,
316 const base::DictionaryValue& properties,
317 const network_handler::StringResultCallback& callback,
318 const network_handler::ErrorCallback& error_callback) const {
319 const Policies* policies = GetPoliciesForUser(userhash);
320 if (!policies) {
321 InvokeErrorCallback("", error_callback, kPoliciesNotInitialized);
322 return;
325 if (policy_util::FindMatchingPolicy(policies->per_network_config,
326 properties)) {
327 InvokeErrorCallback("", error_callback, kNetworkAlreadyConfigured);
328 return;
331 const NetworkProfile* profile =
332 network_profile_handler_->GetProfileForUserhash(userhash);
333 if (!profile) {
334 InvokeErrorCallback("", error_callback, kProfileNotInitialized);
335 return;
338 // TODO(pneubeck): In case of WiFi, check that no other configuration for the
339 // same {SSID, mode, security} exists. We don't support such multiple
340 // configurations, yet.
342 // Generate a new GUID for this configuration. Ignore the maybe provided GUID
343 // in |properties| as it is not our own and from an untrusted source.
344 std::string guid = base::GenerateGUID();
345 scoped_ptr<base::DictionaryValue> shill_dictionary(
346 policy_util::CreateShillConfiguration(*profile,
347 guid,
348 NULL, // no global policy
349 NULL, // no network policy
350 &properties));
352 network_configuration_handler_->CreateShillConfiguration(
353 *shill_dictionary, NetworkConfigurationObserver::SOURCE_USER_ACTION,
354 callback, error_callback);
357 void ManagedNetworkConfigurationHandlerImpl::RemoveConfiguration(
358 const std::string& service_path,
359 const base::Closure& callback,
360 const network_handler::ErrorCallback& error_callback) const {
361 network_configuration_handler_->RemoveConfiguration(
362 service_path, NetworkConfigurationObserver::SOURCE_USER_ACTION, callback,
363 error_callback);
366 void ManagedNetworkConfigurationHandlerImpl::SetPolicy(
367 ::onc::ONCSource onc_source,
368 const std::string& userhash,
369 const base::ListValue& network_configs_onc,
370 const base::DictionaryValue& global_network_config) {
371 VLOG(1) << "Setting policies from " << ToDebugString(onc_source, userhash)
372 << ".";
374 // |userhash| must be empty for device policies.
375 DCHECK(onc_source != ::onc::ONC_SOURCE_DEVICE_POLICY ||
376 userhash.empty());
377 Policies* policies = NULL;
378 if (ContainsKey(policies_by_user_, userhash)) {
379 policies = policies_by_user_[userhash].get();
380 } else {
381 policies = new Policies;
382 policies_by_user_[userhash] = make_linked_ptr(policies);
385 policies->global_network_config.MergeDictionary(&global_network_config);
387 GuidToPolicyMap old_per_network_config;
388 policies->per_network_config.swap(old_per_network_config);
390 // This stores all GUIDs of policies that have changed or are new.
391 std::set<std::string> modified_policies;
393 for (base::ListValue::const_iterator it = network_configs_onc.begin();
394 it != network_configs_onc.end(); ++it) {
395 const base::DictionaryValue* network = NULL;
396 (*it)->GetAsDictionary(&network);
397 DCHECK(network);
399 std::string guid;
400 network->GetStringWithoutPathExpansion(::onc::network_config::kGUID, &guid);
401 DCHECK(!guid.empty());
403 if (policies->per_network_config.count(guid) > 0) {
404 NET_LOG_ERROR("ONC from " + ToDebugString(onc_source, userhash) +
405 " contains several entries for the same GUID ", guid);
406 delete policies->per_network_config[guid];
408 const base::DictionaryValue* new_entry = network->DeepCopy();
409 policies->per_network_config[guid] = new_entry;
411 const base::DictionaryValue* old_entry = old_per_network_config[guid];
412 if (!old_entry || !old_entry->Equals(new_entry))
413 modified_policies.insert(guid);
416 STLDeleteValues(&old_per_network_config);
417 ApplyOrQueuePolicies(userhash, &modified_policies);
418 FOR_EACH_OBSERVER(NetworkPolicyObserver, observers_,
419 PoliciesChanged(userhash));
422 bool ManagedNetworkConfigurationHandlerImpl::IsAnyPolicyApplicationRunning()
423 const {
424 return !policy_applicators_.empty() || !queued_modified_policies_.empty();
427 bool ManagedNetworkConfigurationHandlerImpl::ApplyOrQueuePolicies(
428 const std::string& userhash,
429 std::set<std::string>* modified_policies) {
430 DCHECK(modified_policies);
432 const NetworkProfile* profile =
433 network_profile_handler_->GetProfileForUserhash(userhash);
434 if (!profile) {
435 VLOG(1) << "The relevant Shill profile isn't initialized yet, postponing "
436 << "policy application.";
437 // OnProfileAdded will apply all policies for this userhash.
438 return false;
441 if (ContainsKey(policy_applicators_, userhash)) {
442 // A previous policy application is still running. Queue the modified
443 // policies.
444 // Note, even if |modified_policies| is empty, this means that a policy
445 // application will be queued.
446 queued_modified_policies_[userhash].insert(modified_policies->begin(),
447 modified_policies->end());
448 VLOG(1) << "Previous PolicyApplicator still running. Postponing policy "
449 "application.";
450 return false;
453 const Policies* policies = policies_by_user_[userhash].get();
454 DCHECK(policies);
456 PolicyApplicator* applicator =
457 new PolicyApplicator(*profile,
458 policies->per_network_config,
459 policies->global_network_config,
460 this,
461 modified_policies);
462 policy_applicators_[userhash] = make_linked_ptr(applicator);
463 applicator->Run();
464 return true;
467 void ManagedNetworkConfigurationHandlerImpl::OnProfileAdded(
468 const NetworkProfile& profile) {
469 VLOG(1) << "Adding profile " << profile.ToDebugString() << "'.";
471 const Policies* policies = GetPoliciesForProfile(profile);
472 if (!policies) {
473 VLOG(1) << "The relevant policy is not initialized, "
474 << "postponing policy application.";
475 // See SetPolicy.
476 return;
479 std::set<std::string> policy_guids;
480 for (GuidToPolicyMap::const_iterator it =
481 policies->per_network_config.begin();
482 it != policies->per_network_config.end(); ++it) {
483 policy_guids.insert(it->first);
486 const bool started_policy_application =
487 ApplyOrQueuePolicies(profile.userhash, &policy_guids);
488 DCHECK(started_policy_application);
491 void ManagedNetworkConfigurationHandlerImpl::OnProfileRemoved(
492 const NetworkProfile& profile) {
493 // Nothing to do in this case.
496 void ManagedNetworkConfigurationHandlerImpl::CreateConfigurationFromPolicy(
497 const base::DictionaryValue& shill_properties) {
498 network_configuration_handler_->CreateShillConfiguration(
499 shill_properties, NetworkConfigurationObserver::SOURCE_POLICY,
500 base::Bind(
501 &ManagedNetworkConfigurationHandlerImpl::OnPolicyAppliedToNetwork,
502 weak_ptr_factory_.GetWeakPtr()),
503 base::Bind(&LogErrorWithDict, FROM_HERE));
506 void ManagedNetworkConfigurationHandlerImpl::
507 UpdateExistingConfigurationWithPropertiesFromPolicy(
508 const base::DictionaryValue& existing_properties,
509 const base::DictionaryValue& new_properties) {
510 base::DictionaryValue shill_properties;
512 std::string profile;
513 existing_properties.GetStringWithoutPathExpansion(shill::kProfileProperty,
514 &profile);
515 if (profile.empty()) {
516 NET_LOG_ERROR("Missing profile property",
517 shill_property_util::GetNetworkIdFromProperties(
518 existing_properties));
519 return;
521 shill_properties.SetStringWithoutPathExpansion(shill::kProfileProperty,
522 profile);
524 if (!shill_property_util::CopyIdentifyingProperties(
525 existing_properties,
526 true /* properties were read from Shill */,
527 &shill_properties)) {
528 NET_LOG_ERROR("Missing identifying properties",
529 shill_property_util::GetNetworkIdFromProperties(
530 existing_properties));
533 shill_properties.MergeDictionary(&new_properties);
535 network_configuration_handler_->CreateShillConfiguration(
536 shill_properties, NetworkConfigurationObserver::SOURCE_POLICY,
537 base::Bind(
538 &ManagedNetworkConfigurationHandlerImpl::OnPolicyAppliedToNetwork,
539 weak_ptr_factory_.GetWeakPtr()),
540 base::Bind(&LogErrorWithDict, FROM_HERE));
543 void ManagedNetworkConfigurationHandlerImpl::OnPoliciesApplied(
544 const NetworkProfile& profile) {
545 const std::string& userhash = profile.userhash;
546 VLOG(1) << "Policy application for user '" << userhash << "' finished.";
548 base::MessageLoop::current()->DeleteSoon(
549 FROM_HERE, policy_applicators_[userhash].release());
550 policy_applicators_.erase(userhash);
552 if (ContainsKey(queued_modified_policies_, userhash)) {
553 std::set<std::string> modified_policies;
554 queued_modified_policies_[userhash].swap(modified_policies);
555 // Remove |userhash| from the queue.
556 queued_modified_policies_.erase(userhash);
557 ApplyOrQueuePolicies(userhash, &modified_policies);
558 } else {
559 FOR_EACH_OBSERVER(
560 NetworkPolicyObserver, observers_, PoliciesApplied(userhash));
564 const base::DictionaryValue*
565 ManagedNetworkConfigurationHandlerImpl::FindPolicyByGUID(
566 const std::string userhash,
567 const std::string& guid,
568 ::onc::ONCSource* onc_source) const {
569 *onc_source = ::onc::ONC_SOURCE_NONE;
571 if (!userhash.empty()) {
572 const Policies* user_policies = GetPoliciesForUser(userhash);
573 if (user_policies) {
574 const base::DictionaryValue* policy =
575 GetByGUID(user_policies->per_network_config, guid);
576 if (policy) {
577 *onc_source = ::onc::ONC_SOURCE_USER_POLICY;
578 return policy;
583 const Policies* device_policies = GetPoliciesForUser(std::string());
584 if (device_policies) {
585 const base::DictionaryValue* policy =
586 GetByGUID(device_policies->per_network_config, guid);
587 if (policy) {
588 *onc_source = ::onc::ONC_SOURCE_DEVICE_POLICY;
589 return policy;
593 return NULL;
596 const GuidToPolicyMap*
597 ManagedNetworkConfigurationHandlerImpl::GetNetworkConfigsFromPolicy(
598 const std::string& userhash) const {
599 const Policies* policies = GetPoliciesForUser(userhash);
600 if (!policies)
601 return NULL;
603 return &policies->per_network_config;
606 const base::DictionaryValue*
607 ManagedNetworkConfigurationHandlerImpl::GetGlobalConfigFromPolicy(
608 const std::string& userhash) const {
609 const Policies* policies = GetPoliciesForUser(userhash);
610 if (!policies)
611 return NULL;
613 return &policies->global_network_config;
616 const base::DictionaryValue*
617 ManagedNetworkConfigurationHandlerImpl::FindPolicyByGuidAndProfile(
618 const std::string& guid,
619 const std::string& profile_path) const {
620 const NetworkProfile* profile =
621 network_profile_handler_->GetProfileForPath(profile_path);
622 if (!profile) {
623 NET_LOG_ERROR("Profile path unknown:" + profile_path, guid);
624 return NULL;
627 const Policies* policies = GetPoliciesForProfile(*profile);
628 if (!policies)
629 return NULL;
631 return GetByGUID(policies->per_network_config, guid);
634 const ManagedNetworkConfigurationHandlerImpl::Policies*
635 ManagedNetworkConfigurationHandlerImpl::GetPoliciesForUser(
636 const std::string& userhash) const {
637 UserToPoliciesMap::const_iterator it = policies_by_user_.find(userhash);
638 if (it == policies_by_user_.end())
639 return NULL;
640 return it->second.get();
643 const ManagedNetworkConfigurationHandlerImpl::Policies*
644 ManagedNetworkConfigurationHandlerImpl::GetPoliciesForProfile(
645 const NetworkProfile& profile) const {
646 DCHECK(profile.type() != NetworkProfile::TYPE_SHARED ||
647 profile.userhash.empty());
648 return GetPoliciesForUser(profile.userhash);
651 ManagedNetworkConfigurationHandlerImpl::ManagedNetworkConfigurationHandlerImpl()
652 : network_state_handler_(NULL),
653 network_profile_handler_(NULL),
654 network_configuration_handler_(NULL),
655 network_device_handler_(NULL),
656 weak_ptr_factory_(this) {
657 CHECK(base::MessageLoop::current());
660 ManagedNetworkConfigurationHandlerImpl::
661 ~ManagedNetworkConfigurationHandlerImpl() {
662 if (network_profile_handler_)
663 network_profile_handler_->RemoveObserver(this);
666 void ManagedNetworkConfigurationHandlerImpl::Init(
667 NetworkStateHandler* network_state_handler,
668 NetworkProfileHandler* network_profile_handler,
669 NetworkConfigurationHandler* network_configuration_handler,
670 NetworkDeviceHandler* network_device_handler) {
671 network_state_handler_ = network_state_handler;
672 network_profile_handler_ = network_profile_handler;
673 network_configuration_handler_ = network_configuration_handler;
674 network_device_handler_ = network_device_handler;
675 network_profile_handler_->AddObserver(this);
678 void ManagedNetworkConfigurationHandlerImpl::OnPolicyAppliedToNetwork(
679 const std::string& service_path) {
680 if (service_path.empty())
681 return;
682 FOR_EACH_OBSERVER(
683 NetworkPolicyObserver, observers_, PolicyAppliedToNetwork(service_path));
686 // Get{Managed}Properties helpers
688 void ManagedNetworkConfigurationHandlerImpl::GetDeviceStateProperties(
689 const std::string& service_path,
690 base::DictionaryValue* properties) {
691 std::string connection_state;
692 properties->GetStringWithoutPathExpansion(
693 shill::kStateProperty, &connection_state);
694 if (!NetworkState::StateIsConnected(connection_state))
695 return;
697 // Get the IPConfig properties from the device and store them in "IPConfigs"
698 // (plural) in the properties dictionary. (Note: Shill only provides a single
699 // "IPConfig" property for a network service, but a consumer of this API may
700 // want information about all ipv4 and ipv6 IPConfig properties.
701 std::string device;
702 properties->GetStringWithoutPathExpansion(shill::kDeviceProperty, &device);
703 const DeviceState* device_state =
704 network_state_handler_->GetDeviceState(device);
705 if (!device_state) {
706 NET_LOG_ERROR("GetDeviceProperties: no device: " + device, service_path);
707 return;
710 // Get the hardware MAC address from the DeviceState.
711 if (!device_state->mac_address().empty()) {
712 properties->SetStringWithoutPathExpansion(
713 shill::kAddressProperty, device_state->mac_address());
716 // Convert IPConfig dictionary to a ListValue.
717 base::ListValue* ip_configs = new base::ListValue;
718 for (base::DictionaryValue::Iterator iter(device_state->ip_configs());
719 !iter.IsAtEnd(); iter.Advance()) {
720 ip_configs->Append(iter.value().DeepCopy());
722 properties->SetWithoutPathExpansion(shill::kIPConfigsProperty, ip_configs);
725 void ManagedNetworkConfigurationHandlerImpl::GetPropertiesCallback(
726 GetDevicePropertiesCallback send_callback,
727 const std::string& service_path,
728 const base::DictionaryValue& shill_properties) {
729 scoped_ptr<base::DictionaryValue> shill_properties_copy(
730 shill_properties.DeepCopy());
732 std::string guid;
733 shill_properties.GetStringWithoutPathExpansion(shill::kGuidProperty, &guid);
734 if (guid.empty()) {
735 // Unmanaged networks are assigned a GUID in NetworkState. Provide this
736 // value in the ONC dictionary.
737 const NetworkState* state =
738 network_state_handler_->GetNetworkState(service_path);
739 if (state && !state->guid().empty()) {
740 guid = state->guid();
741 shill_properties_copy->SetStringWithoutPathExpansion(shill::kGuidProperty,
742 guid);
743 } else {
744 LOG(ERROR) << "Network has no GUID specified: " << service_path;
748 std::string type;
749 shill_properties_copy->GetStringWithoutPathExpansion(shill::kTypeProperty,
750 &type);
751 // Add associated DeviceState properties for non-VPN networks.
752 if (type != shill::kTypeVPN)
753 GetDeviceStateProperties(service_path, shill_properties_copy.get());
755 // Only request additional Device properties for Cellular networks with a
756 // valid device.
757 std::string device_path;
758 if (!network_device_handler_ ||
759 type != shill::kTypeCellular ||
760 !shill_properties_copy->GetStringWithoutPathExpansion(
761 shill::kDeviceProperty, &device_path) ||
762 device_path.empty()) {
763 send_callback.Run(service_path, shill_properties_copy.Pass());
764 return;
767 // Request the device properties. On success or failure pass (a possibly
768 // modified) |shill_properties| to |send_callback|.
769 scoped_ptr<base::DictionaryValue> shill_properties_copy_error_copy(
770 shill_properties_copy->DeepCopy());
771 network_device_handler_->GetDeviceProperties(
772 device_path,
773 base::Bind(&ManagedNetworkConfigurationHandlerImpl::
774 GetDevicePropertiesSuccess,
775 weak_ptr_factory_.GetWeakPtr(),
776 service_path,
777 base::Passed(&shill_properties_copy),
778 send_callback),
779 base::Bind(&ManagedNetworkConfigurationHandlerImpl::
780 GetDevicePropertiesFailure,
781 weak_ptr_factory_.GetWeakPtr(),
782 service_path,
783 base::Passed(&shill_properties_copy_error_copy),
784 send_callback));
787 void ManagedNetworkConfigurationHandlerImpl::GetDevicePropertiesSuccess(
788 const std::string& service_path,
789 scoped_ptr<base::DictionaryValue> network_properties,
790 GetDevicePropertiesCallback send_callback,
791 const std::string& device_path,
792 const base::DictionaryValue& device_properties) {
793 // Create a "Device" dictionary in |network_properties|.
794 network_properties->SetWithoutPathExpansion(
795 shill::kDeviceProperty, device_properties.DeepCopy());
796 send_callback.Run(service_path, network_properties.Pass());
799 void ManagedNetworkConfigurationHandlerImpl::GetDevicePropertiesFailure(
800 const std::string& service_path,
801 scoped_ptr<base::DictionaryValue> network_properties,
802 GetDevicePropertiesCallback send_callback,
803 const std::string& error_name,
804 scoped_ptr<base::DictionaryValue> error_data) {
805 NET_LOG_ERROR("Error getting device properties", service_path);
806 send_callback.Run(service_path, network_properties.Pass());
810 } // namespace chromeos