| 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 // TODO(vlaviano): update accessor style (GetFoo/SetFoo -> foo) | 5 // TODO(vlaviano): update accessor style (GetFoo/SetFoo -> foo) |
| 6 | 6 |
| 7 #ifndef SRC_SERVICE_H_ | 7 #ifndef SRC_SERVICE_H_ |
| 8 #define SRC_SERVICE_H_ | 8 #define SRC_SERVICE_H_ |
| 9 | 9 |
| 10 #include <glib.h> | |
| 11 | |
| 12 #include <map> | 10 #include <map> |
| 13 #include <string> | 11 #include <string> |
| 14 | 12 |
| 15 #include <base/basictypes.h> // NOLINT | 13 #include <base/basictypes.h> // NOLINT |
| 16 #include <dbus-c++/dbus.h> // NOLINT | 14 #include <dbus-c++/dbus.h> // NOLINT |
| 17 | 15 |
| 18 #include "src/data_plan.h" | 16 #include "src/data_plan.h" |
| 19 #include "src/data_plan_provider.h" | |
| 20 #include "src/flimflam_service_client_glue.h" | |
| 21 #include "src/property_changed_handler.h" | |
| 22 | 17 |
| 23 namespace cashew { | 18 namespace cashew { |
| 24 | 19 |
| 25 class Device; | 20 class Device; |
| 26 class Policy; | 21 class Policy; |
| 27 class ServiceManager; | 22 class ServiceManager; |
| 28 | 23 |
| 29 // map of key, value pairs representing service properties received via D-Bus | 24 // map of key, value pairs representing service properties received via D-Bus |
| 30 typedef std::map<std::string, DBus::Variant> PropertyMap; | 25 typedef std::map<std::string, DBus::Variant> PropertyMap; |
| 31 | 26 |
| 27 // Service interface |
| 32 // represents a cellular service and monitors Flimflam state for that service | 28 // represents a cellular service and monitors Flimflam state for that service |
| 33 class Service : public org::chromium::flimflam::Service_proxy, | 29 class Service { |
| 34 public DBus::IntrospectableProxy, | |
| 35 public DBus::ObjectProxy, | |
| 36 public DataPlanProviderDelegate, | |
| 37 public PropertyChangedDelegate { | |
| 38 public: | 30 public: |
| 39 Service(ServiceManager * const parent, | 31 Service() {} |
| 40 DBus::Connection& connection, // NOLINT | 32 virtual ~Service() {} |
| 41 const DBus::Path& path); | |
| 42 virtual ~Service(); | |
| 43 | 33 |
| 44 // get D-Bus path for this service | 34 // get D-Bus path for this service |
| 45 virtual const DBus::Path& GetPath() const; | 35 virtual const DBus::Path& GetPath() const = 0; |
| 46 | 36 |
| 47 // valid state values for this service | 37 // valid state values for this service |
| 48 typedef enum { | 38 typedef enum { |
| 49 kStateUnknown = 0, | 39 kStateUnknown = 0, |
| 50 kStateIdle, | 40 kStateIdle, |
| 51 kStateCarrier, | 41 kStateCarrier, |
| 52 kStateAssociation, | 42 kStateAssociation, |
| 53 kStateConfiguration, | 43 kStateConfiguration, |
| 54 kStateReady, | 44 kStateReady, |
| 55 kStateDisconnect, | 45 kStateDisconnect, |
| 56 kStateFailure, | 46 kStateFailure, |
| 57 kStateActivationFailure, | 47 kStateActivationFailure, |
| 58 } State; | 48 } State; |
| 59 | 49 |
| 60 // get state for this service | 50 // get state for this service |
| 61 virtual State GetState() const; | 51 virtual State GetState() const = 0; |
| 62 | 52 |
| 63 // valid type values for this service | 53 // valid type values for this service |
| 64 typedef enum { | 54 typedef enum { |
| 65 kTypeUnknown = 0, | 55 kTypeUnknown = 0, |
| 66 kTypeEthernet, | 56 kTypeEthernet, |
| 67 kTypeWifi, | 57 kTypeWifi, |
| 68 kTypeWimax, | 58 kTypeWimax, |
| 69 kTypeBluetooth, | 59 kTypeBluetooth, |
| 70 kTypeCellular, | 60 kTypeCellular, |
| 71 } Type; | 61 } Type; |
| 72 | 62 |
| 73 // get type for this service | 63 // get type for this service |
| 74 // NOTE: for now, should always be kTypeCellular | 64 // NOTE: for now, should always be kTypeCellular |
| 75 virtual Type GetType() const; | 65 virtual Type GetType() const = 0; |
| 76 | 66 |
| 77 // convert type string to Type enum value | 67 // convert type string to Type enum value |
| 78 static Type TypeFromString(const std::string& type); | 68 static Type TypeFromString(const std::string& type); |
| 79 | 69 |
| 80 // get Device object for this service | 70 // get Device object for this service |
| 81 // this can return NULL if we don't yet have Device info | 71 // this can return NULL if we don't yet have Device info |
| 82 virtual Device* GetDevice() const; | 72 virtual Device* GetDevice() const = 0; |
| 83 | 73 |
| 84 // get data plan info, formatted for D-Bus | 74 // get data plan info, formatted for D-Bus |
| 85 // filters out inactive (future, expired, or consumed) plans | 75 // filters out inactive (future, expired, or consumed) plans |
| 86 virtual DBusDataPlanList GetDBusDataPlans() const; | 76 virtual DBusDataPlanList GetDBusDataPlans() const = 0; |
| 87 | 77 |
| 88 // do we think that we're the default service at this time? | 78 // do we think that we're the default service at this time? |
| 89 virtual bool IsDefaultService() const; | 79 virtual bool IsDefaultService() const = 0; |
| 90 | 80 |
| 91 typedef enum { | 81 typedef enum { |
| 92 kConnectivityStateUnknown = 0, | 82 kConnectivityStateUnknown = 0, |
| 93 kConnectivityStateRestricted, | 83 kConnectivityStateRestricted, |
| 94 kConnectivityStateUnrestricted, | 84 kConnectivityStateUnrestricted, |
| 95 kConnectivityStateNone, | 85 kConnectivityStateNone, |
| 96 } ConnectivityState; | 86 } ConnectivityState; |
| 97 | 87 |
| 98 // get connectivity state for this service | 88 // get connectivity state for this service |
| 99 virtual ConnectivityState GetConnectivityState() const; | 89 virtual ConnectivityState GetConnectivityState() const = 0; |
| 100 | |
| 101 // Flimflam Service D-Bus Proxy methods | |
| 102 | |
| 103 // receive incoming PropertyChanged D-Bus signal from Flimflam service and | |
| 104 // schedule deferred processing | |
| 105 virtual void PropertyChanged(const std::string& property_name, | |
| 106 const DBus::Variant& new_value); | |
| 107 | |
| 108 // PropertyChangedDelegate methods | |
| 109 | |
| 110 virtual void OnPropertyChanged(const PropertyChangedHandler *handler, | |
| 111 const std::string& property_name, | |
| 112 const DBus::Variant& new_value); | |
| 113 | 90 |
| 114 // Device methods | 91 // Device methods |
| 115 | 92 |
| 116 // we've received updated Cellular.Carrier info from our child Device | 93 // we've received updated Cellular.Carrier info from our child Device |
| 117 virtual void OnCarrierUpdate(const std::string& carrier); | 94 virtual void OnCarrierUpdate(const std::string& carrier) = 0; |
| 118 | 95 |
| 119 // we've received updated byte counter info from our child Device | 96 // we've received updated byte counter info from our child Device |
| 120 virtual void OnByteCounterUpdate(uint64 rx_bytes, uint64 tx_bytes); | 97 virtual void OnByteCounterUpdate(uint64 rx_bytes, uint64 tx_bytes) = 0; |
| 121 | |
| 122 // DataPlanProviderDelegate methods | |
| 123 | |
| 124 // a request to our carrier usage API proxy has completed | |
| 125 virtual void OnRequestComplete(const DataPlanProvider *provider, | |
| 126 bool successful, | |
| 127 const Value *parsed_usage_update); | |
| 128 | 98 |
| 129 // Service Manager methods | 99 // Service Manager methods |
| 130 | 100 |
| 131 // we've received an update from our parent about whether or not we're the | 101 // we've received an update from our parent about whether or not we're the |
| 132 // default service | 102 // default service |
| 133 virtual void OnDefaultServiceUpdate(bool is_default_service); | 103 virtual void OnDefaultServiceUpdate(bool is_default_service) = 0; |
| 134 | 104 |
| 135 // we've received an update from our parent that Flimflam has come online | 105 // we've received an update from our parent that Flimflam has come online |
| 136 virtual void OnFlimflamOnline(); | 106 virtual void OnFlimflamOnline() = 0; |
| 137 | 107 |
| 138 // we've received an update from our parent that Flimflam has gone offline | 108 // we've received an update from our parent that Flimflam has gone offline |
| 139 virtual void OnFlimflamOffline(); | 109 virtual void OnFlimflamOffline() = 0; |
| 140 | 110 |
| 141 // glib integration interface | 111 // factory |
| 142 | 112 static Service* NewService(ServiceManager * const parent, |
| 143 // get idle/timer source id | 113 DBus::Connection& connection, // NOLINT |
| 144 virtual guint GetGetPropertiesSourceId() const; | 114 const DBus::Path& path); |
| 145 | |
| 146 // set idle/timer source id | |
| 147 virtual void SetGetPropertiesSourceId(guint source_id); | |
| 148 | |
| 149 // are we in the process of retrying our GetProperties call? | |
| 150 virtual bool RetryingGetProperties() const; | |
| 151 | |
| 152 // set the retrying flag | |
| 153 virtual void OnRetryingGetProperties(bool retrying); | |
| 154 | 115 |
| 155 private: | 116 private: |
| 156 // back pointer to our parent ServiceManager | |
| 157 ServiceManager * const parent_; | |
| 158 | |
| 159 // D-Bus connection, owned by our creator | |
| 160 // shared with Device obj that we create | |
| 161 DBus::Connection& connection_; | |
| 162 | |
| 163 // D-Bus path for Flimflam service that we represent | |
| 164 // this is our unique identifier | |
| 165 const DBus::Path path_; | |
| 166 | |
| 167 // Service connection state | |
| 168 State state_; | |
| 169 | |
| 170 // Service type | |
| 171 Type type_; | |
| 172 | |
| 173 // Device corresponding to our service | |
| 174 Device *device_; | |
| 175 | |
| 176 // cached data plan info | |
| 177 DataPlanList data_plans_; | |
| 178 | |
| 179 // carrier usage API proxy | |
| 180 DataPlanProvider *provider_; | |
| 181 | |
| 182 // carrier usage API URL | |
| 183 // empty string represents "unknown" | |
| 184 std::string usage_url_; | |
| 185 | |
| 186 // do we have an outstanding usage API request | |
| 187 bool request_in_progress_; | |
| 188 | |
| 189 // periodic update timeout | |
| 190 GSource *update_timeout_source_; | |
| 191 | |
| 192 // carrier policy | |
| 193 Policy *policy_; | |
| 194 | |
| 195 // do we think that we're the default service? | |
| 196 bool is_default_service_; | |
| 197 | |
| 198 // GetProperties timer glib source id | |
| 199 // 0 means no source | |
| 200 guint get_properties_source_id_; | |
| 201 | |
| 202 // are we in the process of retrying our GetProperties call? | |
| 203 // this flag exists to distinguish between our initial g_idle_add call | |
| 204 // and our subsequent timer calls | |
| 205 bool retrying_get_properties_; | |
| 206 | |
| 207 // Service connectivity state | |
| 208 // finer-grained connectivity info applicable when |state_| == kStateReady | |
| 209 ConnectivityState connectivity_state_; | |
| 210 | |
| 211 // Handler for deferred processing of D-Bus PropertyChanged signals. | |
| 212 // See comments in service_manager.h | |
| 213 PropertyChangedHandler property_changed_handler_; | |
| 214 | |
| 215 // convert connectivity state string to ConnectivityState enum value | |
| 216 static ConnectivityState ConnectivityStateFromString( | |
| 217 const std::string& connectivity_state); | |
| 218 | |
| 219 // convert state string to State enum value | |
| 220 static State StateFromString(const std::string& state); | |
| 221 | |
| 222 // we've received updated Device info from Flimflam | |
| 223 void OnDeviceUpdate(const DBus::Path& device_path); | |
| 224 | |
| 225 // we've received updated Connectivity State info from Flimflam | |
| 226 void OnConnectivityStateUpdate(const std::string& connectivity_state); | |
| 227 | |
| 228 // we've received updated State info from Flimflam | |
| 229 void OnStateUpdate(const std::string& state); | |
| 230 | |
| 231 // we've received updated Type info from Flimflam | |
| 232 void OnTypeUpdate(const std::string& type); | |
| 233 | |
| 234 // we've received updated Cellular.UsageUrl info from Flimflam | |
| 235 void OnUsageUrlUpdate(const std::string& usage_url); | |
| 236 | |
| 237 // glib integration: static wrapper for GetServiceProperties | |
| 238 // takes object ptr as data and invokes object->GetServiceProperties() | |
| 239 static gboolean StaticGetServicePropertiesCallback(gpointer data); | |
| 240 | |
| 241 // get service properties from Flimflam | |
| 242 // returns true on success and false on failure | |
| 243 bool GetServiceProperties(); | |
| 244 | |
| 245 // clear a DataPlanList and delete its data plan objects | |
| 246 void DeleteDataPlans(DataPlanList *data_plans); | |
| 247 | |
| 248 // create a hardcoded data plan and add it to data_plans_ list | |
| 249 // for debugging | |
| 250 void AddHardcodedDataPlan(); | |
| 251 | |
| 252 // ask provider to request a usage update if it makes sense | |
| 253 void RequestUsageUpdate(); | |
| 254 | |
| 255 // cancel any pending usage API requests that we have | |
| 256 void CancelPendingRequests(); | |
| 257 | |
| 258 // update timeout callback | |
| 259 gboolean UpdateTimeoutCallback(); | |
| 260 | |
| 261 // glib integration: static wrapper for UpdateTimeoutCallback | |
| 262 // takes object ptr as data and invokes object->UpdateTimeoutCallback() | |
| 263 static gboolean StaticUpdateTimeoutCallback(gpointer data); | |
| 264 | |
| 265 // create and start an update timer that fires periodically | |
| 266 // |provider_| and |policy_| must both be non-NULL | |
| 267 // the timer interval is retrieved from the current policy | |
| 268 // returns true on success and false on failure | |
| 269 bool CreateUpdateTimer(); | |
| 270 | |
| 271 // stop and destroy the update timer if it exists | |
| 272 void DestroyUpdateTimer(); | |
| 273 | |
| 274 // delete carrier usage API proxy and policy if they exist | |
| 275 // this also destroys the update timer and cancels pending requests | |
| 276 // NOTE: this does not delete usage url (it's considered service state) | |
| 277 void DeleteCarrierState(); | |
| 278 | |
| 279 // does |status| contain a valid usage API status result string? | |
| 280 bool IsValidCrosUsageStatus(const std::string& status) const; | |
| 281 | |
| 282 // usage API request returned an error result | |
| 283 void OnCrosUsageErrorResult(const std::string& status); | |
| 284 | |
| 285 // do we think that we're connected at this time? | |
| 286 bool IsConnected() const; | |
| 287 | |
| 288 // we've just become connected | |
| 289 void OnConnected(); | |
| 290 | |
| 291 // we've just become disconnected | |
| 292 // NOTE: this means we've entered a state other than kStateReady, not | |
| 293 // that we're necessarily in kStateDisconnect | |
| 294 void OnDisconnected(); | |
| 295 | |
| 296 // are we currently sending periodic usage API requests? | |
| 297 bool IsSendingUsageRequests() const; | |
| 298 | |
| 299 // based on our best current knowledge and our policy, should we be | |
| 300 // sending periodic usage API requests? | |
| 301 bool ShouldSendUsageRequests() const; | |
| 302 | |
| 303 // stop sending periodic usage API requests | |
| 304 void StopSendingUsageRequests(); | |
| 305 | |
| 306 // start sending periodic usage API requests | |
| 307 void StartSendingUsageRequests(); | |
| 308 | |
| 309 // something has changed, so reevaluate whether or not we should be sending | |
| 310 // periodic usage API requests and adjust our behavior if necessary | |
| 311 void ReconsiderSendingUsageRequests(); | |
| 312 | |
| 313 // consult policy to determine if we should send an unsolicited update | |
| 314 void MaybeEmitDataPlansUpdate(); | |
| 315 | |
| 316 DISALLOW_COPY_AND_ASSIGN(Service); | 117 DISALLOW_COPY_AND_ASSIGN(Service); |
| 317 }; | 118 }; |
| 318 | 119 |
| 319 } // namespace cashew | 120 } // namespace cashew |
| 320 | 121 |
| 321 #endif // SRC_SERVICE_H_ | 122 #endif // SRC_SERVICE_H_ |
| OLD | NEW |