Arman Uguray | f4c6181 | 2013-01-10 18:58:39 -0800 | [diff] [blame] | 1 | // Copyright (c) 2013 The Chromium OS Authors. All rights reserved. |
Ben Chan | b39cb31 | 2012-11-01 22:55:25 -0700 | [diff] [blame] | 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 SHILL_CELLULAR_OPERATOR_INFO_H_ |
| 6 | #define SHILL_CELLULAR_OPERATOR_INFO_H_ |
| 7 | |
Arman Uguray | f4c6181 | 2013-01-10 18:58:39 -0800 | [diff] [blame] | 8 | #include <map> |
Ben Chan | b39cb31 | 2012-11-01 22:55:25 -0700 | [diff] [blame] | 9 | #include <string> |
Arman Uguray | f4c6181 | 2013-01-10 18:58:39 -0800 | [diff] [blame] | 10 | #include <vector> |
Ben Chan | b39cb31 | 2012-11-01 22:55:25 -0700 | [diff] [blame] | 11 | |
| 12 | #include <base/basictypes.h> |
| 13 | #include <base/file_path.h> |
Arman Uguray | f4c6181 | 2013-01-10 18:58:39 -0800 | [diff] [blame] | 14 | #include <base/memory/scoped_vector.h> |
| 15 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
Ben Chan | b39cb31 | 2012-11-01 22:55:25 -0700 | [diff] [blame] | 16 | |
| 17 | #include "shill/cellular_service.h" |
Arman Uguray | f4c6181 | 2013-01-10 18:58:39 -0800 | [diff] [blame] | 18 | #include "shill/file_reader.h" |
Ben Chan | b39cb31 | 2012-11-01 22:55:25 -0700 | [diff] [blame] | 19 | #include "shill/key_file_store.h" |
| 20 | |
| 21 | namespace shill { |
| 22 | |
Ben Chan | b39cb31 | 2012-11-01 22:55:25 -0700 | [diff] [blame] | 23 | class CellularOperatorInfo { |
| 24 | public: |
Arman Uguray | f4c6181 | 2013-01-10 18:58:39 -0800 | [diff] [blame] | 25 | CellularOperatorInfo(); |
Ben Chan | 1578603 | 2012-11-04 21:28:02 -0800 | [diff] [blame] | 26 | virtual ~CellularOperatorInfo(); |
Ben Chan | b39cb31 | 2012-11-01 22:55:25 -0700 | [diff] [blame] | 27 | |
Arman Uguray | f4c6181 | 2013-01-10 18:58:39 -0800 | [diff] [blame] | 28 | struct LocalizedName { |
| 29 | std::string name; |
| 30 | std::string language; |
| 31 | }; |
| 32 | |
| 33 | class MobileAPN { |
| 34 | public: |
| 35 | MobileAPN(); |
| 36 | ~MobileAPN(); |
| 37 | |
| 38 | const std::string &apn() const { return apn_; } |
| 39 | const std::vector<LocalizedName> &name_list() const { return name_list_; } |
| 40 | const std::string &username() const { return username_; } |
| 41 | const std::string &password() const { return password_; } |
| 42 | |
| 43 | private: |
| 44 | friend class CellularOperatorInfo; |
| 45 | |
| 46 | std::string apn_; |
| 47 | std::vector<LocalizedName> name_list_; |
| 48 | std::string username_; |
| 49 | std::string password_; |
| 50 | |
| 51 | DISALLOW_COPY_AND_ASSIGN(MobileAPN); |
| 52 | }; |
| 53 | |
| 54 | class CellularOperator { |
| 55 | public: |
| 56 | CellularOperator(); |
| 57 | virtual ~CellularOperator(); |
| 58 | |
| 59 | const std::string &country() const { return country_; } |
Arman Uguray | c953357 | 2013-01-22 17:34:20 -0800 | [diff] [blame] | 60 | const std::string &identifier() const { return identifier_; } |
Arman Uguray | f4c6181 | 2013-01-10 18:58:39 -0800 | [diff] [blame] | 61 | const std::vector<std::string> &mccmnc_list() const { |
| 62 | return mccmnc_list_; |
| 63 | } |
| 64 | const std::vector<std::string> &sid_list() const { return sid_list_; } |
| 65 | const std::vector<LocalizedName> &name_list() const { return name_list_; } |
| 66 | const ScopedVector<MobileAPN> &apn_list() const { return apn_list_; } |
| 67 | const ScopedVector<CellularService::OLP> &olp_list() const { |
| 68 | return olp_list_; |
| 69 | } |
| 70 | bool is_primary() const { return is_primary_; } |
| 71 | bool requires_roaming() const { return requires_roaming_; } |
| 72 | |
| 73 | private: |
Arman Uguray | c953357 | 2013-01-22 17:34:20 -0800 | [diff] [blame] | 74 | friend class CellularCapabilityUniversalTest; |
Arman Uguray | f4c6181 | 2013-01-10 18:58:39 -0800 | [diff] [blame] | 75 | friend class CellularOperatorInfo; |
| 76 | friend class CellularOperatorInfoTest; |
Arman Uguray | c953357 | 2013-01-22 17:34:20 -0800 | [diff] [blame] | 77 | FRIEND_TEST(CellularCapabilityUniversalTest, UpdateStorageIdentifier); |
Arman Uguray | f4c6181 | 2013-01-10 18:58:39 -0800 | [diff] [blame] | 78 | FRIEND_TEST(CellularOperatorInfoTest, HandleMCCMNC); |
| 79 | FRIEND_TEST(CellularOperatorInfoTest, HandleSID); |
| 80 | |
| 81 | std::string country_; |
Arman Uguray | c953357 | 2013-01-22 17:34:20 -0800 | [diff] [blame] | 82 | std::string identifier_; |
Arman Uguray | f4c6181 | 2013-01-10 18:58:39 -0800 | [diff] [blame] | 83 | std::vector<std::string> mccmnc_list_; |
| 84 | std::vector<std::string> sid_list_; |
| 85 | std::vector<LocalizedName> name_list_; |
| 86 | ScopedVector<MobileAPN> apn_list_; |
| 87 | ScopedVector<CellularService::OLP> olp_list_; |
| 88 | std::map<std::string, uint32> mccmnc_to_olp_idx_; |
| 89 | std::map<std::string, uint32> sid_to_olp_idx_; |
| 90 | bool is_primary_; |
| 91 | bool requires_roaming_; |
| 92 | |
| 93 | DISALLOW_COPY_AND_ASSIGN(CellularOperator); |
| 94 | }; |
| 95 | |
Ben Chan | b39cb31 | 2012-11-01 22:55:25 -0700 | [diff] [blame] | 96 | // Loads the operator info from |info_file_path|. Returns true on success. |
Ben Chan | 1578603 | 2012-11-04 21:28:02 -0800 | [diff] [blame] | 97 | virtual bool Load(const FilePath &info_file_path); |
Ben Chan | b39cb31 | 2012-11-01 22:55:25 -0700 | [diff] [blame] | 98 | |
Arman Uguray | f4c6181 | 2013-01-10 18:58:39 -0800 | [diff] [blame] | 99 | // Gets the cellular operator info of the operator with MCCMNC |mccmnc|. |
| 100 | // If found, returns a pointer to the matching operator. |
| 101 | virtual const CellularOperator * |
| 102 | GetCellularOperatorByMCCMNC(const std::string &mccmnc); |
| 103 | |
| 104 | // Gets the cellular operator info of the operator with SID |sid|. |
| 105 | // If found, returns a pointer to the matching operator. |
| 106 | virtual const CellularOperator * |
| 107 | GetCellularOperatorBySID(const std::string &sid); |
| 108 | |
| 109 | // Gets the cellular operator info of the operators that match the name |
| 110 | // |name|, such that each element contains information about the operator |
| 111 | // in different countries. The given name must be the first enumerated name |
| 112 | // for the operator in the operator database. |
| 113 | // If found, returns a pointer to a vector containing the matching operators. |
| 114 | virtual const std::vector<const CellularOperator *> * |
| 115 | GetCellularOperatorsByName(const std::string &name); |
| 116 | |
| 117 | // Gets the online payment portal info of the operator with MCCMNC |mccmnc|. |
| 118 | // If found, returns a pointer to the matching OLP. |
| 119 | virtual const CellularService::OLP *GetOLPByMCCMNC(const std::string &mccmnc); |
| 120 | |
| 121 | // Gets the online payment portal info of the operator with SID |sid|. |
| 122 | // If found, returns a pointer to the matching OLP. |
| 123 | virtual const CellularService::OLP *GetOLPBySID(const std::string &sid); |
| 124 | |
| 125 | // Returns a list of all operators. |
| 126 | virtual const ScopedVector<CellularOperator> &operators() const; |
Ben Chan | b39cb31 | 2012-11-01 22:55:25 -0700 | [diff] [blame] | 127 | |
| 128 | private: |
Arman Uguray | f4c6181 | 2013-01-10 18:58:39 -0800 | [diff] [blame] | 129 | friend class CellularOperatorInfoTest; |
| 130 | FRIEND_TEST(CellularOperatorInfoTest, HandleAPN); |
| 131 | FRIEND_TEST(CellularOperatorInfoTest, HandleAPNName); |
| 132 | FRIEND_TEST(CellularOperatorInfoTest, HandleMCCMNC); |
| 133 | FRIEND_TEST(CellularOperatorInfoTest, HandleName); |
| 134 | FRIEND_TEST(CellularOperatorInfoTest, HandleOLP); |
| 135 | FRIEND_TEST(CellularOperatorInfoTest, HandleProvider); |
| 136 | FRIEND_TEST(CellularOperatorInfoTest, HandleSID); |
Arman Uguray | c953357 | 2013-01-22 17:34:20 -0800 | [diff] [blame] | 137 | FRIEND_TEST(CellularOperatorInfoTest, HandleIdentifier); |
Arman Uguray | f4c6181 | 2013-01-10 18:58:39 -0800 | [diff] [blame] | 138 | FRIEND_TEST(CellularOperatorInfoTest, ParseKeyValue); |
| 139 | FRIEND_TEST(CellularOperatorInfoTest, ParseNameLine); |
| 140 | FRIEND_TEST(CellularOperatorInfoTest, ParseSuccess); |
| 141 | |
| 142 | struct ParserState { |
| 143 | ParserState() : provider(NULL), |
| 144 | apn(NULL), |
| 145 | parsing_apn(false) {} |
| 146 | |
| 147 | std::string country; |
| 148 | CellularOperator *provider; |
| 149 | MobileAPN *apn; |
| 150 | |
| 151 | bool parsing_apn; |
| 152 | }; |
| 153 | |
| 154 | bool HandleProvider(ParserState *state, |
| 155 | const std::string &value); |
| 156 | bool HandleMCCMNC(ParserState *state, |
| 157 | const std::string &value); |
| 158 | bool HandleNameKey(ParserState *state, |
| 159 | const std::string &value); |
| 160 | bool HandleName(ParserState *state, |
| 161 | const std::string &value); |
| 162 | bool HandleAPN(ParserState *state, |
| 163 | const std::string &value); |
| 164 | bool HandleAPNName(ParserState *state, |
| 165 | const std::string &value); |
| 166 | bool HandleSID(ParserState *state, |
| 167 | const std::string &value); |
| 168 | bool HandleOLP(ParserState *state, |
| 169 | const std::string &value); |
| 170 | bool HandleCountry(ParserState *state, |
| 171 | const std::string &value); |
Arman Uguray | c953357 | 2013-01-22 17:34:20 -0800 | [diff] [blame] | 172 | bool HandleIdentifier(ParserState *state, |
| 173 | const std::string &value); |
Arman Uguray | f4c6181 | 2013-01-10 18:58:39 -0800 | [diff] [blame] | 174 | bool ParseNameLine(const std::string &value, |
| 175 | LocalizedName *name); |
| 176 | |
| 177 | void ClearOperators(); |
| 178 | |
| 179 | // Functions that return specially formatted strings for logging |
| 180 | // MCCMNC and SID to help with scrubbing. |
| 181 | static std::string FormattedMCCMNC(const std::string &mccmnc); |
| 182 | static std::string FormattedSID(const std::string &sid); |
| 183 | |
| 184 | // Splits |line| into two strings, separated with the |key_value_delimiter|. |
| 185 | // Returns |false| if line does not contain |key_value_delimiter|, otherwise |
| 186 | // returns the first substring in |key| and the second substring in |value|, |
| 187 | // and returns true. |
| 188 | static bool ParseKeyValue(const std::string &line, |
| 189 | char key_value_delimiter, |
| 190 | std::string *key, |
| 191 | std::string *value); |
| 192 | |
| 193 | ScopedVector<CellularOperator> operators_; |
| 194 | std::map<std::string, CellularOperator *> mccmnc_to_operator_; |
| 195 | std::map<std::string, CellularOperator *> sid_to_operator_; |
| 196 | std::map<std::string, |
| 197 | std::vector<const CellularOperator *> > name_to_operators_; |
Ben Chan | b39cb31 | 2012-11-01 22:55:25 -0700 | [diff] [blame] | 198 | |
| 199 | DISALLOW_COPY_AND_ASSIGN(CellularOperatorInfo); |
| 200 | }; |
| 201 | |
| 202 | } // namespace shill |
| 203 | |
| 204 | #endif // SHILL_CELLULAR_OPERATOR_INFO_H_ |