Index: chrome/browser/ui/webui/chromeos/network_config_message_handler.cc |
diff --git a/chrome/browser/ui/webui/chromeos/network_config_message_handler.cc b/chrome/browser/ui/webui/chromeos/network_config_message_handler.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..dbc4a4859f311e523faa89b1f9a85d7fb0d72d0a |
--- /dev/null |
+++ b/chrome/browser/ui/webui/chromeos/network_config_message_handler.cc |
@@ -0,0 +1,111 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/ui/webui/chromeos/network_config_message_handler.h" |
+ |
+#include <string> |
+ |
+#include "base/bind.h" |
+#include "base/bind_helpers.h" |
+#include "base/values.h" |
+#include "chromeos/network/favorite_state.h" |
+#include "chromeos/network/network_state.h" |
+#include "chromeos/network/network_state_handler.h" |
+#include "chromeos/network/onc/onc_signature.h" |
+#include "chromeos/network/onc/onc_translator.h" |
+#include "chromeos/network/onc/onc_utils.h" |
+#include "chromeos/network/shill_property_util.h" |
+#include "components/onc/onc_constants.h" |
+#include "content/public/browser/web_ui.h" |
+#include "third_party/cros_system_api/dbus/service_constants.h" |
+ |
+namespace { |
+ |
+void CopyStringFromDictionary(const std::string& key, |
+ const base::DictionaryValue& source, |
+ base::DictionaryValue* dest) { |
+ std::string string_value; |
+ if (source.GetStringWithoutPathExpansion(key, &string_value)) |
+ dest->SetStringWithoutPathExpansion(key, string_value); |
+} |
+ |
+} // namespace |
+ |
+namespace chromeos { |
+ |
+NetworkConfigMessageHandler::NetworkConfigMessageHandler() { |
+} |
+ |
+NetworkConfigMessageHandler::~NetworkConfigMessageHandler() { |
+} |
+ |
+void NetworkConfigMessageHandler::RegisterMessages() { |
+ web_ui()->RegisterMessageCallback( |
+ "networkConfig.getVisibleNetworks", |
+ base::Bind(&NetworkConfigMessageHandler::GetVisibleNetworks, |
+ base::Unretained(this))); |
+ web_ui()->RegisterMessageCallback( |
+ "networkConfig.getFavoriteNetworks", |
+ base::Bind(&NetworkConfigMessageHandler::GetFavoriteNetworks, |
+ base::Unretained(this))); |
+} |
+ |
+void NetworkConfigMessageHandler::GetVisibleNetworks( |
+ const base::ListValue* value) const { |
michaelpg
2014/04/29 19:51:00
Can you add a comment explaining what value is exp
stevenjb
2014/05/01 01:45:14
Added to header.
|
+ std::string type; |
+ value->GetString(0, &type); |
+ NetworkTypePattern type_pattern = onc::NetworkTypePatternFromOncType(type); |
michaelpg
2014/04/29 19:51:00
What sorcery is this?
stevenjb
2014/04/29 21:44:03
See NetworkTypePattern in shill_property_util.h
|
+ NetworkStateHandler::NetworkStateList network_states; |
+ NetworkHandler::Get()->network_state_handler()->GetNetworkListByType( |
+ type_pattern, &network_states); |
+ |
+ base::ListValue network_properties_list; |
+ for (NetworkStateHandler::NetworkStateList::iterator it = |
+ network_states.begin(); |
+ it != network_states.end(); ++it) { |
+ base::DictionaryValue shill_dictionary; |
+ (*it)->GetStateProperties(&shill_dictionary); |
+ scoped_ptr<base::DictionaryValue> onc_network_part = |
+ chromeos::onc::TranslateShillServiceToONCPart( |
+ shill_dictionary, &chromeos::onc::kNetworkWithStateSignature); |
+ onc_network_part->SetStringWithoutPathExpansion( |
+ ::onc::network_config::kServicePath, (*it)->path()); |
+ network_properties_list.Append(onc_network_part.release()); |
+ } |
+ web_ui()->CallJavascriptFunction( |
+ "networkConfig.getVisibleNetworksCallback", network_properties_list); |
+} |
+ |
+void NetworkConfigMessageHandler::GetFavoriteNetworks( |
+ const base::ListValue* value) const { |
michaelpg
2014/04/29 19:51:00
same as 55
stevenjb
2014/05/01 01:45:14
Done.
|
+ std::string type; |
+ value->GetString(0, &type); |
+ NetworkTypePattern type_pattern = onc::NetworkTypePatternFromOncType(type); |
+ NetworkStateHandler::FavoriteStateList favorite_states; |
+ NetworkHandler::Get()->network_state_handler()->GetFavoriteListByType( |
+ type_pattern, &favorite_states); |
+ |
+ base::ListValue favorite_properties_list; |
+ for (NetworkStateHandler::FavoriteStateList::iterator it = |
+ favorite_states.begin(); |
+ it != favorite_states.end(); ++it) { |
+ base::DictionaryValue shill_dictionary; |
+ (*it)->GetStateProperties(&shill_dictionary); |
+ scoped_ptr<base::DictionaryValue> onc_network_part = |
+ chromeos::onc::TranslateShillServiceToONCPart( |
+ shill_dictionary, &chromeos::onc::kNetworkWithStateSignature); |
+ onc_network_part->SetStringWithoutPathExpansion( |
+ ::onc::network_config::kServicePath, (*it)->path()); |
+ // Copy untranslated (non ONC) values. |
+ CopyStringFromDictionary( |
+ shill::kProfileProperty, shill_dictionary, onc_network_part.get()); |
+ CopyStringFromDictionary( |
+ NetworkUIData::kKeyONCSource, shill_dictionary, onc_network_part.get()); |
+ favorite_properties_list.Append(onc_network_part.release()); |
+ } |
+ web_ui()->CallJavascriptFunction( |
+ "networkConfig.getFavoriteNetworksCallback", favorite_properties_list); |
+} |
+ |
+} // namespace chromeos |