1 // Copyright (c) 2011 The Chromium 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.
5 #ifndef CHROME_BROWSER_CHROMEOS_MOBILE_CONFIG_H_
6 #define CHROME_BROWSER_CHROMEOS_MOBILE_CONFIG_H_
13 #include "base/gtest_prod_util.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/singleton.h"
16 #include "base/time.h"
17 #include "chrome/browser/chromeos/customization_document.h"
22 class DictionaryValue
;
27 // Class that processes mobile (carrier) configuration.
28 // Confugration is defined as a JSON file - global and local one.
29 // First global configuration is loaded then local one if it exists.
30 // Notes on global/local configuration:
31 // 1. All global config data is inherited unless some carrier properties
32 // are overidden or carrier deals are explicitly marked as excluded.
33 // 2. Local config could mark that all carrier deals should be excluded or
34 // only specific carrier deals are excluded.
35 // 3. New ID mappings in local config are not supported.
36 // 4. If local config exists, at least trivial global config should exist too.
37 // 5. If any error occurs while parsing global/local config,
38 // MobileConfig::IsReady() will return false.
39 class MobileConfig
: public CustomizationDocument
{
44 explicit CarrierDeal(base::DictionaryValue
* deal_dict
);
47 // Returns string with the specified |locale| and |id|.
48 // If there's no version for |locale|, default one is returned.
49 // If there's no string with specified |id|, empty string is returned.
50 std::string
GetLocalizedString(const std::string
& locale
,
51 const std::string
& id
) const;
53 const std::string
& deal_id() const { return deal_id_
; }
54 const std::vector
<std::string
>& locales() const { return locales_
; }
55 const std::string
& info_url() const { return info_url_
; }
56 int notification_count() const { return notification_count_
; }
57 base::Time
expire_date() const { return expire_date_
; }
61 std::vector
<std::string
> locales_
;
62 std::string info_url_
;
63 int notification_count_
;
64 base::Time expire_date_
;
65 base::DictionaryValue
* localized_strings_
;
67 DISALLOW_COPY_AND_ASSIGN(CarrierDeal
);
73 Carrier(base::DictionaryValue
* carrier_dict
,
74 const std::string
& initial_locale
);
77 const std::vector
<std::string
>& external_ids() { return external_ids_
; }
78 const std::string
& top_up_url() const { return top_up_url_
; }
80 // Returns "default" carrier deal i.e. first deal defined or NULL
81 // if there're no deals defined.
82 const CarrierDeal
* GetDefaultDeal() const;
84 // Returns carrier deal by ID.
85 const CarrierDeal
* GetDeal(const std::string
& deal_id
) const;
87 // Initializes carrier from supplied dictionary.
88 // Multiple calls supported (i.e. second call for local config).
89 void InitFromDictionary(base::DictionaryValue
* carrier_dict
,
90 const std::string
& initial_locale
);
92 // Removes all carrier deals. Might be executed when local config is loaded.
96 // Maps deal id to deal instance.
97 typedef std::map
<std::string
, CarrierDeal
*> CarrierDeals
;
99 std::string top_up_url_
;
100 std::vector
<std::string
> external_ids_
;
104 DISALLOW_COPY_AND_ASSIGN(Carrier
);
107 // External carrier ID (ex. "Verizon (us)") mapping to internal carrier ID.
108 typedef std::map
<std::string
, std::string
> CarrierIdMap
;
110 // Internal carrier ID mapping to Carrier config.
111 typedef std::map
<std::string
, Carrier
*> Carriers
;
113 static MobileConfig
* GetInstance();
115 // Returns carrier by external ID.
116 const MobileConfig::Carrier
* GetCarrier(const std::string
& carrier_id
) const;
119 virtual bool LoadManifestFromString(const std::string
& manifest
) OVERRIDE
;
122 FRIEND_TEST_ALL_PREFIXES(MobileConfigTest
, Basic
);
123 FRIEND_TEST_ALL_PREFIXES(MobileConfigTest
, BadManifest
);
124 FRIEND_TEST_ALL_PREFIXES(MobileConfigTest
, DealOtherLocale
);
125 FRIEND_TEST_ALL_PREFIXES(MobileConfigTest
, OldDeal
);
126 FRIEND_TEST_ALL_PREFIXES(MobileConfigTest
, LocalConfigNoDeals
);
127 FRIEND_TEST_ALL_PREFIXES(MobileConfigTest
, LocalConfig
);
128 friend struct DefaultSingletonTraits
<MobileConfig
>;
130 // C-tor for singleton construction.
133 // C-tor for test construction.
134 MobileConfig(const std::string
& config
,
135 const std::string
& initial_locale
);
137 virtual ~MobileConfig();
139 // Loads carrier configuration.
142 // Processes global/local config.
143 void ProcessConfig(const std::string
& global_config
,
144 const std::string
& local_config
);
146 // Executes on FILE thread and reads config files to string.
147 void ReadConfigInBackground(const FilePath
& global_config_file
,
148 const FilePath
& local_config_file
);
150 // Maps external carrier ID to internal carrier ID.
151 CarrierIdMap carrier_id_map_
;
153 // Carrier configuration (including carrier deals).
156 // Initial locale value.
157 std::string initial_locale_
;
159 // Root value of the local config (if it exists).
160 // Global config is stored in root_ of the base class.
161 scoped_ptr
<base::DictionaryValue
> local_config_root_
;
163 DISALLOW_COPY_AND_ASSIGN(MobileConfig
);
166 } // namespace chromeos
168 #endif // CHROME_BROWSER_CHROMEOS_MOBILE_CONFIG_H_