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 COMPONENTS_WIFI_WIFI_SERVICE_H_
6 #define COMPONENTS_WIFI_WIFI_SERVICE_H_
7 
8 #include <list>
9 #include <memory>
10 #include <set>
11 #include <string>
12 #include <vector>
13 
14 #include "base/callback.h"
15 #include "base/macros.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/sequenced_task_runner.h"
18 #include "base/single_thread_task_runner.h"
19 #include "base/values.h"
20 #include "components/wifi/wifi_export.h"
21 
22 namespace wifi {
23 
24 // WiFiService interface used by implementation of chrome.networkingPrivate
25 // JavaScript extension API. All methods should be called on worker thread.
26 // It could be created on any (including UI) thread, so nothing expensive should
27 // be done in the constructor. See |NetworkingPrivateService| for wrapper
28 // accessible on UI thread.
29 class WIFI_EXPORT WiFiService {
30  public:
31   using NetworkGuidList = std::vector<std::string>;
32   using NetworkGuidListCallback =
33       base::RepeatingCallback<void(const NetworkGuidList& network_guid_list)>;
34 
~WiFiService()35   virtual ~WiFiService() {}
36 
37   // Initialize WiFiService, store |task_runner| for posting worker tasks.
38   virtual void Initialize(
39       scoped_refptr<base::SequencedTaskRunner> task_runner) = 0;
40 
41   // UnInitialize WiFiService.
42   virtual void UnInitialize() = 0;
43 
44   // Create instance of |WiFiService| for normal use.
45   static WiFiService* Create();
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                              std::unique_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                              std::unique_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,
89                                   bool include_details) = 0;
90 
91   // Request network scan. Send |NetworkListChanged| event on completion.
92   virtual void RequestNetworkScan() = 0;
93 
94   // Start connect to network identified by |network_guid|. Populates |error|
95   // on failure.
96   virtual void StartConnect(const std::string& network_guid,
97                             std::string* error) = 0;
98 
99   // Start disconnect from network identified by |network_guid|. Populates
100   // |error| on failure.
101   virtual void StartDisconnect(const std::string& network_guid,
102                                std::string* error) = 0;
103 
104   // Get WiFi Key for network identified by |network_guid| from the
105   // system (if it has one) and store it in |key_data|. User privilege elevation
106   // may be required, and function will fail if user privileges are not
107   // sufficient. Populates |error| on failure.
108   virtual void GetKeyFromSystem(const std::string& network_guid,
109                                 std::string* key_data,
110                                 std::string* error) = 0;
111 
112   // Set observers to run when |NetworksChanged| and |NetworksListChanged|
113   // events needs to be sent. Notifications are posted on |task_runner|.
114   virtual void SetEventObservers(
115       scoped_refptr<base::SingleThreadTaskRunner> task_runner,
116       NetworkGuidListCallback networks_changed_observer,
117       NetworkGuidListCallback network_list_changed_observer) = 0;
118 
119   // Request update of Connected Network information. Send |NetworksChanged|
120   // event on completion.
121   virtual void RequestConnectedNetworkUpdate() = 0;
122 
123   // Get the SSID of the currently connected network, if any.
124   virtual void GetConnectedNetworkSSID(std::string* ssid,
125                                        std::string* error) = 0;
126 
127  protected:
WiFiService()128   WiFiService() {}
129 
130   // Error constants.
131   static const char kErrorAssociateToNetwork[];
132   static const char kErrorInvalidData[];
133   static const char kErrorNotConfigured[];
134   static const char kErrorNotConnected[];
135   static const char kErrorNotFound[];
136   static const char kErrorNotImplemented[];
137   static const char kErrorScanForNetworksWithName[];
138   static const char kErrorWiFiService[];
139 
140  private:
141   DISALLOW_COPY_AND_ASSIGN(WiFiService);
142 };
143 
144 }  // namespace wifi
145 
146 #endif  // COMPONENTS_WIFI_WIFI_SERVICE_H_
147