fake_wifi_service.cc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. #include "components/wifi/fake_wifi_service.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "base/bind.h"
  8. #include "base/values.h"
  9. #include "components/onc/onc_constants.h"
  10. namespace wifi {
  11. FakeWiFiService::FakeWiFiService() {
  12. // Populate data expected by unit test.
  13. {
  14. NetworkProperties network_properties;
  15. network_properties.connection_state = onc::connection_state::kConnected;
  16. network_properties.guid = "stub_wifi1_guid";
  17. network_properties.name = "wifi1";
  18. network_properties.type = onc::network_type::kWiFi;
  19. network_properties.frequency = 0;
  20. network_properties.ssid = "wifi1";
  21. network_properties.security = onc::wifi::kWEP_PSK;
  22. network_properties.signal_strength = 40;
  23. networks_.push_back(network_properties);
  24. }
  25. {
  26. NetworkProperties network_properties;
  27. network_properties.connection_state = onc::connection_state::kNotConnected;
  28. network_properties.guid = "stub_wifi2_guid";
  29. network_properties.name = "wifi2_PSK";
  30. network_properties.type = onc::network_type::kWiFi;
  31. network_properties.frequency = 5000;
  32. network_properties.frequency_set.insert(2400);
  33. network_properties.frequency_set.insert(5000);
  34. network_properties.ssid = "wifi2_PSK";
  35. network_properties.security = onc::wifi::kWPA_PSK;
  36. network_properties.signal_strength = 80;
  37. networks_.push_back(network_properties);
  38. }
  39. }
  40. FakeWiFiService::~FakeWiFiService() {
  41. }
  42. void FakeWiFiService::Initialize(
  43. scoped_refptr<base::SequencedTaskRunner> task_runner) {
  44. }
  45. void FakeWiFiService::UnInitialize() {
  46. }
  47. void FakeWiFiService::GetProperties(const std::string& network_guid,
  48. base::Value::Dict* properties,
  49. std::string* error) {
  50. NetworkList::iterator network_properties = FindNetwork(network_guid);
  51. if (network_properties == networks_.end()) {
  52. *error = "Error.InvalidNetworkGuid";
  53. return;
  54. }
  55. *properties = network_properties->ToValue(/*network_list=*/false);
  56. }
  57. void FakeWiFiService::GetManagedProperties(
  58. const std::string& network_guid,
  59. base::Value::Dict* managed_properties,
  60. std::string* error) {
  61. // Not implemented
  62. *error = kErrorWiFiService;
  63. }
  64. void FakeWiFiService::GetState(const std::string& network_guid,
  65. base::Value::Dict* properties,
  66. std::string* error) {
  67. NetworkList::iterator network_properties = FindNetwork(network_guid);
  68. if (network_properties == networks_.end()) {
  69. *error = "Error.InvalidNetworkGuid";
  70. return;
  71. }
  72. *properties = network_properties->ToValue(/*network_list=*/true);
  73. }
  74. void FakeWiFiService::SetProperties(const std::string& network_guid,
  75. base::Value::Dict properties,
  76. std::string* error) {
  77. NetworkList::iterator network_properties = FindNetwork(network_guid);
  78. if (network_properties == networks_.end() ||
  79. !network_properties->UpdateFromValue(properties)) {
  80. *error = "Error.DBusFailed";
  81. }
  82. }
  83. void FakeWiFiService::CreateNetwork(bool shared,
  84. base::Value::Dict properties,
  85. std::string* network_guid,
  86. std::string* error) {
  87. NetworkProperties network_properties;
  88. if (network_properties.UpdateFromValue(properties)) {
  89. network_properties.guid = network_properties.ssid;
  90. networks_.push_back(network_properties);
  91. *network_guid = network_properties.guid;
  92. } else {
  93. *error = "Error.DBusFailed";
  94. }
  95. }
  96. void FakeWiFiService::GetVisibleNetworks(const std::string& network_type,
  97. bool include_details,
  98. base::Value::List* network_list) {
  99. for (NetworkList::const_iterator it = networks_.begin();
  100. it != networks_.end();
  101. ++it) {
  102. if (network_type.empty() || network_type == onc::network_type::kAllTypes ||
  103. it->type == network_type) {
  104. network_list->Append(it->ToValue(/*network_list=*/!include_details));
  105. }
  106. }
  107. }
  108. void FakeWiFiService::RequestNetworkScan() {
  109. NotifyNetworkListChanged(networks_);
  110. }
  111. void FakeWiFiService::StartConnect(const std::string& network_guid,
  112. std::string* error) {
  113. NetworkList::iterator network_properties = FindNetwork(network_guid);
  114. if (network_properties == networks_.end()) {
  115. *error = "Error.InvalidNetworkGuid";
  116. return;
  117. }
  118. DisconnectAllNetworksOfType(network_properties->type);
  119. network_properties->connection_state = onc::connection_state::kConnected;
  120. SortNetworks();
  121. NotifyNetworkListChanged(networks_);
  122. NotifyNetworkChanged(network_guid);
  123. }
  124. void FakeWiFiService::StartDisconnect(const std::string& network_guid,
  125. std::string* error) {
  126. NetworkList::iterator network_properties = FindNetwork(network_guid);
  127. if (network_properties == networks_.end()) {
  128. *error = "Error.InvalidNetworkGuid";
  129. return;
  130. }
  131. network_properties->connection_state = onc::connection_state::kNotConnected;
  132. SortNetworks();
  133. NotifyNetworkListChanged(networks_);
  134. NotifyNetworkChanged(network_guid);
  135. }
  136. void FakeWiFiService::GetKeyFromSystem(const std::string& network_guid,
  137. std::string* key_data,
  138. std::string* error) {
  139. *error = "not-found";
  140. }
  141. void FakeWiFiService::SetEventObservers(
  142. scoped_refptr<base::SingleThreadTaskRunner> task_runner,
  143. NetworkGuidListCallback networks_changed_observer,
  144. NetworkGuidListCallback network_list_changed_observer) {
  145. task_runner_.swap(task_runner);
  146. networks_changed_observer_ = std::move(networks_changed_observer);
  147. network_list_changed_observer_ = std::move(network_list_changed_observer);
  148. }
  149. void FakeWiFiService::RequestConnectedNetworkUpdate() {
  150. }
  151. void FakeWiFiService::GetConnectedNetworkSSID(std::string* ssid,
  152. std::string* error) {
  153. *ssid = "";
  154. *error = "";
  155. }
  156. NetworkList::iterator FakeWiFiService::FindNetwork(
  157. const std::string& network_guid) {
  158. for (NetworkList::iterator it = networks_.begin(); it != networks_.end();
  159. ++it) {
  160. if (it->guid == network_guid)
  161. return it;
  162. }
  163. return networks_.end();
  164. }
  165. void FakeWiFiService::DisconnectAllNetworksOfType(const std::string& type) {
  166. for (NetworkList::iterator it = networks_.begin(); it != networks_.end();
  167. ++it) {
  168. if (it->type == type)
  169. it->connection_state = onc::connection_state::kNotConnected;
  170. }
  171. }
  172. void FakeWiFiService::SortNetworks() {
  173. // Sort networks, so connected/connecting is up front, then by type:
  174. // Ethernet, WiFi, Cellular, VPN
  175. networks_.sort(NetworkProperties::OrderByType);
  176. }
  177. void FakeWiFiService::NotifyNetworkListChanged(const NetworkList& networks) {
  178. WiFiService::NetworkGuidList current_networks;
  179. for (NetworkList::const_iterator it = networks.begin(); it != networks.end();
  180. ++it) {
  181. current_networks.push_back(it->guid);
  182. }
  183. task_runner_->PostTask(
  184. FROM_HERE,
  185. base::BindOnce(network_list_changed_observer_, current_networks));
  186. }
  187. void FakeWiFiService::NotifyNetworkChanged(const std::string& network_guid) {
  188. WiFiService::NetworkGuidList changed_networks(1, network_guid);
  189. task_runner_->PostTask(
  190. FROM_HERE, base::BindOnce(networks_changed_observer_, changed_networks));
  191. }
  192. } // namespace wifi