| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2010, 2011 The Chromium OS Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef SRC_SERVICE_MANAGER_H_ | 5 #ifndef SRC_SERVICE_MANAGER_H_ |
| 6 #define SRC_SERVICE_MANAGER_H_ | 6 #define SRC_SERVICE_MANAGER_H_ |
| 7 | 7 |
| 8 #include <glib.h> | 8 #include <glib.h> |
| 9 | 9 |
| 10 #include <map> | 10 #include <map> |
| 11 #include <string> | 11 #include <string> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include <base/basictypes.h> // NOLINT | 14 #include <base/basictypes.h> // NOLINT |
| 15 #include <dbus-c++/dbus.h> // NOLINT | 15 #include <dbus-c++/dbus.h> // NOLINT |
| 16 | 16 |
| 17 #include "src/flimflam_manager_client_glue.h" | |
| 18 #include "src/property_changed_handler.h" | |
| 19 #include "src/service.h" | 17 #include "src/service.h" |
| 20 | 18 |
| 21 namespace cashew { | 19 namespace cashew { |
| 22 | 20 |
| 23 class CashewServer; | 21 class CashewServer; |
| 24 | 22 |
| 25 // map of service path names to Service objects | 23 // map of service path names to Service objects |
| 26 typedef std::map<std::string, Service*> ServiceMap; | 24 typedef std::map<std::string, Service*> ServiceMap; |
| 27 | 25 |
| 28 // vector of DBus::Path objects (strings) representing service path names | 26 // vector of DBus::Path objects (strings) representing service path names |
| 29 typedef std::vector<DBus::Path> ServicePathList; | 27 typedef std::vector<DBus::Path> ServicePathList; |
| 30 | 28 |
| 29 // ServiceManager interface |
| 31 // monitors Flimflam and maintains a collection of cellular service objects | 30 // monitors Flimflam and maintains a collection of cellular service objects |
| 32 class ServiceManager : public org::chromium::flimflam::Manager_proxy, | 31 class ServiceManager { |
| 33 public DBus::IntrospectableProxy, | |
| 34 public DBus::ObjectProxy, | |
| 35 public PropertyChangedDelegate { | |
| 36 public: | 32 public: |
| 37 ServiceManager(DBus::Connection& connection, // NOLINT | 33 ServiceManager() {} |
| 38 GMainLoop * const main_loop); | 34 virtual ~ServiceManager() {} |
| 39 virtual ~ServiceManager(); | |
| 40 | 35 |
| 41 // look up a service by its path name | 36 // look up a service by its path name |
| 42 // returns NULL if service is not found | 37 // returns NULL if service is not found |
| 43 virtual const Service* GetService(const std::string& service_path) const; | 38 virtual const Service* GetService(const std::string& service_path) const |
| 39 = 0; |
| 44 | 40 |
| 45 // set Cashew server | 41 // set Cashew server |
| 46 // we'll talk to it when we want to emit updates to the world on behalf of | 42 // we'll talk to it when we want to emit updates to the world on behalf of |
| 47 // our child Services. | 43 // our child Services. |
| 48 // it's ok to clear this by setting it to NULL, in which case our updates | 44 // it's ok to clear this by setting it to NULL, in which case our updates |
| 49 // will go into a black hole. | 45 // will go into a black hole. |
| 50 virtual void SetCashewServer(CashewServer *cashew_server); | 46 virtual void SetCashewServer(CashewServer *cashew_server) = 0; |
| 51 | 47 |
| 52 // return our latest info on the current default technology | 48 // return our latest info on the current default technology |
| 53 virtual Service::Type GetDefaultTechnology() const; | 49 virtual Service::Type GetDefaultTechnology() const = 0; |
| 54 | 50 |
| 55 // valid Flimflam global connectivity state values | 51 // valid Flimflam global connectivity state values |
| 56 typedef enum { | 52 typedef enum { |
| 57 kConnectivityStateUnknown = 0, | 53 kConnectivityStateUnknown = 0, |
| 58 kConnectivityStateOffline, | 54 kConnectivityStateOffline, |
| 59 kConnectivityStateConnected, | 55 kConnectivityStateConnected, |
| 60 kConnectivityStateOnline, | 56 kConnectivityStateOnline, |
| 61 } ConnectivityState; | 57 } ConnectivityState; |
| 62 | 58 |
| 63 // return our latest info on Flimflam's global connectivity state | 59 // return our latest info on Flimflam's global connectivity state |
| 64 virtual ConnectivityState GetConnectivityState() const; | 60 virtual ConnectivityState GetConnectivityState() const = 0; |
| 65 | 61 |
| 66 // does |state| represent an offline state | 62 // does |state| represent an offline state |
| 67 static bool IsOfflineConnectivityState(ConnectivityState state); | 63 static bool IsOfflineConnectivityState(ConnectivityState state); |
| 68 | 64 |
| 69 // does |state| represent an online state | 65 // does |state| represent an online state |
| 70 static bool IsOnlineConnectivityState(ConnectivityState state); | 66 static bool IsOnlineConnectivityState(ConnectivityState state); |
| 71 | 67 |
| 72 // Flimflam Manager D-Bus Proxy methods | |
| 73 | |
| 74 // receive incoming PropertyChanged D-Bus signal from Flimflam and | |
| 75 // schedule deferred processing | |
| 76 virtual void PropertyChanged(const std::string& property_name, | |
| 77 const DBus::Variant& new_value); | |
| 78 | |
| 79 // receive incoming StateChanged D-Bus signal from Flimflam, convert to a | |
| 80 // PropertyChanged signal, and schedule deferred processing | |
| 81 virtual void StateChanged(const std::string& new_state_string); | |
| 82 | |
| 83 // PropertyChangedDelegate methods | |
| 84 | |
| 85 // Perform deferred processing for an incoming PropertyChanged D-Bus signal | |
| 86 // from Flimflam | |
| 87 virtual void OnPropertyChanged(const PropertyChangedHandler *handler, | |
| 88 const std::string& property_name, | |
| 89 const DBus::Variant& new_value); | |
| 90 | |
| 91 // Service methods | 68 // Service methods |
| 92 | 69 |
| 93 // a child Service wants us to emit an update about its data plans | 70 // a child Service wants us to emit an update about its data plans |
| 94 virtual void EmitDataPlansUpdate(const Service& service); | 71 virtual void EmitDataPlansUpdate(const Service& service) = 0; |
| 95 | 72 |
| 96 // glib integration interface | 73 // factory |
| 97 | 74 static ServiceManager* NewServiceManager( |
| 98 // get idle/timer source id | 75 DBus::Connection& connection, // NOLINT |
| 99 virtual guint GetGetPropertiesSourceId() const; | 76 GMainLoop * const main_loop); |
| 100 | |
| 101 // set idle/timer source id | |
| 102 virtual void SetGetPropertiesSourceId(guint source_id); | |
| 103 | |
| 104 // are we in the process of retrying our GetProperties call? | |
| 105 virtual bool RetryingGetProperties() const; | |
| 106 | |
| 107 // set the retrying flag | |
| 108 virtual void OnRetryingGetProperties(bool retrying); | |
| 109 | 77 |
| 110 private: | 78 private: |
| 111 // D-Bus connection owned by our creator | |
| 112 // shared with Service objs that we create | |
| 113 DBus::Connection& connection_; | |
| 114 | |
| 115 // glib main loop | |
| 116 GMainLoop * const main_loop_; | |
| 117 | |
| 118 // collection of Service objs representing cellular services | |
| 119 ServiceMap services_; | |
| 120 | |
| 121 // Cashew server implementing our front-end interface | |
| 122 CashewServer *cashew_server_; | |
| 123 | |
| 124 // default technology most recently reported by Flimflam | |
| 125 Service::Type default_technology_; | |
| 126 | |
| 127 // the cellular service, if any, that we think is the default service | |
| 128 // can be NULL if we don't know the default service or a non-cellular | |
| 129 // service is the default | |
| 130 Service *default_cellular_service_; | |
| 131 | |
| 132 // global connectivity state most recently reported by Flimflam | |
| 133 ConnectivityState connectivity_state_; | |
| 134 | |
| 135 // GetProperties timer glib source id | |
| 136 // 0 means no source | |
| 137 guint get_properties_source_id_; | |
| 138 | |
| 139 // are we in the process of retrying our GetProperties call? | |
| 140 // this flag exists to distinguish between our initial g_idle_add call | |
| 141 // and our subsequent timer calls | |
| 142 bool retrying_get_properties_; | |
| 143 | |
| 144 // Handler for deferred processing of D-Bus PropertyChanged signals. | |
| 145 // | |
| 146 // We construct and enqueue PropertyChangedSignal objects as D-Bus signals | |
| 147 // arrive, and the handler calls our OnPropertyChanged method (part of our | |
| 148 // PropertyChangedDelegate interface) later from the glib main loop. | |
| 149 // | |
| 150 // This is done to avoid a libdbus-c++ deadlock that can occur when dbus | |
| 151 // messages are sent from within a dbus callback. | |
| 152 PropertyChangedHandler property_changed_handler_; | |
| 153 | |
| 154 // delete a collection of services | |
| 155 void DeleteServices(ServiceMap *service_map); | |
| 156 | |
| 157 // glib integration: static wrapper for GetFlimflamProperties | |
| 158 // takes object ptr as data and invokes object->GetFlimflamProperties() | |
| 159 static gboolean StaticGetFlimflamPropertiesCallback(gpointer data); | |
| 160 | |
| 161 // get interesting properties from Flimflam such as Services and | |
| 162 // DefaultTechnology | |
| 163 // returns true on success and false on failure | |
| 164 bool GetFlimflamProperties(); | |
| 165 | |
| 166 // we've received updated State info from Flimflam | |
| 167 void OnStateUpdate(const std::string& new_state_string); | |
| 168 | |
| 169 // we've received updated DefaultTechnology info from Flimflam | |
| 170 void OnDefaultTechnologyUpdate(const std::string& default_technology); | |
| 171 | |
| 172 // we've received updated Services info from Flimflam | |
| 173 void OnServicesUpdate(const ServicePathList& paths); | |
| 174 | |
| 175 // Flimflam has started advertising a new service | |
| 176 void OnNewService(const DBus::Path& path); | |
| 177 | |
| 178 // we've received updated default service info from Flimflam | |
| 179 // NULL |path| means that there is no default service | |
| 180 void OnDefaultServiceUpdate(const DBus::Path *path); | |
| 181 | |
| 182 // if |default_cellular_service_| points to a service, tell it | |
| 183 // that it is no longer the default service and clear our ptr | |
| 184 void ClearDefaultCellularService(); | |
| 185 | |
| 186 // tell |service| that it is now the default service and set our | |
| 187 // |default_cellular_service_| ptr to point to it. | |
| 188 // if there is already a default service, first clear it. | |
| 189 // NULL |service| is ok and is equivalent to ClearDefaultCellularService | |
| 190 // no-op if |service| == |default_cellular_service_| | |
| 191 void SetDefaultCellularService(Service *service); | |
| 192 | |
| 193 // convert connectivity state string to ConnectivityState enum value | |
| 194 static ConnectivityState ConnectivityStateFromString( | |
| 195 const std::string& state); | |
| 196 | |
| 197 // Flimflam has told us that it has come online | |
| 198 void OnFlimflamOnline(); | |
| 199 | |
| 200 // Flimflam has told us that it has gone offline | |
| 201 void OnFlimflamOffline(); | |
| 202 | |
| 203 DISALLOW_COPY_AND_ASSIGN(ServiceManager); | 79 DISALLOW_COPY_AND_ASSIGN(ServiceManager); |
| 204 }; | 80 }; |
| 205 | 81 |
| 206 } // namespace cashew | 82 } // namespace cashew |
| 207 | 83 |
| 208 #endif // SRC_SERVICE_MANAGER_H_ | 84 #endif // SRC_SERVICE_MANAGER_H_ |
| OLD | NEW |