shell_network_controller_chromeos.h revision 5f1c94371a64b3196d4be9466099bb892df9b88e
1// Copyright 2014 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 EXTENSIONS_SHELL_BROWSER_SHELL_NETWORK_CONTROLLER_CHROMEOS_H_
6#define EXTENSIONS_SHELL_BROWSER_SHELL_NETWORK_CONTROLLER_CHROMEOS_H_
7
8#include <string>
9
10#include "base/compiler_specific.h"
11#include "base/macros.h"
12#include "base/memory/scoped_ptr.h"
13#include "base/memory/weak_ptr.h"
14#include "base/timer/timer.h"
15#include "base/values.h"
16#include "chromeos/network/network_state_handler_observer.h"
17
18namespace extensions {
19
20// Handles network-related tasks for app_shell on Chrome OS.
21class ShellNetworkController : public chromeos::NetworkStateHandlerObserver {
22 public:
23  // This class must be instantiated after chromeos::DBusThreadManager and
24  // destroyed before it.
25  explicit ShellNetworkController(const std::string& preferred_network_name);
26  virtual ~ShellNetworkController();
27
28  // chromeos::NetworkStateHandlerObserver overrides:
29  virtual void NetworkListChanged() OVERRIDE;
30  virtual void NetworkConnectionStateChanged(
31      const chromeos::NetworkState* state) OVERRIDE;
32
33 private:
34  // State of communication with the connection manager.
35  enum State {
36    // No in-progress requests.
37    STATE_IDLE = 0,
38    // Waiting for the result of an attempt to connect to the preferred network.
39    STATE_WAITING_FOR_PREFERRED_RESULT,
40    // Waiting for the result of an attempt to connect to a non-preferred
41    // network.
42    STATE_WAITING_FOR_NON_PREFERRED_RESULT,
43  };
44
45  // Returns the connected or connecting WiFi network or NULL if no network
46  // matches that description.
47  const chromeos::NetworkState* GetActiveWiFiNetwork();
48
49  // Controls whether scanning is performed periodically.
50  void SetScanningEnabled(bool enabled);
51
52  // Asks the connection manager to scan for networks.
53  void RequestScan();
54
55  // If not currently connected or connecting, chooses a wireless network and
56  // asks the connection manager to connect to it. Also switches to
57  // |preferred_network_name_| if possible.
58  void ConnectIfUnconnected();
59
60  // Handles a successful or failed connection attempt.
61  void HandleConnectionSuccess();
62  void HandleConnectionError(const std::string& error_name,
63                             scoped_ptr<base::DictionaryValue> error_data);
64
65  // Current status of communication with the chromeos::NetworkStateHandler.
66  // This is tracked to avoid sending duplicate requests before the handler has
67  // acknowledged the initial connection attempt.
68  State state_;
69
70  // Invokes RequestScan() periodically.
71  base::RepeatingTimer<ShellNetworkController> scan_timer_;
72
73  // Optionally-supplied name of the preferred network.
74  std::string preferred_network_name_;
75
76  // True if the preferred network is connected or connecting.
77  bool preferred_network_is_active_;
78
79  base::WeakPtrFactory<ShellNetworkController> weak_ptr_factory_;
80
81  DISALLOW_COPY_AND_ASSIGN(ShellNetworkController);
82};
83
84}  // namespace extensions
85
86#endif  // EXTENSIONS_SHELL_BROWSER_SHELL_NETWORK_CONTROLLER_CHROMEOS_H_
87