| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/installer/util/master_preferences.h" | 5 #include "chrome/installer/util/master_preferences.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
| 10 #include "chrome/common/json_value_serializer.h" | 10 #include "chrome/common/json_value_serializer.h" |
| 11 | 11 |
| 12 namespace { | 12 namespace { |
| 13 | 13 |
| 14 DictionaryValue* ReadJSONPrefs(const std::string& data) { | 14 const wchar_t* kDistroDict = L"distribution"; |
| 15 JSONStringValueSerializer json(data); | 15 |
| 16 DictionaryValue* GetPrefsFromFile(const FilePath& master_prefs_path) { |
| 17 std::string json_data; |
| 18 if (!file_util::ReadFileToString(master_prefs_path, &json_data)) |
| 19 return NULL; |
| 20 |
| 21 JSONStringValueSerializer json(json_data); |
| 16 scoped_ptr<Value> root(json.Deserialize(NULL)); | 22 scoped_ptr<Value> root(json.Deserialize(NULL)); |
| 23 |
| 17 if (!root.get()) | 24 if (!root.get()) |
| 18 return NULL; | 25 return NULL; |
| 26 |
| 19 if (!root->IsType(Value::TYPE_DICTIONARY)) | 27 if (!root->IsType(Value::TYPE_DICTIONARY)) |
| 20 return NULL; | 28 return NULL; |
| 21 | 29 |
| 22 return static_cast<DictionaryValue*>(root.release()); | 30 return static_cast<DictionaryValue*>(root.release()); |
| 23 } | 31 } |
| 32 } // namespace |
| 24 | 33 |
| 25 DictionaryValue* GetPrefsFromFile(const std::wstring& master_prefs_path) { | 34 namespace installer_util { |
| 26 std::string json_data; | 35 namespace master_preferences { |
| 27 if (!file_util::ReadFileToString(master_prefs_path, &json_data)) | 36 const wchar_t kAltFirstRunBubble[] = L"oem_bubble"; |
| 28 return NULL; | 37 const wchar_t kAltShortcutText[] = L"alternate_shortcut_text"; |
| 29 return ReadJSONPrefs(json_data); | 38 const wchar_t kCreateAllShortcuts[] = L"create_all_shortcuts"; |
| 39 const wchar_t kDistroImportBookmarksPref[] = L"import_bookmarks"; |
| 40 const wchar_t kDistroImportHistoryPref[] = L"import_history"; |
| 41 const wchar_t kDistroImportHomePagePref[] = L"import_home_page"; |
| 42 const wchar_t kDistroImportSearchPref[] = L"import_search_engine"; |
| 43 const wchar_t kDistroPingDelay[] = L"ping_delay"; |
| 44 const wchar_t kDistroShowWelcomePage[] = L"show_welcome_page"; |
| 45 const wchar_t kDistroSkipFirstRunPref[] = L"skip_first_run_ui"; |
| 46 const wchar_t kDoNotLaunchChrome[] = L"do_not_launch_chrome"; |
| 47 const wchar_t kMakeChromeDefault[] = L"make_chrome_default"; |
| 48 const wchar_t kMakeChromeDefaultForUser[] = L"make_chrome_default_for_user"; |
| 49 const wchar_t kRequireEula[] = L"require_eula"; |
| 50 const wchar_t kSystemLevel[] = L"system_level"; |
| 51 const wchar_t kVerboseLogging[] = L"verbose_logging"; |
| 30 } | 52 } |
| 31 | 53 |
| 32 bool GetBooleanPref(const DictionaryValue* prefs, const std::wstring& name) { | 54 bool GetDistroBooleanPreference(const DictionaryValue* prefs, |
| 55 const std::wstring& name) { |
| 56 |
| 33 bool value = false; | 57 bool value = false; |
| 34 prefs->GetBoolean(name, &value); | 58 DictionaryValue* distro = NULL; |
| 59 if (prefs && prefs->GetDictionary(kDistroDict, &distro) && distro) |
| 60 distro->GetBoolean(name, &value); |
| 35 return value; | 61 return value; |
| 36 } | 62 } |
| 37 | 63 |
| 38 } // namespace | 64 bool GetDistributionPingDelay(const DictionaryValue* prefs, |
| 65 int* ping_delay) { |
| 66 if (!ping_delay) |
| 67 return false; |
| 39 | 68 |
| 40 namespace installer_util { | |
| 41 // All the preferences below are expected to be inside the JSON "distribution" | |
| 42 // block. See master_preferences.h for an example. | |
| 43 | |
| 44 // Boolean pref that triggers skipping the first run dialogs. | |
| 45 const wchar_t kDistroSkipFirstRunPref[] = L"skip_first_run_ui"; | |
| 46 // Boolean pref that triggers loading the welcome page. | |
| 47 const wchar_t kDistroShowWelcomePage[] = L"show_welcome_page"; | |
| 48 // Boolean pref that triggers silent import of the default search engine. | |
| 49 const wchar_t kDistroImportSearchPref[] = L"import_search_engine"; | |
| 50 // Boolean pref that triggers silent import of the default browser history. | |
| 51 const wchar_t kDistroImportHistoryPref[] = L"import_history"; | |
| 52 // Boolean pref that triggers silent import of the default browser bookmarks. | |
| 53 const wchar_t kDistroImportBookmarksPref[] = L"import_bookmarks"; | |
| 54 // RLZ ping delay in seconds | |
| 55 const wchar_t kDistroPingDelay[] = L"ping_delay"; | |
| 56 // Register Chrome as default browser for the current user. | |
| 57 const wchar_t kMakeChromeDefaultForUser[] = L"make_chrome_default_for_user"; | |
| 58 // The following boolean prefs have the same semantics as the corresponding | |
| 59 // setup command line switches. See chrome/installer/util/util_constants.cc | |
| 60 // for more info. | |
| 61 // Create Desktop and QuickLaunch shortcuts. | |
| 62 const wchar_t kCreateAllShortcuts[] = L"create_all_shortcuts"; | |
| 63 // Prevent installer from launching Chrome after a successful first install. | |
| 64 const wchar_t kDoNotLaunchChrome[] = L"do_not_launch_chrome"; | |
| 65 // Register Chrome as default browser on the system. | |
| 66 const wchar_t kMakeChromeDefault[] = L"make_chrome_default"; | |
| 67 // Install Chrome to system wise location. | |
| 68 const wchar_t kSystemLevel[] = L"system_level"; | |
| 69 // Run installer in verbose mode. | |
| 70 const wchar_t kVerboseLogging[] = L"verbose_logging"; | |
| 71 // Show EULA dialog and install only if accepted. | |
| 72 const wchar_t kRequireEula[] = L"require_eula"; | |
| 73 // Use alternate shortcut text for the main shortcut. | |
| 74 const wchar_t kAltShortcutText[] = L"alternate_shortcut_text"; | |
| 75 // Use alternate smaller first run info bubble. | |
| 76 const wchar_t kAltFirstRunBubble[] = L"oem_bubble"; | |
| 77 // Boolean pref that triggers silent import of the default browser homepage. | |
| 78 const wchar_t kDistroImportHomePagePref[] = L"import_home_page"; | |
| 79 | |
| 80 bool GetDistributionPingDelay(const FilePath& master_prefs_path, | |
| 81 int& delay) { | |
| 82 // 90 seconds is the default that we want to use in case master preferences | 69 // 90 seconds is the default that we want to use in case master preferences |
| 83 // is missing or corrupt. | 70 // is missing or corrupt. |
| 84 delay = 90; | 71 *ping_delay = 90; |
| 85 FilePath master_prefs = master_prefs_path; | |
| 86 if (master_prefs.empty()) { | |
| 87 if (!PathService::Get(base::DIR_EXE, &master_prefs)) | |
| 88 return false; | |
| 89 master_prefs = master_prefs.Append(installer_util::kDefaultMasterPrefs); | |
| 90 } | |
| 91 | 72 |
| 92 if (!file_util::PathExists(master_prefs)) | 73 DictionaryValue* distro = NULL; |
| 74 if (!prefs || !prefs->GetDictionary(kDistroDict, &distro) || !distro) |
| 93 return false; | 75 return false; |
| 94 | 76 |
| 95 scoped_ptr<DictionaryValue> json_root( | 77 if (!distro->GetInteger(master_preferences::kDistroPingDelay, ping_delay)) |
| 96 GetPrefsFromFile(master_prefs.ToWStringHack())); | |
| 97 if (!json_root.get()) | |
| 98 return false; | |
| 99 | |
| 100 DictionaryValue* distro = NULL; | |
| 101 if (!json_root->GetDictionary(L"distribution", &distro) || | |
| 102 !distro->GetInteger(kDistroPingDelay, &delay)) | |
| 103 return false; | 78 return false; |
| 104 | 79 |
| 105 return true; | 80 return true; |
| 106 } | 81 } |
| 107 | 82 |
| 108 int ParseDistributionPreferences(const std::wstring& master_prefs_path) { | 83 DictionaryValue* ParseDistributionPreferences( |
| 109 if (!file_util::PathExists(master_prefs_path)) | 84 const FilePath& master_prefs_path) { |
| 110 return MASTER_PROFILE_NOT_FOUND; | 85 if (!file_util::PathExists(master_prefs_path)) { |
| 111 LOG(INFO) << "master profile found"; | 86 LOG(WARNING) << "Master preferences file not found: " |
| 87 << master_prefs_path.value(); |
| 88 return NULL; |
| 89 } |
| 112 | 90 |
| 113 scoped_ptr<DictionaryValue> json_root(GetPrefsFromFile(master_prefs_path)); | 91 return GetPrefsFromFile(master_prefs_path); |
| 114 if (!json_root.get()) | |
| 115 return MASTER_PROFILE_ERROR; | |
| 116 | |
| 117 int parse_result = 0; | |
| 118 DictionaryValue* distro = NULL; | |
| 119 if (json_root->GetDictionary(L"distribution", &distro)) { | |
| 120 if (GetBooleanPref(distro, kDistroSkipFirstRunPref)) | |
| 121 parse_result |= MASTER_PROFILE_NO_FIRST_RUN_UI; | |
| 122 if (GetBooleanPref(distro, kDistroShowWelcomePage)) | |
| 123 parse_result |= MASTER_PROFILE_SHOW_WELCOME; | |
| 124 if (GetBooleanPref(distro, kDistroImportSearchPref)) | |
| 125 parse_result |= MASTER_PROFILE_IMPORT_SEARCH_ENGINE; | |
| 126 if (GetBooleanPref(distro, kDistroImportHistoryPref)) | |
| 127 parse_result |= MASTER_PROFILE_IMPORT_HISTORY; | |
| 128 if (GetBooleanPref(distro, kDistroImportBookmarksPref)) | |
| 129 parse_result |= MASTER_PROFILE_IMPORT_BOOKMARKS; | |
| 130 if (GetBooleanPref(distro, kDistroImportHomePagePref)) | |
| 131 parse_result |= MASTER_PROFILE_IMPORT_HOME_PAGE; | |
| 132 if (GetBooleanPref(distro, kMakeChromeDefaultForUser)) | |
| 133 parse_result |= MASTER_PROFILE_MAKE_CHROME_DEFAULT_FOR_USER; | |
| 134 if (GetBooleanPref(distro, kCreateAllShortcuts)) | |
| 135 parse_result |= MASTER_PROFILE_CREATE_ALL_SHORTCUTS; | |
| 136 if (GetBooleanPref(distro, kDoNotLaunchChrome)) | |
| 137 parse_result |= MASTER_PROFILE_DO_NOT_LAUNCH_CHROME; | |
| 138 if (GetBooleanPref(distro, kMakeChromeDefault)) | |
| 139 parse_result |= MASTER_PROFILE_MAKE_CHROME_DEFAULT; | |
| 140 if (GetBooleanPref(distro, kSystemLevel)) | |
| 141 parse_result |= MASTER_PROFILE_SYSTEM_LEVEL; | |
| 142 if (GetBooleanPref(distro, kVerboseLogging)) | |
| 143 parse_result |= MASTER_PROFILE_VERBOSE_LOGGING; | |
| 144 if (GetBooleanPref(distro, kRequireEula)) | |
| 145 parse_result |= MASTER_PROFILE_REQUIRE_EULA; | |
| 146 if (GetBooleanPref(distro, kAltShortcutText)) | |
| 147 parse_result |= MASTER_PROFILE_ALT_SHORTCUT_TXT; | |
| 148 if (GetBooleanPref(distro, kAltFirstRunBubble)) | |
| 149 parse_result |= MASTER_PROFILE_OEM_FIRST_RUN_BUBBLE; | |
| 150 } | |
| 151 return parse_result; | |
| 152 } | 92 } |
| 153 | 93 |
| 154 std::vector<std::wstring> ParseFirstRunTabs( | 94 std::vector<std::wstring> GetFirstRunTabs(const DictionaryValue* prefs) { |
| 155 const std::wstring& master_prefs_path) { | |
| 156 std::vector<std::wstring> launch_tabs; | 95 std::vector<std::wstring> launch_tabs; |
| 157 scoped_ptr<DictionaryValue> json_root(GetPrefsFromFile(master_prefs_path)); | 96 if (!prefs) |
| 158 if (!json_root.get()) | |
| 159 return launch_tabs; | 97 return launch_tabs; |
| 160 ListValue* tabs_list = NULL; | 98 ListValue* tabs_list = NULL; |
| 161 if (!json_root->GetList(L"first_run_tabs", &tabs_list)) | 99 if (!prefs->GetList(L"first_run_tabs", &tabs_list)) |
| 162 return launch_tabs; | 100 return launch_tabs; |
| 163 for (size_t i = 0; i < tabs_list->GetSize(); ++i) { | 101 for (size_t i = 0; i < tabs_list->GetSize(); ++i) { |
| 164 Value* entry; | 102 Value* entry; |
| 165 std::wstring tab_entry; | 103 std::wstring tab_entry; |
| 166 if (!tabs_list->Get(i, &entry) || !entry->GetAsString(&tab_entry)) { | 104 if (!tabs_list->Get(i, &entry) || !entry->GetAsString(&tab_entry)) { |
| 167 NOTREACHED(); | 105 NOTREACHED(); |
| 168 break; | 106 break; |
| 169 } | 107 } |
| 170 launch_tabs.push_back(tab_entry); | 108 launch_tabs.push_back(tab_entry); |
| 171 } | 109 } |
| 172 return launch_tabs; | 110 return launch_tabs; |
| 173 } | 111 } |
| 174 | 112 |
| 113 bool SetDistroBooleanPreference(DictionaryValue* prefs, |
| 114 const std::wstring& name, |
| 115 bool value) { |
| 116 |
| 117 bool ret = false; |
| 118 if (prefs && !name.empty()) { |
| 119 std::wstring key(kDistroDict); |
| 120 key.append(L"." + name); |
| 121 if (prefs->SetBoolean(key, value)) |
| 122 ret = true; |
| 123 } |
| 124 return ret; |
| 125 } |
| 126 |
| 175 } // installer_util | 127 } // installer_util |
| OLD | NEW |