summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/cros/network_library_impl_base.h
blob: 8d7f981eb0698b69529780b87b9706b4659141d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_CHROMEOS_CROS_NETWORK_LIBRARY_IMPL_BASE_H_
#define CHROME_BROWSER_CHROMEOS_CROS_NETWORK_LIBRARY_IMPL_BASE_H_

#include <list>
#include <set>

#include "chrome/browser/chromeos/cros/network_library.h"
#include "chromeos/network/onc/onc_constants.h"

namespace chromeos {

class NetworkLoginObserver;

class NetworkLibraryImplBase : public NetworkLibrary {
 public:
  NetworkLibraryImplBase();
  virtual ~NetworkLibraryImplBase();

  //////////////////////////////////////////////////////////////////////////////
  // NetworkLibraryImplBase virtual functions.

  // Functions for monitoring networks & devices.
  virtual void MonitorNetworkStart(const std::string& service_path) = 0;
  virtual void MonitorNetworkStop(const std::string& service_path) = 0;
  virtual void MonitorNetworkDeviceStart(const std::string& device_path) = 0;
  virtual void MonitorNetworkDeviceStop(const std::string& device_path) = 0;

  // Called from ConnectToWifiNetwork.
  // Calls ConnectToWifiNetworkUsingConnectData if network request succeeds.
  virtual void CallRequestWifiNetworkAndConnect(
      const std::string& ssid, ConnectionSecurity security) = 0;
  // Called from ConnectToVirtualNetwork*.
  // Calls ConnectToVirtualNetworkUsingConnectData if network request succeeds.
  virtual void CallRequestVirtualNetworkAndConnect(
      const std::string& service_name,
      const std::string& server_hostname,
      ProviderType provider_type) = 0;
  // Call to configure a wifi service. The identifier is either a service_path
  // or a GUID. |info| is a dictionary of property values.
  virtual void CallConfigureService(const std::string& identifier,
                                    const DictionaryValue* info) = 0;
  // Called from NetworkConnectStart.
  // Calls NetworkConnectCompleted when the connection attempt completes.
  virtual void CallConnectToNetwork(Network* network) = 0;
  // Called from DeleteRememberedNetwork.
  virtual void CallDeleteRememberedNetwork(
      const std::string& profile_path, const std::string& service_path) = 0;

  // Called from Enable*NetworkDevice.
  // Asynchronously enables or disables the specified device type.
  virtual void CallEnableNetworkDeviceType(
      ConnectionType device, bool enable) = 0;

  // Called from DeleteRememberedNetwork for VPN services.
  // Asynchronously disconnects and removes the service.
  virtual void CallRemoveNetwork(const Network* network) = 0;

  //////////////////////////////////////////////////////////////////////////////
  // NetworkLibrary implementation.

  // virtual Init implemented in derived classes.
  // virtual IsCros implemented in derived classes.

  virtual void AddNetworkProfileObserver(
      NetworkProfileObserver* observer) OVERRIDE;
  virtual void RemoveNetworkProfileObserver(
      NetworkProfileObserver* observer) OVERRIDE;
  virtual void AddNetworkManagerObserver(
      NetworkManagerObserver* observer) OVERRIDE;
  virtual void RemoveNetworkManagerObserver(
      NetworkManagerObserver* observer) OVERRIDE;
  virtual void AddNetworkObserver(const std::string& service_path,
                                  NetworkObserver* observer) OVERRIDE;
  virtual void RemoveNetworkObserver(const std::string& service_path,
                                     NetworkObserver* observer) OVERRIDE;
  virtual void RemoveObserverForAllNetworks(
      NetworkObserver* observer) OVERRIDE;
  virtual void AddNetworkDeviceObserver(
      const std::string& device_path,
      NetworkDeviceObserver* observer) OVERRIDE;
  virtual void RemoveNetworkDeviceObserver(
      const std::string& device_path,
      NetworkDeviceObserver* observer) OVERRIDE;

  virtual void Lock() OVERRIDE;
  virtual void Unlock() OVERRIDE;
  virtual bool IsLocked() OVERRIDE;

  virtual void AddPinOperationObserver(
      PinOperationObserver* observer) OVERRIDE;
  virtual void RemovePinOperationObserver(
      PinOperationObserver* observer) OVERRIDE;
  virtual void AddUserActionObserver(
      UserActionObserver* observer) OVERRIDE;
  virtual void RemoveUserActionObserver(
      UserActionObserver* observer) OVERRIDE;

