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