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"
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
;
35 const base::ListValue
* value_list
= NULL
;
36 if (!prefs
->GetList(name
, &value_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
)) {
47 list
.push_back(url_entry
);
52 base::DictionaryValue
* ParseDistributionPreferences(
53 const std::string
& json_data
) {
54 JSONStringValueDeserializer
json(json_data
);
56 scoped_ptr
<base::Value
> root(json
.Deserialize(NULL
, &error
));
58 LOG(WARNING
) << "Failed to parse master prefs file: " << error
;
61 if (!root
->IsType(base::Value::TYPE_DICTIONARY
)) {
62 LOG(WARNING
) << "Failed to parse master prefs file: "
63 << "Root item must be a dictionary.";
66 return static_cast<base::DictionaryValue
*>(root
.release());
73 MasterPreferences::MasterPreferences() : distribution_(NULL
),
74 preferences_read_from_file_(false),
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),
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),
92 multi_install_(false) {
93 std::string json_data
;
94 // Failure to read the file is ignored as |json_data| will be the empty string
95 // and the remainder of this MasterPreferences object should still be
96 // initialized as best as possible.
97 if (base::PathExists(prefs_path
) &&
98 !base::ReadFileToString(prefs_path
, &json_data
)) {
99 LOG(ERROR
) << "Failed to read preferences from " << prefs_path
.value();
101 if (InitializeFromString(json_data
))
102 preferences_read_from_file_
= true;
105 MasterPreferences::MasterPreferences(const std::string
& prefs
)
106 : distribution_(NULL
),
107 preferences_read_from_file_(false),
109 multi_install_(false) {
110 InitializeFromString(prefs
);
113 MasterPreferences::~MasterPreferences() {
116 void MasterPreferences::InitializeFromCommandLine(
117 const base::CommandLine
& cmd_line
) {
119 if (cmd_line
.HasSwitch(installer::switches::kInstallerData
)) {
120 base::FilePath
prefs_path(cmd_line
.GetSwitchValuePath(
121 installer::switches::kInstallerData
));
122 this->MasterPreferences::MasterPreferences(prefs_path
);
124 master_dictionary_
.reset(new base::DictionaryValue());
127 DCHECK(master_dictionary_
.get());
129 // A simple map from command line switches to equivalent switches in the
130 // distribution dictionary. Currently all switches added will be set to
132 static const struct CmdLineSwitchToDistributionSwitch
{
133 const char* cmd_line_switch
;
134 const char* distribution_switch
;
135 } translate_switches
[] = {
136 { installer::switches::kAutoLaunchChrome
,
137 installer::master_preferences::kAutoLaunchChrome
},
138 { installer::switches::kChrome
,
139 installer::master_preferences::kChrome
},
140 { installer::switches::kDisableLogging
,
141 installer::master_preferences::kDisableLogging
},
142 { installer::switches::kMsi
,
143 installer::master_preferences::kMsi
},
144 { installer::switches::kMultiInstall
,
145 installer::master_preferences::kMultiInstall
},
146 { installer::switches::kDoNotRegisterForUpdateLaunch
,
147 installer::master_preferences::kDoNotRegisterForUpdateLaunch
},
148 { installer::switches::kDoNotLaunchChrome
,
149 installer::master_preferences::kDoNotLaunchChrome
},
150 { installer::switches::kMakeChromeDefault
,
151 installer::master_preferences::kMakeChromeDefault
},
152 { installer::switches::kSystemLevel
,
153 installer::master_preferences::kSystemLevel
},
154 { installer::switches::kVerboseLogging
,
155 installer::master_preferences::kVerboseLogging
},
158 std::string
name(installer::master_preferences::kDistroDict
);
159 for (int i
= 0; i
< arraysize(translate_switches
); ++i
) {
160 if (cmd_line
.HasSwitch(translate_switches
[i
].cmd_line_switch
)) {
161 name
.assign(installer::master_preferences::kDistroDict
);
162 name
.append(".").append(translate_switches
[i
].distribution_switch
);
163 master_dictionary_
->SetBoolean(name
, true);
167 // See if the log file path was specified on the command line.
168 std::wstring
str_value(cmd_line
.GetSwitchValueNative(
169 installer::switches::kLogFile
));
170 if (!str_value
.empty()) {
171 name
.assign(installer::master_preferences::kDistroDict
);
172 name
.append(".").append(installer::master_preferences::kLogFile
);
173 master_dictionary_
->SetString(name
, str_value
);
176 // Handle the special case of --system-level being implied by the presence of
177 // the kGoogleUpdateIsMachineEnvVar environment variable.
178 scoped_ptr
<base::Environment
> env(base::Environment::Create());
180 std::string is_machine_var
;
181 env
->GetVar(env_vars::kGoogleUpdateIsMachineEnvVar
, &is_machine_var
);
182 if (!is_machine_var
.empty() && is_machine_var
[0] == '1') {
183 VLOG(1) << "Taking system-level from environment.";
184 name
.assign(installer::master_preferences::kDistroDict
);
185 name
.append(".").append(installer::master_preferences::kSystemLevel
);
186 master_dictionary_
->SetBoolean(name
, true);
190 // Cache a pointer to the distribution dictionary. Ignore errors if any.
191 master_dictionary_
->GetDictionary(installer::master_preferences::kDistroDict
,
194 InitializeProductFlags();
198 bool MasterPreferences::InitializeFromString(const std::string
& json_data
) {
199 if (!json_data
.empty())
200 master_dictionary_
.reset(ParseDistributionPreferences(json_data
));
202 bool data_is_valid
= true;
203 if (!master_dictionary_
.get()) {
204 master_dictionary_
.reset(new base::DictionaryValue());
205 data_is_valid
= false;
207 // Cache a pointer to the distribution dictionary.
208 master_dictionary_
->GetDictionary(
209 installer::master_preferences::kDistroDict
, &distribution_
);
212 InitializeProductFlags();
213 EnforceLegacyPreferences();
214 return data_is_valid
;
217 void MasterPreferences::InitializeProductFlags() {
218 // Make sure we start out with the correct defaults.
219 multi_install_
= false;
222 GetBool(installer::master_preferences::kMultiInstall
, &multi_install_
);
224 // When multi-install is specified, the checks are pretty simple (in theory):
225 // In order to be installed/uninstalled, each product must have its switch
226 // present on the command line.
227 // When multi-install is not set, operate on Chrome.
228 if (multi_install_
) {
229 if (!GetBool(installer::master_preferences::kChrome
, &chrome_
))
236 void MasterPreferences::EnforceLegacyPreferences() {
237 // If create_all_shortcuts was explicitly set to false, set
238 // do_not_create_(desktop|quick_launch)_shortcut to true.
239 bool create_all_shortcuts
= true;
240 GetBool(installer::master_preferences::kCreateAllShortcuts
,
241 &create_all_shortcuts
);
242 if (!create_all_shortcuts
) {
243 distribution_
->SetBoolean(
244 installer::master_preferences::kDoNotCreateDesktopShortcut
, true);
245 distribution_
->SetBoolean(
246 installer::master_preferences::kDoNotCreateQuickLaunchShortcut
, true);
249 // If there is no entry for kURLsToRestoreOnStartup and there is one for
250 // kURLsToRestoreOnStartupOld, copy the old to the new.
251 const base::ListValue
* startup_urls_list
= NULL
;
252 if (master_dictionary_
&&
253 !master_dictionary_
->GetList(prefs::kURLsToRestoreOnStartup
, NULL
) &&
254 master_dictionary_
->GetList(prefs::kURLsToRestoreOnStartupOld
,
255 &startup_urls_list
) &&
257 base::ListValue
* new_startup_urls_list
= startup_urls_list
->DeepCopy();
258 master_dictionary_
->Set(prefs::kURLsToRestoreOnStartup
,
259 new_startup_urls_list
);
263 bool MasterPreferences::GetBool(const std::string
& name
, bool* value
) const {
266 ret
= distribution_
->GetBoolean(name
, value
);
270 bool MasterPreferences::GetInt(const std::string
& name
, int* value
) const {
273 ret
= distribution_
->GetInteger(name
, value
);
277 bool MasterPreferences::GetString(const std::string
& name
,
278 std::string
* value
) const {
281 ret
= (distribution_
->GetString(name
, value
) && !value
->empty());
285 std::vector
<std::string
> MasterPreferences::GetFirstRunTabs() const {
286 return GetNamedList(kFirstRunTabs
, master_dictionary_
.get());
289 bool MasterPreferences::GetExtensionsBlock(
290 base::DictionaryValue
** extensions
) const {
291 return master_dictionary_
->GetDictionary(
292 master_preferences::kExtensionsBlock
, extensions
);
295 std::string
MasterPreferences::GetCompressedVariationsSeed() const {
296 return ExtractPrefString(prefs::kVariationsCompressedSeed
);
299 std::string
MasterPreferences::GetVariationsSeed() const {
300 return ExtractPrefString(prefs::kVariationsSeed
);
303 std::string
MasterPreferences::GetVariationsSeedSignature() const {
304 return ExtractPrefString(prefs::kVariationsSeedSignature
);
307 std::string
MasterPreferences::ExtractPrefString(
308 const std::string
& name
) const {
310 scoped_ptr
<base::Value
> pref_value
;
311 if (master_dictionary_
->Remove(name
, &pref_value
)) {
312 if (!pref_value
->GetAsString(&result
))
319 const MasterPreferences
& MasterPreferences::ForCurrentProcess() {
320 return g_master_preferences
.Get();
323 } // namespace installer