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