OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/mobile_config.h" | 5 #include "chrome/browser/chromeos/mobile_config.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 // Location of the local carrier config. | 56 // Location of the local carrier config. |
57 const char kLocalCarrierConfigPath[] = | 57 const char kLocalCarrierConfigPath[] = |
58 "/opt/oem/etc/carrier_config.json"; | 58 "/opt/oem/etc/carrier_config.json"; |
59 | 59 |
60 } // anonymous namespace | 60 } // anonymous namespace |
61 | 61 |
62 namespace chromeos { | 62 namespace chromeos { |
63 | 63 |
64 // MobileConfig::CarrierDeal implementation. ----------------------------------- | 64 // MobileConfig::CarrierDeal implementation. ----------------------------------- |
65 | 65 |
66 MobileConfig::CarrierDeal::CarrierDeal(const DictionaryValue* deal_dict) | 66 MobileConfig::CarrierDeal::CarrierDeal(const base::DictionaryValue* deal_dict) |
67 : notification_count_(0), | 67 : notification_count_(0), |
68 localized_strings_(NULL) { | 68 localized_strings_(NULL) { |
69 deal_dict->GetString(kDealIdAttr, &deal_id_); | 69 deal_dict->GetString(kDealIdAttr, &deal_id_); |
70 | 70 |
71 // Extract list of deal locales. | 71 // Extract list of deal locales. |
72 const ListValue* locale_list = NULL; | 72 const base::ListValue* locale_list = NULL; |
73 if (deal_dict->GetList(kDealLocalesAttr, &locale_list)) { | 73 if (deal_dict->GetList(kDealLocalesAttr, &locale_list)) { |
74 for (size_t i = 0; i < locale_list->GetSize(); ++i) { | 74 for (size_t i = 0; i < locale_list->GetSize(); ++i) { |
75 std::string locale; | 75 std::string locale; |
76 if (locale_list->GetString(i, &locale)) | 76 if (locale_list->GetString(i, &locale)) |
77 locales_.push_back(locale); | 77 locales_.push_back(locale); |
78 } | 78 } |
79 } | 79 } |
80 | 80 |
81 deal_dict->GetString(kInfoURLAttr, &info_url_); | 81 deal_dict->GetString(kInfoURLAttr, &info_url_); |
82 deal_dict->GetInteger(kNotificationCountAttr, ¬ification_count_); | 82 deal_dict->GetInteger(kNotificationCountAttr, ¬ification_count_); |
83 std::string date_string; | 83 std::string date_string; |
84 if (deal_dict->GetString(kDealExpireDateAttr, &date_string)) { | 84 if (deal_dict->GetString(kDealExpireDateAttr, &date_string)) { |
85 if (!base::Time::FromString(date_string.c_str(), &expire_date_)) | 85 if (!base::Time::FromString(date_string.c_str(), &expire_date_)) |
86 LOG(ERROR) << "Error parsing deal_expire_date: " << date_string; | 86 LOG(ERROR) << "Error parsing deal_expire_date: " << date_string; |
87 } | 87 } |
88 deal_dict->GetDictionary(kLocalizedContentAttr, &localized_strings_); | 88 deal_dict->GetDictionary(kLocalizedContentAttr, &localized_strings_); |
89 } | 89 } |
90 | 90 |
91 MobileConfig::CarrierDeal::~CarrierDeal() { | 91 MobileConfig::CarrierDeal::~CarrierDeal() { |
92 } | 92 } |
93 | 93 |
94 std::string MobileConfig::CarrierDeal::GetLocalizedString( | 94 std::string MobileConfig::CarrierDeal::GetLocalizedString( |
95 const std::string& locale, const std::string& id) const { | 95 const std::string& locale, const std::string& id) const { |
96 std::string result; | 96 std::string result; |
97 if (localized_strings_) { | 97 if (localized_strings_) { |
98 const DictionaryValue* locale_dict = NULL; | 98 const base::DictionaryValue* locale_dict = NULL; |
99 if (localized_strings_->GetDictionary(locale, &locale_dict) && | 99 if (localized_strings_->GetDictionary(locale, &locale_dict) && |
100 locale_dict->GetString(id, &result)) { | 100 locale_dict->GetString(id, &result)) { |
101 return result; | 101 return result; |
102 } else if (localized_strings_->GetDictionary(kDefaultAttr, &locale_dict) && | 102 } else if (localized_strings_->GetDictionary(kDefaultAttr, &locale_dict) && |
103 locale_dict->GetString(id, &result)) { | 103 locale_dict->GetString(id, &result)) { |
104 return result; | 104 return result; |
105 } | 105 } |
106 } | 106 } |
107 return result; | 107 return result; |
108 } | 108 } |
109 | 109 |
110 // MobileConfig::Carrier implementation. --------------------------------------- | 110 // MobileConfig::Carrier implementation. --------------------------------------- |
111 | 111 |
112 MobileConfig::Carrier::Carrier(const DictionaryValue* carrier_dict, | 112 MobileConfig::Carrier::Carrier(const base::DictionaryValue* carrier_dict, |
113 const std::string& initial_locale) | 113 const std::string& initial_locale) |
114 : show_portal_button_(false) { | 114 : show_portal_button_(false) { |
115 InitFromDictionary(carrier_dict, initial_locale); | 115 InitFromDictionary(carrier_dict, initial_locale); |
116 } | 116 } |
117 | 117 |
118 MobileConfig::Carrier::~Carrier() { | 118 MobileConfig::Carrier::~Carrier() { |
119 RemoveDeals(); | 119 RemoveDeals(); |
120 } | 120 } |
121 | 121 |
122 const MobileConfig::CarrierDeal* MobileConfig::Carrier::GetDefaultDeal() const { | 122 const MobileConfig::CarrierDeal* MobileConfig::Carrier::GetDefaultDeal() const { |
(...skipping 28 matching lines...) Expand all Loading... |
151 carrier_dict->GetString(kTopUpURLAttr, &top_up_url_); | 151 carrier_dict->GetString(kTopUpURLAttr, &top_up_url_); |
152 carrier_dict->GetBoolean(kShowPortalButtonAttr, &show_portal_button_); | 152 carrier_dict->GetBoolean(kShowPortalButtonAttr, &show_portal_button_); |
153 | 153 |
154 bool exclude_deals = false; | 154 bool exclude_deals = false; |
155 if (carrier_dict->GetBoolean(kExcludeDealsAttr, &exclude_deals) && | 155 if (carrier_dict->GetBoolean(kExcludeDealsAttr, &exclude_deals) && |
156 exclude_deals) { | 156 exclude_deals) { |
157 RemoveDeals(); | 157 RemoveDeals(); |
158 } | 158 } |
159 | 159 |
160 // Extract list of external IDs for this carrier. | 160 // Extract list of external IDs for this carrier. |
161 const ListValue* id_list = NULL; | 161 const base::ListValue* id_list = NULL; |
162 if (carrier_dict->GetList(kCarrierIdsAttr, &id_list)) { | 162 if (carrier_dict->GetList(kCarrierIdsAttr, &id_list)) { |
163 for (size_t i = 0; i < id_list->GetSize(); ++i) { | 163 for (size_t i = 0; i < id_list->GetSize(); ++i) { |
164 const DictionaryValue* id_dict = NULL; | 164 const base::DictionaryValue* id_dict = NULL; |
165 std::string external_id; | 165 std::string external_id; |
166 if (id_list->GetDictionary(i, &id_dict) && | 166 if (id_list->GetDictionary(i, &id_dict) && |
167 id_dict->GetString(kCarrierIdAttr, &external_id)) { | 167 id_dict->GetString(kCarrierIdAttr, &external_id)) { |
168 external_ids_.push_back(external_id); | 168 external_ids_.push_back(external_id); |
169 } | 169 } |
170 } | 170 } |
171 } | 171 } |
172 | 172 |
173 // Extract list of deals for this carrier. | 173 // Extract list of deals for this carrier. |
174 const ListValue* deals_list = NULL; | 174 const base::ListValue* deals_list = NULL; |
175 if (carrier_dict->GetList(kDealsAttr, &deals_list)) { | 175 if (carrier_dict->GetList(kDealsAttr, &deals_list)) { |
176 for (size_t i = 0; i < deals_list->GetSize(); ++i) { | 176 for (size_t i = 0; i < deals_list->GetSize(); ++i) { |
177 const DictionaryValue* deal_dict = NULL; | 177 const base::DictionaryValue* deal_dict = NULL; |
178 if (deals_list->GetDictionary(i, &deal_dict)) { | 178 if (deals_list->GetDictionary(i, &deal_dict)) { |
179 scoped_ptr<CarrierDeal> deal(new CarrierDeal(deal_dict)); | 179 scoped_ptr<CarrierDeal> deal(new CarrierDeal(deal_dict)); |
180 // Filter out deals by initial_locale right away. | 180 // Filter out deals by initial_locale right away. |
181 std::vector<std::string>::const_iterator iter = | 181 std::vector<std::string>::const_iterator iter = |
182 std::find(deal->locales().begin(), | 182 std::find(deal->locales().begin(), |
183 deal->locales().end(), | 183 deal->locales().end(), |
184 initial_locale); | 184 initial_locale); |
185 if (iter != deal->locales().end()) { | 185 if (iter != deal->locales().end()) { |
186 const std::string& deal_id = deal->deal_id(); | 186 const std::string& deal_id = deal->deal_id(); |
187 deals_[deal_id] = deal.release(); | 187 deals_[deal_id] = deal.release(); |
188 } | 188 } |
189 } | 189 } |
190 } | 190 } |
191 } | 191 } |
192 } | 192 } |
193 | 193 |
194 void MobileConfig::Carrier::RemoveDeals() { | 194 void MobileConfig::Carrier::RemoveDeals() { |
195 STLDeleteValues(&deals_); | 195 STLDeleteValues(&deals_); |
196 } | 196 } |
197 | 197 |
198 // MobileConfig::LocaleConfig implementation. ---------------------------------- | 198 // MobileConfig::LocaleConfig implementation. ---------------------------------- |
199 | 199 |
200 MobileConfig::LocaleConfig::LocaleConfig(DictionaryValue* locale_dict) { | 200 MobileConfig::LocaleConfig::LocaleConfig(base::DictionaryValue* locale_dict) { |
201 InitFromDictionary(locale_dict); | 201 InitFromDictionary(locale_dict); |
202 } | 202 } |
203 | 203 |
204 MobileConfig::LocaleConfig::~LocaleConfig() { | 204 MobileConfig::LocaleConfig::~LocaleConfig() { |
205 } | 205 } |
206 | 206 |
207 void MobileConfig::LocaleConfig::InitFromDictionary( | 207 void MobileConfig::LocaleConfig::InitFromDictionary( |
208 base::DictionaryValue* locale_dict) { | 208 base::DictionaryValue* locale_dict) { |
209 locale_dict->GetString(kSetupURLAttr, &setup_url_); | 209 locale_dict->GetString(kSetupURLAttr, &setup_url_); |
210 } | 210 } |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 if (root_.get() && | 247 if (root_.get() && |
248 root_->GetBoolean(kExcludeDealsAttr, &exclude_deals) && | 248 root_->GetBoolean(kExcludeDealsAttr, &exclude_deals) && |
249 exclude_deals) { | 249 exclude_deals) { |
250 for (Carriers::iterator iter = carriers_.begin(); | 250 for (Carriers::iterator iter = carriers_.begin(); |
251 iter != carriers_.end(); ++iter) { | 251 iter != carriers_.end(); ++iter) { |
252 iter->second->RemoveDeals(); | 252 iter->second->RemoveDeals(); |
253 } | 253 } |
254 } | 254 } |
255 | 255 |
256 // Other parts are optional and are the same among global/local config. | 256 // Other parts are optional and are the same among global/local config. |
257 DictionaryValue* carriers = NULL; | 257 base::DictionaryValue* carriers = NULL; |
258 if (root_.get() && root_->GetDictionary(kCarriersAttr, &carriers)) { | 258 if (root_.get() && root_->GetDictionary(kCarriersAttr, &carriers)) { |
259 for (DictionaryValue::Iterator iter(*carriers); !iter.IsAtEnd(); | 259 for (base::DictionaryValue::Iterator iter(*carriers); !iter.IsAtEnd(); |
260 iter.Advance()) { | 260 iter.Advance()) { |
261 const DictionaryValue* carrier_dict = NULL; | 261 const base::DictionaryValue* carrier_dict = NULL; |
262 if (iter.value().GetAsDictionary(&carrier_dict)) { | 262 if (iter.value().GetAsDictionary(&carrier_dict)) { |
263 const std::string& internal_id = iter.key(); | 263 const std::string& internal_id = iter.key(); |
264 Carriers::iterator inner_iter = carriers_.find(internal_id); | 264 Carriers::iterator inner_iter = carriers_.find(internal_id); |
265 if (inner_iter != carriers_.end()) { | 265 if (inner_iter != carriers_.end()) { |
266 // Carrier already defined i.e. loading from the local config. | 266 // Carrier already defined i.e. loading from the local config. |
267 // New ID mappings in local config is not supported. | 267 // New ID mappings in local config is not supported. |
268 inner_iter->second->InitFromDictionary(carrier_dict, initial_locale_); | 268 inner_iter->second->InitFromDictionary(carrier_dict, initial_locale_); |
269 } else { | 269 } else { |
270 Carrier* carrier = new Carrier(carrier_dict, initial_locale_); | 270 Carrier* carrier = new Carrier(carrier_dict, initial_locale_); |
271 if (!carrier->external_ids().empty()) { | 271 if (!carrier->external_ids().empty()) { |
272 // Map all external IDs to a single internal one. | 272 // Map all external IDs to a single internal one. |
273 for (std::vector<std::string>::const_iterator | 273 for (std::vector<std::string>::const_iterator |
274 i = carrier->external_ids().begin(); | 274 i = carrier->external_ids().begin(); |
275 i != carrier->external_ids().end(); ++i) { | 275 i != carrier->external_ids().end(); ++i) { |
276 carrier_id_map_[*i] = internal_id; | 276 carrier_id_map_[*i] = internal_id; |
277 } | 277 } |
278 } else { | 278 } else { |
279 // Trivial case - using same ID for external/internal one. | 279 // Trivial case - using same ID for external/internal one. |
280 carrier_id_map_[internal_id] = internal_id; | 280 carrier_id_map_[internal_id] = internal_id; |
281 } | 281 } |
282 carriers_[internal_id] = carrier; | 282 carriers_[internal_id] = carrier; |
283 } | 283 } |
284 } | 284 } |
285 } | 285 } |
286 } | 286 } |
287 | 287 |
288 DictionaryValue* initial_locales = NULL; | 288 base::DictionaryValue* initial_locales = NULL; |
289 if (root_.get() && root_->GetDictionary(kInitialLocalesAttr, | 289 if (root_.get() && root_->GetDictionary(kInitialLocalesAttr, |
290 &initial_locales)) { | 290 &initial_locales)) { |
291 DictionaryValue* locale_config_dict = NULL; | 291 base::DictionaryValue* locale_config_dict = NULL; |
292 // Search for a config based on current initial locale. | 292 // Search for a config based on current initial locale. |
293 if (initial_locales->GetDictionary(initial_locale_, | 293 if (initial_locales->GetDictionary(initial_locale_, |
294 &locale_config_dict)) { | 294 &locale_config_dict)) { |
295 locale_config_.reset(new LocaleConfig(locale_config_dict)); | 295 locale_config_.reset(new LocaleConfig(locale_config_dict)); |
296 } | 296 } |
297 } | 297 } |
298 | 298 |
299 return true; | 299 return true; |
300 } | 300 } |
301 | 301 |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
367 << local_config_file.value(); | 367 << local_config_file.value(); |
368 } | 368 } |
369 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 369 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
370 base::Bind(&MobileConfig::ProcessConfig, | 370 base::Bind(&MobileConfig::ProcessConfig, |
371 base::Unretained(this), // singleton. | 371 base::Unretained(this), // singleton. |
372 global_config, | 372 global_config, |
373 local_config)); | 373 local_config)); |
374 } | 374 } |
375 | 375 |
376 } // namespace chromeos | 376 } // namespace chromeos |
OLD | NEW |