blob: a0b3f6d8f2e829b045ef61262efee8f589f7fc47 [file] [log] [blame]
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001// Copyright (c) 2014 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
5#ifndef SHILL_MOBILE_OPERATOR_IMPL_H_
6#define SHILL_MOBILE_OPERATOR_IMPL_H_
7
8#include <string>
9#include <vector>
10
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -070011#include <base/cancelable_callback.h>
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -070012#include <base/file_util.h>
13#include <base/memory/scoped_ptr.h>
14#include <base/memory/scoped_vector.h>
15#include <base/memory/weak_ptr.h>
16#include <base/observer_list.h>
17
18#include "shill/event_dispatcher.h"
19#include "shill/mobile_operator_info.h"
20#include "shill/proto_bindings/mobile_operator_db/mobile_operator_db.pb.h"
21
22namespace shill {
23
24class MobileOperatorInfoImpl {
25 public:
26 typedef
27 std::map<std::string,
28 std::vector<const mobile_operator_db::MobileNetworkOperator *>>
29 StringToMNOListMap;
30
Miao-chen Chou70190b32014-05-14 18:24:30 -070031 MobileOperatorInfoImpl(EventDispatcher *dispatcher,
32 const std::string &info_owner);
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -070033 ~MobileOperatorInfoImpl();
34
35 // API functions of the interface.
36 // See mobile_operator_info_impl.h for details.
37 void ClearDatabasePaths();
38 void AddDatabasePath(const base::FilePath &absolute_path);
39 bool Init();
40 void AddObserver(MobileOperatorInfo::Observer *observer);
41 void RemoveObserver(MobileOperatorInfo::Observer *observer);
42 bool IsMobileNetworkOperatorKnown() const;
43 bool IsMobileVirtualNetworkOperatorKnown() const;
Miao-chen Chou70190b32014-05-14 18:24:30 -070044 const std::string &info_owner() const;
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -070045 const std::string &uuid() const;
46 const std::string &operator_name() const;
47 const std::string &country() const;
48 const std::string &mccmnc() const;
49 const std::string &sid() const;
50 const std::string &nid() const;
51 const std::vector<std::string> &mccmnc_list() const;
52 const std::vector<std::string> &sid_list() const;
53 const std::vector<MobileOperatorInfo::LocalizedName>
54 &operator_name_list() const;
55 const ScopedVector<MobileOperatorInfo::MobileAPN> &apn_list() const;
56 const std::vector<MobileOperatorInfo::OnlinePortal> &olp_list() const;
57 const std::string &activation_code() const;
58 bool requires_roaming() const;
59 void Reset();
60 void UpdateIMSI(const std::string &imsi);
61 void UpdateICCID(const std::string &iccid);
62 void UpdateMCCMNC(const std::string &mccmnc);
63 void UpdateSID(const std::string &sid);
64 void UpdateNID(const std::string &nid);
65 void UpdateOperatorName(const std::string &operator_name);
66 void UpdateOnlinePortal(const std::string &url,
67 const std::string &method,
68 const std::string &post_data);
69
70 private:
71 friend class MobileOperatorInfoInitTest;
72
73 // ///////////////////////////////////////////////////////////////////////////
74 // Static variables.
75 // Default databases to load.
Miao-chen Chou70190b32014-05-14 18:24:30 -070076 static const char *const kDefaultDatabasePaths[];
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -070077 // MCCMNC can be of length 5 or 6. When using this constant, keep in mind that
78 // the lenght of MCCMNC can by |kMCCMNCMinLen| or |kMCCMNCMinLen + 1|.
79 static const int kMCCMNCMinLen;
80
81 // ///////////////////////////////////////////////////////////////////////////
82 // Functions.
83 void PreprocessDatabase();
84 // This function assumes that duplicate |values| are never inserted for the
85 // same |key|. If you do that, the function is too dumb to deduplicate the
86 // |value|s, and two copies will get stored.
87 void InsertIntoStringToMNOListMap(
88 StringToMNOListMap &table,
89 const std::string &key,
90 const mobile_operator_db::MobileNetworkOperator *value);
91
92 bool UpdateMNO();
93 bool UpdateMVNO();
Prathmesh Prabhu0db0bf02014-05-06 15:17:44 -070094 bool FilterMatches(const shill::mobile_operator_db::Filter &filter);
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -070095 // Reloads the information about M[V]NO from the database.
96 void RefreshDBInformation();
97 void ClearDBInformation();
98 // Reload all data from |data|.
99 // Semantics: If a field data.x exists, then it *overwrites* the current
100 // information gained from data.x. E.g., if |data.name_size() > 0| is true,
101 // then we replace *all* names. Otherwise, we leave names untouched.
102 // This allows MVNOs to overwrite information obtained from the corresponding
103 // MNO.
104 void ReloadData(const mobile_operator_db::Data &data);
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700105 // Append candidates recognized by |mccmnc| to the candidate list.
106 bool AppendToCandidatesByMCCMNC(const std::string &mccmnc);
Prathmesh Prabhuc1175bf2014-04-15 16:58:05 -0700107 bool AppendToCandidatesBySID(const std::string &sid);
108 std::string OperatorCodeString() const;
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700109
110 // Notifies all observers that the operator has changed.
111 void PostNotifyOperatorChanged();
112 // The actual notification is sent out here. This should not be called
113 // directly from any function.
114 void NotifyOperatorChanged();
115
116 // For a property update that does not result in an M[V]NO update, this
117 // function determines whether observers should be notified anyway.
118 bool ShouldNotifyPropertyUpdate() const;
119
120 // These functions encapsulate the logic to update different properties
121 // properly whenever an update is either received from the user or the
122 // database.
123 void HandleMCCMNCUpdate();
124 void HandleOperatorNameUpdate();
125 void HandleSIDUpdate();
126 void HandleOnlinePortalUpdate();
127
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700128 // Accessor functions for testing purpose only.
129 mobile_operator_db::MobileOperatorDB *database() {
130 return database_.get();
131 }
132
133 // ///////////////////////////////////////////////////////////////////////////
134 // Data.
135 // Not owned by MobileOperatorInfoImpl.
136 EventDispatcher *const dispatcher_;
137
Miao-chen Chou70190b32014-05-14 18:24:30 -0700138 const std::string info_owner_;
139
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700140 // Owned by MobileOperatorInfoImpl, may be created externally.
141 std::vector<base::FilePath> database_paths_;
142
143 // Owned and modified only by MobileOperatorInfoImpl.
144 // The observers added to this list are not owned by this object. Moreover,
145 // the observer is likely to outlive this object. We do enforce removal of all
146 // observers before this object is destroyed.
147 ObserverList<MobileOperatorInfo::Observer> observers_;
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700148 base::CancelableClosure notify_operator_changed_task_;
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700149
150 scoped_ptr<mobile_operator_db::MobileOperatorDB> database_;
151 StringToMNOListMap mccmnc_to_mnos_;
Prathmesh Prabhuc1175bf2014-04-15 16:58:05 -0700152 StringToMNOListMap sid_to_mnos_;
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700153 StringToMNOListMap name_to_mnos_;
Prathmesh Prabhuc1175bf2014-04-15 16:58:05 -0700154
155 // |candidates_by_operator_code| can be determined either using MCCMNC or
156 // using SID. At any one time, we only expect one of these operator codes to
157 // be updated by the user. We use |operator_code_type_| to keep track of which
158 // update we have received and warn the user if we receive both.
159 enum OperatorCodeType {
160 kOperatorCodeTypeUnknown = 0,
161 kOperatorCodeTypeMCCMNC,
162 kOperatorCodeTypeSID,
163 };
164 OperatorCodeType operator_code_type_;
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700165 std::vector<const mobile_operator_db::MobileNetworkOperator *>
Prathmesh Prabhuc1175bf2014-04-15 16:58:05 -0700166 candidates_by_operator_code_;
167
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700168 std::vector<const mobile_operator_db::MobileNetworkOperator *>
169 candidates_by_name_;
170 const mobile_operator_db::MobileNetworkOperator *current_mno_;
171 const mobile_operator_db::MobileVirtualNetworkOperator *current_mvno_;
172
173 // These fields are the information expected to be populated by this object
174 // after successfully determining the MVNO.
175 std::string uuid_;
176 std::string operator_name_;
177 std::string country_;
178 std::string mccmnc_;
179 std::string sid_;
180 std::string nid_;
181 std::vector<std::string> mccmnc_list_;
182 std::vector<std::string> sid_list_;
183 std::vector<MobileOperatorInfo::LocalizedName> operator_name_list_;
184 ScopedVector<MobileOperatorInfo::MobileAPN> apn_list_;
185 std::vector<MobileOperatorInfo::OnlinePortal> olp_list_;
Prathmesh Prabhu0db0bf02014-05-06 15:17:44 -0700186 std::vector<mobile_operator_db::OnlinePortal> raw_olp_list_;
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700187 std::string activation_code_;
188 bool requires_roaming_;
189 // These fields store the data obtained from the Update* methods.
190 // The database information is kept separate from the information gathered
191 // through the Update* methods, because one or the other may be given
192 // precedence in different situations.
193 // Note: For simplicity, we do not allow the user to enforce an empty value
194 // for these variables. So, if |user_mccmnc_| == "", the |mccmnc_| obtained
195 // from the database will be used, even if |user_mccmnc_| was explicitly set
196 // by the user.
197 std::string user_imsi_;
198 std::string user_iccid_;
199 std::string user_mccmnc_;
200 std::string user_sid_;
201 std::string user_nid_;
202 std::string user_operator_name_;
203 bool user_olp_empty_;
204 MobileOperatorInfo::OnlinePortal user_olp_;
205
206 // This must be the last data member of this class.
207 base::WeakPtrFactory<MobileOperatorInfoImpl> weak_ptr_factory_;
208
209 DISALLOW_COPY_AND_ASSIGN(MobileOperatorInfoImpl);
210};
211
212} // namespace shill
213
214#endif // SHILL_MOBILE_OPERATOR_IMPL_H_