diff options
-rw-r--r-- | chrome/browser/first_run/first_run.cc | 89 | ||||
-rw-r--r-- | chrome/installer/setup/install.cc | 34 | ||||
-rw-r--r-- | chrome/installer/setup/install.h | 3 | ||||
-rw-r--r-- | chrome/installer/setup/setup_main.cc | 34 | ||||
-rw-r--r-- | chrome/installer/util/install_util.cc | 30 | ||||
-rw-r--r-- | chrome/installer/util/install_util.h | 7 | ||||
-rw-r--r-- | chrome/installer/util/master_preferences.cc | 238 | ||||
-rw-r--r-- | chrome/installer/util/master_preferences.h | 176 | ||||
-rw-r--r-- | chrome/installer/util/master_preferences_dummy.cc | 47 | ||||
-rw-r--r-- | chrome/installer/util/master_preferences_unittest.cc | 247 | ||||
-rw-r--r-- | chrome/installer/util/shell_util.cc | 9 |
11 files changed, 420 insertions, 494 deletions
diff --git a/chrome/browser/first_run/first_run.cc b/chrome/browser/first_run/first_run.cc index 525c091..ce89af5 100644 --- a/chrome/browser/first_run/first_run.cc +++ b/chrome/browser/first_run/first_run.cc @@ -28,6 +28,7 @@ #include "chrome/common/chrome_switches.h" #include "chrome/common/pref_names.h" #include "chrome/installer/util/master_preferences.h" +#include "chrome/installer/util/master_preferences_constants.h" #include "chrome/installer/util/util_constants.h" namespace { @@ -95,28 +96,26 @@ bool FirstRun::ProcessMasterPreferences(const FilePath& user_data_dir, return true; master_prefs = master_prefs.AppendASCII(installer_util::kDefaultMasterPrefs); - scoped_ptr<DictionaryValue> prefs( - installer_util::ParseDistributionPreferences(master_prefs)); - if (!prefs.get()) + installer_util::MasterPreferences prefs(master_prefs); + if (!prefs.read_from_file()) return true; - out_prefs->new_tabs = installer_util::GetFirstRunTabs(prefs.get()); + out_prefs->new_tabs = prefs.GetFirstRunTabs(); bool value = false; #if defined(OS_WIN) // RLZ is currently a Windows-only phenomenon. When it comes to the Mac/ // Linux, enable it here. - if (!installer_util::GetDistroIntegerPreference(prefs.get(), - installer_util::master_preferences::kDistroPingDelay, - &out_prefs->ping_delay)) { + if (!prefs.GetInt(installer_util::master_preferences::kDistroPingDelay, + &out_prefs->ping_delay)) { // 90 seconds is the default that we want to use in case master // preferences is missing, corrupt or ping_delay is missing. out_prefs->ping_delay = 90; } - if (installer_util::GetDistroBooleanPreference(prefs.get(), - installer_util::master_preferences::kRequireEula, &value) && value) { + if (prefs.GetBool(installer_util::master_preferences::kRequireEula, &value) && + value) { // Show the post-installation EULA. This is done by setup.exe and the // result determines if we continue or not. We wait here until the user // dismisses the dialog. @@ -143,9 +142,10 @@ bool FirstRun::ProcessMasterPreferences(const FilePath& user_data_dir, } #endif - if (installer_util::GetDistroBooleanPreference(prefs.get(), - installer_util::master_preferences::kAltFirstRunBubble, &value) && value) + if (prefs.GetBool(installer_util::master_preferences::kAltFirstRunBubble, + &value) && value) { FirstRun::SetOEMFirstRunBubblePref(); + } FilePath user_prefs = GetDefaultPrefFilePath(true, user_data_dir); if (user_prefs.empty()) @@ -158,14 +158,14 @@ bool FirstRun::ProcessMasterPreferences(const FilePath& user_data_dir, #if defined(OS_WIN) DictionaryValue* extensions = 0; - if (installer_util::HasExtensionsBlock(prefs.get(), &extensions)) { + if (prefs.GetExtensionsBlock(&extensions)) { VLOG(1) << "Extensions block found in master preferences"; DoDelayedInstallExtensions(); } #endif - if (installer_util::GetDistroBooleanPreference(prefs.get(), - installer_util::master_preferences::kDistroImportSearchPref, &value)) { + if (prefs.GetBool(installer_util::master_preferences::kDistroImportSearchPref, + &value)) { if (value) { out_prefs->do_import_items |= importer::SEARCH_ENGINES; } else { @@ -174,22 +174,25 @@ bool FirstRun::ProcessMasterPreferences(const FilePath& user_data_dir, } // Check to see if search engine logos should be randomized. - if (installer_util::GetDistroBooleanPreference(prefs.get(), - installer_util::master_preferences::kSearchEngineExperimentRandomizePref, - &value) && value) + if (prefs.GetBool( + installer_util::master_preferences:: + kSearchEngineExperimentRandomizePref, + &value) && value) { out_prefs->randomize_search_engine_experiment = true; + } // If we're suppressing the first-run bubble, set that preference now. // Otherwise, wait until the user has completed first run to set it, so the // user is guaranteed to see the bubble iff he or she has completed the first // run process. - if (installer_util::GetDistroBooleanPreference(prefs.get(), - installer_util::master_preferences::kDistroSuppressFirstRunBubble, - &value) && value) + if (prefs.GetBool( + installer_util::master_preferences::kDistroSuppressFirstRunBubble, + &value) && value) FirstRun::SetShowFirstRunBubblePref(false); - if (installer_util::GetDistroBooleanPreference(prefs.get(), - installer_util::master_preferences::kDistroImportHistoryPref, &value)) { + if (prefs.GetBool( + installer_util::master_preferences::kDistroImportHistoryPref, + &value)) { if (value) { out_prefs->do_import_items |= importer::HISTORY; } else { @@ -198,10 +201,11 @@ bool FirstRun::ProcessMasterPreferences(const FilePath& user_data_dir, } std::string not_used; - out_prefs->homepage_defined = prefs->GetString(prefs::kHomePage, ¬_used); + out_prefs->homepage_defined = prefs.GetString(prefs::kHomePage, ¬_used); - if (installer_util::GetDistroBooleanPreference(prefs.get(), - installer_util::master_preferences::kDistroImportHomePagePref, &value)) { + if (prefs.GetBool( + installer_util::master_preferences::kDistroImportHomePagePref, + &value)) { if (value) { out_prefs->do_import_items |= importer::HOME_PAGE; } else { @@ -210,25 +214,27 @@ bool FirstRun::ProcessMasterPreferences(const FilePath& user_data_dir, } // Bookmarks are never imported unless specifically turned on. - if (installer_util::GetDistroBooleanPreference(prefs.get(), - installer_util::master_preferences::kDistroImportBookmarksPref, &value) - && value) { + if (prefs.GetBool( + installer_util::master_preferences::kDistroImportBookmarksPref, + &value) && value) { out_prefs->do_import_items |= importer::FAVORITES; } - if (installer_util::GetDistroBooleanPreference(prefs.get(), - installer_util::master_preferences::kMakeChromeDefaultForUser, &value) && - value) + if (prefs.GetBool( + installer_util::master_preferences::kMakeChromeDefaultForUser, + &value) && value) { out_prefs->make_chrome_default = true; + } // TODO(mirandac): Refactor skip-first-run-ui process into regular first run // import process. http://crbug.com/49647 // Note we are skipping all other master preferences if skip-first-run-ui // is *not* specified. (That is, we continue only if skipping first run ui.) - if (!installer_util::GetDistroBooleanPreference(prefs.get(), - installer_util::master_preferences::kDistroSkipFirstRunPref, &value) || - !value) + if (!prefs.GetBool( + installer_util::master_preferences::kDistroSkipFirstRunPref, + &value) || !value) { return true; + } #if !defined(OS_WIN) // From here on we won't show first run so we need to do the work to show the @@ -242,13 +248,13 @@ bool FirstRun::ProcessMasterPreferences(const FilePath& user_data_dir, if (!FirstRun::CreateSentinel()) return false; - if (installer_util::GetDistroBooleanPreference(prefs.get(), - installer_util::master_preferences::kDistroShowWelcomePage, &value) && - value) + if (prefs.GetBool(installer_util::master_preferences::kDistroShowWelcomePage, + &value) && value) { FirstRun::SetShowWelcomePagePref(); + } std::string import_bookmarks_path; - installer_util::GetDistroStringPreference(prefs.get(), + prefs.GetString( installer_util::master_preferences::kDistroImportBookmarksFromFilePref, &import_bookmarks_path); @@ -295,10 +301,11 @@ bool FirstRun::ProcessMasterPreferences(const FilePath& user_data_dir, } #endif - if (installer_util::GetDistroBooleanPreference(prefs.get(), - installer_util::master_preferences::kMakeChromeDefaultForUser, &value) && - value) + if (prefs.GetBool( + installer_util::master_preferences::kMakeChromeDefaultForUser, + &value) && value) { ShellIntegration::SetAsDefaultBrowser(); + } return false; } diff --git a/chrome/installer/setup/install.cc b/chrome/installer/setup/install.cc index 1f2ea11..adc7eb8 100644 --- a/chrome/installer/setup/install.cc +++ b/chrome/installer/setup/install.cc @@ -20,7 +20,7 @@ #include "chrome/installer/util/google_update_constants.h" #include "chrome/installer/util/helper.h" #include "chrome/installer/util/install_util.h" -#include "chrome/installer/util/master_preferences.h" +#include "chrome/installer/util/master_preferences_constants.h" #include "chrome/installer/util/set_reg_value_work_item.h" #include "chrome/installer/util/shell_util.h" #include "chrome/installer/util/util_constants.h" @@ -729,11 +729,11 @@ std::wstring installer::GetInstallerPathUnderChrome( installer_util::InstallStatus installer::InstallOrUpdateChrome( const std::wstring& exe_path, const std::wstring& archive_path, const std::wstring& install_temp_path, const std::wstring& prefs_path, - const DictionaryValue* prefs, const Version& new_version, + const installer_util::MasterPreferences& prefs, const Version& new_version, const Version* installed_version) { bool system_install = false; - installer_util::GetDistroBooleanPreference(prefs, - installer_util::master_preferences::kSystemLevel, &system_install); + prefs.GetBool(installer_util::master_preferences::kSystemLevel, + &system_install); std::wstring install_path(GetChromeInstallPath(system_install)); if (install_path.empty()) { LOG(ERROR) << "Could not get installation destination path."; @@ -757,27 +757,25 @@ installer_util::InstallStatus installer::InstallOrUpdateChrome( CopyPreferenceFileForFirstRun(system_install, prefs_path); bool value = false; - if (!installer_util::GetDistroBooleanPreference(prefs, - installer_util::master_preferences::kDoNotCreateShortcuts, &value) || - !value) { + if (!prefs.GetBool( + installer_util::master_preferences::kDoNotCreateShortcuts, + &value) || !value) { bool create_all_shortcut = false; - installer_util::GetDistroBooleanPreference(prefs, - installer_util::master_preferences::kCreateAllShortcuts, - &create_all_shortcut); + prefs.GetBool(installer_util::master_preferences::kCreateAllShortcuts, + &create_all_shortcut); bool alt_shortcut = false; - installer_util::GetDistroBooleanPreference(prefs, - installer_util::master_preferences::kAltShortcutText, - &alt_shortcut); + prefs.GetBool(installer_util::master_preferences::kAltShortcutText, + &alt_shortcut); if (!CreateOrUpdateChromeShortcuts(exe_path, install_path, new_version.GetString(), result, system_install, create_all_shortcut, - alt_shortcut)) + alt_shortcut)) { LOG(WARNING) << "Failed to create/update start menu shortcut."; + } bool make_chrome_default = false; - installer_util::GetDistroBooleanPreference(prefs, - installer_util::master_preferences::kMakeChromeDefault, - &make_chrome_default); + prefs.GetBool(installer_util::master_preferences::kMakeChromeDefault, + &make_chrome_default); // If this is not the user's first Chrome install, but they have chosen // Chrome to become their default browser on the download page, we must @@ -786,7 +784,7 @@ installer_util::InstallStatus installer::InstallOrUpdateChrome( bool force_chrome_default_for_user = false; if (result == installer_util::NEW_VERSION_UPDATED || result == installer_util::INSTALL_REPAIRED) { - installer_util::GetDistroBooleanPreference(prefs, + prefs.GetBool( installer_util::master_preferences::kMakeChromeDefaultForUser, &force_chrome_default_for_user); } diff --git a/chrome/installer/setup/install.h b/chrome/installer/setup/install.h index a3c050f..1c8ccc4 100644 --- a/chrome/installer/setup/install.h +++ b/chrome/installer/setup/install.h @@ -10,6 +10,7 @@ #include <string> +#include "chrome/installer/util/master_preferences.h" #include "chrome/installer/util/util_constants.h" #include "chrome/installer/util/version.h" @@ -41,7 +42,7 @@ std::wstring GetInstallerPathUnderChrome(const std::wstring& install_path, installer_util::InstallStatus InstallOrUpdateChrome( const std::wstring& exe_path, const std::wstring& archive_path, const std::wstring& install_temp_path, const std::wstring& prefs_path, - const DictionaryValue* prefs, const Version& new_version, + const installer_util::MasterPreferences& prefs, const Version& new_version, const Version* installed_version); } diff --git a/chrome/installer/setup/setup_main.cc b/chrome/installer/setup/setup_main.cc index 6d5cbae..0898017 100644 --- a/chrome/installer/setup/setup_main.cc +++ b/chrome/installer/setup/setup_main.cc @@ -38,6 +38,7 @@ #include "chrome/installer/util/google_update_settings.h" #include "chrome/installer/util/google_update_constants.h" #include "chrome/installer/util/master_preferences.h" +#include "chrome/installer/util/master_preferences_constants.h" #include "chrome/installer/util/shell_util.h" #include "chrome/installer/util/util_constants.h" @@ -217,10 +218,11 @@ bool CheckPreInstallConditions(const installer::Version* installed_version, } installer_util::InstallStatus InstallChrome(const CommandLine& cmd_line, - const installer::Version* installed_version, const DictionaryValue* prefs) { + const installer::Version* installed_version, + const installer_util::MasterPreferences& prefs) { bool system_level = false; - installer_util::GetDistroBooleanPreference(prefs, - installer_util::master_preferences::kSystemLevel, &system_level); + prefs.GetBool(installer_util::master_preferences::kSystemLevel, + &system_level); installer_util::InstallStatus install_status = installer_util::UNKNOWN_STATUS; if (!CheckPreInstallConditions(installed_version, system_level, install_status)) @@ -325,7 +327,7 @@ installer_util::InstallStatus InstallChrome(const CommandLine& cmd_line, } bool value = false; - installer_util::GetDistroBooleanPreference(prefs, + prefs.GetBool( installer_util::master_preferences::kDoNotRegisterForUpdateLaunch, &value); bool write_chrome_launch_string = (!value) && @@ -338,9 +340,8 @@ installer_util::InstallStatus InstallChrome(const CommandLine& cmd_line, VLOG(1) << "First install successful."; // We never want to launch Chrome in system level install mode. bool do_not_launch_chrome = false; - installer_util::GetDistroBooleanPreference(prefs, - installer_util::master_preferences::kDoNotLaunchChrome, - &do_not_launch_chrome); + prefs.GetBool(installer_util::master_preferences::kDoNotLaunchChrome, + &do_not_launch_chrome); if (!system_level && !do_not_launch_chrome) installer::LaunchChrome(system_level); } else if ((install_status == installer_util::NEW_VERSION_UPDATED) || @@ -631,19 +632,19 @@ int WINAPI wWinMain(HINSTANCE instance, HINSTANCE prev_instance, const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess(); installer::InitInstallerLogging(parsed_command_line); - scoped_ptr<DictionaryValue> prefs(installer_util::GetInstallPreferences( - parsed_command_line)); + const installer_util::MasterPreferences& prefs = + InstallUtil::GetMasterPreferencesForCurrentProcess(); bool value = false; - if (installer_util::GetDistroBooleanPreference(prefs.get(), - installer_util::master_preferences::kVerboseLogging, &value) && - value) + if (prefs.GetBool(installer_util::master_preferences::kVerboseLogging, + &value) && value) { logging::SetMinLogLevel(logging::LOG_INFO); + } VLOG(1) << "Command Line: " << parsed_command_line.command_line_string(); bool system_install = false; - installer_util::GetDistroBooleanPreference(prefs.get(), - installer_util::master_preferences::kSystemLevel, &system_install); + prefs.GetBool(installer_util::master_preferences::kSystemLevel, + &system_install); VLOG(1) << "system install is " << system_install; // Check to make sure current system is WinXP or later. If not, log @@ -723,9 +724,8 @@ int WINAPI wWinMain(HINSTANCE instance, HINSTANCE prev_instance, system_install); // If --uninstall option is not specified, we assume it is install case. } else { - install_status = InstallChrome(parsed_command_line, - installed_version.get(), - prefs.get()); + install_status = InstallChrome(parsed_command_line, installed_version.get(), + prefs); } BrowserDistribution* dist = BrowserDistribution::GetDistribution(); diff --git a/chrome/installer/util/install_util.cc b/chrome/installer/util/install_util.cc index 5e7673b..d5fe07d 100644 --- a/chrome/installer/util/install_util.cc +++ b/chrome/installer/util/install_util.cc @@ -25,11 +25,17 @@ #include "chrome/installer/util/browser_distribution.h" #include "chrome/installer/util/google_update_constants.h" #include "chrome/installer/util/l10n_string_util.h" -#include "chrome/installer/util/master_preferences.h" +#include "chrome/installer/util/master_preferences_constants.h" #include "chrome/installer/util/util_constants.h" #include "chrome/installer/util/work_item_list.h" using base::win::RegKey; +using installer_util::MasterPreferences; + +const MasterPreferences& InstallUtil::GetMasterPreferencesForCurrentProcess() { + static MasterPreferences prefs(*CommandLine::ForCurrentProcess()); + return prefs; +} bool InstallUtil::ExecuteExeAsAdmin(const std::wstring& exe, const std::wstring& params, @@ -132,20 +138,13 @@ bool InstallUtil::IsPerUserInstall(const wchar_t* const exe_path) { } bool InstallUtil::IsChromeFrameProcess() { - CommandLine* command_line = CommandLine::ForCurrentProcess(); - DCHECK(command_line) - << "IsChromeFrameProcess() called before ComamandLine::Init()"; - FilePath module_path; PathService::Get(base::FILE_MODULE, &module_path); std::wstring module_name(module_path.BaseName().value()); - scoped_ptr<DictionaryValue> prefs(installer_util::GetInstallPreferences( - *command_line)); - DCHECK(prefs.get()); + const MasterPreferences& prefs = GetMasterPreferencesForCurrentProcess(); bool is_cf = false; - installer_util::GetDistroBooleanPreference(prefs.get(), - installer_util::master_preferences::kChromeFrame, &is_cf); + prefs.GetBool(installer_util::master_preferences::kChromeFrame, &is_cf); // Also assume this to be a ChromeFrame process if we are running inside // the Chrome Frame DLL. @@ -175,18 +174,13 @@ bool InstallUtil::IsMSIProcess(bool system_level) { static bool is_msi_ = false; static bool msi_checked_ = false; - CommandLine* command_line = CommandLine::ForCurrentProcess(); - CHECK(command_line); - if (!msi_checked_) { msi_checked_ = true; - scoped_ptr<DictionaryValue> prefs(installer_util::GetInstallPreferences( - *command_line)); - DCHECK(prefs.get()); + const MasterPreferences& prefs = GetMasterPreferencesForCurrentProcess(); + bool is_msi = false; - installer_util::GetDistroBooleanPreference(prefs.get(), - installer_util::master_preferences::kMsi, &is_msi); + prefs.GetBool(installer_util::master_preferences::kMsi, &is_msi); if (!is_msi) { // We didn't find it in the preferences, try looking in the registry. diff --git a/chrome/installer/util/install_util.h b/chrome/installer/util/install_util.h index f33bfb6..1e8fd79 100644 --- a/chrome/installer/util/install_util.h +++ b/chrome/installer/util/install_util.h @@ -15,6 +15,7 @@ #include <string> #include "base/basictypes.h" +#include "chrome/installer/util/master_preferences.h" #include "chrome/installer/util/util_constants.h" #include "chrome/installer/util/version.h" @@ -118,6 +119,12 @@ class InstallUtil { static bool DeleteRegistryValue(HKEY reg_root, const std::wstring& key_path, const std::wstring& value_name); + // Returns a static preference object that has been initialized with the + // CommandLine object for the current process. + // NOTE: Must not be called before CommandLine::Init() is called! + static const installer_util::MasterPreferences& + GetMasterPreferencesForCurrentProcess(); + private: DISALLOW_COPY_AND_ASSIGN(InstallUtil); }; diff --git a/chrome/installer/util/master_preferences.cc b/chrome/installer/util/master_preferences.cc index 2e037d0..b2ae7ef 100644 --- a/chrome/installer/util/master_preferences.cc +++ b/chrome/installer/util/master_preferences.cc @@ -9,6 +9,7 @@ #include "base/path_service.h" #include "base/string_util.h" #include "chrome/common/json_value_serializer.h" +#include "chrome/installer/util/master_preferences_constants.h" #include "chrome/installer/util/util_constants.h" #include "googleurl/src/gurl.h" @@ -16,6 +17,7 @@ namespace { const char kDistroDict[] = "distribution"; +const char kFirstRunTabs[] = "first_run_tabs"; bool GetGURLFromValue(const Value* in_value, GURL* out_value) { if (!in_value || !out_value) @@ -47,130 +49,11 @@ std::vector<GURL> GetNamedList(const char* name, return list; } -} // namespace - -namespace installer_util { - -bool GetDistroBooleanPreference(const DictionaryValue* prefs, - const std::string& name, - bool* value) { - if (!prefs || !value) - return false; - - DictionaryValue* distro = NULL; - if (!prefs->GetDictionary(kDistroDict, &distro) || !distro) - return false; - - if (!distro->GetBoolean(name, value)) - return false; - - return true; -} - -bool GetDistroStringPreference(const DictionaryValue* prefs, - const std::string& name, - std::string* value) { - if (!prefs || !value) - return false; - - DictionaryValue* distro = NULL; - if (!prefs->GetDictionary(kDistroDict, &distro) || !distro) - return false; - - std::string str_value; - if (!distro->GetString(name, &str_value)) - return false; - - if (str_value.empty()) - return false; - - *value = str_value; - return true; -} - -bool GetDistroIntegerPreference(const DictionaryValue* prefs, - const std::string& name, - int* value) { - if (!prefs || !value) - return false; - - DictionaryValue* distro = NULL; - if (!prefs->GetDictionary(kDistroDict, &distro) || !distro) - return false; - - if (!distro->GetInteger(name, value)) - return false; - - return true; -} - -DictionaryValue* GetInstallPreferences(const CommandLine& cmd_line) { - DictionaryValue* prefs = NULL; -#if defined(OS_WIN) - if (cmd_line.HasSwitch(installer_util::switches::kInstallerData)) { - FilePath prefs_path = cmd_line.GetSwitchValuePath( - installer_util::switches::kInstallerData); - prefs = installer_util::ParseDistributionPreferences(prefs_path); - } - - if (!prefs) - prefs = new DictionaryValue(); - - if (cmd_line.HasSwitch(installer_util::switches::kChromeFrame)) - installer_util::SetDistroBooleanPreference( - prefs, installer_util::master_preferences::kChromeFrame, true); - - if (cmd_line.HasSwitch(installer_util::switches::kCreateAllShortcuts)) - installer_util::SetDistroBooleanPreference( - prefs, installer_util::master_preferences::kCreateAllShortcuts, true); - - if (cmd_line.HasSwitch(installer_util::switches::kDoNotCreateShortcuts)) - installer_util::SetDistroBooleanPreference( - prefs, installer_util::master_preferences::kDoNotCreateShortcuts, true); - - if (cmd_line.HasSwitch(installer_util::switches::kMsi)) - installer_util::SetDistroBooleanPreference( - prefs, installer_util::master_preferences::kMsi, true); - - if (cmd_line.HasSwitch( - installer_util::switches::kDoNotRegisterForUpdateLaunch)) - installer_util::SetDistroBooleanPreference( - prefs, - installer_util::master_preferences::kDoNotRegisterForUpdateLaunch, - true); - - if (cmd_line.HasSwitch(installer_util::switches::kDoNotLaunchChrome)) - installer_util::SetDistroBooleanPreference( - prefs, installer_util::master_preferences::kDoNotLaunchChrome, true); - - if (cmd_line.HasSwitch(installer_util::switches::kMakeChromeDefault)) - installer_util::SetDistroBooleanPreference( - prefs, installer_util::master_preferences::kMakeChromeDefault, true); - - if (cmd_line.HasSwitch(installer_util::switches::kSystemLevel)) - installer_util::SetDistroBooleanPreference( - prefs, installer_util::master_preferences::kSystemLevel, true); - - if (cmd_line.HasSwitch(installer_util::switches::kVerboseLogging)) - installer_util::SetDistroBooleanPreference( - prefs, installer_util::master_preferences::kVerboseLogging, true); - - if (cmd_line.HasSwitch(installer_util::switches::kAltDesktopShortcut)) - installer_util::SetDistroBooleanPreference( - prefs, installer_util::master_preferences::kAltShortcutText, true); -#endif - - return prefs; -} - DictionaryValue* ParseDistributionPreferences( const FilePath& master_prefs_path) { - if (!file_util::PathExists(master_prefs_path)) - return NULL; - std::string json_data; if (!file_util::ReadFileToString(master_prefs_path, &json_data)) { - LOG(WARNING) << "Failed to read master prefs file."; + LOG(WARNING) << "Failed to read master prefs file. "; return NULL; } JSONStringValueSerializer json(json_data); @@ -188,23 +71,112 @@ DictionaryValue* ParseDistributionPreferences( return static_cast<DictionaryValue*>(root.release()); } -std::vector<GURL> GetFirstRunTabs(const DictionaryValue* prefs) { - return GetNamedList("first_run_tabs", prefs); +} // namespace + +namespace installer_util { + +MasterPreferences::MasterPreferences(const CommandLine& cmd_line) + : distribution_(NULL), preferences_read_from_file_(false) { +#if defined(OS_WIN) + if (cmd_line.HasSwitch(installer_util::switches::kInstallerData)) { + FilePath prefs_path(cmd_line.GetSwitchValuePath( + installer_util::switches::kInstallerData)); + this->MasterPreferences::MasterPreferences(prefs_path); + } else { + master_dictionary_.reset(new DictionaryValue()); + } + + DCHECK(master_dictionary_.get()); + + // A simple map from command line switches to equivalent switches in the + // distribution dictionary. Currently all switches added will be set to + // 'true'. + static const struct CmdLineSwitchToDistributionSwitch { + const wchar_t* cmd_line_switch; + const char* distribution_switch; + } translate_switches[] = { + { installer_util::switches::kChromeFrame, + installer_util::master_preferences::kChromeFrame }, + { installer_util::switches::kCreateAllShortcuts, + installer_util::master_preferences::kCreateAllShortcuts }, + { installer_util::switches::kDoNotCreateShortcuts, + installer_util::master_preferences::kDoNotCreateShortcuts }, + { installer_util::switches::kMsi, + installer_util::master_preferences::kMsi }, + { installer_util::switches::kDoNotRegisterForUpdateLaunch, + installer_util::master_preferences::kDoNotRegisterForUpdateLaunch }, + { installer_util::switches::kDoNotLaunchChrome, + installer_util::master_preferences::kDoNotLaunchChrome }, + { installer_util::switches::kMakeChromeDefault, + installer_util::master_preferences::kMakeChromeDefault }, + { installer_util::switches::kSystemLevel, + installer_util::master_preferences::kSystemLevel }, + { installer_util::switches::kVerboseLogging, + installer_util::master_preferences::kVerboseLogging }, + { installer_util::switches::kAltDesktopShortcut, + installer_util::master_preferences::kAltShortcutText }, + }; + + std::string name(kDistroDict); + for (int i = 0; i < arraysize(translate_switches); ++i) { + if (cmd_line.HasSwitch(translate_switches[i].cmd_line_switch)) { + name.resize(arraysize(kDistroDict) - 1); + name.append(".").append(translate_switches[i].distribution_switch); + master_dictionary_->SetBoolean(name, true); + } + } + + // Cache a pointer to the distribution dictionary. Ignore errors if any. + master_dictionary_->GetDictionary(kDistroDict, &distribution_); +#endif +} + +MasterPreferences::MasterPreferences(const FilePath& prefs_path) + : distribution_(NULL), preferences_read_from_file_(false) { + master_dictionary_.reset(ParseDistributionPreferences(prefs_path)); + LOG_IF(ERROR, !master_dictionary_.get()) << "Failed to parse " + << prefs_path.value(); + if (!master_dictionary_.get()) { + master_dictionary_.reset(new DictionaryValue()); + } else { + preferences_read_from_file_ = true; + // Cache a pointer to the distribution dictionary. + master_dictionary_->GetDictionary(kDistroDict, &distribution_); + } } -bool SetDistroBooleanPreference(DictionaryValue* prefs, - const std::string& name, - bool value) { - if (!prefs || name.empty()) - return false; - prefs->SetBoolean(std::string(kDistroDict) + "." + name, value); - return true; +MasterPreferences::~MasterPreferences() { +} + +bool MasterPreferences::GetBool(const std::string& name, bool* value) const { + bool ret = false; + if (distribution_) + ret = distribution_->GetBoolean(name, value); + return ret; +} + +bool MasterPreferences::GetInt(const std::string& name, int* value) const { + bool ret = false; + if (distribution_) + ret = distribution_->GetInteger(name, value); + return ret; +} + +bool MasterPreferences::GetString(const std::string& name, + std::string* value) const { + bool ret = false; + if (distribution_) + ret = (distribution_->GetString(name, value) && !value->empty()); + return ret; +} + +std::vector<GURL> MasterPreferences::GetFirstRunTabs() const { + return GetNamedList(kFirstRunTabs, master_dictionary_.get()); } -bool HasExtensionsBlock(const DictionaryValue* prefs, - DictionaryValue** extensions) { - return (prefs->GetDictionary(master_preferences::kExtensionsBlock, - extensions)); +bool MasterPreferences::GetExtensionsBlock(DictionaryValue** extensions) const { + return master_dictionary_->GetDictionary( + master_preferences::kExtensionsBlock, extensions); } } // installer_util diff --git a/chrome/installer/util/master_preferences.h b/chrome/installer/util/master_preferences.h index 0082a17..15e1cb5 100644 --- a/chrome/installer/util/master_preferences.h +++ b/chrome/installer/util/master_preferences.h @@ -12,8 +12,8 @@ #include <vector> #include "base/command_line.h" +#include "base/scoped_ptr.h" #include "googleurl/src/gurl.h" -#include "chrome/installer/util/master_preferences_constants.h" class DictionaryValue; class FilePath; @@ -24,30 +24,6 @@ namespace installer_util { // values in the user profile at first run. const char kDefaultMasterPrefs[] = "master_preferences"; -// Parse command line and read master preferences, if present, to get -// distribution related install options. Merge them if any command line -// options present (command line value takes precedence). -DictionaryValue* GetInstallPreferences(const CommandLine& cmd_line); - -// Gets the value of given boolean preference |name| from |prefs| dictionary -// which is assumed to contain a dictionary named "distribution". Returns -// true if the value is read successfully, otherwise false. -bool GetDistroBooleanPreference(const DictionaryValue* prefs, - const std::string& name, - bool* value); - -// This function gets value of a string preference from master -// preferences. Returns true if the value is read successfully, otherwise false. -bool GetDistroStringPreference(const DictionaryValue* prefs, - const std::string& name, - std::string* value); - -// This function gets value of an integer preference from master -// preferences. Returns true if the value is read successfully, otherwise false. -bool GetDistroIntegerPreference(const DictionaryValue* prefs, - const std::string& name, - int* value); - // The master preferences is a JSON file with the same entries as the // 'Default\Preferences' file. This function parses the distribution // section of the preferences file. @@ -93,68 +69,94 @@ bool GetDistroIntegerPreference(const DictionaryValue* prefs, // installation properties. This entry will be ignored at other times. // This function parses the 'distribution' entry and returns a combination // of MasterPrefResult. -DictionaryValue* ParseDistributionPreferences( - const FilePath& master_prefs_path); - -// As part of the master preferences an optional section indicates the tabs -// to open during first run. An example is the following: -// -// { -// "first_run_tabs": [ -// "http://google.com/f1", -// "https://google.com/f2" -// ] -// } -// -// Note that the entries are usually urls but they don't have to. -// -// This function retuns the list as a vector of GURLs. If the master -// preferences file does not contain such list the vector is empty. -std::vector<GURL> GetFirstRunTabs(const DictionaryValue* prefs); - -// Sets the value of given boolean preference |name| in "distribution" -// dictionary inside |prefs| dictionary. -bool SetDistroBooleanPreference(DictionaryValue* prefs, - const std::string& name, - bool value); - -// The master preferences can also contain a regular extensions -// preference block. If so, the extensions referenced there will be -// installed during the first run experience. -// An extension can go in the master prefs needs just the basic -// elements such as: -// 1- An extension entry under settings, assigned by the gallery -// 2- The "location" : 1 entry -// 3- A minimal "manifest" block with key, name, permissions, update url -// and version. The version needs to be lower than the version of -// the extension that is hosted in the gallery. -// 4- The "path" entry with the version as last component -// 5- The "state" : 1 entry -// -// The following is an example of a master pref file that installs -// Google XYZ: -// -// { -// "extensions": { -// "settings": { -// "ppflmjolhbonpkbkooiamcnenbmbjcbb": { -// "location": 1, -// "manifest": { -// "key": "MIGfMA0GCSqGSIb3DQEBAQUAA4<rest of key ommited>", -// "name": "Google XYZ (Installing...)", -// "permissions": [ "tabs", "http://xyz.google.com/" ], -// "update_url": "http://fixme.com/fixme/fixme/crx", -// "version": "0.0" -// }, -// "path": "ppflmjolhbonpkbkooiamcnenbmbjcbb\\0.0", -// "state": 1 -// } -// } -// } -// } -// -bool HasExtensionsBlock(const DictionaryValue* prefs, - DictionaryValue** extensions); +class MasterPreferences { + public: + // Parses the command line and optionally reads the master preferences file + // to get distribution related install options (if the "installerdata" switch + // is present in the command line. + // The options from the preference file and command line are merged, with the + // ones from the command line taking precedence in case of a conflict. + explicit MasterPreferences(const CommandLine& cmd_line); + + // Parses a specific preferences file and does not merge any command line + // switches with the distribution dictionary. + explicit MasterPreferences(const FilePath& prefs_path); + + ~MasterPreferences(); + + // Each of the Get methods below returns true if the named value was found in + // the distribution dictionary and its value assigned to the 'value' + // parameter. If the value wasn't found, the return value is false. + bool GetBool(const std::string& name, bool* value) const; + bool GetInt(const std::string& name, int* value) const; + bool GetString(const std::string& name, std::string* value) const; + + // As part of the master preferences an optional section indicates the tabs + // to open during first run. An example is the following: + // + // { + // "first_run_tabs": [ + // "http://google.com/f1", + // "https://google.com/f2" + // ] + // } + // + // Note that the entries are usually urls but they don't have to be. + // + // This function returns the list as a vector of GURLs. If the master + // preferences file does not contain such a list the vector is empty. + std::vector<GURL> GetFirstRunTabs() const; + + // The master preferences can also contain a regular extensions + // preference block. If so, the extensions referenced there will be + // installed during the first run experience. + // An extension can go in the master prefs needs just the basic + // elements such as: + // 1- An extension entry under settings, assigned by the gallery + // 2- The "location" : 1 entry + // 3- A minimal "manifest" block with key, name, permissions, update url + // and version. The version needs to be lower than the version of + // the extension that is hosted in the gallery. + // 4- The "path" entry with the version as last component + // 5- The "state" : 1 entry + // + // The following is an example of a master pref file that installs + // Google XYZ: + // + // { + // "extensions": { + // "settings": { + // "ppflmjolhbonpkbkooiamcnenbmbjcbb": { + // "location": 1, + // "manifest": { + // "key": "MIGfMA0GCSqGSIb3DQEBAQUAA4<rest of key ommited>", + // "name": "Google XYZ (Installing...)", + // "permissions": [ "tabs", "http://xyz.google.com/" ], + // "update_url": "http://fixme.com/fixme/fixme/crx", + // "version": "0.0" + // }, + // "path": "ppflmjolhbonpkbkooiamcnenbmbjcbb\\0.0", + // "state": 1 + // } + // } + // } + // } + // + bool GetExtensionsBlock(DictionaryValue** extensions) const; + + // Returns true iff the master preferences were successfully read from a file. + bool read_from_file() const { + return preferences_read_from_file_; + } + + protected: + scoped_ptr<DictionaryValue> master_dictionary_; + DictionaryValue* distribution_; + bool preferences_read_from_file_; + + private: + DISALLOW_COPY_AND_ASSIGN(MasterPreferences); +}; } // namespace installer_util diff --git a/chrome/installer/util/master_preferences_dummy.cc b/chrome/installer/util/master_preferences_dummy.cc index f11a445..2ae6d4b 100644 --- a/chrome/installer/util/master_preferences_dummy.cc +++ b/chrome/installer/util/master_preferences_dummy.cc @@ -18,54 +18,37 @@ namespace installer_util { -bool GetDistroBooleanPreference(const DictionaryValue* prefs, - const std::string& name, - bool* value) { - // This function is called by InstallUtil::IsChromeFrameProcess() - // We return false because GetInstallPreferences returns an empty value below. - return false; +MasterPreferences::MasterPreferences(const CommandLine& cmd_line) + : distribution_(NULL), preferences_read_from_file_(false) { } -bool GetDistroIntegerPreference(const DictionaryValue* prefs, - const std::string& name, - int* value) { - NOTREACHED(); - return false; +MasterPreferences::MasterPreferences(const FilePath& prefs_path) + : distribution_(NULL), preferences_read_from_file_(false) { } -DictionaryValue* GetInstallPreferences(const CommandLine& cmd_line) { - // This function is called by InstallUtil::IsChromeFrameProcess() - // so we cannot make it NOTREACHED() - return new DictionaryValue();; -} - -DictionaryValue* ParseDistributionPreferences( - const FilePath& master_prefs_path) { - NOTREACHED(); - return NULL; +MasterPreferences::~MasterPreferences() { } -std::vector<GURL> GetFirstRunTabs(const DictionaryValue* prefs) { - NOTREACHED(); - return std::vector<GURL>(); +bool MasterPreferences::GetBool(const std::string& name, bool* value) const { + // This function is called by InstallUtil::IsChromeFrameProcess() + // We return false because GetInstallPreferences returns an empty value below. + return false; } -std::vector<GURL> GetDefaultBookmarks(const DictionaryValue* prefs) { +bool MasterPreferences::GetInt(const std::string& name, int* value) const { NOTREACHED(); - return std::vector<GURL>(); + return false; } -bool SetDistroBooleanPreference(DictionaryValue* prefs, - const std::string& name, - bool value) { +bool MasterPreferences::GetString(const std::string& name, + std::string* value) const { NOTREACHED(); return false; } -bool HasExtensionsBlock(const DictionaryValue* prefs, - DictionaryValue** extensions) { +std::vector<GURL> MasterPreferences::GetFirstRunTabs() const { NOTREACHED(); - return false; + return std::vector<GURL>(); } } diff --git a/chrome/installer/util/master_preferences_unittest.cc b/chrome/installer/util/master_preferences_unittest.cc index 87b8358..d1650fc 100644 --- a/chrome/installer/util/master_preferences_unittest.cc +++ b/chrome/installer/util/master_preferences_unittest.cc @@ -10,6 +10,7 @@ #include "chrome/common/chrome_paths.h" #include "chrome/common/json_value_serializer.h" #include "chrome/installer/util/master_preferences.h" +#include "chrome/installer/util/master_preferences_constants.h" #include "googleurl/src/gurl.h" #include "testing/gtest/include/gtest/gtest.h" @@ -29,6 +30,13 @@ class MasterPreferencesTest : public testing::Test { private: FilePath prefs_file_; }; + +// Used to specify an expected value for a set boolean preference variable. +struct ExpectedBooleans { + const char* name; + bool expected_value; +}; + } // namespace TEST_F(MasterPreferencesTest, ParseDistroParams) { @@ -61,76 +69,48 @@ TEST_F(MasterPreferencesTest, ParseDistroParams) { "} \n"; EXPECT_TRUE(file_util::WriteFile(prefs_file(), text, sizeof(text))); - scoped_ptr<DictionaryValue> prefs( - installer_util::ParseDistributionPreferences(prefs_file())); - EXPECT_TRUE(prefs.get() != NULL); - bool value = true; - EXPECT_TRUE(installer_util::GetDistroBooleanPreference(prefs.get(), - installer_util::master_preferences::kDistroSkipFirstRunPref, &value) && - value); - EXPECT_TRUE(installer_util::GetDistroBooleanPreference(prefs.get(), - installer_util::master_preferences::kDistroShowWelcomePage, &value) && - value); - - EXPECT_TRUE(installer_util::GetDistroBooleanPreference(prefs.get(), - installer_util::master_preferences::kDistroImportSearchPref, &value) && - value); - EXPECT_TRUE(installer_util::GetDistroBooleanPreference(prefs.get(), - installer_util::master_preferences::kDistroImportHistoryPref, &value) && - value); - EXPECT_TRUE(installer_util::GetDistroBooleanPreference(prefs.get(), - installer_util::master_preferences::kDistroImportBookmarksPref, &value) && - value); + installer_util::MasterPreferences prefs(prefs_file()); + + const char* expected_true[] = { + installer_util::master_preferences::kDistroSkipFirstRunPref, + installer_util::master_preferences::kDistroShowWelcomePage, + installer_util::master_preferences::kDistroImportSearchPref, + installer_util::master_preferences::kDistroImportHistoryPref, + installer_util::master_preferences::kDistroImportBookmarksPref, + installer_util::master_preferences::kDistroImportHomePagePref, + installer_util::master_preferences::kCreateAllShortcuts, + installer_util::master_preferences::kDoNotLaunchChrome, + installer_util::master_preferences::kMakeChromeDefault, + installer_util::master_preferences::kMakeChromeDefaultForUser, + installer_util::master_preferences::kSystemLevel, + installer_util::master_preferences::kVerboseLogging, + installer_util::master_preferences::kRequireEula, + installer_util::master_preferences::kAltShortcutText, + installer_util::master_preferences::kAltFirstRunBubble, + installer_util::master_preferences::kSearchEngineExperimentPref, + }; + + for (int i = 0; i < arraysize(expected_true); ++i) { + bool value = false; + EXPECT_TRUE(prefs.GetBool(expected_true[i], &value)); + EXPECT_TRUE(value) << expected_true[i]; + } + std::string str_value; - EXPECT_TRUE(installer_util::GetDistroStringPreference(prefs.get(), + EXPECT_TRUE(prefs.GetString( installer_util::master_preferences::kDistroImportBookmarksFromFilePref, &str_value)); EXPECT_STREQ("c:\\foo", str_value.c_str()); - EXPECT_TRUE(installer_util::GetDistroBooleanPreference(prefs.get(), - installer_util::master_preferences::kDistroImportHomePagePref, &value) && - value); - - EXPECT_TRUE(installer_util::GetDistroBooleanPreference(prefs.get(), - installer_util::master_preferences::kCreateAllShortcuts, &value) && - value); - EXPECT_TRUE(installer_util::GetDistroBooleanPreference(prefs.get(), - installer_util::master_preferences::kDoNotLaunchChrome, &value) && - value); - EXPECT_TRUE(installer_util::GetDistroBooleanPreference(prefs.get(), - installer_util::master_preferences::kMakeChromeDefault, &value) && - value); - EXPECT_TRUE(installer_util::GetDistroBooleanPreference(prefs.get(), - installer_util::master_preferences::kMakeChromeDefaultForUser, &value) && - value); - - EXPECT_TRUE(installer_util::GetDistroBooleanPreference(prefs.get(), - installer_util::master_preferences::kSystemLevel, &value) && - value); - EXPECT_TRUE(installer_util::GetDistroBooleanPreference(prefs.get(), - installer_util::master_preferences::kVerboseLogging, &value) && - value); - EXPECT_TRUE(installer_util::GetDistroBooleanPreference(prefs.get(), - installer_util::master_preferences::kRequireEula, &value) && - value); - EXPECT_TRUE(installer_util::GetDistroBooleanPreference(prefs.get(), - installer_util::master_preferences::kAltShortcutText, &value) && - value); - - EXPECT_TRUE(installer_util::GetDistroBooleanPreference(prefs.get(), - installer_util::master_preferences::kAltFirstRunBubble, &value) && - value); + int icon_index = 0; - EXPECT_TRUE(installer_util::GetDistroIntegerPreference(prefs.get(), + EXPECT_TRUE(prefs.GetInt( installer_util::master_preferences::kChromeShortcutIconIndex, &icon_index)); EXPECT_EQ(icon_index, 1); int ping_delay = 90; - EXPECT_TRUE(installer_util::GetDistroIntegerPreference(prefs.get(), - installer_util::master_preferences::kDistroPingDelay, &ping_delay)); + EXPECT_TRUE(prefs.GetInt(installer_util::master_preferences::kDistroPingDelay, + &ping_delay)); EXPECT_EQ(ping_delay, 40); - EXPECT_TRUE(installer_util::GetDistroBooleanPreference(prefs.get(), - installer_util::master_preferences::kSearchEngineExperimentPref, &value) && - value); } TEST_F(MasterPreferencesTest, ParseMissingDistroParams) { @@ -148,52 +128,48 @@ TEST_F(MasterPreferencesTest, ParseMissingDistroParams) { "} \n"; EXPECT_TRUE(file_util::WriteFile(prefs_file(), text, sizeof(text))); - scoped_ptr<DictionaryValue> prefs( - installer_util::ParseDistributionPreferences(prefs_file())); - EXPECT_TRUE(prefs.get() != NULL); + installer_util::MasterPreferences prefs(prefs_file()); + + ExpectedBooleans expected_bool[] = { + { installer_util::master_preferences::kDistroSkipFirstRunPref, true }, + { installer_util::master_preferences::kDistroImportSearchPref, true }, + { installer_util::master_preferences::kDistroImportBookmarksPref, false }, + { installer_util::master_preferences::kCreateAllShortcuts, true }, + { installer_util::master_preferences::kDoNotLaunchChrome, true }, + }; + bool value = false; - EXPECT_TRUE(installer_util::GetDistroBooleanPreference(prefs.get(), - installer_util::master_preferences::kDistroSkipFirstRunPref, &value) && - value); - EXPECT_FALSE(installer_util::GetDistroBooleanPreference(prefs.get(), - installer_util::master_preferences::kDistroShowWelcomePage, &value)); - - EXPECT_TRUE(installer_util::GetDistroBooleanPreference(prefs.get(), - installer_util::master_preferences::kDistroImportSearchPref, &value) && - value); - EXPECT_FALSE(installer_util::GetDistroBooleanPreference(prefs.get(), - installer_util::master_preferences::kDistroImportHistoryPref, &value)); - EXPECT_TRUE(installer_util::GetDistroBooleanPreference(prefs.get(), - installer_util::master_preferences::kDistroImportBookmarksPref, &value)); - EXPECT_FALSE(value); + for (int i = 0; i < arraysize(expected_bool); ++i) { + EXPECT_TRUE(prefs.GetBool(expected_bool[i].name, &value)); + EXPECT_EQ(value, expected_bool[i].expected_value) << expected_bool[i].name; + } + + const char* missing_bools[] = { + installer_util::master_preferences::kDistroShowWelcomePage, + installer_util::master_preferences::kDistroImportHistoryPref, + installer_util::master_preferences::kDistroImportHomePagePref, + installer_util::master_preferences::kDoNotRegisterForUpdateLaunch, + installer_util::master_preferences::kMakeChromeDefault, + installer_util::master_preferences::kMakeChromeDefaultForUser, + }; + + for (int i = 0; i < arraysize(missing_bools); ++i) { + EXPECT_FALSE(prefs.GetBool(missing_bools[i], &value)) << missing_bools[i]; + } + std::string str_value; - EXPECT_FALSE(installer_util::GetDistroStringPreference(prefs.get(), + EXPECT_FALSE(prefs.GetString( installer_util::master_preferences::kDistroImportBookmarksFromFilePref, &str_value)); - EXPECT_FALSE(installer_util::GetDistroBooleanPreference(prefs.get(), - installer_util::master_preferences::kDistroImportHomePagePref, &value)); - - EXPECT_TRUE(installer_util::GetDistroBooleanPreference(prefs.get(), - installer_util::master_preferences::kCreateAllShortcuts, &value) && - value); - EXPECT_TRUE(installer_util::GetDistroBooleanPreference(prefs.get(), - installer_util::master_preferences::kDoNotLaunchChrome, &value) && - value); - EXPECT_FALSE(installer_util::GetDistroBooleanPreference(prefs.get(), - installer_util::master_preferences::kDoNotRegisterForUpdateLaunch, - &value)); - EXPECT_FALSE(installer_util::GetDistroBooleanPreference(prefs.get(), - installer_util::master_preferences::kMakeChromeDefault, &value)); - EXPECT_FALSE(installer_util::GetDistroBooleanPreference(prefs.get(), - installer_util::master_preferences::kMakeChromeDefaultForUser, &value)); int icon_index = 0; - EXPECT_FALSE(installer_util::GetDistroIntegerPreference(prefs.get(), + EXPECT_FALSE(prefs.GetInt( installer_util::master_preferences::kChromeShortcutIconIndex, &icon_index)); EXPECT_EQ(icon_index, 0); + int ping_delay = 90; - EXPECT_FALSE(installer_util::GetDistroIntegerPreference(prefs.get(), + EXPECT_FALSE(prefs.GetInt( installer_util::master_preferences::kDistroPingDelay, &ping_delay)); EXPECT_EQ(ping_delay, 90); } @@ -212,12 +188,9 @@ TEST_F(MasterPreferencesTest, FirstRunTabs) { "} \n"; EXPECT_TRUE(file_util::WriteFile(prefs_file(), text, sizeof(text))); - scoped_ptr<DictionaryValue> prefs( - installer_util::ParseDistributionPreferences(prefs_file())); - EXPECT_TRUE(prefs.get() != NULL); - + installer_util::MasterPreferences prefs(prefs_file()); typedef std::vector<GURL> TabsVector; - TabsVector tabs = installer_util::GetFirstRunTabs(prefs.get()); + TabsVector tabs = prefs.GetFirstRunTabs(); ASSERT_EQ(3, tabs.size()); EXPECT_EQ(GURL("http://google.com/f1"), tabs[0]); EXPECT_EQ(GURL("https://google.com/f2"), tabs[1]); @@ -234,11 +207,9 @@ TEST(MasterPrefsExtension, ValidateExtensionJSON) { prefs_path = prefs_path.AppendASCII("extensions") .AppendASCII("good").AppendASCII("Preferences"); - scoped_ptr<DictionaryValue> prefs( - installer_util::ParseDistributionPreferences(prefs_path)); - ASSERT_TRUE(prefs.get() != NULL); + installer_util::MasterPreferences prefs(prefs_path); DictionaryValue* extensions = NULL; - EXPECT_TRUE(installer_util::HasExtensionsBlock(prefs.get(), &extensions)); + EXPECT_TRUE(prefs.GetExtensionsBlock(&extensions)); int location = 0; EXPECT_TRUE(extensions->GetInteger( "behllobkkfkfnphdnhnkndlbkcpglgmj.location", &location)); @@ -283,34 +254,24 @@ TEST_F(MasterPreferencesTest, GetInstallPreferencesTest) { cmd_str.append(L" --do-not-launch-chrome"); cmd_str.append(L" --alt-desktop-shortcut"); CommandLine cmd_line = CommandLine::FromString(cmd_str); - scoped_ptr<DictionaryValue> prefs( - installer_util::GetInstallPreferences(cmd_line)); - EXPECT_TRUE(prefs.get() != NULL); + installer_util::MasterPreferences prefs(cmd_line); // Check prefs that do not have any equivalent command line option. - bool value = false; - EXPECT_TRUE(installer_util::GetDistroBooleanPreference(prefs.get(), - installer_util::master_preferences::kDistroSkipFirstRunPref, &value) && - value); - EXPECT_FALSE(installer_util::GetDistroBooleanPreference(prefs.get(), - installer_util::master_preferences::kDistroShowWelcomePage, &value)); + ExpectedBooleans expected_bool[] = { + { installer_util::master_preferences::kDistroSkipFirstRunPref, true }, + { installer_util::master_preferences::kCreateAllShortcuts, true }, + { installer_util::master_preferences::kDoNotLaunchChrome, true }, + { installer_util::master_preferences::kAltShortcutText, true }, + { installer_util::master_preferences::kSystemLevel, true }, + { installer_util::master_preferences::kVerboseLogging, false }, + }; // Now check that prefs got merged correctly. - EXPECT_TRUE(installer_util::GetDistroBooleanPreference(prefs.get(), - installer_util::master_preferences::kCreateAllShortcuts, &value) && - value); - EXPECT_TRUE(installer_util::GetDistroBooleanPreference(prefs.get(), - installer_util::master_preferences::kDoNotLaunchChrome, &value) && - value); - EXPECT_TRUE(installer_util::GetDistroBooleanPreference(prefs.get(), - installer_util::master_preferences::kAltShortcutText, &value) && - value); - EXPECT_TRUE(installer_util::GetDistroBooleanPreference(prefs.get(), - installer_util::master_preferences::kSystemLevel, &value) && - value); - EXPECT_TRUE(installer_util::GetDistroBooleanPreference(prefs.get(), - installer_util::master_preferences::kVerboseLogging, &value)); - EXPECT_FALSE(value); + bool value = false; + for (int i = 0; i < arraysize(expected_bool); ++i) { + EXPECT_TRUE(prefs.GetBool(expected_bool[i].name, &value)); + EXPECT_EQ(value, expected_bool[i].expected_value) << expected_bool[i].name; + } // Delete temporary prefs file. EXPECT_TRUE(file_util::Delete(prefs_file, false)); @@ -320,19 +281,21 @@ TEST_F(MasterPreferencesTest, GetInstallPreferencesTest) { cmd_str = L"setup.exe --create-all-shortcuts --do-not-launch-chrome" L" --alt-desktop-shortcut"; cmd_line.ParseFromString(cmd_str); - prefs.reset(installer_util::GetInstallPreferences(cmd_line)); - EXPECT_TRUE(prefs.get() != NULL); - EXPECT_TRUE(installer_util::GetDistroBooleanPreference(prefs.get(), - installer_util::master_preferences::kCreateAllShortcuts, &value) && - value); - EXPECT_TRUE(installer_util::GetDistroBooleanPreference(prefs.get(), - installer_util::master_preferences::kDoNotLaunchChrome, &value) && - value); - EXPECT_TRUE(installer_util::GetDistroBooleanPreference(prefs.get(), - installer_util::master_preferences::kAltShortcutText, &value) && - value); - EXPECT_FALSE(installer_util::GetDistroBooleanPreference(prefs.get(), + installer_util::MasterPreferences prefs2(cmd_line); + ExpectedBooleans expected_bool2[] = { + { installer_util::master_preferences::kCreateAllShortcuts, true }, + { installer_util::master_preferences::kDoNotLaunchChrome, true }, + { installer_util::master_preferences::kAltShortcutText, true }, + }; + + for (int i = 0; i < arraysize(expected_bool2); ++i) { + EXPECT_TRUE(prefs2.GetBool(expected_bool2[i].name, &value)); + EXPECT_EQ(value, expected_bool2[i].expected_value) + << expected_bool2[i].name; + } + + EXPECT_FALSE(prefs2.GetBool( installer_util::master_preferences::kSystemLevel, &value)); - EXPECT_FALSE(installer_util::GetDistroBooleanPreference(prefs.get(), + EXPECT_FALSE(prefs2.GetBool( installer_util::master_preferences::kVerboseLogging, &value)); } diff --git a/chrome/installer/util/shell_util.cc b/chrome/installer/util/shell_util.cc index 3d2e7a6..947c8a0 100644 --- a/chrome/installer/util/shell_util.cc +++ b/chrome/installer/util/shell_util.cc @@ -30,6 +30,7 @@ #include "chrome/installer/util/browser_distribution.h" #include "chrome/installer/util/install_util.h" #include "chrome/installer/util/master_preferences.h" +#include "chrome/installer/util/master_preferences_constants.h" #include "installer_util_strings.h" @@ -783,12 +784,10 @@ bool ShellUtil::UpdateChromeShortcut(const std::wstring& chrome_exe, FilePath prefs_path(chrome_path); prefs_path = prefs_path.AppendASCII(installer_util::kDefaultMasterPrefs); - scoped_ptr<DictionaryValue> prefs( - installer_util::ParseDistributionPreferences(prefs_path)); + installer_util::MasterPreferences prefs(prefs_path); int icon_index = dist->GetIconIndex(); - installer_util::GetDistroIntegerPreference(prefs.get(), - installer_util::master_preferences::kChromeShortcutIconIndex, - &icon_index); + prefs.GetInt(installer_util::master_preferences::kChromeShortcutIconIndex, + &icon_index); if (create_new) { return file_util::CreateShortcutLink( chrome_exe.c_str(), // target |