  virtual const EthernetNetwork* ethernet_network() const OVERRIDE;
  virtual bool ethernet_connecting() const OVERRIDE;
  virtual bool ethernet_connected() const OVERRIDE;
  virtual const WifiNetwork* wifi_network() const OVERRIDE;
  virtual bool wifi_connecting() const OVERRIDE;
  virtual bool wifi_connected() const OVERRIDE;
  virtual const CellularNetwork* cellular_network() const OVERRIDE;
  virtual bool cellular_connecting() const OVERRIDE;
  virtual bool cellular_connected() const OVERRIDE;
  virtual const WimaxNetwork* wimax_network() const OVERRIDE;
  virtual bool wimax_connecting() const OVERRIDE;
  virtual bool wimax_connected() const OVERRIDE;
  virtual const Network* mobile_network() const OVERRIDE;
  virtual bool mobile_connecting() const OVERRIDE;
  virtual bool mobile_connected() const OVERRIDE;
  virtual const VirtualNetwork* virtual_network() const OVERRIDE;
  virtual bool virtual_network_connecting() const OVERRIDE;
  virtual bool virtual_network_connected() const OVERRIDE;
  virtual bool Connected() const OVERRIDE;
  virtual bool Connecting() const OVERRIDE;
  virtual const WifiNetworkVector& wifi_networks() const OVERRIDE;
  virtual const WifiNetworkVector& remembered_wifi_networks() const OVERRIDE;
  virtual const CellularNetworkVector& cellular_networks() const OVERRIDE;
  virtual const WimaxNetworkVector& wimax_networks() const OVERRIDE;
  virtual const VirtualNetworkVector& virtual_networks() const OVERRIDE;
  virtual const VirtualNetworkVector&
      remembered_virtual_networks() const OVERRIDE;
  virtual const Network* active_network() const OVERRIDE;
  virtual const Network* connected_network() const OVERRIDE;
  virtual const Network* connecting_network() const OVERRIDE;
  virtual bool ethernet_available() const OVERRIDE;
  virtual bool wifi_available() const OVERRIDE;
  virtual bool wimax_available() const OVERRIDE;
  virtual bool cellular_available() const OVERRIDE;
  virtual bool mobile_available() const OVERRIDE;
  virtual bool ethernet_enabled() const OVERRIDE;
  virtual bool wifi_enabled() const OVERRIDE;
  virtual bool wimax_enabled() const OVERRIDE;
  virtual bool cellular_enabled() const OVERRIDE;
  virtual bool mobile_enabled() const OVERRIDE;
  virtual bool ethernet_busy() const OVERRIDE;
  virtual bool wifi_busy() const OVERRIDE;
  virtual bool wimax_busy() const OVERRIDE;
  virtual bool cellular_busy() const OVERRIDE;
  virtual bool mobile_busy() const OVERRIDE;
  virtual bool wifi_scanning() const OVERRIDE;
  virtual bool offline_mode() const OVERRIDE;
  virtual std::string GetCheckPortalList() const OVERRIDE;
  // virtual SetCheckPortalList implemented in derived classes.
  // virtual SetDefaultCheckPortalList implemented in derived classes.
  virtual const std::string& IPAddress() const OVERRIDE;

  virtual const NetworkDevice* FindNetworkDeviceByPath(
      const std::string& path) const OVERRIDE;
  NetworkDevice* FindNetworkDeviceByPath(const std::string& path);
  virtual const NetworkDevice* FindMobileDevice() const OVERRIDE;
  virtual const NetworkDevice* FindWimaxDevice() const OVERRIDE;
  virtual const NetworkDevice* FindCellularDevice() const OVERRIDE;
  virtual const NetworkDevice* FindEthernetDevice() const OVERRIDE;
  virtual const NetworkDevice* FindWifiDevice() const OVERRIDE;
  virtual Network* FindNetworkByPath(const std::string& path) const OVERRIDE;
  virtual Network* FindNetworkByUniqueId(
      const std::string& unique_id) const OVERRIDE;
  WirelessNetwork* FindWirelessNetworkByPath(const std::string& path) const;
  virtual WifiNetwork* FindWifiNetworkByPath(
      const std::string& path) const OVERRIDE;
  virtual WimaxNetwork* FindWimaxNetworkByPath(
      const std::string& path) const OVERRIDE;
  virtual CellularNetwork* FindCellularNetworkByPath(
      const std::string& path) const OVERRIDE;
  virtual VirtualNetwork* FindVirtualNetworkByPath(
      const std::string& path) const OVERRIDE;
  Network* FindRememberedFromNetwork(const Network* network) const;
  virtual Network* FindRememberedNetworkByPath(
      const std::string& path) const OVERRIDE;

