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 "chromeos/network/favorite_state.h" 6 7 #include "base/logging.h" 8 #include "base/memory/scoped_ptr.h" 9 #include "base/strings/stringprintf.h" 10 #include "chromeos/network/network_event_log.h" 11 #include "chromeos/network/network_profile_handler.h" 12 #include "chromeos/network/network_state.h" 13 #include "chromeos/network/onc/onc_utils.h" 14 #include "chromeos/network/shill_property_util.h" 15 #include "third_party/cros_system_api/dbus/service_constants.h" 16 17 namespace chromeos { 18 19 FavoriteState::FavoriteState(const std::string& path) 20 : ManagedState(MANAGED_TYPE_FAVORITE, path) { 21 } 22 23 FavoriteState::~FavoriteState() { 24 } 25 26 bool FavoriteState::PropertyChanged(const std::string& key, 27 const base::Value& value) { 28 if (ManagedStatePropertyChanged(key, value)) 29 return true; 30 if (key == shill::kProfileProperty) { 31 return GetStringValue(key, value, &profile_path_); 32 } else if (key == shill::kUIDataProperty) { 33 scoped_ptr<NetworkUIData> new_ui_data = 34 shill_property_util::GetUIDataFromValue(value); 35 if (!new_ui_data) { 36 NET_LOG_ERROR("Failed to parse " + key, path()); 37 return false; 38 } 39 ui_data_ = *new_ui_data; 40 return true; 41 } else if (key == shill::kGuidProperty) { 42 return GetStringValue(key, value, &guid_); 43 } else if (key == shill::kProxyConfigProperty) { 44 std::string proxy_config_str; 45 if (!value.GetAsString(&proxy_config_str)) { 46 NET_LOG_ERROR("Failed to parse " + key, path()); 47 return false; 48 } 49 50 proxy_config_.Clear(); 51 if (proxy_config_str.empty()) 52 return true; 53 54 scoped_ptr<base::DictionaryValue> proxy_config_dict( 55 onc::ReadDictionaryFromJson(proxy_config_str)); 56 if (proxy_config_dict) { 57 // Warning: The DictionaryValue returned from 58 // ReadDictionaryFromJson/JSONParser is an optimized derived class that 59 // doesn't allow releasing ownership of nested values. A Swap in the wrong 60 // order leads to memory access errors. 61 proxy_config_.MergeDictionary(proxy_config_dict.get()); 62 } else { 63 NET_LOG_ERROR("Failed to parse " + key, path()); 64 } 65 return true; 66 } 67 return false; 68 } 69 70 bool FavoriteState::IsPrivate() const { 71 return !profile_path_.empty() && 72 profile_path_ != NetworkProfileHandler::kSharedProfilePath; 73 } 74 75 } // namespace chromeos 76