Upstream version 5.34.104.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   // Create instance of |WiFiService| for unit test use.
45   static WiFiService* CreateForTest();
46
47   // Get Properties of network identified by |network_guid|. Populates
48   // |properties| on success, |error| on failure.
49   virtual void GetProperties(const std::string& network_guid,
50                              base::DictionaryValue* properties,
51                              std::string* error) = 0;
52
53   // Gets the merged properties of the network with id |network_guid| from the
54   // sources: User settings, shared settings, user policy, device policy and
55   // the currently active settings. Populates |managed_properties| on success,
56   // |error| on failure.
57   virtual void GetManagedProperties(const std::string& network_guid,
58                                     base::DictionaryValue* managed_properties,
59                                     std::string* error) = 0;
60
61   // Get the cached read-only properties of the network with id |network_guid|.
62   // This is meant to be a higher performance function than |GetProperties|,
63   // which requires a round trip to query the networking subsystem. It only
64   // returns a subset of the properties returned by |GetProperties|. Populates
65   // |properties| on success, |error| on failure.
66   virtual void GetState(const std::string& network_guid,
67                         base::DictionaryValue* properties,
68                         std::string* error) = 0;
69
70   // Set Properties of network identified by |network_guid|. Populates |error|
71   // on failure.
72   virtual void SetProperties(const std::string& network_guid,
73                              scoped_ptr<base::DictionaryValue> properties,
74                              std::string* error) = 0;
75
76   // Creates a new network configuration from |properties|. If |shared| is true,
77   // share this network configuration with other users. If a matching configured
78   // network already exists, this will fail and populate |error|. On success
79   // populates the |network_guid| of the new network.
80   virtual void CreateNetwork(bool shared,
81                              scoped_ptr<base::DictionaryValue> properties,
82                              std::string* network_guid,
83                              std::string* error) = 0;
84
85   // Get list of visible networks of |network_type| (one of onc::network_type).
86   // Populates |network_list| on success.
87   virtual void GetVisibleNetworks(const std::string& network_type,
88                                   base::ListValue* network_list) = 0;
89
90   // Request network scan. Send |NetworkListChanged| event on completion.
91   virtual void RequestNetworkScan() = 0;
92
93   // Start connect to network identified by |network_guid|. Populates |error|
94   // on failure.
95   virtual void StartConnect(const std::string& network_guid,
96                             std::string* error) = 0;
97
98   // Start disconnect from network identified by |network_guid|. Populates
99   // |error| on failure.
100   virtual void StartDisconnect(const std::string& network_guid,
101                                std::string* error) = 0;
102
103   // Get WiFi Key for network identified by |network_guid| from the
104   // system (if it has one) and store it in |key_data|. User privilege elevation
105   // may be required, and function will fail if user privileges are not
106   // sufficient. Populates |error| on failure.
107   virtual void GetKeyFromSystem(const std::string& network_guid,
108                                 std::string* key_data,
109                                 std::string* error) = 0;
110
111   // Set observers to run when |NetworksChanged| and |NetworksListChanged|
112   // events needs to be sent. Notifications are posted on |message_loop_proxy|.
113   virtual void SetEventObservers(
114       scoped_refptr<base::MessageLoopProxy> message_loop_proxy,
115       const NetworkGuidListCallback& networks_changed_observer,
116       const NetworkGuidListCallback& network_list_changed_observer) = 0;
117
118   // Request update of Connected Network information. Send |NetworksChanged|
119   // event on completion.
120   virtual void RequestConnectedNetworkUpdate() = 0;
121
122  protected:
123   WiFiService() {}
124
125   typedef int32 Frequency;
126   enum FrequencyEnum {
127     kFrequencyAny = 0,
128     kFrequencyUnknown = 0,
129     kFrequency2400 = 2400,
130     kFrequency5000 = 5000
131   };
132
133   typedef std::set<Frequency> FrequencySet;
134   // Network Properties, used as result of |GetProperties| and
135   // |GetVisibleNetworks|.
136   struct WIFI_EXPORT NetworkProperties {
137     NetworkProperties();
138     ~NetworkProperties();
139
140     std::string connection_state;
141     std::string guid;
142     std::string name;
143     std::string ssid;
144     std::string bssid;
145     std::string type;
146     std::string security;
147     // |password| field is used to pass wifi password for network creation via
148     // |CreateNetwork| or connection via |StartConnect|. It does not persist
149     // once operation is completed.
150     std::string password;
151     // WiFi Signal Strength. 0..100
152     uint32 signal_strength;
153     bool auto_connect;
154     Frequency frequency;
155     FrequencySet frequency_set;
156
157     std::string json_extra;  // Extra JSON properties for unit tests
158
159     scoped_ptr<base::DictionaryValue> ToValue(bool network_list) const;
160     // Updates only properties set in |value|.
161     bool UpdateFromValue(const base::DictionaryValue& value);
162     static std::string MacAddressAsString(const uint8 mac_as_int[6]);
163     static bool OrderByType(const NetworkProperties& l,
164                             const NetworkProperties& r);
165   };
166
167   typedef std::list<NetworkProperties> NetworkList;
168
169  private:
170   DISALLOW_COPY_AND_ASSIGN(WiFiService);
171 };
172
173 }  // namespace wifi
174
175 #endif  // CHROME_UTILITY_WIFI_WIFI_SERVICE_H_