diff options
author | kuchhal@chromium.org <kuchhal@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-08-03 17:46:05 +0000 |
---|---|---|
committer | kuchhal@chromium.org <kuchhal@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-08-03 17:46:05 +0000 |
commit | 6752d476fbb54c3dd00343c7b194c23620c3d1e5 (patch) | |
tree | 77afb9a9f6a819e509f0a223f3b1e77012b9d47b /chrome/installer/util/master_preferences.cc | |
parent | bf0791a666425e2e61400953fa538ed1d2b2edf8 (diff) | |
download | chromium_src-6752d476fbb54c3dd00343c7b194c23620c3d1e5.zip chromium_src-6752d476fbb54c3dd00343c7b194c23620c3d1e5.tar.gz chromium_src-6752d476fbb54c3dd00343c7b194c23620c3d1e5.tar.bz2 |
Refactoring of master preferences parsing before adding a new preference.
Currently we are parsing master preferences file three time on startup. Since we only return an int bit mask flag after parsing preferences, it can not handle any preference other than boolean and we end up reading it again for first run tabs and ping delay. This change refactors all the preferences parsing logic to directly pass around DictionaryValue object around in Chrome as well as installer.
No functional change but this will make adding a new preference for new icon more logical since we will not read the preferences file once again.
BUG=12701
TEST=Make sure all the distribution preferences still work as before.
Review URL: http://codereview.chromium.org/159539
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@22284 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/installer/util/master_preferences.cc')
-rw-r--r-- | chrome/installer/util/master_preferences.cc | 180 |
1 files changed, 66 insertions, 114 deletions
diff --git a/chrome/installer/util/master_preferences.cc b/chrome/installer/util/master_preferences.cc index 8a7c637..f8c8018 100644 --- a/chrome/installer/util/master_preferences.cc +++ b/chrome/installer/util/master_preferences.cc @@ -11,154 +11,92 @@ namespace { -DictionaryValue* ReadJSONPrefs(const std::string& data) { - JSONStringValueSerializer json(data); +const wchar_t* kDistroDict = L"distribution"; + +DictionaryValue* GetPrefsFromFile(const FilePath& master_prefs_path) { + std::string json_data; + if (!file_util::ReadFileToString(master_prefs_path, &json_data)) + return NULL; + + JSONStringValueSerializer json(json_data); scoped_ptr<Value> root(json.Deserialize(NULL)); + if (!root.get()) return NULL; + if (!root->IsType(Value::TYPE_DICTIONARY)) return NULL; return static_cast<DictionaryValue*>(root.release()); } - -DictionaryValue* GetPrefsFromFile(const std::wstring& master_prefs_path) { - std::string json_data; - if (!file_util::ReadFileToString(master_prefs_path, &json_data)) - return NULL; - return ReadJSONPrefs(json_data); -} - -bool GetBooleanPref(const DictionaryValue* prefs, const std::wstring& name) { - bool value = false; - prefs->GetBoolean(name, &value); - return value; -} - } // namespace namespace installer_util { -// All the preferences below are expected to be inside the JSON "distribution" -// block. See master_preferences.h for an example. - -// Boolean pref that triggers skipping the first run dialogs. -const wchar_t kDistroSkipFirstRunPref[] = L"skip_first_run_ui"; -// Boolean pref that triggers loading the welcome page. -const wchar_t kDistroShowWelcomePage[] = L"show_welcome_page"; -// Boolean pref that triggers silent import of the default search engine. -const wchar_t kDistroImportSearchPref[] = L"import_search_engine"; -// Boolean pref that triggers silent import of the default browser history. -const wchar_t kDistroImportHistoryPref[] = L"import_history"; -// Boolean pref that triggers silent import of the default browser bookmarks. +namespace master_preferences { +const wchar_t kAltFirstRunBubble[] = L"oem_bubble"; +const wchar_t kAltShortcutText[] = L"alternate_shortcut_text"; +const wchar_t kCreateAllShortcuts[] = L"create_all_shortcuts"; const wchar_t kDistroImportBookmarksPref[] = L"import_bookmarks"; -// RLZ ping delay in seconds +const wchar_t kDistroImportHistoryPref[] = L"import_history"; +const wchar_t kDistroImportHomePagePref[] = L"import_home_page"; +const wchar_t kDistroImportSearchPref[] = L"import_search_engine"; const wchar_t kDistroPingDelay[] = L"ping_delay"; -// Register Chrome as default browser for the current user. -const wchar_t kMakeChromeDefaultForUser[] = L"make_chrome_default_for_user"; -// The following boolean prefs have the same semantics as the corresponding -// setup command line switches. See chrome/installer/util/util_constants.cc -// for more info. -// Create Desktop and QuickLaunch shortcuts. -const wchar_t kCreateAllShortcuts[] = L"create_all_shortcuts"; -// Prevent installer from launching Chrome after a successful first install. +const wchar_t kDistroShowWelcomePage[] = L"show_welcome_page"; +const wchar_t kDistroSkipFirstRunPref[] = L"skip_first_run_ui"; const wchar_t kDoNotLaunchChrome[] = L"do_not_launch_chrome"; -// Register Chrome as default browser on the system. const wchar_t kMakeChromeDefault[] = L"make_chrome_default"; -// Install Chrome to system wise location. +const wchar_t kMakeChromeDefaultForUser[] = L"make_chrome_default_for_user"; +const wchar_t kRequireEula[] = L"require_eula"; const wchar_t kSystemLevel[] = L"system_level"; -// Run installer in verbose mode. const wchar_t kVerboseLogging[] = L"verbose_logging"; -// Show EULA dialog and install only if accepted. -const wchar_t kRequireEula[] = L"require_eula"; -// Use alternate shortcut text for the main shortcut. -const wchar_t kAltShortcutText[] = L"alternate_shortcut_text"; -// Use alternate smaller first run info bubble. -const wchar_t kAltFirstRunBubble[] = L"oem_bubble"; -// Boolean pref that triggers silent import of the default browser homepage. -const wchar_t kDistroImportHomePagePref[] = L"import_home_page"; +} -bool GetDistributionPingDelay(const FilePath& master_prefs_path, - int& delay) { - // 90 seconds is the default that we want to use in case master preferences - // is missing or corrupt. - delay = 90; - FilePath master_prefs = master_prefs_path; - if (master_prefs.empty()) { - if (!PathService::Get(base::DIR_EXE, &master_prefs)) - return false; - master_prefs = master_prefs.Append(installer_util::kDefaultMasterPrefs); - } +bool GetDistroBooleanPreference(const DictionaryValue* prefs, + const std::wstring& name) { - if (!file_util::PathExists(master_prefs)) - return false; + bool value = false; + DictionaryValue* distro = NULL; + if (prefs && prefs->GetDictionary(kDistroDict, &distro) && distro) + distro->GetBoolean(name, &value); + return value; +} - scoped_ptr<DictionaryValue> json_root( - GetPrefsFromFile(master_prefs.ToWStringHack())); - if (!json_root.get()) +bool GetDistributionPingDelay(const DictionaryValue* prefs, + int* ping_delay) { + if (!ping_delay) return false; + // 90 seconds is the default that we want to use in case master preferences + // is missing or corrupt. + *ping_delay = 90; + DictionaryValue* distro = NULL; - if (!json_root->GetDictionary(L"distribution", &distro) || - !distro->GetInteger(kDistroPingDelay, &delay)) + if (!prefs || !prefs->GetDictionary(kDistroDict, &distro) || !distro) + return false; + + if (!distro->GetInteger(master_preferences::kDistroPingDelay, ping_delay)) return false; return true; } -int ParseDistributionPreferences(const std::wstring& master_prefs_path) { - if (!file_util::PathExists(master_prefs_path)) - return MASTER_PROFILE_NOT_FOUND; - LOG(INFO) << "master profile found"; - - scoped_ptr<DictionaryValue> json_root(GetPrefsFromFile(master_prefs_path)); - if (!json_root.get()) - return MASTER_PROFILE_ERROR; - - int parse_result = 0; - DictionaryValue* distro = NULL; - if (json_root->GetDictionary(L"distribution", &distro)) { - if (GetBooleanPref(distro, kDistroSkipFirstRunPref)) - parse_result |= MASTER_PROFILE_NO_FIRST_RUN_UI; - if (GetBooleanPref(distro, kDistroShowWelcomePage)) - parse_result |= MASTER_PROFILE_SHOW_WELCOME; - if (GetBooleanPref(distro, kDistroImportSearchPref)) - parse_result |= MASTER_PROFILE_IMPORT_SEARCH_ENGINE; - if (GetBooleanPref(distro, kDistroImportHistoryPref)) - parse_result |= MASTER_PROFILE_IMPORT_HISTORY; - if (GetBooleanPref(distro, kDistroImportBookmarksPref)) - parse_result |= MASTER_PROFILE_IMPORT_BOOKMARKS; - if (GetBooleanPref(distro, kDistroImportHomePagePref)) - parse_result |= MASTER_PROFILE_IMPORT_HOME_PAGE; - if (GetBooleanPref(distro, kMakeChromeDefaultForUser)) - parse_result |= MASTER_PROFILE_MAKE_CHROME_DEFAULT_FOR_USER; - if (GetBooleanPref(distro, kCreateAllShortcuts)) - parse_result |= MASTER_PROFILE_CREATE_ALL_SHORTCUTS; - if (GetBooleanPref(distro, kDoNotLaunchChrome)) - parse_result |= MASTER_PROFILE_DO_NOT_LAUNCH_CHROME; - if (GetBooleanPref(distro, kMakeChromeDefault)) - parse_result |= MASTER_PROFILE_MAKE_CHROME_DEFAULT; - if (GetBooleanPref(distro, kSystemLevel)) - parse_result |= MASTER_PROFILE_SYSTEM_LEVEL; - if (GetBooleanPref(distro, kVerboseLogging)) - parse_result |= MASTER_PROFILE_VERBOSE_LOGGING; - if (GetBooleanPref(distro, kRequireEula)) - parse_result |= MASTER_PROFILE_REQUIRE_EULA; - if (GetBooleanPref(distro, kAltShortcutText)) - parse_result |= MASTER_PROFILE_ALT_SHORTCUT_TXT; - if (GetBooleanPref(distro, kAltFirstRunBubble)) - parse_result |= MASTER_PROFILE_OEM_FIRST_RUN_BUBBLE; +DictionaryValue* ParseDistributionPreferences( + const FilePath& master_prefs_path) { + if (!file_util::PathExists(master_prefs_path)) { + LOG(WARNING) << "Master preferences file not found: " + << master_prefs_path.value(); + return NULL; } - return parse_result; + + return GetPrefsFromFile(master_prefs_path); } -std::vector<std::wstring> ParseFirstRunTabs( - const std::wstring& master_prefs_path) { +std::vector<std::wstring> GetFirstRunTabs(const DictionaryValue* prefs) { std::vector<std::wstring> launch_tabs; - scoped_ptr<DictionaryValue> json_root(GetPrefsFromFile(master_prefs_path)); - if (!json_root.get()) + if (!prefs) return launch_tabs; ListValue* tabs_list = NULL; - if (!json_root->GetList(L"first_run_tabs", &tabs_list)) + if (!prefs->GetList(L"first_run_tabs", &tabs_list)) return launch_tabs; for (size_t i = 0; i < tabs_list->GetSize(); ++i) { Value* entry; @@ -172,4 +110,18 @@ std::vector<std::wstring> ParseFirstRunTabs( return launch_tabs; } +bool SetDistroBooleanPreference(DictionaryValue* prefs, + const std::wstring& name, + bool value) { + + bool ret = false; + if (prefs && !name.empty()) { + std::wstring key(kDistroDict); + key.append(L"." + name); + if (prefs->SetBoolean(key, value)) + ret = true; + } + return ret; +} + } // installer_util |