Index: chrome/installer/util/master_preferences.cc |
=================================================================== |
--- chrome/installer/util/master_preferences.cc (revision 64604) |
+++ chrome/installer/util/master_preferences.cc (working copy) |
@@ -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 @@ |
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 @@ |
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 |
} |
-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(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 HasExtensionsBlock(const DictionaryValue* prefs, |
- DictionaryValue** extensions) { |
- return (prefs->GetDictionary(master_preferences::kExtensionsBlock, |
- extensions)); |
+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 MasterPreferences::GetExtensionsBlock(DictionaryValue** extensions) const { |
+ return master_dictionary_->GetDictionary( |
+ master_preferences::kExtensionsBlock, extensions); |
+} |
+ |
} // installer_util |