managed_state.h revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
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 90 void set_name(const std::string& name) { name_ = name; } 91 92 private: 93 friend class NetworkChangeNotifierChromeosUpdateTest; 94 95 ManagedType managed_type_; 96 97 // The path (e.g. service path or device path) of the managed state object. 98 std::string path_; 99 100 // Common properties shared by all managed state objects. 101 std::string name_; // flimflam::kNameProperty 102 std::string type_; // flimflam::kTypeProperty 103 104 // Tracks when an update has been requested. 105 bool update_requested_; 106 107 DISALLOW_COPY_AND_ASSIGN(ManagedState); 108}; 109 110} // namespace chromeos 111 112#endif // CHROMEOS_NETWORK_MANAGED_STATE_H_ 113