  virtual const base::DictionaryValue* FindOncForNetwork(
      const std::string& unique_id) const OVERRIDE;

  virtual void SignalCellularPlanPayment() OVERRIDE;
  virtual bool HasRecentCellularPlanPayment() OVERRIDE;
  virtual const std::string& GetCellularHomeCarrierId() const OVERRIDE;

  // virtual ChangePin implemented in derived classes.
  // virtual ChangeRequiredPin implemented in derived classes.
  // virtual EnterPin implemented in derived classes.
  // virtual UnblockPin implemented in derived classes.

  // virtual RequestCellularScan implemented in derived classes.
  // virtual RequestCellularRegister implemented in derived classes.
  // virtual SetCellularDataRoamingAllowed implemented in derived classes.
  // virtual SetCarrier implemented in derived classes.
  // virtual IsCellularAlwaysInRoaming implemented in derived classes.
  // virtual RequestNetworkScan implemented in derived classes.
  // virtual GetWifiAccessPoints implemented in derived classes.

  virtual bool HasProfileType(NetworkProfileType type) const OVERRIDE;
  virtual bool CanConnectToNetwork(const Network* network) const OVERRIDE;

  // Connect to an existing network.
  virtual void ConnectToWifiNetwork(WifiNetwork* wifi) OVERRIDE;
  virtual void ConnectToWifiNetwork(WifiNetwork* wifi, bool shared) OVERRIDE;
  virtual void ConnectToWimaxNetwork(WimaxNetwork* wimax) OVERRIDE;
  virtual void ConnectToWimaxNetwork(WimaxNetwork* wimax, bool shared) OVERRIDE;
  virtual void ConnectToCellularNetwork(CellularNetwork* cellular) OVERRIDE;
  virtual void ConnectToVirtualNetwork(VirtualNetwork* vpn) OVERRIDE;

  // Request a network and connect to it.
  virtual void ConnectToUnconfiguredWifiNetwork(
      const std::string& ssid,
      ConnectionSecurity security,
      const std::string& passphrase,
      const EAPConfigData* eap_config,
      bool save_credentials,
      bool shared) OVERRIDE;

  virtual void ConnectToUnconfiguredVirtualNetwork(
      const std::string& service_name,
      const std::string& server_hostname,
      ProviderType provider_type,
      const VPNConfigData& config) OVERRIDE;

  // virtual DisconnectFromNetwork implemented in derived classes.
  virtual void ForgetNetwork(const std::string& service_path) OVERRIDE;
  virtual void EnableEthernetNetworkDevice(bool enable) OVERRIDE;
  virtual void EnableWifiNetworkDevice(bool enable) OVERRIDE;
  virtual void EnableMobileNetworkDevice(bool enable) OVERRIDE;
  virtual void EnableWimaxNetworkDevice(bool enable) OVERRIDE;
  virtual void EnableCellularNetworkDevice(bool enable) OVERRIDE;
  // virtual EnableOfflineMode implemented in derived classes.
  // virtual GetIPConfigs implemented in derived classes.
  // virtual SetIPConfig implemented in derived classes.
  virtual void SwitchToPreferredNetwork() OVERRIDE;
  virtual bool LoadOncNetworks(const std::string& onc_blob,
                               const std::string& passphrase,
                               onc::ONCSource source,
                               bool allow_web_trust_from_policy) OVERRIDE;
  virtual bool SetActiveNetwork(ConnectionType type,
                                const std::string& service_path) OVERRIDE;

 protected:
  typedef ObserverList<NetworkObserver> NetworkObserverList;
  typedef std::map<std::string, NetworkObserverList*> NetworkObserverMap;

  typedef ObserverList<NetworkDeviceObserver> NetworkDeviceObserverList;
  typedef std::map<std::string, NetworkDeviceObserverList*>
      NetworkDeviceObserverMap;

