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/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/path_service.h"
13 #include "base/strings/string_util.h"
14 #include "chrome/common/env_vars.h"
15 #include "chrome/common/pref_names.h"
16 #include "chrome/installer/util/master_preferences_constants.h"
17 #include "chrome/installer/util/util_constants.h"
21 const char kFirstRunTabs
[] = "first_run_tabs";
23 base::LazyInstance
<installer::MasterPreferences
> g_master_preferences
=
24 LAZY_INSTANCE_INITIALIZER
;
26 bool GetURLFromValue(const base::Value
* in_value
, std::string
* out_value
) {
27 return in_value
&& out_value
&& in_value
->GetAsString(out_value
);
30 std::vector
<std::string
> GetNamedList(const char* name
,
31 const base::DictionaryValue
* prefs
) {
32 std::vector
<std::string
> list
;
36 const base::ListValue
* value_list
= NULL
;
37 if (!prefs
->GetList(name
, &value_list
))
40 list
.reserve(value_list
->GetSize());
41 for (size_t i
= 0; i
< value_list
->GetSize(); ++i
) {
42 const base::Value
* entry
;
43 std::string url_entry
;
44 if (!value_list
->Get(i
, &entry
) || !GetURLFromValue(entry
, &url_entry
)) {
48 list
.push_back(url_entry
);
53 base::DictionaryValue
* ParseDistributionPreferences(
54 const std::string
& json_data
) {
55 JSONStringValueSerializer
json(json_data
);
57 scoped_ptr
<base::Value
> root(json
.Deserialize(NULL
, &error
));
59 LOG(WARNING
) << "Failed to parse master prefs file: " << error
;
62 if (!root
->IsType(base::Value::TYPE_DICTIONARY
)) {
63 LOG(WARNING
) << "Failed to parse master prefs file: "
64 << "Root item must be a dictionary.";
67 return static_cast<base::DictionaryValue
*>(root
.release());
74 MasterPreferences::MasterPreferences() : distribution_(NULL
),
75 preferences_read_from_file_(false),
77 chrome_app_launcher_(false),
78 multi_install_(false) {
79 InitializeFromCommandLine(*CommandLine::ForCurrentProcess());
82 MasterPreferences::MasterPreferences(const CommandLine
& cmd_line
)
83 : distribution_(NULL
),
84 preferences_read_from_file_(false),
86 chrome_app_launcher_(false),
87 multi_install_(false) {
88 InitializeFromCommandLine(cmd_line
);
91 MasterPreferences::MasterPreferences(const base::FilePath
& prefs_path
)
92 : distribution_(NULL
),
93 preferences_read_from_file_(false),
95 chrome_app_launcher_(false),
96 multi_install_(false) {
97 std::string json_data
;
98 // Failure to read the file is ignored as |json_data| will be the empty string
99 // and the remainder of this MasterPreferences object should still be
100 // initialized as best as possible.
101 if (base::PathExists(prefs_path
) &&
102 !base::ReadFileToString(prefs_path
, &json_data
)) {
103 LOG(ERROR
) << "Failed to read preferences from " << prefs_path
.value();
105 if (InitializeFromString(json_data
))
106 preferences_read_from_file_
= true;
109 MasterPreferences::MasterPreferences(const std::string
& prefs
)
110 : distribution_(NULL
),
111 preferences_read_from_file_(false),
113 chrome_app_launcher_(false),
114 multi_install_(false) {
115 InitializeFromString(prefs
);
118 MasterPreferences::~MasterPreferences() {
121 void MasterPreferences::InitializeFromCommandLine(const CommandLine
& cmd_line
) {
123 if (cmd_line
.HasSwitch(installer::switches::kInstallerData
)) {
124 base::FilePath
prefs_path(cmd_line
.GetSwitchValuePath(
125 installer::switches::kInstallerData
));
126 this->MasterPreferences::MasterPreferences(prefs_path
);
128 master_dictionary_
.reset(new base::DictionaryValue());
131 DCHECK(master_dictionary_
.get());
133 // A simple map from command line switches to equivalent switches in the
134 // distribution dictionary. Currently all switches added will be set to
136 static const struct CmdLineSwitchToDistributionSwitch
{
137 const char* cmd_line_switch
;
138 const char* distribution_switch
;
139 } translate_switches
[] = {
140 { installer::switches::kAutoLaunchChrome
,
141 installer::master_preferences::kAutoLaunchChrome
},
142 { installer::switches::kChromeAppHostDeprecated
,
143 installer::master_preferences::kChromeAppHostDeprecated
},
144 { installer::switches::kChromeAppLauncher
,
145 installer::master_preferences::kChromeAppLauncher
},
146 { installer::switches::kChrome
,
147 installer::master_preferences::kChrome
},
148 { installer::switches::kDisableLogging
,
149 installer::master_preferences::kDisableLogging
},
150 { installer::switches::kMsi
,
151 installer::master_preferences::kMsi
},
152 { installer::switches::kMultiInstall
,
153 installer::master_preferences::kMultiInstall
},
154 { installer::switches::kDoNotRegisterForUpdateLaunch
,
155 installer::master_preferences::kDoNotRegisterForUpdateLaunch
},
156 { installer::switches::kDoNotLaunchChrome
,
157 installer::master_preferences::kDoNotLaunchChrome
},
158 { installer::switches::kMakeChromeDefault
,
159 installer::master_preferences::kMakeChromeDefault
},
160 { installer::switches::kSystemLevel
,
161 installer::master_preferences::kSystemLevel
},
162 { installer::switches::kVerboseLogging
,
163 installer::master_preferences::kVerboseLogging
},
166 std::string
name(installer::master_preferences::kDistroDict
);
167 for (int i
= 0; i
< arraysize(translate_switches
); ++i
) {
168 if (cmd_line
.HasSwitch(translate_switches
[i
].cmd_line_switch
)) {
169 name
.assign(installer::master_preferences::kDistroDict
);
170 name
.append(".").append(translate_switches
[i
].distribution_switch
);
171 master_dictionary_
->SetBoolean(name
, true);
175 // See if the log file path was specified on the command line.
176 std::wstring
str_value(cmd_line
.GetSwitchValueNative(
177 installer::switches::kLogFile
));
178 if (!str_value
.empty()) {
179 name
.assign(installer::master_preferences::kDistroDict
);
180 name
.append(".").append(installer::master_preferences::kLogFile
);
181 master_dictionary_
->SetString(name
, str_value
);
184 // Handle the special case of --system-level being implied by the presence of
185 // the kGoogleUpdateIsMachineEnvVar environment variable.
186 scoped_ptr
<base::Environment
> env(base::Environment::Create());
188 std::string is_machine_var
;
189 env
->GetVar(env_vars::kGoogleUpdateIsMachineEnvVar
, &is_machine_var
);
190 if (!is_machine_var
.empty() && is_machine_var
[0] == '1') {
191 VLOG(1) << "Taking system-level from environment.";
192 name
.assign(installer::master_preferences::kDistroDict
);
193 name
.append(".").append(installer::master_preferences::kSystemLevel
);
194 master_dictionary_
->SetBoolean(name
, true);
198 // Cache a pointer to the distribution dictionary. Ignore errors if any.
199 master_dictionary_
->GetDictionary(installer::master_preferences::kDistroDict
,
202 InitializeProductFlags();
206 bool MasterPreferences::InitializeFromString(const std::string
& json_data
) {
207 if (!json_data
.empty())
208 master_dictionary_
.reset(ParseDistributionPreferences(json_data
));
210 bool data_is_valid
= true;
211 if (!master_dictionary_
.get()) {
212 master_dictionary_
.reset(new base::DictionaryValue());
213 data_is_valid
= false;
215 // Cache a pointer to the distribution dictionary.
216 master_dictionary_
->GetDictionary(
217 installer::master_preferences::kDistroDict
, &distribution_
);
220 InitializeProductFlags();
221 EnforceLegacyPreferences();
222 return data_is_valid
;
225 void MasterPreferences::InitializeProductFlags() {
226 // Make sure we start out with the correct defaults.
227 multi_install_
= false;
228 chrome_app_launcher_
= false;
231 GetBool(installer::master_preferences::kMultiInstall
, &multi_install_
);
233 GetBool(installer::master_preferences::kChromeAppLauncher
,
234 &chrome_app_launcher_
);
236 // The deprecated switch --app-host behaves like --app-launcher.
237 bool chrome_app_host
= false;
238 GetBool(installer::master_preferences::kChromeAppHostDeprecated
,
240 chrome_app_launcher_
= chrome_app_launcher_
|| chrome_app_host
;
242 // When multi-install is specified, the checks are pretty simple (in theory):
243 // In order to be installed/uninstalled, each product must have its switch
244 // present on the command line.
245 // When multi-install is not set, operate on Chrome.
246 if (multi_install_
) {
247 if (!GetBool(installer::master_preferences::kChrome
, &chrome_
))
254 void MasterPreferences::EnforceLegacyPreferences() {
255 // If create_all_shortcuts was explicitly set to false, set
256 // do_not_create_(desktop|quick_launch)_shortcut to true.
257 bool create_all_shortcuts
= true;
258 GetBool(installer::master_preferences::kCreateAllShortcuts
,
259 &create_all_shortcuts
);
260 if (!create_all_shortcuts
) {
261 distribution_
->SetBoolean(
262 installer::master_preferences::kDoNotCreateDesktopShortcut
, true);
263 distribution_
->SetBoolean(
264 installer::master_preferences::kDoNotCreateQuickLaunchShortcut
, true);
267 // If there is no entry for kURLsToRestoreOnStartup and there is one for
268 // kURLsToRestoreOnStartupOld, copy the old to the new.
269 const base::ListValue
* startup_urls_list
= NULL
;
270 if (master_dictionary_
&&
271 !master_dictionary_
->GetList(prefs::kURLsToRestoreOnStartup
, NULL
) &&
272 master_dictionary_
->GetList(prefs::kURLsToRestoreOnStartupOld
,
273 &startup_urls_list
) &&
275 base::ListValue
* new_startup_urls_list
= startup_urls_list
->DeepCopy();
276 master_dictionary_
->Set(prefs::kURLsToRestoreOnStartup
,
277 new_startup_urls_list
);
281 bool MasterPreferences::GetBool(const std::string
& name
, bool* value
) const {
284 ret
= distribution_
->GetBoolean(name
, value
);
288 bool MasterPreferences::GetInt(const std::string
& name
, int* value
) const {
291 ret
= distribution_
->GetInteger(name
, value
);
295 bool MasterPreferences::GetString(const std::string
& name
,
296 std::string
* value
) const {
299 ret
= (distribution_
->GetString(name
, value
) && !value
->empty());
303 std::vector
<std::string
> MasterPreferences::GetFirstRunTabs() const {
304 return GetNamedList(kFirstRunTabs
, master_dictionary_
.get());
307 bool MasterPreferences::GetExtensionsBlock(
308 base::DictionaryValue
** extensions
) const {
309 return master_dictionary_
->GetDictionary(
310 master_preferences::kExtensionsBlock
, extensions
);
313 std::string
MasterPreferences::GetVariationsSeed() const {
314 std::string variations_seed
;
315 master_dictionary_
->GetString(prefs::kVariationsSeed
, &variations_seed
);
316 return variations_seed
;
320 const MasterPreferences
& MasterPreferences::ForCurrentProcess() {
321 return g_master_preferences
.Get();
324 } // namespace installer