Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium OS 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 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 5 | #ifndef SHILL_SERVICE_ |
| 6 | #define SHILL_SERVICE_ |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 7 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 8 | #include <string> |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 9 | #include <map> |
| 10 | #include <vector> |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 11 | |
| 12 | #include <base/memory/ref_counted.h> |
Paul Stewart | ba41b99 | 2011-05-26 07:02:46 -0700 | [diff] [blame] | 13 | #include <base/memory/scoped_ptr.h> |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 14 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 15 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 16 | #include "shill/accessor_interface.h" |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 17 | #include "shill/property_store.h" |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 18 | #include "shill/refptr_types.h" |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 19 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 20 | namespace shill { |
| 21 | |
| 22 | class Connection; |
| 23 | class Configuration; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 24 | class ControlInterface; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 25 | class Endpoint; |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 26 | class Error; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 27 | class EventDispatcher; |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 28 | class Manager; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 29 | class ServiceAdaptorInterface; |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 30 | class StoreInterface; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 31 | |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 32 | // A Service is a uniquely named entity, which the system can |
| 33 | // connect in order to begin sending and receiving network traffic. |
| 34 | // All Services are bound to an Entry, which represents the persistable |
| 35 | // state of the Service. If the Entry is populated at the time of Service |
| 36 | // creation, that information is used to prime the Service. If not, the Entry |
| 37 | // becomes populated over time. |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 38 | class Service : public base::RefCounted<Service> { |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 39 | public: |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 40 | static const char kCheckPortalAuto[]; |
| 41 | static const char kCheckPortalFalse[]; |
| 42 | static const char kCheckPortalTrue[]; |
| 43 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 44 | enum ConnectFailure { |
| 45 | kServiceFailureUnknown, |
| 46 | kServiceFailureActivationFailure, |
| 47 | kServiceFailureOutOfRange, |
| 48 | kServiceFailurePinMissing, |
| 49 | kServiceFailureConfigurationFailed, |
| 50 | kServiceFailureBadCredentials, |
| 51 | kServiceFailureNeedEVDO, |
| 52 | kServiceFailureNeedHomeNetwork, |
| 53 | kServiceFailureOTASPFailure, |
| 54 | kServiceFailureAAAFailure |
| 55 | }; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 56 | enum ConnectState { |
| 57 | kServiceStateUnknown, |
| 58 | kServiceStateIdle, |
| 59 | kServiceStateAssociating, |
| 60 | kServiceStateConfiguring, |
| 61 | kServiceStateConnected, |
| 62 | kServiceStateDisconnected, |
| 63 | kServiceStateFailure |
| 64 | }; |
Chris Masone | b2e326b | 2011-07-12 13:28:51 -0700 | [diff] [blame] | 65 | struct EapCredentials { |
| 66 | EapCredentials() : use_system_cas(false) {} |
| 67 | std::string identity; |
| 68 | std::string eap; |
| 69 | std::string inner_eap; |
| 70 | std::string anonymous_identity; |
| 71 | std::string client_cert; |
| 72 | std::string cert_id; |
| 73 | std::string private_key; |
| 74 | std::string private_key_password; |
| 75 | std::string key_id; |
| 76 | std::string ca_cert; |
| 77 | std::string ca_cert_id; |
| 78 | bool use_system_cas; |
| 79 | std::string pin; |
| 80 | std::string password; |
| 81 | std::string key_management; |
| 82 | }; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 83 | |
| 84 | // A constructor for the Service object |
| 85 | Service(ControlInterface *control_interface, |
| 86 | EventDispatcher *dispatcher, |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 87 | Manager *manager); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 88 | virtual ~Service(); |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 89 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 90 | virtual void Connect() = 0; |
| 91 | virtual void Disconnect() = 0; |
Chris Masone | a82b711 | 2011-05-25 15:16:29 -0700 | [diff] [blame] | 92 | |
Darin Petkov | c408e69 | 2011-08-17 13:47:15 -0700 | [diff] [blame] | 93 | // The default implementation asserts. |
| 94 | virtual void ActivateCellularModem(const std::string &carrier); |
| 95 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 96 | virtual bool IsActive() { return false; } |
| 97 | |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 98 | // Returns a string that is guaranteed to uniquely identify this Service |
| 99 | // instance. |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 100 | const std::string &UniqueName() const { return name_; } |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 101 | |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 102 | virtual std::string GetRpcIdentifier() const; |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 103 | |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 104 | // Returns the unique persistent storage identifier for the service. |
| 105 | std::string GetStorageIdentifier(); |
| 106 | |
| 107 | // Loads the service from persistent |storage|. Returns true on success. |
| 108 | virtual bool Load(StoreInterface *storage); |
| 109 | |
| 110 | // Saves the service to persistent |storage|. Returns true on success. |
| 111 | virtual bool Save(StoreInterface *storage); |
| 112 | |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 113 | bool auto_connect() const { return auto_connect_; } |
| 114 | void set_auto_connect(bool connect) { auto_connect_ = connect; } |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 115 | |
Darin Petkov | 5148900 | 2011-08-18 13:13:20 -0700 | [diff] [blame] | 116 | const std::string &error() const { return error_; } |
| 117 | void set_error(const std::string &error) { error_ = error; } |
| 118 | |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 119 | const ProfileRefPtr &profile() const; |
| 120 | void set_profile(const ProfileRefPtr &p); |
| 121 | |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 122 | PropertyStore *store() { return &store_; } |
| 123 | |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 124 | protected: |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 125 | static const int kPriorityNone = 0; |
| 126 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 127 | virtual std::string CalculateState() = 0; |
| 128 | |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 129 | void HelpRegisterDerivedBool(const std::string &name, |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 130 | bool(Service::*get)(void), |
| 131 | bool(Service::*set)(const bool&)); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 132 | void HelpRegisterDerivedString(const std::string &name, |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 133 | std::string(Service::*get)(void), |
| 134 | bool(Service::*set)(const std::string&)); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 135 | |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 136 | // Assigns |value| to |key| in |storage| if |value| is non-empty and |save| is |
| 137 | // true. Otherwise, removes |key| from |storage|. If |crypted| is true, the |
| 138 | // value is encrypted. |
| 139 | void SaveString(StoreInterface *storage, |
| 140 | const std::string &key, |
| 141 | const std::string &value, |
| 142 | bool crypted, |
| 143 | bool save); |
| 144 | |
| 145 | void LoadEapCredentials(StoreInterface *storage); |
| 146 | void SaveEapCredentials(StoreInterface *storage); |
| 147 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 148 | // Properties |
| 149 | bool auto_connect_; |
| 150 | std::string check_portal_; |
| 151 | bool connectable_; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 152 | std::string error_; |
| 153 | bool favorite_; |
| 154 | int32 priority_; |
| 155 | std::string proxy_config_; |
Chris Masone | b2e326b | 2011-07-12 13:28:51 -0700 | [diff] [blame] | 156 | bool save_credentials_; |
| 157 | EapCredentials eap_; // Only saved if |save_credentials_| is true. |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 158 | |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 159 | ProfileRefPtr profile_; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 160 | PropertyStore store_; |
| 161 | |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 162 | EventDispatcher *dispatcher_; |
| 163 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 164 | private: |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 165 | friend class ServiceAdaptorInterface; |
| 166 | FRIEND_TEST(ServiceTest, Constructor); |
| 167 | FRIEND_TEST(ServiceTest, Save); |
| 168 | FRIEND_TEST(ServiceTest, SaveString); |
| 169 | FRIEND_TEST(ServiceTest, SaveStringCrypted); |
| 170 | FRIEND_TEST(ServiceTest, SaveStringDontSave); |
| 171 | FRIEND_TEST(ServiceTest, SaveStringEmpty); |
| 172 | |
| 173 | static const char kStorageAutoConnect[]; |
| 174 | static const char kStorageCheckPortal[]; |
| 175 | static const char kStorageEapAnonymousIdentity[]; |
| 176 | static const char kStorageEapCACert[]; |
| 177 | static const char kStorageEapCACertID[]; |
| 178 | static const char kStorageEapCertID[]; |
| 179 | static const char kStorageEapClientCert[]; |
| 180 | static const char kStorageEapEap[]; |
| 181 | static const char kStorageEapIdentity[]; |
| 182 | static const char kStorageEapInnerEap[]; |
| 183 | static const char kStorageEapKeyID[]; |
| 184 | static const char kStorageEapKeyManagement[]; |
| 185 | static const char kStorageEapPIN[]; |
| 186 | static const char kStorageEapPassword[]; |
| 187 | static const char kStorageEapPrivateKey[]; |
| 188 | static const char kStorageEapPrivateKeyPassword[]; |
| 189 | static const char kStorageEapUseSystemCAs[]; |
| 190 | static const char kStorageFavorite[]; |
| 191 | static const char kStorageName[]; |
| 192 | static const char kStoragePriority[]; |
| 193 | static const char kStorageProxyConfig[]; |
| 194 | static const char kStorageSaveCredentials[]; |
| 195 | |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 196 | virtual std::string GetDeviceRpcId() = 0; |
| 197 | |
| 198 | std::string GetProfileRpcId() { |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 199 | return ""; // Will need to call Profile to get this. |
| 200 | } |
| 201 | |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 202 | static unsigned int serial_number_; |
Chris Masone | a82b711 | 2011-05-25 15:16:29 -0700 | [diff] [blame] | 203 | const std::string name_; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 204 | bool available_; |
| 205 | bool configured_; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 206 | Configuration *configuration_; |
| 207 | Connection *connection_; |
Paul Stewart | ba41b99 | 2011-05-26 07:02:46 -0700 | [diff] [blame] | 208 | scoped_ptr<ServiceAdaptorInterface> adaptor_; |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 209 | Manager *manager_; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 210 | |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 211 | DISALLOW_COPY_AND_ASSIGN(Service); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 212 | }; |
| 213 | |
| 214 | } // namespace shill |
| 215 | |
| 216 | #endif // SHILL_SERVICE_ |