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