Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / components / wifi / wifi_service.h
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 <set>
10 #include <string>
11 #include <vector>
12
13 #include "base/callback.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/message_loop/message_loop_proxy.h"
16 #include "base/threading/sequenced_worker_pool.h"
17 #include "base/values.h"
18 #include "components/wifi/wifi_export.h"
19
20 namespace wifi {
21
22 // WiFiService interface used by implementation of chrome.networkingPrivate
23 // JavaScript extension API. All methods should be called on worker thread.
24 // It could be created on any (including UI) thread, so nothing expensive should
25 // be done in the constructor. See |NetworkingPrivateService| for wrapper
26 // accessible on UI thread.
27 class WIFI_EXPORT WiFiService {
28  public:
29   typedef std::vector<std::string> NetworkGuidList;
30   typedef base::Callback<
31       void(const NetworkGuidList& network_guid_list)> NetworkGuidListCallback;
32
33   virtual ~WiFiService() {}
34
35   // Initialize WiFiService, store |task_runner| for posting worker tasks.
36   virtual void Initialize(
37       scoped_refptr<base::SequencedTaskRunner> task_runner) = 0;
38
39   // UnInitialize WiFiService.
40   virtual void UnInitialize() = 0;
41
42   // Create instance of |WiFiService| for normal use.
43   static WiFiService* Create();
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                              base::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                                     base::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                         base::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                                   base::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   // Get WiFi Key for network identified by |network_guid| from the
102   // system (if it has one) and store it in |key_data|. User privilege elevation
103   // may be required, and function will fail if user privileges are not
104   // sufficient. Populates |error| on failure.
105   virtual void GetKeyFromSystem(const std::string& network_guid,
106                                 std::string* key_data,
107                                 std::string* error) = 0;
108
109   // Set observers to run when |NetworksChanged| and |NetworksListChanged|
110   // events needs to be sent. Notifications are posted on |message_loop_proxy|.
111   virtual void SetEventObservers(
112       scoped_refptr<base::MessageLoopProxy> message_loop_proxy,
113       const NetworkGuidListCallback& networks_changed_observer,
114       const NetworkGuidListCallback& network_list_changed_observer) = 0;
115
116   // Request update of Connected Network information. Send |NetworksChanged|
117   // event on completion.
118   virtual void RequestConnectedNetworkUpdate() = 0;
119
120  protected:
121   WiFiService() {}
122
123   typedef int32 Frequency;
124   enum FrequencyEnum {
125     kFrequencyAny = 0,
126     kFrequencyUnknown = 0,
127     kFrequency2400 = 2400,
128     kFrequency5000 = 5000
129   };
130
131   typedef std::set<Frequency> FrequencySet;
132   // Network Properties, used as result of |GetProperties| and
133   // |GetVisibleNetworks|.
134   struct WIFI_EXPORT NetworkProperties {
135     NetworkProperties();
136     ~NetworkProperties();
137
138     std::string connection_state;
139     std::string guid;
140     std::string name;
141     std::string ssid;
142     std::string bssid;
143     std::string type;
144     std::string security;
145     // |password| field is used to pass wifi password for network creation via
146     // |CreateNetwork| or connection via |StartConnect|. It does not persist
147     // once operation is completed.
148     std::string password;
149     // WiFi Signal Strength. 0..100
150     uint32 signal_strength;
151     bool auto_connect;
152     Frequency frequency;
153     FrequencySet frequency_set;
154
155     std::string json_extra;  // Extra JSON properties for unit tests
156
157     scoped_ptr<base::DictionaryValue> ToValue(bool network_list) const;
158     // Updates only properties set in |value|.
159     bool UpdateFromValue(const base::DictionaryValue& value);
160     static std::string MacAddressAsString(const uint8 mac_as_int[6]);
161     static bool OrderByType(const NetworkProperties& l,
162                             const NetworkProperties& r);
163   };
164
165   typedef std::list<NetworkProperties> NetworkList;
166
167   // Error constants.
168   static const char kErrorAssociateToNetwork[];
169   static const char kErrorInvalidData[];
170   static const char kErrorNotConfigured[];
171   static const char kErrorNotConnected[];
172   static const char kErrorNotFound[];
173   static const char kErrorNotImplemented[];
174   static const char kErrorScanForNetworksWithName[];
175   static const char kErrorWiFiService[];
176
177  private:
178   DISALLOW_COPY_AND_ASSIGN(WiFiService);
179 };
180
181 }  // namespace wifi
182
183 #endif  // CHROME_UTILITY_WIFI_WIFI_SERVICE_H_