| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chromeos/network/network_profile_handler.h" | 5 #include "chromeos/network/network_profile_handler.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "chromeos/dbus/dbus_thread_manager.h" | 12 #include "chromeos/dbus/dbus_thread_manager.h" |
| 13 #include "chromeos/dbus/shill_manager_client.h" | 13 #include "chromeos/dbus/shill_manager_client.h" |
| 14 #include "chromeos/dbus/shill_profile_client.h" | 14 #include "chromeos/dbus/shill_profile_client.h" |
| 15 #include "chromeos/network/network_profile_observer.h" | 15 #include "chromeos/network/network_profile_observer.h" |
| 16 #include "dbus/object_path.h" | 16 #include "dbus/object_path.h" |
| 17 #include "third_party/cros_system_api/dbus/service_constants.h" | 17 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 18 | 18 |
| 19 namespace chromeos { | 19 namespace chromeos { |
| 20 | 20 |
| 21 static NetworkProfileHandler* g_profile_handler_instance = NULL; | |
| 22 | |
| 23 namespace { | 21 namespace { |
| 24 | 22 |
| 25 bool ConvertListValueToStringVector(const base::ListValue& string_list, | 23 bool ConvertListValueToStringVector(const base::ListValue& string_list, |
| 26 std::vector<std::string>* result) { | 24 std::vector<std::string>* result) { |
| 27 for (size_t i = 0; i < string_list.GetSize(); ++i) { | 25 for (size_t i = 0; i < string_list.GetSize(); ++i) { |
| 28 std::string str; | 26 std::string str; |
| 29 if (!string_list.GetString(i, &str)) | 27 if (!string_list.GetString(i, &str)) |
| 30 return false; | 28 return false; |
| 31 result->push_back(str); | 29 result->push_back(str); |
| 32 } | 30 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 47 } | 45 } |
| 48 | 46 |
| 49 bool operator()(const NetworkProfile& profile) { | 47 bool operator()(const NetworkProfile& profile) { |
| 50 return profile.path == path_; | 48 return profile.path == path_; |
| 51 } | 49 } |
| 52 | 50 |
| 53 private: | 51 private: |
| 54 std::string path_; | 52 std::string path_; |
| 55 }; | 53 }; |
| 56 | 54 |
| 57 class NetworkProfileHandlerImpl : public NetworkProfileHandler { | |
| 58 public: | |
| 59 NetworkProfileHandlerImpl() { | |
| 60 DBusThreadManager::Get()->GetShillManagerClient()-> | |
| 61 AddPropertyChangedObserver(this); | |
| 62 | |
| 63 // Request the initial profile list. | |
| 64 DBusThreadManager::Get()->GetShillManagerClient()->GetProperties( | |
| 65 base::Bind(&NetworkProfileHandler::GetManagerPropertiesCallback, | |
| 66 weak_ptr_factory_.GetWeakPtr())); | |
| 67 } | |
| 68 | |
| 69 virtual ~NetworkProfileHandlerImpl() { | |
| 70 DBusThreadManager::Get()->GetShillManagerClient()-> | |
| 71 RemovePropertyChangedObserver(this); | |
| 72 } | |
| 73 }; | |
| 74 | |
| 75 } // namespace | 55 } // namespace |
| 76 | 56 |
| 77 // static | |
| 78 NetworkProfileHandler* NetworkProfileHandler::Initialize() { | |
| 79 CHECK(!g_profile_handler_instance); | |
| 80 g_profile_handler_instance = new NetworkProfileHandlerImpl(); | |
| 81 return g_profile_handler_instance; | |
| 82 } | |
| 83 | |
| 84 // static | |
| 85 bool NetworkProfileHandler::IsInitialized() { | |
| 86 return g_profile_handler_instance; | |
| 87 } | |
| 88 | |
| 89 // static | |
| 90 void NetworkProfileHandler::Shutdown() { | |
| 91 CHECK(g_profile_handler_instance); | |
| 92 delete g_profile_handler_instance; | |
| 93 g_profile_handler_instance = NULL; | |
| 94 } | |
| 95 | |
| 96 // static | |
| 97 NetworkProfileHandler* NetworkProfileHandler::Get() { | |
| 98 CHECK(g_profile_handler_instance); | |
| 99 return g_profile_handler_instance; | |
| 100 } | |
| 101 | |
| 102 void NetworkProfileHandler::AddObserver(NetworkProfileObserver* observer) { | 57 void NetworkProfileHandler::AddObserver(NetworkProfileObserver* observer) { |
| 103 observers_.AddObserver(observer); | 58 observers_.AddObserver(observer); |
| 104 } | 59 } |
| 105 | 60 |
| 106 void NetworkProfileHandler::RemoveObserver(NetworkProfileObserver* observer) { | 61 void NetworkProfileHandler::RemoveObserver(NetworkProfileObserver* observer) { |
| 107 observers_.RemoveObserver(observer); | 62 observers_.RemoveObserver(observer); |
| 108 } | 63 } |
| 109 | 64 |
| 110 void NetworkProfileHandler::GetManagerPropertiesCallback( | 65 void NetworkProfileHandler::GetManagerPropertiesCallback( |
| 111 DBusMethodCallStatus call_status, | 66 DBusMethodCallStatus call_status, |
| (...skipping 20 matching lines...) Expand all Loading... |
| 132 | 87 |
| 133 const base::ListValue* profiles_value = NULL; | 88 const base::ListValue* profiles_value = NULL; |
| 134 value.GetAsList(&profiles_value); | 89 value.GetAsList(&profiles_value); |
| 135 DCHECK(profiles_value); | 90 DCHECK(profiles_value); |
| 136 | 91 |
| 137 std::vector<std::string> new_profile_paths; | 92 std::vector<std::string> new_profile_paths; |
| 138 bool result = ConvertListValueToStringVector(*profiles_value, | 93 bool result = ConvertListValueToStringVector(*profiles_value, |
| 139 &new_profile_paths); | 94 &new_profile_paths); |
| 140 DCHECK(result); | 95 DCHECK(result); |
| 141 | 96 |
| 97 VLOG(2) << "Profiles: " << profiles_.size(); |
| 142 // Search for removed profiles. | 98 // Search for removed profiles. |
| 143 std::vector<std::string> removed_profile_paths; | 99 std::vector<std::string> removed_profile_paths; |
| 144 for (ProfileList::const_iterator it = profiles_.begin(); | 100 for (ProfileList::const_iterator it = profiles_.begin(); |
| 145 it != profiles_.end(); ++it) { | 101 it != profiles_.end(); ++it) { |
| 146 if (std::find(new_profile_paths.begin(), | 102 if (std::find(new_profile_paths.begin(), |
| 147 new_profile_paths.end(), | 103 new_profile_paths.end(), |
| 148 it->path) == new_profile_paths.end()) { | 104 it->path) == new_profile_paths.end()) { |
| 149 removed_profile_paths.push_back(it->path); | 105 removed_profile_paths.push_back(it->path); |
| 150 } | 106 } |
| 151 } | 107 } |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 profiles_.begin(); | 174 profiles_.begin(); |
| 219 it != profiles_.end(); ++it) { | 175 it != profiles_.end(); ++it) { |
| 220 if (it->userhash == userhash) | 176 if (it->userhash == userhash) |
| 221 return &*it; | 177 return &*it; |
| 222 } | 178 } |
| 223 return NULL; | 179 return NULL; |
| 224 } | 180 } |
| 225 | 181 |
| 226 NetworkProfileHandler::NetworkProfileHandler() | 182 NetworkProfileHandler::NetworkProfileHandler() |
| 227 : weak_ptr_factory_(this) { | 183 : weak_ptr_factory_(this) { |
| 184 DBusThreadManager::Get()->GetShillManagerClient()-> |
| 185 AddPropertyChangedObserver(this); |
| 186 |
| 187 // Request the initial profile list. |
| 188 DBusThreadManager::Get()->GetShillManagerClient()->GetProperties( |
| 189 base::Bind(&NetworkProfileHandler::GetManagerPropertiesCallback, |
| 190 weak_ptr_factory_.GetWeakPtr())); |
| 228 } | 191 } |
| 229 | 192 |
| 230 NetworkProfileHandler::~NetworkProfileHandler() { | 193 NetworkProfileHandler::~NetworkProfileHandler() { |
| 194 DBusThreadManager::Get()->GetShillManagerClient()-> |
| 195 RemovePropertyChangedObserver(this); |
| 231 } | 196 } |
| 232 | 197 |
| 233 } // namespace chromeos | 198 } // namespace chromeos |
| OLD | NEW |