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