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 5 #ifndef CHROME_UTILITY_WIFI_WIFI_SERVICE_H_ 6 #define CHROME_UTILITY_WIFI_WIFI_SERVICE_H_ 7 8 #include <list> 9 #include <string> 10 #include <vector> 11 12 #include "base/callback.h" 13 #include "base/memory/ref_counted.h" 14 #include "base/message_loop/message_loop_proxy.h" 15 #include "base/threading/sequenced_worker_pool.h" 16 #include "base/values.h" 17 #include "components/wifi/wifi_export.h" 18 19 namespace wifi { 20 21 // WiFiService interface used by implementation of chrome.networkingPrivate 22 // JavaScript extension API. All methods should be called on worker thread. 23 // It could be created on any (including UI) thread, so nothing expensive should 24 // be done in the constructor. 25 class WIFI_EXPORT WiFiService { 26 public: 27 typedef std::vector<std::string> NetworkGuidList; 28 typedef base::Callback< 29 void(const NetworkGuidList& network_guid_list)> NetworkGuidListCallback; 30 ~WiFiService()31 virtual ~WiFiService() {} 32 33 // Initialize WiFiService, store |task_runner| for posting worker tasks. 34 virtual void Initialize( 35 scoped_refptr<base::SequencedTaskRunner> task_runner) = 0; 36 37 // UnInitialize WiFiService. 38 virtual void UnInitialize() = 0; 39 40 // Create instance of |WiFiService| for normal use. 41 static WiFiService* Create(); 42 // Create instance of |WiFiService| for unit test use. 43 static WiFiService* CreateForTest(); 44 45 // Get Properties of network identified by |network_guid|. Populates 46 // |properties| on success, |error| on failure. 47 virtual void GetProperties(const std::string& network_guid, 48 DictionaryValue* properties, 49 std::string* error) = 0; 50 51 // Gets the merged properties of the network with id |network_guid| from the 52 // sources: User settings, shared settings, user policy, device policy and 53 // the currently active settings. Populates |managed_properties| on success, 54 // |error| on failure. 55 virtual void GetManagedProperties(const std::string& network_guid, 56 DictionaryValue* managed_properties, 57 std::string* error) = 0; 58 59 // Get the cached read-only properties of the network with id |network_guid|. 60 // This is meant to be a higher performance function than |GetProperties|, 61 // which requires a round trip to query the networking subsystem. It only 62 // returns a subset of the properties returned by |GetProperties|. Populates 63 // |properties| on success, |error| on failure. 64 virtual void GetState(const std::string& network_guid, 65 DictionaryValue* properties, 66 std::string* error) = 0; 67 68 // Set Properties of network identified by |network_guid|. Populates |error| 69 // on failure. 70 virtual void SetProperties(const std::string& network_guid, 71 scoped_ptr<base::DictionaryValue> properties, 72 std::string* error) = 0; 73 74 // Creates a new network configuration from |properties|. If |shared| is true, 75 // share this network configuration with other users. If a matching configured 76 // network already exists, this will fail and populate |error|. On success 77 // populates the |network_guid| of the new network. 78 virtual void CreateNetwork(bool shared, 79 scoped_ptr<base::DictionaryValue> properties, 80 std::string* network_guid, 81 std::string* error) = 0; 82 83 // Get list of visible networks of |network_type| (one of onc::network_type). 84 // Populates |network_list| on success. 85 virtual void GetVisibleNetworks(const std::string& network_type, 86 ListValue* network_list) = 0; 87 88 // Request network scan. Send |NetworkListChanged| event on completion. 89 virtual void RequestNetworkScan() = 0; 90 91 // Start connect to network identified by |network_guid|. Populates |error| 92 // on failure. 93 virtual void StartConnect(const std::string& network_guid, 94 std::string* error) = 0; 95 96 // Start disconnect from network identified by |network_guid|. Populates 97 // |error| on failure. 98 virtual void StartDisconnect(const std::string& network_guid, 99 std::string* error) = 0; 100 101 // Set observers to run when |NetworksChanged| and |NetworksListChanged| 102 // events needs to be sent. Notifications are posted on |message_loop_proxy|. 103 virtual void SetEventObservers( 104 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, 105 const NetworkGuidListCallback& networks_changed_observer, 106 const NetworkGuidListCallback& network_list_changed_observer) = 0; 107 108 protected: WiFiService()109 WiFiService() {} 110 111 typedef int32 Frequency; 112 enum FrequencyEnum { 113 kFrequencyAny = 0, 114 kFrequencyUnknown = 0, 115 kFrequency2400 = 2400, 116 kFrequency5000 = 5000 117 }; 118 119 typedef std::list<Frequency> FrequencyList; 120 // Network Properties, used as result of |GetProperties| and 121 // |GetVisibleNetworks|. 122 struct WIFI_EXPORT NetworkProperties { 123 NetworkProperties(); 124 ~NetworkProperties(); 125 126 std::string connection_state; 127 std::string guid; 128 std::string name; 129 std::string ssid; 130 std::string bssid; 131 std::string type; 132 std::string security; 133 // |password| field is used to pass wifi password for network creation via 134 // |CreateNetwork| or connection via |StartConnect|. It does not persist 135 // once operation is completed. 136 std::string password; 137 // WiFi Signal Strength. 0..100 138 uint32 signal_strength; 139 bool auto_connect; 140 Frequency frequency; 141 FrequencyList frequency_list; 142 143 std::string json_extra; // Extra JSON properties for unit tests 144 145 scoped_ptr<base::DictionaryValue> ToValue(bool network_list) const; 146 // Updates only properties set in |value|. 147 bool UpdateFromValue(const base::DictionaryValue& value); 148 static std::string MacAddressAsString(const uint8 mac_as_int[6]); 149 static bool OrderByType(const NetworkProperties& l, 150 const NetworkProperties& r); 151 }; 152 153 typedef std::list<NetworkProperties> NetworkList; 154 155 private: 156 DISALLOW_COPY_AND_ASSIGN(WiFiService); 157 }; 158 159 } // namespace wifi 160 161 #endif // CHROME_UTILITY_WIFI_WIFI_SERVICE_H_ 162