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