blob: d240fc8af93950d0874f3da750ab929508cfa50e [file] [log] [blame]
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Paul Stewart75897df2011-04-27 09:05:53 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Ben Chan19f83972012-10-03 23:25:56 -07005#ifndef SHILL_SERVICE_H_
6#define SHILL_SERVICE_H_
Paul Stewart75897df2011-04-27 09:05:53 -07007
mukesh agrawal568b5c62012-02-28 14:44:47 -08008#include <time.h>
9
Chris Masone9be4a9d2011-05-16 15:44:09 -070010#include <string>
Chris Masone8fe2c7e2011-06-09 15:51:19 -070011#include <map>
Paul Stewartcb59fed2012-03-21 21:14:46 -070012#include <set>
Chris Masone8fe2c7e2011-06-09 15:51:19 -070013#include <vector>
Chris Masone9be4a9d2011-05-16 15:44:09 -070014
Christopher Wiley0801d192012-09-24 11:57:15 -070015#include <base/cancelable_callback.h>
Chris Masone9be4a9d2011-05-16 15:44:09 -070016#include <base/memory/ref_counted.h>
Paul Stewartba41b992011-05-26 07:02:46 -070017#include <base/memory/scoped_ptr.h>
Christopher Wiley0801d192012-09-24 11:57:15 -070018#include <base/memory/weak_ptr.h>
Darin Petkovba40dd32011-07-11 20:06:39 -070019#include <gtest/gtest_prod.h> // for FRIEND_TEST
Paul Stewart75897df2011-04-27 09:05:53 -070020
Eric Shienbrood9a245532012-03-07 14:20:39 -050021#include "shill/adaptor_interfaces.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070022#include "shill/accessor_interface.h"
Eric Shienbrood9a245532012-03-07 14:20:39 -050023#include "shill/callbacks.h"
Chris Masoneb925cc82011-06-22 15:39:57 -070024#include "shill/property_store.h"
Chris Masone7aa5f902011-07-11 11:13:35 -070025#include "shill/refptr_types.h"
Paul Stewart1062d9d2012-04-27 10:42:27 -070026#include "shill/static_ip_parameters.h"
Paul Stewart22aa71b2011-09-16 12:15:11 -070027#include "shill/technology.h"
Chris Masonec1e50412011-06-07 13:04:53 -070028
Thieu Leb84ba342012-03-02 15:15:19 -080029namespace chromeos_metrics {
30class Timer;
31}
32
Paul Stewart75897df2011-04-27 09:05:53 -070033namespace shill {
34
Chris Masone9be4a9d2011-05-16 15:44:09 -070035class ControlInterface;
Paul Stewart75897df2011-04-27 09:05:53 -070036class Endpoint;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070037class Error;
Chris Masone9be4a9d2011-05-16 15:44:09 -070038class EventDispatcher;
Paul Stewartbe5f5b32011-12-07 17:11:11 -080039class HTTPProxy;
mukesh agrawal7a4e4002011-09-06 11:26:05 -070040class KeyValueStore;
Chris Masone6791a432011-07-12 13:23:19 -070041class Manager;
Thieu Le48e6d6d2011-12-06 00:40:27 +000042class Metrics;
Chris Masone9be4a9d2011-05-16 15:44:09 -070043class ServiceAdaptorInterface;
Darin Petkov5eb05422012-05-11 15:45:25 +020044class Sockets;
Darin Petkovba40dd32011-07-11 20:06:39 -070045class StoreInterface;
Paul Stewart75897df2011-04-27 09:05:53 -070046
Chris Masone7aa5f902011-07-11 11:13:35 -070047// A Service is a uniquely named entity, which the system can
48// connect in order to begin sending and receiving network traffic.
49// All Services are bound to an Entry, which represents the persistable
50// state of the Service. If the Entry is populated at the time of Service
51// creation, that information is used to prime the Service. If not, the Entry
52// becomes populated over time.
Chris Masone27c4aa52011-07-02 13:10:14 -070053class Service : public base::RefCounted<Service> {
Paul Stewart75897df2011-04-27 09:05:53 -070054 public:
Darin Petkovba40dd32011-07-11 20:06:39 -070055 static const char kCheckPortalAuto[];
56 static const char kCheckPortalFalse[];
57 static const char kCheckPortalTrue[];
58
Paul Stewart0756db92012-01-27 08:34:47 -080059 // TODO(pstew): Storage constants shouldn't need to be public
60 // crosbug.com/25813
61 static const char kStorageAutoConnect[];
62 static const char kStorageCheckPortal[];
63 static const char kStorageEapAnonymousIdentity[];
64 static const char kStorageEapCACert[];
65 static const char kStorageEapCACertID[];
Paul Stewartecf4cd12012-04-17 11:08:39 -070066 static const char kStorageEapCACertNSS[];
Paul Stewart0756db92012-01-27 08:34:47 -080067 static const char kStorageEapCertID[];
68 static const char kStorageEapClientCert[];
69 static const char kStorageEapEap[];
70 static const char kStorageEapIdentity[];
71 static const char kStorageEapInnerEap[];
72 static const char kStorageEapKeyID[];
73 static const char kStorageEapKeyManagement[];
74 static const char kStorageEapPIN[];
75 static const char kStorageEapPassword[];
76 static const char kStorageEapPrivateKey[];
77 static const char kStorageEapPrivateKeyPassword[];
78 static const char kStorageEapUseSystemCAs[];
79 static const char kStorageError[];
80 static const char kStorageFavorite[];
81 static const char kStorageGUID[];
mukesh agrawalcf24a242012-05-21 16:46:11 -070082 static const char kStorageHasEverConnected[];
Paul Stewart0756db92012-01-27 08:34:47 -080083 static const char kStorageName[];
84 static const char kStoragePriority[];
85 static const char kStorageProxyConfig[];
86 static const char kStorageSaveCredentials[];
87 static const char kStorageType[];
88 static const char kStorageUIData[];
89
mukesh agrawal8f3f7752012-02-17 19:42:09 -080090 static const uint8 kStrengthMax;
91 static const uint8 kStrengthMin;
92
Paul Stewart75897df2011-04-27 09:05:53 -070093 enum ConnectFailure {
Paul Stewart03dba0b2011-08-22 16:32:45 -070094 kFailureUnknown,
Paul Stewartf2d60912012-07-15 08:37:30 -070095 kFailureActivation,
Paul Stewart03dba0b2011-08-22 16:32:45 -070096 kFailureOutOfRange,
97 kFailurePinMissing,
Paul Stewartf2d60912012-07-15 08:37:30 -070098 kFailureDHCP,
99 kFailureBadPassphrase,
Paul Stewart03dba0b2011-08-22 16:32:45 -0700100 kFailureNeedEVDO,
101 kFailureNeedHomeNetwork,
Paul Stewartf2d60912012-07-15 08:37:30 -0700102 kFailureOTASP,
103 kFailureAAA,
104 kFailureBadWEPKey,
105 kFailureConnect,
106 kFailureDNSLookup,
107 kFailureHTTPGet,
108 kFailurePPPAuth,
Thieu Le48e6d6d2011-12-06 00:40:27 +0000109 kFailureMax
Paul Stewart75897df2011-04-27 09:05:53 -0700110 };
Chris Masone9be4a9d2011-05-16 15:44:09 -0700111 enum ConnectState {
Paul Stewart03dba0b2011-08-22 16:32:45 -0700112 kStateUnknown,
113 kStateIdle,
114 kStateAssociating,
115 kStateConfiguring,
116 kStateConnected,
117 kStateDisconnected,
Thieu Le48e6d6d2011-12-06 00:40:27 +0000118 kStatePortal,
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800119 kStateFailure,
120 kStateOnline
Chris Masone9be4a9d2011-05-16 15:44:09 -0700121 };
Chris Masoneb2e326b2011-07-12 13:28:51 -0700122 struct EapCredentials {
Paul Stewart20550982012-04-16 12:16:11 -0700123 EapCredentials() : use_system_cas(true) {}
Paul Stewartbc6e7392012-05-24 07:07:48 -0700124 // Who we identify ourselves as to the EAP authenticator.
Chris Masoneb2e326b2011-07-12 13:28:51 -0700125 std::string identity;
Paul Stewartbc6e7392012-05-24 07:07:48 -0700126 // The outer or only EAP authetnication type.
Chris Masoneb2e326b2011-07-12 13:28:51 -0700127 std::string eap;
Paul Stewartbc6e7392012-05-24 07:07:48 -0700128 // The inner EAP authentication type.
Chris Masoneb2e326b2011-07-12 13:28:51 -0700129 std::string inner_eap;
Paul Stewartbc6e7392012-05-24 07:07:48 -0700130 // When there is an inner EAP type, use this identity for the outer.
Chris Masoneb2e326b2011-07-12 13:28:51 -0700131 std::string anonymous_identity;
Paul Stewartbc6e7392012-05-24 07:07:48 -0700132 // Filename of the client certificate.
Chris Masoneb2e326b2011-07-12 13:28:51 -0700133 std::string client_cert;
Paul Stewartbc6e7392012-05-24 07:07:48 -0700134 // Locator for the client certificate within the security token.
Chris Masoneb2e326b2011-07-12 13:28:51 -0700135 std::string cert_id;
Paul Stewartbc6e7392012-05-24 07:07:48 -0700136 // Filename of the client private key.
Chris Masoneb2e326b2011-07-12 13:28:51 -0700137 std::string private_key;
Paul Stewartbc6e7392012-05-24 07:07:48 -0700138 // Password for decrypting the client private key file.
Chris Masoneb2e326b2011-07-12 13:28:51 -0700139 std::string private_key_password;
Paul Stewartbc6e7392012-05-24 07:07:48 -0700140 // Locator for the client private key within the security token.
Chris Masoneb2e326b2011-07-12 13:28:51 -0700141 std::string key_id;
Paul Stewartbc6e7392012-05-24 07:07:48 -0700142 // Filename of the certificate authority (CA) certificate.
Chris Masoneb2e326b2011-07-12 13:28:51 -0700143 std::string ca_cert;
Paul Stewartbc6e7392012-05-24 07:07:48 -0700144 // Locator for the CA certificate within the security token.
Chris Masoneb2e326b2011-07-12 13:28:51 -0700145 std::string ca_cert_id;
Paul Stewartbc6e7392012-05-24 07:07:48 -0700146 // Locator for the CA certificate within the user NSS database.
Paul Stewartecf4cd12012-04-17 11:08:39 -0700147 std::string ca_cert_nss;
Paul Stewartbc6e7392012-05-24 07:07:48 -0700148 // If true, use the system-wide CA database to authenticate the remote.
Chris Masoneb2e326b2011-07-12 13:28:51 -0700149 bool use_system_cas;
Paul Stewartbc6e7392012-05-24 07:07:48 -0700150 // PIN code for accessing the security token.
Chris Masoneb2e326b2011-07-12 13:28:51 -0700151 std::string pin;
Paul Stewartbc6e7392012-05-24 07:07:48 -0700152 // Password to use for EAP methods which require one.
Chris Masoneb2e326b2011-07-12 13:28:51 -0700153 std::string password;
Paul Stewartbc6e7392012-05-24 07:07:48 -0700154 // Key management algorithm to use after EAP succeeds.
Chris Masoneb2e326b2011-07-12 13:28:51 -0700155 std::string key_management;
Paul Stewartbc6e7392012-05-24 07:07:48 -0700156 // If non-empty, string to match remote subject against before connecting.
157 std::string subject_match;
158 // List of subject names reported by remote entity during TLS setup.
159 std::vector<std::string> remote_certification;
Chris Masoneb2e326b2011-07-12 13:28:51 -0700160 };
Chris Masone9be4a9d2011-05-16 15:44:09 -0700161
Paul Stewartac4ac002011-08-26 12:04:26 -0700162 static const int kPriorityNone;
163
Chris Masone9be4a9d2011-05-16 15:44:09 -0700164 // A constructor for the Service object
165 Service(ControlInterface *control_interface,
166 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -0800167 Metrics *metrics,
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700168 Manager *manager,
Gaurav Shah435de2c2011-11-17 19:01:07 -0800169 Technology::Identifier technology);
Darin Petkovba40dd32011-07-11 20:06:39 -0700170
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000171 // AutoConnect MAY choose to ignore the connection request in some
172 // cases. For example, if the corresponding Device only supports one
173 // concurrent connection, and another Service is already connected
174 // or connecting.
175 //
176 // AutoConnect MAY issue RPCs immediately. So AutoConnect MUST NOT
177 // be called from a D-Bus signal handler context.
178 virtual void AutoConnect();
179 // Queue up a connection attempt.
mukesh agrawaladb68482012-01-17 16:31:51 -0800180 virtual void Connect(Error *error);
Christopher Wileyabd3b502012-09-26 13:08:52 -0700181 // Disconnect this service. Override this method to add your service specific
182 // disconnect logic, but call the super class's Disconnect() first.
mukesh agrawaladb68482012-01-17 16:31:51 -0800183 virtual void Disconnect(Error *error);
Christopher Wileyabd3b502012-09-26 13:08:52 -0700184 // Disconnects this service via Disconnect(). Marks the service as having
185 // failed with |failure|. Do not override this method.
186 virtual void DisconnectWithFailure(ConnectFailure failure, Error *error);
187 // Disconnects this service via Disconnect(). The service will not be eligible
188 // for auto-connect until a subsequent call to Connect, or Load. Do not
189 // override this method.
190 virtual void UserInitiatedDisconnect(Error *error);
Chris Masonea82b7112011-05-25 15:16:29 -0700191
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500192 // The default implementation returns the error kInvalidArguments.
193 virtual void ActivateCellularModem(const std::string &carrier,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500194 Error *error,
195 const ResultCallback &callback);
Darin Petkovc408e692011-08-17 13:47:15 -0700196
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800197 virtual bool IsActive(Error *error);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700198
Paul Stewart7fb09382012-07-18 17:32:27 -0700199 // Returns whether services of this type should be auto-connect by default.
200 virtual bool IsAutoConnectByDefault() const { return false; }
201
Paul Stewart03dba0b2011-08-22 16:32:45 -0700202 virtual ConnectState state() const { return state_; }
203 // Updates the state of the Service and alerts the manager. Also
204 // clears |failure_| if the new state isn't a failure.
205 virtual void SetState(ConnectState state);
Darin Petkov58f0b6d2012-06-12 12:52:30 +0200206 std::string GetStateString() const;
Paul Stewart03dba0b2011-08-22 16:32:45 -0700207
Paul Stewart22aa71b2011-09-16 12:15:11 -0700208 // State utility functions
Paul Stewart20088d82012-02-16 06:58:55 -0800209 virtual bool IsConnected() const {
210 return state() == kStateConnected || state() == kStatePortal ||
211 state() == kStateOnline;
212 }
Gaurav Shah435de2c2011-11-17 19:01:07 -0800213 virtual bool IsConnecting() const {
Paul Stewart22aa71b2011-09-16 12:15:11 -0700214 return state() == kStateAssociating || state() == kStateConfiguring;
215 }
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000216 virtual bool IsFailed() const {
mukesh agrawal568b5c62012-02-28 14:44:47 -0800217 // We sometimes lie about the failure state, to keep Chrome happy
218 // (see comment in WiFi::HandleDisconnect). Hence, we check both
219 // state and |failed_time_|.
220 return state() == kStateFailure || failed_time_ > 0;
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000221 }
Paul Stewartdf3c0a82012-11-09 15:54:33 -0800222
223 // Returns true if the connection for |this| depends on service |b|.
224 virtual bool IsDependentOn(const ServiceRefPtr &b) const;
225
Paul Stewarta121c442012-06-09 14:12:58 -0700226 virtual bool IsPortalled() const {
227 return state() == kStatePortal;
228 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700229
Paul Stewart03dba0b2011-08-22 16:32:45 -0700230 virtual ConnectFailure failure() const { return failure_; }
mukesh agrawal568b5c62012-02-28 14:44:47 -0800231 // Records the failure mode and time. Sets the Service state to "Failure".
Paul Stewart03dba0b2011-08-22 16:32:45 -0700232 virtual void SetFailure(ConnectFailure failure);
mukesh agrawal568b5c62012-02-28 14:44:47 -0800233 // Records the failure mode and time. Sets the Service state to "Idle".
234 // Avoids showing a failure mole in the UI.
235 virtual void SetFailureSilent(ConnectFailure failure);
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700236
Darin Petkovba40dd32011-07-11 20:06:39 -0700237 // Returns a string that is guaranteed to uniquely identify this Service
238 // instance.
mukesh agrawald835b202011-10-07 15:26:47 -0700239 const std::string &UniqueName() const { return unique_name_; }
Darin Petkovafa6fc42011-06-21 16:21:08 -0700240
Chris Masone6791a432011-07-12 13:23:19 -0700241 virtual std::string GetRpcIdentifier() const;
Chris Masone3c3f6a12011-07-01 10:01:41 -0700242
Darin Petkovba40dd32011-07-11 20:06:39 -0700243 // Returns the unique persistent storage identifier for the service.
Chris Masone6515aab2011-10-12 16:19:09 -0700244 virtual std::string GetStorageIdentifier() const = 0;
Darin Petkovba40dd32011-07-11 20:06:39 -0700245
Paul Stewartbba6a5b2011-11-02 18:45:59 -0700246 // Returns whether the service configuration can be loaded from |storage|.
247 virtual bool IsLoadableFrom(StoreInterface *storage) const;
248
Paul Stewart81426132012-05-16 10:05:10 -0700249 // Returns true if the service uses 802.1x for key management.
250 virtual bool Is8021x() const { return false; };
251
Darin Petkovba40dd32011-07-11 20:06:39 -0700252 // Loads the service from persistent |storage|. Returns true on success.
Chris Masone9d779932011-08-25 16:33:41 -0700253 virtual bool Load(StoreInterface *storage);
Darin Petkovba40dd32011-07-11 20:06:39 -0700254
Paul Stewarta41e38d2011-11-11 07:47:29 -0800255 // Indicate to service that it is no longer persisted to storage. It
Paul Stewart65512e12012-03-26 18:01:08 -0700256 // should purge any stored profile state (e.g., credentials). Returns
257 // true to indicate that this service should also be unregistered from
258 // the manager, false otherwise.
259 virtual bool Unload();
Paul Stewarta41e38d2011-11-11 07:47:29 -0800260
Darin Petkovba40dd32011-07-11 20:06:39 -0700261 // Saves the service to persistent |storage|. Returns true on success.
Chris Masone9d779932011-08-25 16:33:41 -0700262 virtual bool Save(StoreInterface *storage);
Darin Petkovba40dd32011-07-11 20:06:39 -0700263
Thieu Led4e9e552012-02-16 16:26:07 -0800264 // Saves the service to the current profile.
Gary Moraind93615e2012-04-27 11:50:03 -0700265 virtual void SaveToCurrentProfile();
Thieu Led4e9e552012-02-16 16:26:07 -0800266
Paul Stewartcb59fed2012-03-21 21:14:46 -0700267 // Applies all the properties in |args| to this service object's mutable
268 // store, except for those in parameters_ignored_for_configure_.
269 // Returns an error in |error| if one or more parameter set attempts
270 // fails, but will only return the first error.
271 virtual void Configure(const KeyValueStore &args, Error *error);
272
Paul Stewartd215af62012-04-24 23:25:50 -0700273 // Returns whether portal detection is explicitly disabled on this service
274 // via a property set on it.
275 virtual bool IsPortalDetectionDisabled() const;
276
277 // Returns whether portal detection is set to follow the default setting
278 // of this service's technology via a property set on it.
279 virtual bool IsPortalDetectionAuto() const;
280
Paul Stewart10ccbb32012-04-26 15:59:30 -0700281 // Returns true if the service is persisted to a non-ephemeral profile.
282 virtual bool IsRemembered() const;
283
Paul Stewarta41e38d2011-11-11 07:47:29 -0800284 // Returns true if the service RPC identifier should be part of the
285 // manager's advertised services list, false otherwise.
286 virtual bool IsVisible() const { return true; }
287
Paul Stewart20088d82012-02-16 06:58:55 -0800288 // Returns true if there is a proxy configuration set on this service.
289 virtual bool HasProxyConfig() const { return !proxy_config_.empty(); }
290
mukesh agrawal00917ce2011-11-22 23:56:55 +0000291 virtual void MakeFavorite();
292
Darin Petkov5eb05422012-05-11 15:45:25 +0200293 // Set the connection for this service. If the connection is non-NULL, create
294 // an HTTP Proxy that will utilize this service's connection to serve
295 // requests.
296 virtual void SetConnection(const ConnectionRefPtr &connection);
297 virtual const ConnectionRefPtr &connection() const { return connection_; }
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800298
Gaurav Shah10109f22011-11-11 20:16:22 -0800299 // Examines the EAP credentials for the service and returns true if a
300 // connection attempt can be made.
301 bool Is8021xConnectable() const;
302
Paul Stewartbc6e7392012-05-24 07:07:48 -0700303 // Add an EAP certification id |name| at position |depth| in the stack.
304 // Returns true if entry was added, false otherwise.
305 virtual bool AddEAPCertification(const std::string &name, size_t depth);
306 // Clear all EAP certification elements.
307 void ClearEAPCertification();
308
Thieu Le48e6d6d2011-12-06 00:40:27 +0000309 // The inherited class should register any custom metrics in this method.
310 virtual void InitializeCustomMetrics() const {}
311
312 // The inherited class that needs to send metrics after the service has
Thieu Leb84ba342012-03-02 15:15:19 -0800313 // transitioned to the ready state should override this method.
314 // |time_resume_to_ready_milliseconds| holds the elapsed time from when
315 // the system was resumed until when the service transitioned to the
316 // connected state. This value is non-zero for the first service transition
317 // to the connected state after a resume.
318 virtual void SendPostReadyStateMetrics(
319 int64 /*time_resume_to_ready_milliseconds*/) const {}
Thieu Le48e6d6d2011-12-06 00:40:27 +0000320
Darin Petkovafa6fc42011-06-21 16:21:08 -0700321 bool auto_connect() const { return auto_connect_; }
322 void set_auto_connect(bool connect) { auto_connect_ = connect; }
Paul Stewart75897df2011-04-27 09:05:53 -0700323
Gaurav Shah435de2c2011-11-17 19:01:07 -0800324 bool connectable() const { return connectable_; }
Darin Petkovb2ba39f2012-06-06 10:33:43 +0200325 // TODO(petkov): Remove this method in favor of SetConnectable.
mukesh agrawal29c13a12011-11-24 00:09:19 +0000326 void set_connectable(bool connectable);
Darin Petkovb2ba39f2012-06-06 10:33:43 +0200327 // Sets the connectable property of the service. Broadcasts the new value and
328 // alerts the manager if necessary.
329 void SetConnectable(bool connectable);
Gaurav Shah435de2c2011-11-17 19:01:07 -0800330
Thieu Le67370f62012-02-14 23:01:42 +0000331 virtual bool explicitly_disconnected() const {
332 return explicitly_disconnected_;
333 }
334
Paul Stewart22aa71b2011-09-16 12:15:11 -0700335 bool favorite() const { return favorite_; }
mukesh agrawal00917ce2011-11-22 23:56:55 +0000336 // Setter is deliberately omitted; use MakeFavorite.
Paul Stewart22aa71b2011-09-16 12:15:11 -0700337
mukesh agrawal15908392011-11-16 18:29:25 +0000338 const std::string &friendly_name() const { return friendly_name_; }
Darin Petkov7f060332012-03-14 11:46:47 +0100339 void set_friendly_name(const std::string &n) { friendly_name_ = n; }
mukesh agrawal15908392011-11-16 18:29:25 +0000340
Paul Stewart4c561612012-03-21 12:49:01 -0700341 const std::string &guid() const { return guid_; }
342 void set_guid(const std::string &guid) { guid_ = guid; }
343
mukesh agrawalcf24a242012-05-21 16:46:11 -0700344 bool has_ever_connected() const { return has_ever_connected_; }
345
Paul Stewart22aa71b2011-09-16 12:15:11 -0700346 int32 priority() const { return priority_; }
347 void set_priority(int32 priority) { priority_ = priority; }
348
Paul Stewart1ca3e852011-11-04 07:50:49 -0700349 int32 security_level() const { return security_level_; }
350 void set_security_level(int32 security) { security_level_ = security; }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700351
Darin Petkovd78ee7e2012-01-12 11:21:10 +0100352 void SetStrength(uint8 strength);
mukesh agrawale1d90e92012-02-15 17:36:08 -0800353
354 // uint8 streams out as a char. Coerce to a larger type, so that
355 // it prints as a number.
356 uint16 strength() const { return strength_; }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700357
Gaurav Shah435de2c2011-11-17 19:01:07 -0800358 virtual Technology::Identifier technology() const { return technology_; }
Darin Petkov58f0b6d2012-06-12 12:52:30 +0200359 std::string GetTechnologyString() const;
Gaurav Shah435de2c2011-11-17 19:01:07 -0800360
Gaurav Shah10109f22011-11-11 20:16:22 -0800361 const EapCredentials &eap() const { return eap_; }
362 virtual void set_eap(const EapCredentials &eap);
363
Darin Petkovcb715292012-04-25 13:04:37 +0200364 bool save_credentials() const { return save_credentials_; }
365 void set_save_credentials(bool save) { save_credentials_ = save; }
366
Darin Petkov51489002011-08-18 13:13:20 -0700367 const std::string &error() const { return error_; }
368 void set_error(const std::string &error) { error_ = error; }
369
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800370 static const char *ConnectFailureToString(const ConnectFailure &state);
371 static const char *ConnectStateToString(const ConnectState &state);
372
Paul Stewart22aa71b2011-09-16 12:15:11 -0700373 // Compare two services. Returns true if Service a should be displayed
374 // above Service b
375 static bool Compare(ServiceRefPtr a,
376 ServiceRefPtr b,
mukesh agrawalddc378f2012-02-17 18:26:20 -0800377 const std::vector<Technology::Identifier> &tech_order,
378 const char **reason);
Paul Stewart22aa71b2011-09-16 12:15:11 -0700379
Chris Masone34af2182011-08-22 11:59:36 -0700380 // These are defined in service.cc so that we don't have to include profile.h
Chris Masone9d779932011-08-25 16:33:41 -0700381 // TODO(cmasone): right now, these are here only so that we can get the
382 // profile name as a property. Can we store just the name, and then handle
383 // setting the profile for this service via |manager_|?
Chris Masone6791a432011-07-12 13:23:19 -0700384 const ProfileRefPtr &profile() const;
385 void set_profile(const ProfileRefPtr &p);
386
Paul Stewart4357f4e2012-04-26 17:39:26 -0700387 // Notification that occurs when a service now has profile data saved
388 // on its behalf. Some service types like WiFi can choose to register
389 // themselves at this point.
390 virtual void OnProfileConfigured() {}
391
Paul Stewartff14b022012-04-24 20:06:23 -0700392 // Notification that occurs when a single property has been changed via
393 // the RPC adaptor.
Paul Stewart81426132012-05-16 10:05:10 -0700394 virtual void OnPropertyChanged(const std::string &property);
Paul Stewartff14b022012-04-24 20:06:23 -0700395
Christopher Wiley0801d192012-09-24 11:57:15 -0700396 // Called by the manager once after a resume.
397 virtual void OnAfterResume();
398
mukesh agrawalde29fa82011-09-16 16:16:36 -0700399 PropertyStore *mutable_store() { return &store_; }
400 const PropertyStore &store() const { return store_; }
Paul Stewartdef189e2012-08-02 20:12:09 -0700401 StaticIPParameters *mutable_static_ip_parameters() {
402 return &static_ip_parameters_;
403 }
Paul Stewart1062d9d2012-04-27 10:42:27 -0700404 const StaticIPParameters &static_ip_parameters() const {
405 return static_ip_parameters_;
406 }
Chris Masone27c4aa52011-07-02 13:10:14 -0700407
mukesh agrawalb54601c2011-06-07 17:39:22 -0700408 protected:
Darin Petkov5eb05422012-05-11 15:45:25 +0200409 friend class base::RefCounted<Service>;
410
411 virtual ~Service();
412
Chris Masone34af2182011-08-22 11:59:36 -0700413 // Returns true if a character is allowed to be in a service storage id.
414 static bool LegalChar(char a) { return isalnum(a) || a == '_'; }
415
Darin Petkov31332412012-01-28 01:50:02 +0100416 // Returns true if a character is disallowed to be in a service storage id.
417 static bool IllegalChar(char a) { return !LegalChar(a); }
418
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800419 virtual std::string CalculateState(Error *error);
Darin Petkov58f0b6d2012-06-12 12:52:30 +0200420 std::string CalculateTechnology(Error *error);
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700421
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000422 // Returns whether this service is in a state conducive to auto-connect.
423 // This should include any tests used for computing connectable(),
424 // as well as other critera such as whether the device associated with
425 // this service is busy with another connection.
mukesh agrawalbf14e942012-03-02 14:36:34 -0800426 //
427 // If the service is not auto-connectable, |*reason| will be set to
428 // point to C-string explaining why the service is not auto-connectable.
429 virtual bool IsAutoConnectable(const char **reason) const;
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000430
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800431 // HelpRegisterDerived*: Expose a property over RPC, with the name |name|.
432 //
433 // Reads of the property will be handled by invoking |get|.
434 // Writes to the property will be handled by invoking |set|.
435 // Clearing the property will be handled by PropertyStore.
mukesh agrawalffa3d042011-10-06 15:26:10 -0700436 void HelpRegisterDerivedBool(
437 const std::string &name,
Hristo Stefanoved2c28c2011-11-29 15:37:30 -0800438 bool(Service::*get)(Error *error),
439 void(Service::*set)(const bool &value, Error *error));
mukesh agrawalffa3d042011-10-06 15:26:10 -0700440 void HelpRegisterDerivedString(
441 const std::string &name,
Hristo Stefanoved2c28c2011-11-29 15:37:30 -0800442 std::string(Service::*get)(Error *error),
443 void(Service::*set)(const std::string &value, Error *error));
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800444 void HelpRegisterDerivedUint16(
445 const std::string &name,
446 uint16(Service::*get)(Error *error),
447 void(Service::*set)(const uint16 &value, Error *error));
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400448 void HelpRegisterDerivedRpcIdentifier(
449 const std::string &name,
450 std::string(Service::*get)(Error *),
451 void(Service::*set)(const RpcIdentifier&, Error *));
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800452 // Expose a property over RPC, with the name |name|.
453 //
454 // Reads of the property will be handled by invoking |get|.
455 // Writes to the property will be handled by invoking |set|.
456 //
457 // Clearing the property will be handled by invoking |clear|, or
458 // calling |set| with |default_value| (whichever is non-NULL). It
459 // is an error to call this method with both |clear| and
460 // |default_value| non-NULL.
461 void HelpRegisterWriteOnlyDerivedString(
462 const std::string &name,
463 void(Service::*set)(const std::string &value, Error *error),
464 void(Service::*clear)(Error *error),
465 const std::string *default_value);
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700466
Darin Petkovb72cf402011-11-22 14:51:39 +0100467 ServiceAdaptorInterface *adaptor() const { return adaptor_.get(); }
468
Darin Petkovba40dd32011-07-11 20:06:39 -0700469 // Assigns |value| to |key| in |storage| if |value| is non-empty and |save| is
470 // true. Otherwise, removes |key| from |storage|. If |crypted| is true, the
471 // value is encrypted.
472 void SaveString(StoreInterface *storage,
Chris Masone34af2182011-08-22 11:59:36 -0700473 const std::string &id,
Darin Petkovba40dd32011-07-11 20:06:39 -0700474 const std::string &key,
475 const std::string &value,
476 bool crypted,
477 bool save);
478
Chris Masone34af2182011-08-22 11:59:36 -0700479 void LoadEapCredentials(StoreInterface *storage, const std::string &id);
480 void SaveEapCredentials(StoreInterface *storage, const std::string &id);
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800481 void UnloadEapCredentials();
Darin Petkovba40dd32011-07-11 20:06:39 -0700482
Paul Stewartcb59fed2012-03-21 21:14:46 -0700483 // Ignore |parameter| when performing a Configure() operation.
484 void IgnoreParameterForConfigure(const std::string &parameter);
485
Paul Stewartf2d60912012-07-15 08:37:30 -0700486 // Update the service's string-based "Error" RPC property based on the
487 // failure_ enum.
488 void UpdateErrorProperty();
489
Paul Stewartcf199de2012-08-16 07:50:41 -0700490 // RPC setter for the the "AutoConnect" property.
491 virtual void SetAutoConnect(const bool &connect, Error *error);
492
Paul Stewartac4ac002011-08-26 12:04:26 -0700493 // Property accessors reserved for subclasses
494 EventDispatcher *dispatcher() const { return dispatcher_; }
495 const std::string &GetEAPKeyManagement() const;
496 void SetEAPKeyManagement(const std::string &key_management);
Paul Stewart9f32d192012-01-30 20:37:50 -0800497 void SetEAPPassword(const std::string &password, Error *error);
498 void SetEAPPrivateKeyPassword(const std::string &password, Error *error);
Paul Stewart65512e12012-03-26 18:01:08 -0700499 Manager *manager() const { return manager_; }
Thieu Le48e6d6d2011-12-06 00:40:27 +0000500 Metrics *metrics() const { return metrics_; }
mukesh agrawalb54601c2011-06-07 17:39:22 -0700501
Darin Petkov1d0080a2012-04-30 17:10:36 +0200502 void set_favorite(bool favorite) { favorite_ = favorite; }
503
Paul Stewart75897df2011-04-27 09:05:53 -0700504 private:
Paul Stewartcf199de2012-08-16 07:50:41 -0700505 friend class EthernetServiceTest;
Thieu Le48e6d6d2011-12-06 00:40:27 +0000506 friend class MetricsTest;
Paul Stewart0756db92012-01-27 08:34:47 -0800507 friend class ServiceAdaptorInterface;
Darin Petkov5eb05422012-05-11 15:45:25 +0200508 friend class VPNServiceTest;
mukesh agrawale1d90e92012-02-15 17:36:08 -0800509 friend class WiFiServiceTest;
Darin Petkov6b9b2e12012-07-10 15:51:42 +0200510 friend class WiMaxServiceTest;
Christopher Wiley0801d192012-09-24 11:57:15 -0700511 FRIEND_TEST(AllMockServiceTest, AutoConnectWithFailures);
Ben Chan19f83972012-10-03 23:25:56 -0700512 FRIEND_TEST(CellularServiceTest, IsAutoConnectable);
mukesh agrawalcc0fded2012-05-09 13:40:58 -0700513 FRIEND_TEST(DeviceTest, IPConfigUpdatedFailureWithStatic);
Darin Petkov58f0b6d2012-06-12 12:52:30 +0200514 FRIEND_TEST(ServiceTest, CalculateState);
515 FRIEND_TEST(ServiceTest, CalculateTechnology);
Paul Stewartbc6e7392012-05-24 07:07:48 -0700516 FRIEND_TEST(ServiceTest, Certification);
Paul Stewartcb59fed2012-03-21 21:14:46 -0700517 FRIEND_TEST(ServiceTest, ConfigureIgnoredProperty);
518 FRIEND_TEST(ServiceTest, ConfigureStringProperty);
Darin Petkovba40dd32011-07-11 20:06:39 -0700519 FRIEND_TEST(ServiceTest, Constructor);
Paul Stewart10241e32012-04-23 18:15:06 -0700520 FRIEND_TEST(ServiceTest, GetIPConfigRpcIdentifier);
Thieu Le284fe792012-01-31 17:53:19 -0800521 FRIEND_TEST(ServiceTest, GetProperties);
mukesh agrawal76d13882012-01-12 15:23:11 -0800522 FRIEND_TEST(ServiceTest, IsAutoConnectable);
Paul Stewartdf3c0a82012-11-09 15:54:33 -0800523 FRIEND_TEST(ServiceTest, IsDependentOn);
Paul Stewartd215af62012-04-24 23:25:50 -0700524 FRIEND_TEST(ServiceTest, RecheckPortal);
Darin Petkovba40dd32011-07-11 20:06:39 -0700525 FRIEND_TEST(ServiceTest, Save);
526 FRIEND_TEST(ServiceTest, SaveString);
527 FRIEND_TEST(ServiceTest, SaveStringCrypted);
528 FRIEND_TEST(ServiceTest, SaveStringDontSave);
529 FRIEND_TEST(ServiceTest, SaveStringEmpty);
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800530 FRIEND_TEST(ServiceTest, SetProperty);
Paul Stewartd215af62012-04-24 23:25:50 -0700531 FRIEND_TEST(ServiceTest, SetCheckPortal);
Darin Petkovb2ba39f2012-06-06 10:33:43 +0200532 FRIEND_TEST(ServiceTest, SetConnectable);
mukesh agrawal568b5c62012-02-28 14:44:47 -0800533 FRIEND_TEST(ServiceTest, State);
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800534 FRIEND_TEST(ServiceTest, Unload);
mukesh agrawalcf24a242012-05-21 16:46:11 -0700535 FRIEND_TEST(WiFiMainTest, SuspectCredentialsWPAPreviouslyConnected);
Christopher Wiley0801d192012-09-24 11:57:15 -0700536 FRIEND_TEST(WiFiTimerTest, ReconnectTimer);
Darin Petkovba40dd32011-07-11 20:06:39 -0700537
mukesh agrawalbf14e942012-03-02 14:36:34 -0800538 static const char kAutoConnConnected[];
539 static const char kAutoConnConnecting[];
540 static const char kAutoConnExplicitDisconnect[];
541 static const char kAutoConnNotConnectable[];
Christopher Wiley0801d192012-09-24 11:57:15 -0700542 static const char kAutoConnThrottled[];
mukesh agrawalbf14e942012-03-02 14:36:34 -0800543
Paul Stewartbc6e7392012-05-24 07:07:48 -0700544 static const size_t kEAPMaxCertificationElements;
545
Darin Petkov2f903b32012-04-18 12:56:43 +0200546 static const char kServiceSortAutoConnect[];
547 static const char kServiceSortConnectable[];
548 static const char kServiceSortFavorite[];
mukesh agrawalddc378f2012-02-17 18:26:20 -0800549 static const char kServiceSortIsConnected[];
Paul Stewartdf3c0a82012-11-09 15:54:33 -0800550 static const char kServiceSortDependency[];
mukesh agrawalddc378f2012-02-17 18:26:20 -0800551 static const char kServiceSortIsConnecting[];
552 static const char kServiceSortIsFailed[];
Paul Stewarta121c442012-06-09 14:12:58 -0700553 static const char kServiceSortIsPortalled[];
Darin Petkov2f903b32012-04-18 12:56:43 +0200554 static const char kServiceSortPriority[];
mukesh agrawalddc378f2012-02-17 18:26:20 -0800555 static const char kServiceSortSecurityEtc[];
Darin Petkov2f903b32012-04-18 12:56:43 +0200556 static const char kServiceSortTechnology[];
mukesh agrawalddc378f2012-02-17 18:26:20 -0800557 static const char kServiceSortUniqueName[];
558
Christopher Wiley0801d192012-09-24 11:57:15 -0700559 static const uint64 kMaxAutoConnectCooldownTimeMilliseconds;
560 static const uint64 kMinAutoConnectCooldownTimeMilliseconds;
561 static const uint64 kAutoConnectCooldownBackoffFactor;
562
Thieu Le284fe792012-01-31 17:53:19 -0800563 bool GetAutoConnect(Error *error);
Thieu Le284fe792012-01-31 17:53:19 -0800564
Paul Stewartd215af62012-04-24 23:25:50 -0700565 std::string GetCheckPortal(Error *error);
566 void SetCheckPortal(const std::string &check_portal, Error *error);
567
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800568 virtual std::string GetDeviceRpcId(Error *error) = 0;
Chris Masone95207da2011-06-29 16:50:49 -0700569
Paul Stewart10241e32012-04-23 18:15:06 -0700570 std::string GetIPConfigRpcIdentifier(Error *error);
571
Paul Stewart0c438332012-04-11 07:55:27 -0700572 std::string GetNameProperty(Error *error);
573 void AssertTrivialSetNameProperty(const std::string &name, Error *error);
574
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800575 std::string GetProfileRpcId(Error *error);
576 void SetProfileRpcId(const std::string &profile, Error *error);
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700577
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800578 // Returns TCP port of service's HTTP proxy in host order.
579 uint16 GetHTTPProxyPort(Error *error);
580
Christopher Wiley0801d192012-09-24 11:57:15 -0700581 void ReEnableAutoConnectTask();
582 // Disables autoconnect and posts a task to re-enable it after a cooldown.
583 // Note that autoconnect could be disabled for other reasons as well.
584 void ThrottleFutureAutoConnects();
585
mukesh agrawalcf24a242012-05-21 16:46:11 -0700586 // Saves settings to profile, if we have one. Unlike
587 // SaveServiceToProfile, SaveToProfile never assigns this service
588 // into a profile.
589 void SaveToProfile();
590
Paul Stewart22aa71b2011-09-16 12:15:11 -0700591 // Utility function that returns true if a is different from b. When they
592 // are, "decision" is populated with the boolean value of "a > b".
593 static bool DecideBetween(int a, int b, bool *decision);
594
Paul Stewartac4ac002011-08-26 12:04:26 -0700595 ConnectState state_;
596 ConnectFailure failure_;
597 bool auto_connect_;
598 std::string check_portal_;
599 bool connectable_;
600 std::string error_;
mukesh agrawaladb68482012-01-17 16:31:51 -0800601 bool explicitly_disconnected_;
Paul Stewartac4ac002011-08-26 12:04:26 -0700602 bool favorite_;
603 int32 priority_;
Paul Stewart1ca3e852011-11-04 07:50:49 -0700604 int32 security_level_;
Darin Petkovd78ee7e2012-01-12 11:21:10 +0100605 uint8 strength_;
Paul Stewartac4ac002011-08-26 12:04:26 -0700606 std::string proxy_config_;
Paul Stewart987e71e2011-12-05 09:45:06 -0800607 std::string ui_data_;
Paul Stewart4c561612012-03-21 12:49:01 -0700608 std::string guid_;
Paul Stewartac4ac002011-08-26 12:04:26 -0700609 bool save_credentials_;
610 EapCredentials eap_; // Only saved if |save_credentials_| is true.
Gaurav Shah435de2c2011-11-17 19:01:07 -0800611 Technology::Identifier technology_;
mukesh agrawal568b5c62012-02-28 14:44:47 -0800612 // The time of the most recent failure. Value is 0 if the service is
613 // not currently failed.
614 time_t failed_time_;
mukesh agrawalcf24a242012-05-21 16:46:11 -0700615 // Whether or not this service has ever reached kStateConnected.
616 bool has_ever_connected_;
Paul Stewartac4ac002011-08-26 12:04:26 -0700617
Christopher Wiley0801d192012-09-24 11:57:15 -0700618 base::CancelableClosure reenable_auto_connect_task_;
619 uint64 auto_connect_cooldown_milliseconds_ ;
620
Paul Stewartac4ac002011-08-26 12:04:26 -0700621 ProfileRefPtr profile_;
622 PropertyStore store_;
Paul Stewartcb59fed2012-03-21 21:14:46 -0700623 std::set<std::string> parameters_ignored_for_configure_;
Paul Stewartac4ac002011-08-26 12:04:26 -0700624
625 EventDispatcher *dispatcher_;
mukesh agrawal51a7e932011-07-27 16:18:26 -0700626 static unsigned int serial_number_;
mukesh agrawald835b202011-10-07 15:26:47 -0700627 std::string unique_name_; // MUST be unique amongst service instances
628 std::string friendly_name_; // MAY be same as |unique_name_|
Paul Stewartba41b992011-05-26 07:02:46 -0700629 scoped_ptr<ServiceAdaptorInterface> adaptor_;
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800630 scoped_ptr<HTTPProxy> http_proxy_;
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800631 ConnectionRefPtr connection_;
Paul Stewart1062d9d2012-04-27 10:42:27 -0700632 StaticIPParameters static_ip_parameters_;
Thieu Le3426c8f2012-01-11 17:35:11 -0800633 Metrics *metrics_;
Chris Masone6791a432011-07-12 13:23:19 -0700634 Manager *manager_;
Darin Petkov5eb05422012-05-11 15:45:25 +0200635 scoped_ptr<Sockets> sockets_;
Christopher Wiley0801d192012-09-24 11:57:15 -0700636 base::WeakPtrFactory<Service> weak_ptr_factory_;
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700637
mukesh agrawalb54601c2011-06-07 17:39:22 -0700638 DISALLOW_COPY_AND_ASSIGN(Service);
Paul Stewart75897df2011-04-27 09:05:53 -0700639};
640
641} // namespace shill
642
Ben Chan19f83972012-10-03 23:25:56 -0700643#endif // SHILL_SERVICE_H_