managed_state.h revision 90dce4d38c5ff5333bea97d859d4e484e27edf0c
1// Copyright (c) 2012 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 CHROMEOS_NETWORK_MANAGED_STATE_H_
6#define CHROMEOS_NETWORK_MANAGED_STATE_H_
7
8#include <string>
9
10#include "base/basictypes.h"
11#include "base/compiler_specific.h"
12#include "chromeos/chromeos_export.h"
13
14namespace base {
15class Value;
16}
17
18namespace chromeos {
19
20class DeviceState;
21class NetworkState;
22
23// Base class for states managed by NetworkStateManger which are associated
24// with a Shill path (e.g. service path or device path).
25class ManagedState {
26 public:
27  enum ManagedType {
28    MANAGED_TYPE_NETWORK,
29    MANAGED_TYPE_DEVICE
30  };
31
32  virtual ~ManagedState();
33
34  // This will construct and return a new instance of the appropriate class
35  // based on |type|.
36  static ManagedState* Create(ManagedType type, const std::string& path);
37
38  // Returns the specific class pointer if this is the correct type, or
39  // NULL if it is not.
40  NetworkState* AsNetworkState();
41  DeviceState* AsDeviceState();
42
43  // Called by NetworkStateHandler when a property changes. Returns false if
44  // the property was not recognized, was not parsed successfully, or is
45  // unchanged (complex properties may be assumed to have changed).
46  virtual bool PropertyChanged(const std::string& key,
47                               const base::Value& value) = 0;
48
49  // Called by NetworkStateHandler after all calls to PropertyChanged for the
50  // initial set of properties. Used to update state requiring multiple
51  // parsed properties, e.g. name from hex_ssid in NetworkState.
52  virtual void InitialPropertiesReceived();
53
54  const ManagedType managed_type() const { return managed_type_; }
55  const std::string& path() const { return path_; }
56  const std::string& name() const { return name_; }
57  const std::string& type() const { return type_; }
58  bool is_observed() const { return is_observed_; }
59  void set_is_observed(bool is_observed) { is_observed_ = is_observed; }
60  bool update_requested() const { return update_requested_; }
61  void set_update_requested(bool update_requested) {
62    update_requested_ = update_requested;
63  }
64
65 protected:
66  ManagedState(ManagedType type, const std::string& path);
67
68  // Parses common property keys (name, type).
69  bool ManagedStatePropertyChanged(const std::string& key,
70                                   const base::Value& value);
71
72  // Helper methods that log warnings and return true if parsing succeeded and
73  // the new value does not match the existing output value.
74  bool GetBooleanValue(const std::string& key,
75                       const base::Value& value,
76                       bool* out_value);
77  bool GetIntegerValue(const std::string& key,
78                       const base::Value& value,
79                       int* out_value);
80  bool GetStringValue(const std::string& key,
81                      const base::Value& value,
82                      std::string* out_value);
83
84  void set_name(const std::string& name) { name_ = name; }
85
86 private:
87  friend class NetworkChangeNotifierChromeosUpdateTest;
88
89  ManagedType managed_type_;
90
91  // The path (e.g. service path or device path) of the managed state object.
92  std::string path_;
93
94  // Common properties shared by all managed state objects.
95  std::string name_;  // flimflam::kNameProperty
96  std::string type_;  // flimflam::kTypeProperty
97
98  // Tracks when the state is being observed.
99  bool is_observed_;
100
101  // Tracks when an update has been requested.
102  bool update_requested_;
103
104  DISALLOW_COPY_AND_ASSIGN(ManagedState);
105};
106
107}  // namespace chromeos
108
109#endif  // CHROMEOS_NETWORK_MANAGED_STATE_H_
110