Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(47)

Side by Side Diff: chrome/installer/util/master_preferences.cc

Issue 4342001: Refactoring the master_preferences functions.... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 10 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 "base/string_util.h" 10 #include "base/string_util.h"
11 #include "chrome/common/json_value_serializer.h" 11 #include "chrome/common/json_value_serializer.h"
12 #include "chrome/installer/util/master_preferences_constants.h"
12 #include "chrome/installer/util/util_constants.h" 13 #include "chrome/installer/util/util_constants.h"
13 #include "googleurl/src/gurl.h" 14 #include "googleurl/src/gurl.h"
14 15
15 16
16 namespace { 17 namespace {
17 18
18 const char kDistroDict[] = "distribution"; 19 const char kDistroDict[] = "distribution";
20 const char kFirstRunTabs[] = "first_run_tabs";
19 21
20 bool GetGURLFromValue(const Value* in_value, GURL* out_value) { 22 bool GetGURLFromValue(const Value* in_value, GURL* out_value) {
21 if (!in_value || !out_value) 23 if (!in_value || !out_value)
22 return false; 24 return false;
23 std::string url; 25 std::string url;
24 in_value->GetAsString(&url); 26 in_value->GetAsString(&url);
25 GURL gurl(url); 27 GURL gurl(url);
26 *out_value = gurl; 28 *out_value = gurl;
27 return true; 29 return true;
28 } 30 }
(...skipping 11 matching lines...) Expand all
40 GURL gurl_entry; 42 GURL gurl_entry;
41 if (!value_list->Get(i, &entry) || !GetGURLFromValue(entry, &gurl_entry)) { 43 if (!value_list->Get(i, &entry) || !GetGURLFromValue(entry, &gurl_entry)) {
42 NOTREACHED(); 44 NOTREACHED();
43 break; 45 break;
44 } 46 }
45 list.push_back(gurl_entry); 47 list.push_back(gurl_entry);
46 } 48 }
47 return list; 49 return list;
48 } 50 }
49 51
50 } // namespace
51
52 namespace installer_util {
53
54 bool GetDistroBooleanPreference(const DictionaryValue* prefs,
55 const std::string& name,
56 bool* value) {
57 if (!prefs || !value)
58 return false;
59
60 DictionaryValue* distro = NULL;
61 if (!prefs->GetDictionary(kDistroDict, &distro) || !distro)
62 return false;
63
64 if (!distro->GetBoolean(name, value))
65 return false;
66
67 return true;
68 }
69
70 bool GetDistroStringPreference(const DictionaryValue* prefs,
71 const std::string& name,
72 std::string* value) {
73 if (!prefs || !value)
74 return false;
75
76 DictionaryValue* distro = NULL;
77 if (!prefs->GetDictionary(kDistroDict, &distro) || !distro)
78 return false;
79
80 std::string str_value;
81 if (!distro->GetString(name, &str_value))
82 return false;
83
84 if (str_value.empty())
85 return false;
86
87 *value = str_value;
88 return true;
89 }
90
91 bool GetDistroIntegerPreference(const DictionaryValue* prefs,
92 const std::string& name,
93 int* value) {
94 if (!prefs || !value)
95 return false;
96
97 DictionaryValue* distro = NULL;
98 if (!prefs->GetDictionary(kDistroDict, &distro) || !distro)
99 return false;
100
101 if (!distro->GetInteger(name, value))
102 return false;
103
104 return true;
105 }
106
107 DictionaryValue* GetInstallPreferences(const CommandLine& cmd_line) {
108 DictionaryValue* prefs = NULL;
109 #if defined(OS_WIN)
110 if (cmd_line.HasSwitch(installer_util::switches::kInstallerData)) {
111 FilePath prefs_path = cmd_line.GetSwitchValuePath(
112 installer_util::switches::kInstallerData);
113 prefs = installer_util::ParseDistributionPreferences(prefs_path);
114 }
115
116 if (!prefs)
117 prefs = new DictionaryValue();
118
119 if (cmd_line.HasSwitch(installer_util::switches::kChromeFrame))
120 installer_util::SetDistroBooleanPreference(
121 prefs, installer_util::master_preferences::kChromeFrame, true);
122
123 if (cmd_line.HasSwitch(installer_util::switches::kCreateAllShortcuts))
124 installer_util::SetDistroBooleanPreference(
125 prefs, installer_util::master_preferences::kCreateAllShortcuts, true);
126
127 if (cmd_line.HasSwitch(installer_util::switches::kDoNotCreateShortcuts))
128 installer_util::SetDistroBooleanPreference(
129 prefs, installer_util::master_preferences::kDoNotCreateShortcuts, true);
130
131 if (cmd_line.HasSwitch(installer_util::switches::kMsi))
132 installer_util::SetDistroBooleanPreference(
133 prefs, installer_util::master_preferences::kMsi, true);
134
135 if (cmd_line.HasSwitch(
136 installer_util::switches::kDoNotRegisterForUpdateLaunch))
137 installer_util::SetDistroBooleanPreference(
138 prefs,
139 installer_util::master_preferences::kDoNotRegisterForUpdateLaunch,
140 true);
141
142 if (cmd_line.HasSwitch(installer_util::switches::kDoNotLaunchChrome))
143 installer_util::SetDistroBooleanPreference(
144 prefs, installer_util::master_preferences::kDoNotLaunchChrome, true);
145
146 if (cmd_line.HasSwitch(installer_util::switches::kMakeChromeDefault))
147 installer_util::SetDistroBooleanPreference(
148 prefs, installer_util::master_preferences::kMakeChromeDefault, true);
149
150 if (cmd_line.HasSwitch(installer_util::switches::kSystemLevel))
151 installer_util::SetDistroBooleanPreference(
152 prefs, installer_util::master_preferences::kSystemLevel, true);
153
154 if (cmd_line.HasSwitch(installer_util::switches::kVerboseLogging))
155 installer_util::SetDistroBooleanPreference(
156 prefs, installer_util::master_preferences::kVerboseLogging, true);
157
158 if (cmd_line.HasSwitch(installer_util::switches::kAltDesktopShortcut))
159 installer_util::SetDistroBooleanPreference(
160 prefs, installer_util::master_preferences::kAltShortcutText, true);
161 #endif
162
163 return prefs;
164 }
165
166 DictionaryValue* ParseDistributionPreferences( 52 DictionaryValue* ParseDistributionPreferences(
167 const FilePath& master_prefs_path) { 53 const FilePath& master_prefs_path) {
168 if (!file_util::PathExists(master_prefs_path))
169 return NULL;
170
171 std::string json_data; 54 std::string json_data;
172 if (!file_util::ReadFileToString(master_prefs_path, &json_data)) { 55 if (!file_util::ReadFileToString(master_prefs_path, &json_data)) {
173 LOG(WARNING) << "Failed to read master prefs file."; 56 LOG(WARNING) << "Failed to read master prefs file. ";
174 return NULL; 57 return NULL;
175 } 58 }
176 JSONStringValueSerializer json(json_data); 59 JSONStringValueSerializer json(json_data);
177 std::string error; 60 std::string error;
178 scoped_ptr<Value> root(json.Deserialize(NULL, &error)); 61 scoped_ptr<Value> root(json.Deserialize(NULL, &error));
179 if (!root.get()) { 62 if (!root.get()) {
180 LOG(WARNING) << "Failed to parse master prefs file: " << error; 63 LOG(WARNING) << "Failed to parse master prefs file: " << error;
181 return NULL; 64 return NULL;
182 } 65 }
183 if (!root->IsType(Value::TYPE_DICTIONARY)) { 66 if (!root->IsType(Value::TYPE_DICTIONARY)) {
184 LOG(WARNING) << "Failed to parse master prefs file: " 67 LOG(WARNING) << "Failed to parse master prefs file: "
185 << "Root item must be a dictionary."; 68 << "Root item must be a dictionary.";
186 return NULL; 69 return NULL;
187 } 70 }
188 return static_cast<DictionaryValue*>(root.release()); 71 return static_cast<DictionaryValue*>(root.release());
189 } 72 }
190 73
191 std::vector<GURL> GetFirstRunTabs(const DictionaryValue* prefs) { 74 } // namespace
192 return GetNamedList("first_run_tabs", prefs); 75
76 namespace installer_util {
77
78 MasterPreferences::MasterPreferences(const CommandLine& cmd_line)
79 : distribution_(NULL), preferences_read_from_file_(false) {
80 #if defined(OS_WIN)
81 if (cmd_line.HasSwitch(installer_util::switches::kInstallerData)) {
82 FilePath prefs_path(cmd_line.GetSwitchValuePath(
83 installer_util::switches::kInstallerData));
84 this->MasterPreferences::MasterPreferences(prefs_path);
85 } else {
86 master_dictionary_.reset(new DictionaryValue());
87 }
88
89 DCHECK(master_dictionary_.get());
90
91 // A simple map from command line switches to equivalent switches in the
92 // distribution dictionary. Currently all switches added will be set to
93 // 'true'.
94 static const struct CmdLineSwitchToDistributionSwitch {
95 const wchar_t* cmd_line_switch;
96 const char* distribution_switch;
97 } translate_switches[] = {
98 { installer_util::switches::kChromeFrame,
99 installer_util::master_preferences::kChromeFrame },
100 { installer_util::switches::kCreateAllShortcuts,
101 installer_util::master_preferences::kCreateAllShortcuts },
102 { installer_util::switches::kDoNotCreateShortcuts,
103 installer_util::master_preferences::kDoNotCreateShortcuts },
104 { installer_util::switches::kMsi,
105 installer_util::master_preferences::kMsi },
106 { installer_util::switches::kDoNotRegisterForUpdateLaunch,
107 installer_util::master_preferences::kDoNotRegisterForUpdateLaunch },
108 { installer_util::switches::kDoNotLaunchChrome,
109 installer_util::master_preferences::kDoNotLaunchChrome },
110 { installer_util::switches::kMakeChromeDefault,
111 installer_util::master_preferences::kMakeChromeDefault },
112 { installer_util::switches::kSystemLevel,
113 installer_util::master_preferences::kSystemLevel },
114 { installer_util::switches::kVerboseLogging,
115 installer_util::master_preferences::kVerboseLogging },
116 { installer_util::switches::kAltDesktopShortcut,
117 installer_util::master_preferences::kAltShortcutText },
118 };
119
120 std::string name(kDistroDict);
121 for (int i = 0; i < arraysize(translate_switches); ++i) {
122 if (cmd_line.HasSwitch(translate_switches[i].cmd_line_switch)) {
123 name.resize(arraysize(kDistroDict) - 1);
124 name.append(".").append(translate_switches[i].distribution_switch);
125 master_dictionary_->SetBoolean(name, true);
126 }
127 }
128
129 // Cache a pointer to the distribution dictionary. Ignore errors if any.
130 master_dictionary_->GetDictionary(kDistroDict, &distribution_);
131 #endif
193 } 132 }
194 133
195 bool SetDistroBooleanPreference(DictionaryValue* prefs, 134 MasterPreferences::MasterPreferences(const FilePath& prefs_path)
196 const std::string& name, 135 : distribution_(NULL), preferences_read_from_file_(false) {
197 bool value) { 136 master_dictionary_.reset(ParseDistributionPreferences(prefs_path));
198 if (!prefs || name.empty()) 137 LOG_IF(ERROR, !master_dictionary_.get()) << "Failed to parse "
199 return false; 138 << prefs_path.value();
200 prefs->SetBoolean(std::string(kDistroDict) + "." + name, value); 139 if (!master_dictionary_.get()) {
201 return true; 140 master_dictionary_.reset(new DictionaryValue());
141 } else {
142 preferences_read_from_file_ = true;
143 // Cache a pointer to the distribution dictionary.
144 master_dictionary_->GetDictionary(kDistroDict, &distribution_);
145 }
202 } 146 }
203 147
204 bool HasExtensionsBlock(const DictionaryValue* prefs, 148 MasterPreferences::~MasterPreferences() {
205 DictionaryValue** extensions) { 149 }
206 return (prefs->GetDictionary(master_preferences::kExtensionsBlock, 150
207 extensions)); 151 bool MasterPreferences::GetBool(const std::string& name, bool* value) const {
152 bool ret = false;
153 if (distribution_)
154 ret = distribution_->GetBoolean(name, value);
155 return ret;
156 }
157
158 bool MasterPreferences::GetInt(const std::string& name, int* value) const {
159 bool ret = false;
160 if (distribution_)
161 ret = distribution_->GetInteger(name, value);
162 return ret;
163 }
164
165 bool MasterPreferences::GetString(const std::string& name,
166 std::string* value) const {
167 bool ret = false;
168 if (distribution_)
169 ret = (distribution_->GetString(name, value) && !value->empty());
170 return ret;
171 }
172
173 std::vector<GURL> MasterPreferences::GetFirstRunTabs() const {
174 return GetNamedList(kFirstRunTabs, master_dictionary_.get());
175 }
176
177 bool MasterPreferences::GetExtensionsBlock(DictionaryValue** extensions) const {
178 return master_dictionary_->GetDictionary(
179 master_preferences::kExtensionsBlock, extensions);
208 } 180 }
209 181
210 } // installer_util 182 } // installer_util
OLDNEW
« no previous file with comments | « chrome/installer/util/master_preferences.h ('k') | chrome/installer/util/master_preferences_dummy.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698