Home | History | Annotate | Download | only in chromeos
      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
      3 // found in the LICENSE file.
      4 
      5 #ifndef CHROME_BROWSER_CHROMEOS_MOBILE_CONFIG_H_
      6 #define CHROME_BROWSER_CHROMEOS_MOBILE_CONFIG_H_
      7 
      8 #include <map>
      9 #include <string>
     10 #include <vector>
     11 
     12 #include "base/gtest_prod_util.h"
     13 #include "base/memory/scoped_ptr.h"
     14 #include "base/memory/singleton.h"
     15 #include "base/time/time.h"
     16 #include "chrome/browser/chromeos/customization_document.h"
     17 
     18 namespace base {
     19 class DictionaryValue;
     20 class FilePath;
     21 }
     22 
     23 namespace chromeos {
     24 
     25 // Class that processes mobile (carrier) configuration.
     26 // Confugration is defined as a JSON file - global and local one.
     27 // First global configuration is loaded then local one if it exists.
     28 // Notes on global/local configuration:
     29 // 1. All global config data is inherited unless some carrier properties
     30 //    are overidden or carrier deals are explicitly marked as excluded.
     31 // 2. Local config could mark that all carrier deals should be excluded or
     32 //    only specific carrier deals are excluded.
     33 // 3. New ID mappings in local config are not supported.
     34 // 4. If local config exists, at least trivial global config should exist too.
     35 // 5. If any error occurs while parsing global/local config,
     36 //    MobileConfig::IsReady() will return false.
     37 class MobileConfig : public CustomizationDocument  {
     38  public:
     39   // Carrier deal.
     40   class CarrierDeal {
     41    public:
     42     explicit CarrierDeal(const base::DictionaryValue* deal_dict);
     43     ~CarrierDeal();
     44 
     45     // Returns string with the specified |locale| and |id|.
     46     // If there's no version for |locale|, default one is returned.
     47     // If there's no string with specified |id|, empty string is returned.
     48     std::string GetLocalizedString(const std::string& locale,
     49                                    const std::string& id) const;
     50 
     51     const std::string& deal_id() const { return deal_id_; }
     52     const std::vector<std::string>& locales() const { return locales_; }
     53     const std::string& info_url() const { return info_url_; }
     54     int notification_count() const { return notification_count_; }
     55     base::Time expire_date() const { return expire_date_; }
     56 
     57    private:
     58     std::string deal_id_;
     59     std::vector<std::string> locales_;
     60     std::string info_url_;
     61     int notification_count_;
     62     base::Time expire_date_;
     63     const base::DictionaryValue* localized_strings_;
     64 
     65     DISALLOW_COPY_AND_ASSIGN(CarrierDeal);
     66   };
     67 
     68   // Carrier config.
     69   class Carrier {
     70    public:
     71     Carrier(const base::DictionaryValue* carrier_dict,
     72             const std::string& initial_locale);
     73     ~Carrier();
     74 
     75     const std::vector<std::string>& external_ids() { return external_ids_; }
     76     const std::string& top_up_url() const { return top_up_url_; }
     77     bool show_portal_button() const { return show_portal_button_; }
     78 
     79     // Returns "default" carrier deal i.e. first deal defined or NULL
     80     // if there're no deals defined.
     81     const CarrierDeal* GetDefaultDeal() const;
     82 
     83     // Returns carrier deal by ID.
     84     const CarrierDeal* GetDeal(const std::string& deal_id) const;
     85 
     86     // Initializes carrier from supplied dictionary.
     87     // Multiple calls supported (i.e. second call for local config).
     88     void InitFromDictionary(const base::DictionaryValue* carrier_dict,
     89                             const std::string& initial_locale);
     90 
     91     // Removes all carrier deals. Might be executed when local config is loaded.
     92     void RemoveDeals();
     93 
     94    private:
     95     // Maps deal id to deal instance.
     96     typedef std::map<std::string, CarrierDeal*> CarrierDeals;
     97 
     98     // List of external IDs that should map to this carrier.
     99     std::vector<std::string> external_ids_;
    100 
    101     // Top-up URL. Used in network menu ("View account" link) +
    102     // carrier name in network details (in settings) is a link.
    103     std::string top_up_url_;
    104 
    105     // If true, show a separate "View account" button on network details page
    106     // even if device is activated and doesn't need new data plan.
    107     // It's not shown when one of the "Buy plan" / "Activate" is shown.
    108     // All "Buy plan" / "Activate" / "View account" buttons launch
    109     // carrier portal (chrome://mobilesetup/ extension).
    110     bool show_portal_button_;
    111 
    112     CarrierDeals deals_;
    113 
    114     DISALLOW_COPY_AND_ASSIGN(Carrier);
    115   };
    116 
    117   // Carrier config for a specific initial locale.
    118   class LocaleConfig {
    119    public:
    120     explicit LocaleConfig(base::DictionaryValue* locale_dict);
    121     ~LocaleConfig();
    122 
    123     const std::string& setup_url() const { return setup_url_; }
    124 
    125     // Initializes local config carrier from supplied dictionary.
    126     // Multiple calls supported (i.e. second call for local config).
    127     void InitFromDictionary(base::DictionaryValue* locale_dict);
    128 
    129    private:
    130     // Carrier setup URL. Used in network menu ("Set-up Mobile Data" link).
    131     // Displayed when SIM card is not installed on the device with a
    132     // particular initial locale.
    133     std::string setup_url_;
    134 
    135     DISALLOW_COPY_AND_ASSIGN(LocaleConfig);
    136   };
    137 
    138   // External carrier ID (ex. "Verizon (us)") mapping to internal carrier ID.
    139   typedef std::map<std::string, std::string> CarrierIdMap;
    140 
    141   // Internal carrier ID mapping to Carrier config.
    142   typedef std::map<std::string, Carrier*> Carriers;
    143 
    144   static MobileConfig* GetInstance();
    145 
    146   // Returns carrier by external ID or NULL if there's no such carrier.
    147   const MobileConfig::Carrier* GetCarrier(const std::string& carrier_id) const;
    148 
    149   // Returns locale specific config by initial locale or NULL
    150   // if there's no such config defined.
    151   const MobileConfig::LocaleConfig* GetLocaleConfig() const;
    152 
    153  protected:
    154   virtual bool LoadManifestFromString(const std::string& manifest) OVERRIDE;
    155 
    156  private:
    157   FRIEND_TEST_ALL_PREFIXES(MobileConfigTest, Basic);
    158   FRIEND_TEST_ALL_PREFIXES(MobileConfigTest, BadManifest);
    159   FRIEND_TEST_ALL_PREFIXES(MobileConfigTest, DealOtherLocale);
    160   FRIEND_TEST_ALL_PREFIXES(MobileConfigTest, OldDeal);
    161   FRIEND_TEST_ALL_PREFIXES(MobileConfigTest, LocalConfigNoDeals);
    162   FRIEND_TEST_ALL_PREFIXES(MobileConfigTest, LocalConfig);
    163   friend struct DefaultSingletonTraits<MobileConfig>;
    164 
    165   // C-tor for singleton construction.
    166   MobileConfig();
    167 
    168   // C-tor for test construction.
    169   MobileConfig(const std::string& config,
    170                const std::string& initial_locale);
    171 
    172   virtual ~MobileConfig();
    173 
    174   // Loads carrier configuration.
    175   void LoadConfig();
    176 
    177   // Processes global/local config.
    178   void ProcessConfig(const std::string& global_config,
    179                      const std::string& local_config);
    180 
    181   // Executes on FILE thread and reads config files to string.
    182   void ReadConfigInBackground(const base::FilePath& global_config_file,
    183                               const base::FilePath& local_config_file);
    184 
    185   // Maps external carrier ID to internal carrier ID.
    186   CarrierIdMap carrier_id_map_;
    187 
    188   // Carrier configuration (including carrier deals).
    189   Carriers carriers_;
    190 
    191   // Initial locale specific config if defined.
    192   scoped_ptr<LocaleConfig> locale_config_;
    193 
    194   // Initial locale value.
    195   std::string initial_locale_;
    196 
    197   // Root value of the local config (if it exists).
    198   // Global config is stored in root_ of the base class.
    199   scoped_ptr<base::DictionaryValue> local_config_root_;
    200 
    201   DISALLOW_COPY_AND_ASSIGN(MobileConfig);
    202 };
    203 
    204 }  // namespace chromeos
    205 
    206 #endif  // CHROME_BROWSER_CHROMEOS_MOBILE_CONFIG_H_
    207