[sql] Remove _HAS_EXCEPTIONS=0 from build info.
[chromium-blink-merge.git] / chrome / installer / util / master_preferences.cc
blob08d1de3266af160bf5e13796f8a842ffb862b759
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.
5 #include "chrome/installer/util/master_preferences.h"
7 #include "base/environment.h"
8 #include "base/files/file_util.h"
9 #include "base/json/json_string_value_serializer.h"
10 #include "base/lazy_instance.h"
11 #include "base/logging.h"
12 #include "base/strings/string_util.h"
13 #include "chrome/common/env_vars.h"
14 #include "chrome/common/pref_names.h"
15 #include "chrome/installer/util/master_preferences_constants.h"
16 #include "chrome/installer/util/util_constants.h"
18 namespace {
20 const char kFirstRunTabs[] = "first_run_tabs";
22 base::LazyInstance<installer::MasterPreferences> g_master_preferences =
23 LAZY_INSTANCE_INITIALIZER;
25 bool GetURLFromValue(const base::Value* in_value, std::string* out_value) {
26 return in_value && out_value && in_value->GetAsString(out_value);
29 std::vector<std::string> GetNamedList(const char* name,
30 const base::DictionaryValue* prefs) {
31 std::vector<std::string> list;
32 if (!prefs)
33 return list;
35 const base::ListValue* value_list = NULL;
36 if (!prefs->GetList(name, &value_list))
37 return list;
39 list.reserve(value_list->GetSize());
40 for (size_t i = 0; i < value_list->GetSize(); ++i) {
41 const base::Value* entry;
42 std::string url_entry;
43 if (!value_list->Get(i, &entry) || !GetURLFromValue(entry, &url_entry)) {
44 NOTREACHED();
45 break;
47 list.push_back(url_entry);
49 return list;
52 base::DictionaryValue* ParseDistributionPreferences(
53 const std::string& json_data) {
54 JSONStringValueDeserializer json(json_data);
55 std::string error;
56 scoped_ptr<base::Value> root(json.Deserialize(NULL, &error));
57 if (!root.get()) {
58 LOG(WARNING) << "Failed to parse master prefs file: " << error;
59 return NULL;
61 if (!root->IsType(base::Value::TYPE_DICTIONARY)) {
62 LOG(WARNING) << "Failed to parse master prefs file: "
63 << "Root item must be a dictionary.";
64 return NULL;
66 return static_cast<base::DictionaryValue*>(root.release());
69 } // namespace
71 namespace installer {
73 MasterPreferences::MasterPreferences() : distribution_(NULL),
74 preferences_read_from_file_(false),
75 chrome_(true),
76 multi_install_(false) {
77 InitializeFromCommandLine(*base::CommandLine::ForCurrentProcess());
80 MasterPreferences::MasterPreferences(const base::CommandLine& cmd_line)
81 : distribution_(NULL),
82 preferences_read_from_file_(false),
83 chrome_(true),
84 multi_install_(false) {
85 InitializeFromCommandLine(cmd_line);
88 MasterPreferences::MasterPreferences(const base::FilePath& prefs_path)
89 : distribution_(NULL),
90 preferences_read_from_file_(false),
91 chrome_(true),
92 multi_install_(false) {
93 InitializeFromFilePath(prefs_path);
96 MasterPreferences::MasterPreferences(const std::string& prefs)
97 : distribution_(NULL),
98 preferences_read_from_file_(false),
99 chrome_(true),
100 multi_install_(false) {
101 InitializeFromString(prefs);
104 MasterPreferences::~MasterPreferences() {
107 void MasterPreferences::InitializeFromCommandLine(
108 const base::CommandLine& cmd_line) {
109 #if defined(OS_WIN)
110 if (cmd_line.HasSwitch(installer::switches::kInstallerData)) {
111 base::FilePath prefs_path(cmd_line.GetSwitchValuePath(
112 installer::switches::kInstallerData));
113 InitializeFromFilePath(prefs_path);
114 } else {
115 master_dictionary_.reset(new base::DictionaryValue());
118 DCHECK(master_dictionary_.get());
120 // A simple map from command line switches to equivalent switches in the
121 // distribution dictionary. Currently all switches added will be set to
122 // 'true'.
123 static const struct CmdLineSwitchToDistributionSwitch {
124 const char* cmd_line_switch;
125 const char* distribution_switch;
126 } translate_switches[] = {
127 { installer::switches::kAutoLaunchChrome,
128 installer::master_preferences::kAutoLaunchChrome },
129 { installer::switches::kChrome,
130 installer::master_preferences::kChrome },
131 { installer::switches::kDisableLogging,
132 installer::master_preferences::kDisableLogging },
133 { installer::switches::kMsi,
134 installer::master_preferences::kMsi },
135 { installer::switches::kMultiInstall,
136 installer::master_preferences::kMultiInstall },
137 { installer::switches::kDoNotRegisterForUpdateLaunch,
138 installer::master_preferences::kDoNotRegisterForUpdateLaunch },
139 { installer::switches::kDoNotLaunchChrome,
140 installer::master_preferences::kDoNotLaunchChrome },
141 { installer::switches::kMakeChromeDefault,
142 installer::master_preferences::kMakeChromeDefault },
143 { installer::switches::kSystemLevel,
144 installer::master_preferences::kSystemLevel },
145 { installer::switches::kVerboseLogging,
146 installer::master_preferences::kVerboseLogging },
149 std::string name(installer::master_preferences::kDistroDict);
150 for (int i = 0; i < arraysize(translate_switches); ++i) {
151 if (cmd_line.HasSwitch(translate_switches[i].cmd_line_switch)) {
152 name.assign(installer::master_preferences::kDistroDict);
153 name.append(".").append(translate_switches[i].distribution_switch);
154 master_dictionary_->SetBoolean(name, true);
158 // See if the log file path was specified on the command line.
159 std::wstring str_value(cmd_line.GetSwitchValueNative(
160 installer::switches::kLogFile));
161 if (!str_value.empty()) {
162 name.assign(installer::master_preferences::kDistroDict);
163 name.append(".").append(installer::master_preferences::kLogFile);
164 master_dictionary_->SetString(name, str_value);
167 // Handle the special case of --system-level being implied by the presence of
168 // the kGoogleUpdateIsMachineEnvVar environment variable.
169 scoped_ptr<base::Environment> env(base::Environment::Create());
170 if (env != NULL) {
171 std::string is_machine_var;
172 env->GetVar(env_vars::kGoogleUpdateIsMachineEnvVar, &is_machine_var);
173 if (!is_machine_var.empty() && is_machine_var[0] == '1') {
174 VLOG(1) << "Taking system-level from environment.";
175 name.assign(installer::master_preferences::kDistroDict);
176 name.append(".").append(installer::master_preferences::kSystemLevel);
177 master_dictionary_->SetBoolean(name, true);
181 // Cache a pointer to the distribution dictionary. Ignore errors if any.
182 master_dictionary_->GetDictionary(installer::master_preferences::kDistroDict,
183 &distribution_);
185 InitializeProductFlags();
186 #endif
189 void MasterPreferences::InitializeFromFilePath(
190 const base::FilePath& prefs_path) {
191 std::string json_data;
192 // Failure to read the file is ignored as |json_data| will be the empty string
193 // and the remainder of this MasterPreferences object should still be
194 // initialized as best as possible.
195 if (base::PathExists(prefs_path) &&
196 !base::ReadFileToString(prefs_path, &json_data)) {
197 LOG(ERROR) << "Failed to read preferences from " << prefs_path.value();
199 if (InitializeFromString(json_data))
200 preferences_read_from_file_ = true;
203 bool MasterPreferences::InitializeFromString(const std::string& json_data) {
204 if (!json_data.empty())
205 master_dictionary_.reset(ParseDistributionPreferences(json_data));
207 bool data_is_valid = true;
208 if (!master_dictionary_.get()) {
209 master_dictionary_.reset(new base::DictionaryValue());
210 data_is_valid = false;
211 } else {
212 // Cache a pointer to the distribution dictionary.
213 master_dictionary_->GetDictionary(
214 installer::master_preferences::kDistroDict, &distribution_);
217 InitializeProductFlags();
218 EnforceLegacyPreferences();
219 return data_is_valid;
222 void MasterPreferences::InitializeProductFlags() {
223 // Make sure we start out with the correct defaults.
224 multi_install_ = false;
225 chrome_ = true;
227 GetBool(installer::master_preferences::kMultiInstall, &multi_install_);
229 // When multi-install is specified, the checks are pretty simple (in theory):
230 // In order to be installed/uninstalled, each product must have its switch
231 // present on the command line.
232 // When multi-install is not set, operate on Chrome.
233 if (multi_install_) {
234 if (!GetBool(installer::master_preferences::kChrome, &chrome_))
235 chrome_ = false;
236 } else {
237 chrome_ = true;
241 void MasterPreferences::EnforceLegacyPreferences() {
242 // If create_all_shortcuts was explicitly set to false, set
243 // do_not_create_(desktop|quick_launch)_shortcut to true.
244 bool create_all_shortcuts = true;
245 GetBool(installer::master_preferences::kCreateAllShortcuts,
246 &create_all_shortcuts);
247 if (!create_all_shortcuts) {
248 distribution_->SetBoolean(
249 installer::master_preferences::kDoNotCreateDesktopShortcut, true);
250 distribution_->SetBoolean(
251 installer::master_preferences::kDoNotCreateQuickLaunchShortcut, true);
254 // If there is no entry for kURLsToRestoreOnStartup and there is one for
255 // kURLsToRestoreOnStartupOld, copy the old to the new.
256 const base::ListValue* startup_urls_list = NULL;
257 if (master_dictionary_ &&
258 !master_dictionary_->GetList(prefs::kURLsToRestoreOnStartup, NULL) &&
259 master_dictionary_->GetList(prefs::kURLsToRestoreOnStartupOld,
260 &startup_urls_list) &&
261 startup_urls_list) {
262 base::ListValue* new_startup_urls_list = startup_urls_list->DeepCopy();
263 master_dictionary_->Set(prefs::kURLsToRestoreOnStartup,
264 new_startup_urls_list);
268 bool MasterPreferences::GetBool(const std::string& name, bool* value) const {
269 bool ret = false;
270 if (distribution_)
271 ret = distribution_->GetBoolean(name, value);
272 return ret;
275 bool MasterPreferences::GetInt(const std::string& name, int* value) const {
276 bool ret = false;
277 if (distribution_)
278 ret = distribution_->GetInteger(name, value);
279 return ret;
282 bool MasterPreferences::GetString(const std::string& name,
283 std::string* value) const {
284 bool ret = false;
285 if (distribution_)
286 ret = (distribution_->GetString(name, value) && !value->empty());
287 return ret;
290 std::vector<std::string> MasterPreferences::GetFirstRunTabs() const {
291 return GetNamedList(kFirstRunTabs, master_dictionary_.get());
294 bool MasterPreferences::GetExtensionsBlock(
295 base::DictionaryValue** extensions) const {
296 return master_dictionary_->GetDictionary(
297 master_preferences::kExtensionsBlock, extensions);
300 std::string MasterPreferences::GetCompressedVariationsSeed() const {
301 return ExtractPrefString(prefs::kVariationsCompressedSeed);
304 std::string MasterPreferences::GetVariationsSeed() const {
305 return ExtractPrefString(prefs::kVariationsSeed);
308 std::string MasterPreferences::GetVariationsSeedSignature() const {
309 return ExtractPrefString(prefs::kVariationsSeedSignature);
312 std::string MasterPreferences::ExtractPrefString(
313 const std::string& name) const {
314 std::string result;
315 scoped_ptr<base::Value> pref_value;
316 if (master_dictionary_->Remove(name, &pref_value)) {
317 if (!pref_value->GetAsString(&result))
318 NOTREACHED();
320 return result;
323 // static
324 const MasterPreferences& MasterPreferences::ForCurrentProcess() {
325 return g_master_preferences.Get();
328 } // namespace installer