blob: 21bd6cf1a874c70e698934ae632b651046227596 [file] [log] [blame]
Arman Ugurayf4c61812013-01-10 18:58:39 -08001// Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
Ben Chanb39cb312012-11-01 22:55:25 -07002// 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 Ugurayf4c61812013-01-10 18:58:39 -08008#include <map>
Ben Chanb39cb312012-11-01 22:55:25 -07009#include <string>
Arman Ugurayf4c61812013-01-10 18:58:39 -080010#include <vector>
Ben Chanb39cb312012-11-01 22:55:25 -070011
12#include <base/basictypes.h>
13#include <base/file_path.h>
Arman Ugurayf4c61812013-01-10 18:58:39 -080014#include <base/memory/scoped_vector.h>
15#include <gtest/gtest_prod.h> // for FRIEND_TEST
Ben Chanb39cb312012-11-01 22:55:25 -070016
17#include "shill/cellular_service.h"
Arman Ugurayf4c61812013-01-10 18:58:39 -080018#include "shill/file_reader.h"
Ben Chanb39cb312012-11-01 22:55:25 -070019#include "shill/key_file_store.h"
20
21namespace shill {
22
Ben Chanb39cb312012-11-01 22:55:25 -070023class CellularOperatorInfo {
24 public:
Arman Ugurayf4c61812013-01-10 18:58:39 -080025 CellularOperatorInfo();
Ben Chan15786032012-11-04 21:28:02 -080026 virtual ~CellularOperatorInfo();
Ben Chanb39cb312012-11-01 22:55:25 -070027
Arman Ugurayf4c61812013-01-10 18:58:39 -080028 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 Ugurayc9533572013-01-22 17:34:20 -080060 const std::string &identifier() const { return identifier_; }
Arman Ugurayf4c61812013-01-10 18:58:39 -080061 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 Ugurayc9533572013-01-22 17:34:20 -080074 friend class CellularCapabilityUniversalTest;
Arman Ugurayf4c61812013-01-10 18:58:39 -080075 friend class CellularOperatorInfo;
76 friend class CellularOperatorInfoTest;
Arman Uguray1361c032013-02-11 17:53:39 -080077 FRIEND_TEST(CellularCapabilityUniversalMainTest, UpdateStorageIdentifier);
Arman Ugurayf4c61812013-01-10 18:58:39 -080078 FRIEND_TEST(CellularOperatorInfoTest, HandleMCCMNC);
79 FRIEND_TEST(CellularOperatorInfoTest, HandleSID);
80
81 std::string country_;
Arman Ugurayc9533572013-01-22 17:34:20 -080082 std::string identifier_;
Arman Ugurayf4c61812013-01-10 18:58:39 -080083 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 Chanb39cb312012-11-01 22:55:25 -070096 // Loads the operator info from |info_file_path|. Returns true on success.
Ben Chan15786032012-11-04 21:28:02 -080097 virtual bool Load(const FilePath &info_file_path);
Ben Chanb39cb312012-11-01 22:55:25 -070098
Arman Ugurayf4c61812013-01-10 18:58:39 -080099 // 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 Chanb39cb312012-11-01 22:55:25 -0700127
128 private:
Arman Ugurayf4c61812013-01-10 18:58:39 -0800129 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 Ugurayc9533572013-01-22 17:34:20 -0800137 FRIEND_TEST(CellularOperatorInfoTest, HandleIdentifier);
Arman Ugurayf4c61812013-01-10 18:58:39 -0800138 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 Ugurayc9533572013-01-22 17:34:20 -0800172 bool HandleIdentifier(ParserState *state,
173 const std::string &value);
Arman Ugurayf4c61812013-01-10 18:58:39 -0800174 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 Chanb39cb312012-11-01 22:55:25 -0700198
199 DISALLOW_COPY_AND_ASSIGN(CellularOperatorInfo);
200};
201
202} // namespace shill
203
204#endif // SHILL_CELLULAR_OPERATOR_INFO_H_