managed_state.h revision 68043e1e95eeb07d5cae7aca370b26518b0867d6
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;
16class DictionaryValue;
17}
18
19namespace chromeos {
20
21class DeviceState;
22class FavoriteState;
23class NetworkState;
24class NetworkTypePattern;
25
26// Base class for states managed by NetworkStateManger which are associated
27// with a Shill path (e.g. service path or device path).
28class CHROMEOS_EXPORT ManagedState {
29 public:
30  enum ManagedType {
31    MANAGED_TYPE_NETWORK,
32    MANAGED_TYPE_FAVORITE,
33    MANAGED_TYPE_DEVICE
34  };
35
36  virtual ~ManagedState();
37
38  // This will construct and return a new instance of the appropriate class
39  // based on |type|.
40  static ManagedState* Create(ManagedType type, const std::string& path);
41
42  // Returns the specific class pointer if this is the correct type, or
43  // NULL if it is not.
44  NetworkState* AsNetworkState();
45  DeviceState* AsDeviceState();
46  FavoriteState* AsFavoriteState();
47
48  // Called by NetworkStateHandler when a property was received. The return
49  // value indicates if the state changed and is used to reduce the number of
50  // notifications. The only guarantee however is: If the return value is false
51  // then the state wasn't modified. This might happen because of
52  // * |key| was not recognized.
53  // * |value| was not parsed successfully.
54  // * |value| is equal to the cached property value.
55  // If the return value is true, the state might or might not be modified.
56  virtual bool PropertyChanged(const std::string& key,
57                               const base::Value& value) = 0;
58
59  // Called by NetworkStateHandler after all calls to PropertyChanged for the
60  // initial set of properties. Used to update state requiring multiple
61  // properties, e.g. name from hex_ssid in NetworkState.
62  // |properties| contains the complete set of initial properties.
63  // Returns true if any additional properties are updated.
64  virtual bool InitialPropertiesReceived(
65      const base::DictionaryValue& properties);
66
67  const ManagedType managed_type() const { return managed_type_; }
68  const std::string& path() const { return path_; }
69  const std::string& name() const { return name_; }
70  const std::string& type() const { return type_; }
71  bool update_received() const { return update_received_; }
72  void set_update_received() { update_received_ = true; }
73  bool update_requested() const { return update_requested_; }
74  void set_update_requested(bool update_requested) {
75    update_requested_ = update_requested;
76  }
77
78  bool Matches(const NetworkTypePattern& pattern) const;
79
80 protected:
81  ManagedState(ManagedType type, const std::string& path);
82
83  // Parses common property keys (name, type).
84  bool ManagedStatePropertyChanged(const std::string& key,
85                                   const base::Value& value);
86
87  // Helper methods that log warnings and return true if parsing succeeded and
88  // the new value does not match the existing output value.
89  bool GetBooleanValue(const std::string& key,
90                       const base::Value& value,
91                       bool* out_value);
92  bool GetIntegerValue(const std::string& key,
93                       const base::Value& value,
94                       int* out_value);
95  bool GetStringValue(const std::string& key,
96                      const base::Value& value,
97                      std::string* out_value);
98  bool GetUInt32Value(const std::string& key,
99                      const base::Value& value,
100                      uint32* out_value);
101
102  void set_name(const std::string& name) { name_ = name; }
103
104 private:
105  friend class NetworkChangeNotifierChromeosUpdateTest;
106
107  ManagedType managed_type_;
108
109  // The path (e.g. service path or device path) of the managed state object.
110  std::string path_;
111
112  // Common properties shared by all managed state objects.
113  std::string name_;  // shill::kNameProperty
114  std::string type_;  // shill::kTypeProperty
115
116  // Set to true when the an update has been received.
117  bool update_received_;
118
119  // Tracks when an update has been requested.
120  bool update_requested_;
121
122  DISALLOW_COPY_AND_ASSIGN(ManagedState);
123};
124
125}  // namespace chromeos
126
127#endif  // CHROMEOS_NETWORK_MANAGED_STATE_H_
128