  typedef std::map<std::string, Network*> NetworkMap;
  typedef std::map<std::string, int> PriorityMap;
  typedef std::map<std::string, NetworkDevice*> NetworkDeviceMap;
  typedef std::map<std::string, const base::DictionaryValue*> NetworkOncMap;
  typedef std::map<onc::ONCSource,
                   std::set<std::string> > NetworkSourceMap;

  struct NetworkProfile {
    NetworkProfile(const std::string& p, NetworkProfileType t);
    ~NetworkProfile();
    std::string path;
    NetworkProfileType type;
    typedef std::set<std::string> ServiceList;
    ServiceList services;
  };
  typedef std::list<NetworkProfile> NetworkProfileList;

  struct ConnectData {
    ConnectData();
    ~ConnectData();
    ConnectionSecurity security;
    std::string service_name;  // For example, SSID.
    std::string username;
    std::string passphrase;
    std::string otp;
    std::string group_name;
    std::string server_hostname;
    std::string server_ca_cert_nss_nickname;
    std::string client_cert_pkcs11_id;
    EAPMethod eap_method;
    EAPPhase2Auth eap_auth;
    bool eap_use_system_cas;
    std::string eap_identity;
    std::string eap_anonymous_identity;
    std::string psk_key;
    bool save_credentials;
    NetworkProfileType profile_type;
  };

  enum NetworkConnectStatus {
    CONNECT_SUCCESS,
    CONNECT_BAD_PASSPHRASE,
    CONNECT_FAILED
  };

  // Finds device by connection type.
  const NetworkDevice* FindDeviceByType(ConnectionType type) const;
  // Called from ConnectTo*Network.
  void NetworkConnectStartWifi(
      WifiNetwork* network, NetworkProfileType profile_type);
  void NetworkConnectStartVPN(VirtualNetwork* network);
  void NetworkConnectStart(Network* network, NetworkProfileType profile_type);
  // Called from CallConnectToNetwork.
  void NetworkConnectCompleted(Network* network,
                               NetworkConnectStatus status);
  // Called from CallRequestWifiNetworkAndConnect.
  void ConnectToWifiNetworkUsingConnectData(WifiNetwork* wifi);
  // Called from CallRequestVirtualNetworkAndConnect.
  void ConnectToVirtualNetworkUsingConnectData(VirtualNetwork* vpn);

  // Network list management functions.
  void ClearActiveNetwork(ConnectionType type);
  void UpdateActiveNetwork(Network* network);
  void AddNetwork(Network* network);
  void DeleteNetwork(Network* network);

  // Calls ForgetNetwork for remembered wifi and virtual networks based on id.
  // When |if_found| is true, then it forgets networks that appear in |ids|.
  // When |if_found| is false, it removes networks that do NOT appear in |ids|.
  // |source| is the import source of the data.
  void ForgetNetworksById(onc::ONCSource source,
                          std::set<std::string> ids,
                          bool if_found);

  // Checks whether |network| has meanwhile been pruned by ONC policy. If so,
  // instructs shill to remove the network, deletes |network| and returns
  // false.
  bool ValidateRememberedNetwork(Network* network);

  // Adds |network| to the remembered networks data structures and returns true
  // if ValidateRememberedNetwork(network) returns true. Returns false
  // otherwise.
  bool ValidateAndAddRememberedNetwork(Network* network);

  void DeleteRememberedNetwork(const std::string& service_path);
  void ClearNetworks();
  void DeleteRememberedNetworks();
  void DeleteDevice(const std::string& device_path);
  void DeleteDeviceFromDeviceObserversMap(const std::string& device_path);

  // Profile management functions.
  void AddProfile(const std::string& profile_path,
                  NetworkProfileType profile_type);
  NetworkProfile* GetProfileForType(NetworkProfileType type);
  void SetProfileType(Network* network, NetworkProfileType type);
  void SetProfileTypeFromPath(Network* network);
  std::string GetProfilePath(NetworkProfileType type);

  // Notifications.
  void NotifyNetworkProfileObservers();
  void NotifyNetworkManagerChanged(bool force_update);
  void SignalNetworkManagerObservers();
  void NotifyNetworkChanged(const Network* network);
  void NotifyNetworkDeviceChanged(NetworkDevice* device, PropertyIndex index);
  void NotifyPinOperationCompleted(PinOperationError error);
  void NotifyUserConnectionInitiated(const Network* network);

