Home | History | Annotate | Download | only in util
      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 // This file contains functions processing master preference file used by
      6 // setup and first run.
      7 
      8 #ifndef CHROME_INSTALLER_UTIL_MASTER_PREFERENCES_H_
      9 #define CHROME_INSTALLER_UTIL_MASTER_PREFERENCES_H_
     10 
     11 #include <string>
     12 #include <vector>
     13 
     14 #include "base/command_line.h"
     15 #include "base/memory/scoped_ptr.h"
     16 
     17 namespace base {
     18 class DictionaryValue;
     19 class FilePath;
     20 }
     21 
     22 namespace installer {
     23 
     24 #if !defined(OS_MACOSX)
     25 // This is the default name for the master preferences file used to pre-set
     26 // values in the user profile at first run.
     27 const char kDefaultMasterPrefs[] = "master_preferences";
     28 #endif
     29 
     30 // The master preferences is a JSON file with the same entries as the
     31 // 'Default\Preferences' file. This function parses the distribution
     32 // section of the preferences file.
     33 //
     34 // A prototypical 'master_preferences' file looks like this:
     35 //
     36 // {
     37 //   "distribution": {
     38 //      "alternate_shortcut_text": false,
     39 //      "auto_launch_chrome": false,
     40 //      "chrome_shortcut_icon_index": 0,
     41 //      "create_all_shortcuts": true,
     42 //      "import_bookmarks": false,
     43 //      "import_bookmarks_from_file": "c:\\path",
     44 //      "import_history": false,
     45 //      "import_home_page": false,
     46 //      "import_search_engine": true,
     47 //      "ping_delay": 40,
     48 //      "show_welcome_page": true,
     49 //      "skip_first_run_ui": true,
     50 //      "do_not_launch_chrome": false,
     51 //      "make_chrome_default": false,
     52 //      "make_chrome_default_for_user": true,
     53 //      "require_eula": true,
     54 //      "system_level": false,
     55 //      "verbose_logging": true
     56 //   },
     57 //   "browser": {
     58 //      "show_home_button": true
     59 //   },
     60 //   "bookmark_bar": {
     61 //      "show_on_all_tabs": true
     62 //   },
     63 //   "first_run_tabs": [
     64 //      "http://gmail.com",
     65 //      "https://igoogle.com"
     66 //   ],
     67 //   "homepage": "http://example.org",
     68 //   "homepage_is_newtabpage": false
     69 // }
     70 //
     71 // A reserved "distribution" entry in the file is used to group related
     72 // installation properties. This entry will be ignored at other times.
     73 // This function parses the 'distribution' entry and returns a combination
     74 // of MasterPrefResult.
     75 
     76 class MasterPreferences {
     77  public:
     78   // Construct a master preferences from the current process' current command
     79   // line. Equivalent to calling
     80   // MasterPreferences(*CommandLine::ForCurrentProcess()).
     81   MasterPreferences();
     82 
     83   // Parses the command line and optionally reads the master preferences file
     84   // to get distribution related install options (if the "installerdata" switch
     85   // is present in the command line.
     86   // The options from the preference file and command line are merged, with the
     87   // ones from the command line taking precedence in case of a conflict.
     88   explicit MasterPreferences(const CommandLine& cmd_line);
     89 
     90   // Parses a specific preferences file and does not merge any command line
     91   // switches with the distribution dictionary.
     92   explicit MasterPreferences(const base::FilePath& prefs_path);
     93 
     94   // Parses a preferences directly from |prefs| and does not merge any command
     95   // line switches with the distribution dictionary.
     96   explicit MasterPreferences(const std::string& prefs);
     97 
     98   ~MasterPreferences();
     99 
    100   // Each of the Get methods below returns true if the named value was found in
    101   // the distribution dictionary and its value assigned to the 'value'
    102   // parameter.  If the value wasn't found, the return value is false.
    103   bool GetBool(const std::string& name, bool* value) const;
    104   bool GetInt(const std::string& name, int* value) const;
    105   bool GetString(const std::string& name, std::string* value) const;
    106 
    107   // As part of the master preferences an optional section indicates the tabs
    108   // to open during first run. An example is the following:
    109   //
    110   //  {
    111   //    "first_run_tabs": [
    112   //       "http://google.com/f1",
    113   //       "https://google.com/f2"
    114   //    ]
    115   //  }
    116   //
    117   // Note that the entries are usually urls but they don't have to be.
    118   //
    119   // An empty vector is returned if the first_run_tabs preference is absent.
    120   std::vector<std::string> GetFirstRunTabs() const;
    121 
    122   // The master preferences can also contain a regular extensions
    123   // preference block. If so, the extensions referenced there will be
    124   // installed during the first run experience.
    125   // An extension can go in the master prefs needs just the basic
    126   // elements such as:
    127   //   1- An extension entry under settings, assigned by the gallery
    128   //   2- The "location" : 1 entry
    129   //   3- A minimal "manifest" block with key, name, permissions, update url
    130   //      and version. The version needs to be lower than the version of
    131   //      the extension that is hosted in the gallery.
    132   //   4- The "path" entry with the version as last component
    133   //   5- The "state" : 1 entry
    134   //
    135   // The following is an example of a master pref file that installs
    136   // Google XYZ:
    137   //
    138   //  {
    139   //     "extensions": {
    140   //        "settings": {
    141   //           "ppflmjolhbonpkbkooiamcnenbmbjcbb": {
    142   //              "location": 1,
    143   //              "manifest": {
    144   //                 "key": "MIGfMA0GCSqGSIb3DQEBAQUAA4<rest of key ommited>",
    145   //                 "name": "Google XYZ (Installing...)",
    146   //                 "permissions": [ "tabs", "http://xyz.google.com/" ],
    147   //                 "update_url": "http://fixme.com/fixme/fixme/crx",
    148   //                 "version": "0.0"
    149   //              },
    150   //              "path": "ppflmjolhbonpkbkooiamcnenbmbjcbb\\0.0",
    151   //              "state": 1
    152   //           }
    153   //        }
    154   //     }
    155   //  }
    156   //
    157   bool GetExtensionsBlock(base::DictionaryValue** extensions) const;
    158 
    159   // Returns the variations seed entry from the master prefs.
    160   std::string GetVariationsSeed() const;
    161 
    162   // Returns true iff the master preferences were successfully read from a file.
    163   bool read_from_file() const {
    164     return preferences_read_from_file_;
    165   }
    166 
    167   bool install_chrome() const {
    168     return chrome_;
    169   }
    170 
    171   bool install_chrome_app_launcher() const {
    172     return chrome_app_launcher_;
    173   }
    174 
    175   bool is_multi_install() const {
    176     return multi_install_;
    177   }
    178 
    179   // Returns a reference to this MasterPreferences' root dictionary of values.
    180   const base::DictionaryValue& master_dictionary() const {
    181     return *master_dictionary_.get();
    182   }
    183 
    184   // Returns a static preference object that has been initialized with the
    185   // CommandLine object for the current process.
    186   // NOTE: Must not be called before CommandLine::Init() is called!
    187   // OTHER NOTE: Not thread safe.
    188   static const MasterPreferences& ForCurrentProcess();
    189 
    190  protected:
    191   void InitializeFromCommandLine(const CommandLine& cmd_line);
    192 
    193   // Initializes the instance from a given JSON string, returning true if the
    194   // string was successfully parsed.
    195   bool InitializeFromString(const std::string& json_data);
    196 
    197   void InitializeProductFlags();
    198 
    199   // Enforces legacy preferences that should no longer be used, but could be
    200   // found in older master_preferences files.
    201   void EnforceLegacyPreferences();
    202 
    203  protected:
    204   scoped_ptr<base::DictionaryValue> master_dictionary_;
    205   base::DictionaryValue* distribution_;
    206   bool preferences_read_from_file_;
    207   bool chrome_;
    208   bool chrome_app_launcher_;
    209   bool multi_install_;
    210 
    211  private:
    212   DISALLOW_COPY_AND_ASSIGN(MasterPreferences);
    213 };
    214 
    215 }  // namespace installer
    216 
    217 #endif  // CHROME_INSTALLER_UTIL_MASTER_PREFERENCES_H_
    218