1 // Copyright 2014 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 "chrome/browser/ui/webui/chromeos/network_config_message_handler.h" 6 7 #include <string> 8 9 #include "base/bind.h" 10 #include "base/bind_helpers.h" 11 #include "base/logging.h" 12 #include "base/values.h" 13 #include "chromeos/network/managed_network_configuration_handler.h" 14 #include "chromeos/network/network_state.h" 15 #include "chromeos/network/network_state_handler.h" 16 #include "chromeos/network/network_util.h" 17 #include "chromeos/network/onc/onc_signature.h" 18 #include "chromeos/network/onc/onc_utils.h" 19 #include "chromeos/network/shill_property_util.h" 20 #include "components/onc/onc_constants.h" 21 #include "content/public/browser/web_ui.h" 22 #include "third_party/cros_system_api/dbus/service_constants.h" 23 24 namespace chromeos { 25 26 namespace { 27 28 bool GetServicePathFromGuid(const std::string& guid, 29 std::string* service_path) { 30 const NetworkState* network = 31 NetworkHandler::Get()->network_state_handler()->GetNetworkStateFromGuid( 32 guid); 33 if (!network) 34 return false; 35 *service_path = network->path(); 36 return true; 37 } 38 39 } // namespace 40 41 NetworkConfigMessageHandler::NetworkConfigMessageHandler() 42 : weak_ptr_factory_(this) { 43 } 44 45 NetworkConfigMessageHandler::~NetworkConfigMessageHandler() { 46 } 47 48 void NetworkConfigMessageHandler::RegisterMessages() { 49 web_ui()->RegisterMessageCallback( 50 "networkConfig.getNetworks", 51 base::Bind(&NetworkConfigMessageHandler::GetNetworks, 52 base::Unretained(this))); 53 web_ui()->RegisterMessageCallback( 54 "networkConfig.getProperties", 55 base::Bind(&NetworkConfigMessageHandler::GetProperties, 56 base::Unretained(this))); 57 } 58 59 void NetworkConfigMessageHandler::GetNetworks( 60 const base::ListValue* arg_list) const { 61 int callback_id = 0; 62 const base::DictionaryValue* filter = NULL; 63 if (!arg_list->GetInteger(0, &callback_id) || 64 !arg_list->GetDictionary(1, &filter)) { 65 NOTREACHED(); 66 } 67 std::string type = ::onc::network_type::kAllTypes; 68 bool visible_only = false; 69 bool configured_only = false; 70 int limit = 1000; 71 filter->GetString("type", &type); 72 NetworkTypePattern pattern = onc::NetworkTypePatternFromOncType(type); 73 filter->GetBoolean("visible", &visible_only); 74 filter->GetBoolean("configured", &configured_only); 75 filter->GetInteger("limit", &limit); 76 77 base::ListValue return_arg_list; 78 return_arg_list.AppendInteger(callback_id); 79 80 scoped_ptr<base::ListValue> network_properties_list = 81 chromeos::network_util::TranslateNetworkListToONC( 82 pattern, configured_only, visible_only, limit, 83 true /* debugging_properties */); 84 85 return_arg_list.Append(network_properties_list.release()); 86 87 InvokeCallback(return_arg_list); 88 } 89 90 void NetworkConfigMessageHandler::GetProperties( 91 const base::ListValue* arg_list) { 92 int callback_id = 0; 93 std::string guid; 94 if (!arg_list->GetInteger(0, &callback_id) || 95 !arg_list->GetString(1, &guid)) { 96 NOTREACHED(); 97 } 98 std::string service_path; 99 if (!GetServicePathFromGuid(guid, &service_path)) { 100 scoped_ptr<base::DictionaryValue> error_data; 101 ErrorCallback(callback_id, "Error.InvalidNetworkGuid", error_data.Pass()); 102 return; 103 } 104 NetworkHandler::Get()->managed_network_configuration_handler()->GetProperties( 105 service_path, 106 base::Bind(&NetworkConfigMessageHandler::GetPropertiesSuccess, 107 weak_ptr_factory_.GetWeakPtr(), callback_id), 108 base::Bind(&NetworkConfigMessageHandler::ErrorCallback, 109 weak_ptr_factory_.GetWeakPtr(), callback_id)); 110 } 111 112 void NetworkConfigMessageHandler::GetPropertiesSuccess( 113 int callback_id, 114 const std::string& service_path, 115 const base::DictionaryValue& dictionary) const { 116 base::ListValue return_arg_list; 117 return_arg_list.AppendInteger(callback_id); 118 119 base::DictionaryValue* network_properties = dictionary.DeepCopy(); 120 network_properties->SetStringWithoutPathExpansion( 121 ::onc::network_config::kGUID, service_path); 122 return_arg_list.Append(network_properties); 123 InvokeCallback(return_arg_list); 124 } 125 126 void NetworkConfigMessageHandler::InvokeCallback( 127 const base::ListValue& arg_list) const { 128 web_ui()->CallJavascriptFunction( 129 "networkConfig.chromeCallbackSuccess", arg_list); 130 } 131 132 void NetworkConfigMessageHandler::ErrorCallback( 133 int callback_id, 134 const std::string& error_name, 135 scoped_ptr<base::DictionaryValue> error_data) const { 136 LOG(ERROR) << "NetworkConfigMessageHandler Error: " << error_name; 137 base::ListValue arg_list; 138 arg_list.AppendInteger(callback_id); 139 arg_list.AppendString(error_name); 140 web_ui()->CallJavascriptFunction( 141 "networkConfig.chromeCallbackError", arg_list); 142 } 143 144 } // namespace chromeos 145