  // TPM related functions.
  void GetTpmInfo();
  const std::string& GetTpmSlot();
  const std::string& GetTpmPin();

  // Network profile observer list.
  ObserverList<NetworkProfileObserver> network_profile_observers_;

  // Network manager observer list.
  ObserverList<NetworkManagerObserver> network_manager_observers_;

  // PIN operation observer list.
  ObserverList<PinOperationObserver> pin_operation_observers_;

  // User action observer list.
  ObserverList<UserActionObserver> user_action_observers_;

  // Network observer map.
  NetworkObserverMap network_observers_;

  // Network device observer map.
  NetworkDeviceObserverMap network_device_observers_;

  // Network login observer.
  scoped_ptr<NetworkLoginObserver> network_login_observer_;

  // List of profiles.
  NetworkProfileList profile_list_;

  // A service path based map of all visible Networks.
  NetworkMap network_map_;

  // A unique_id based map of all visible Networks.
  NetworkMap network_unique_id_map_;

  // A service path based map of all remembered Networks.
  NetworkMap remembered_network_map_;

  // A list of services that we are awaiting updates for.
  PriorityMap network_update_requests_;

  // A device path based map of all NetworkDevices.
  NetworkDeviceMap device_map_;

  // The ethernet network.
  EthernetNetwork* ethernet_;

  // The list of available wifi networks.
  WifiNetworkVector wifi_networks_;

  // The current connected (or connecting) wifi network.
  WifiNetwork* active_wifi_;

  // The remembered wifi networks.
  WifiNetworkVector remembered_wifi_networks_;

  // The list of available cellular networks.
  CellularNetworkVector cellular_networks_;

  // The list of available wimax networks.
  WimaxNetworkVector wimax_networks_;

  // The current connected (or connecting) cellular network.
  CellularNetwork* active_cellular_;

  // The current connected (or connecting) Wimax network.
  WimaxNetwork* active_wimax_;

  // The list of available virtual networks.
  VirtualNetworkVector virtual_networks_;

  // The current connected (or connecting) virtual network.
  VirtualNetwork* active_virtual_;

  // The remembered virtual networks.
  VirtualNetworkVector remembered_virtual_networks_;

  // The path of the active profile (for retrieving remembered services).
  std::string active_profile_path_;

  // The current available network devices. Bitwise flag of ConnectionTypes.
  int available_devices_;

  // The current enabled network devices. Bitwise flag of ConnectionTypes.
  int enabled_devices_;

  // The current busy network devices. Bitwise flag of ConnectionTypes.
  // Busy means device is switching from enable/disable state.
  int busy_devices_;

  // The current connected network devices. Bitwise flag of ConnectionTypes.
  int connected_devices_;

  // True if we are currently scanning for wifi networks.
  bool wifi_scanning_;

  // Currently not implemented. TODO(stevenjb): implement or eliminate.
  bool offline_mode_;

  // List of interfaces for which portal check is enabled.
  std::string check_portal_list_;

  // True if access network library is locked.
  bool is_locked_;

  // TPM module user slot and PIN, needed by shill to access certificates.
  std::string tpm_slot_;
  std::string tpm_pin_;

  // Type of pending SIM operation, SIM_OPERATION_NONE otherwise.
  SimOperationType sim_operation_;

 private:
  // List of networks to move to the user profile once logged in.
  std::list<std::string> user_networks_;

  // Weak pointer factory for canceling a network change callback.
  base::WeakPtrFactory<NetworkLibraryImplBase> notify_manager_weak_factory_;

  // Cellular plan payment time.
  base::Time cellular_plan_payment_time_;

  // Temporary connection data for async connect calls.
  ConnectData connect_data_;

  // Holds unique id to ONC mapping.
  NetworkOncMap network_onc_map_;

  // Keeps track of what networks ONC has configured. This is used to weed out
  // stray networks that shill still has on file, but are not known on the
  // Chrome side.
  NetworkSourceMap network_source_map_;

  DISALLOW_COPY_AND_ASSIGN(NetworkLibraryImplBase);
};

}  // namespace chromeos

#endif  // CHROME_BROWSER_CHROMEOS_CROS_NETWORK_LIBRARY_IMPL_BASE_H_