1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/importer/firefox_importer_utils.h"
#include <algorithm>
#include <map>
#include <string>
#include "base/file_util.h"
#include "base/logging.h"
#include "base/stringprintf.h"
#include "base/string_number_conversions.h"
#include "base/string_split.h"
#include "base/string_util.h"
#include "base/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/browser/search_engines/template_url.h"
#include "chrome/browser/search_engines/template_url_service.h"
#include "chrome/browser/search_engines/template_url_parser.h"
#include "chrome/browser/search_engines/template_url_prepopulate_data.h"
#include "googleurl/src/gurl.h"
#include "grit/generated_resources.h"
#include "ui/base/l10n/l10n_util.h"
namespace {
// FirefoxURLParameterFilter is used to remove parameter mentioning Firefox from
// the search URL when importing search engines.
class FirefoxURLParameterFilter : public TemplateURLParser::ParameterFilter {
public:
FirefoxURLParameterFilter() {}
virtual ~FirefoxURLParameterFilter() {}
// TemplateURLParser::ParameterFilter method.
virtual bool KeepParameter(const std::string& key,
const std::string& value) {
std::string low_value = StringToLowerASCII(value);
if (low_value.find("mozilla") != std::string::npos ||
low_value.find("firefox") != std::string::npos ||
low_value.find("moz:") != std::string::npos )
return false;
return true;
}
private:
DISALLOW_COPY_AND_ASSIGN(FirefoxURLParameterFilter);
};
} // namespace
FilePath GetFirefoxProfilePath() {
DictionaryValue root;
FilePath ini_file = GetProfilesINI();
ParseProfileINI(ini_file, &root);
FilePath source_path;
for (int i = 0; ; ++i) {
std::string current_profile = StringPrintf("Profile%d", i);
if (!root.HasKey(current_profile)) {
// Profiles are continuously numbered. So we exit when we can't
// find the i-th one.
break;
}
std::string is_relative;
string16 path16;
if (root.GetStringASCII(current_profile + ".IsRelative", &is_relative) &&
root.GetString(current_profile + ".Path", &path16)) {
#if defined(OS_WIN)
ReplaceSubstringsAfterOffset(
&path16, 0, ASCIIToUTF16("/"), ASCIIToUTF16("\\"));
#endif
FilePath path = FilePath::FromWStringHack(UTF16ToWide(path16));
// IsRelative=1 means the folder path would be relative to the
// path of profiles.ini. IsRelative=0 refers to a custom profile
// location.
if (is_relative == "1") {
path = ini_file.DirName().Append(path);
}
// We only import the default profile when multiple profiles exist,
// since the other profiles are used mostly by developers for testing.
// Otherwise, Profile0 will be imported.
std::string is_default;
if ((root.GetStringASCII(current_profile + ".Default", &is_default) &&
is_default == "1") || i == 0) {
// We have found the default profile.
return path;
}
}
}
return FilePath();
}
bool GetFirefoxVersionAndPathFromProfile(const FilePath& profile_path,
int* version,
FilePath* app_path) {
bool ret = false;
FilePath compatibility_file = profile_path.AppendASCII("compatibility.ini");
std::string content;
file_util::ReadFileToString(compatibility_file, &content);
ReplaceSubstringsAfterOffset(&content, 0, "\r\n", "\n");
std::vector<std::string> lines;
base::SplitString(content, '\n', &lines);
for (size_t i = 0; i < lines.size(); ++i) {
const std::string& line = lines[i];
if (line.empty() || line[0] == '#' || line[0] == ';')
continue;
size_t equal = line.find('=');
if (equal != std::string::npos) {
std::string key = line.substr(0, equal);
if (key == "LastVersion") {
base::StringToInt(line.substr(equal + 1), version);
ret = true;
} else if (key == "LastAppDir") {
// TODO(evanm): If the path in question isn't convertible to
// UTF-8, what does Firefox do? If it puts raw bytes in the
// file, we could go straight from bytes -> filepath;
// otherwise, we're out of luck here.
*app_path = FilePath::FromWStringHack(
UTF8ToWide(line.substr(equal + 1)));
}
}
}
return ret;
}
void ParseProfileINI(const FilePath& file, DictionaryValue* root) {
// Reads the whole INI file.
std::string content;
file_util::ReadFileToString(file, &content);
ReplaceSubstringsAfterOffset(&content, 0, "\r\n", "\n");
std::vector<std::string> lines;
base::SplitString(content, '\n', &lines);
// Parses the file.
root->Clear();
std::string current_section;
for (size_t i = 0; i < lines.size(); ++i) {
std::string line = lines[i];
if (line.empty()) {
// Skips the empty line.
continue;
}
if (line[0] == '#' || line[0] == ';') {
// This line is a comment.
continue;
}
if (line[0] == '[') {
// It is a section header.
current_section = line.substr(1);
size_t end = current_section.rfind(']');
if (end != std::string::npos)
current_section.erase(end);
} else {
std::string key, value;
size_t equal = line.find('=');
if (equal != std::string::npos) {
key = line.substr(0, equal);
value = line.substr(equal + 1);
// Checks whether the section and key contain a '.' character.
// Those sections and keys break DictionaryValue's path format,
// so we discard them.
if (current_section.find('.') == std::string::npos &&
key.find('.') == std::string::npos)
root->SetString(current_section + "." + key, value);
}
}
}
}
bool CanImportURL(const GURL& url) {
const char* kInvalidSchemes[] = {"wyciwyg", "place", "about", "chrome"};
// The URL is not valid.
if (!url.is_valid())
return false;
// Filter out the URLs with unsupported schemes.
for (size_t i = 0; i < arraysize(kInvalidSchemes); ++i) {
if (url.SchemeIs(kInvalidSchemes[i]))
return false;
}
return true;
}
void ParseSearchEnginesFromXMLFiles(const std::vector<FilePath>& xml_files,
std::vector<TemplateURL*>* search_engines) {
DCHECK(search_engines);
typedef std::map<std::string, TemplateURL*> SearchEnginesMap;
SearchEnginesMap search_engine_for_url;
std::string content;
// The first XML file represents the default search engine in Firefox 3, so we
// need to keep it on top of the list.
SearchEnginesMap::const_iterator default_turl = search_engine_for_url.end();
for (std::vector<FilePath>::const_iterator file_iter = xml_files.begin();
file_iter != xml_files.end(); ++file_iter) {
file_util::ReadFileToString(*file_iter, &content);
FirefoxURLParameterFilter param_filter;
TemplateURL* template_url = TemplateURLParser::Parse(NULL, true,
content.data(), content.length(), ¶m_filter);
if (template_url) {
SearchEnginesMap::iterator iter =
search_engine_for_url.find(template_url->url());
if (iter == search_engine_for_url.end()) {
iter = search_engine_for_url.insert(
std::make_pair(template_url->url(), template_url)).first;
} else {
// We have already found a search engine with the same URL. We give
// priority to the latest one found, as GetSearchEnginesXMLFiles()
// returns a vector with first Firefox default search engines and then
// the user's ones. We want to give priority to the user ones.
delete iter->second;
iter->second = template_url;
}
if (default_turl == search_engine_for_url.end())
default_turl = iter;
}
content.clear();
}
// Put the results in the |search_engines| vector.
for (SearchEnginesMap::iterator t_iter = search_engine_for_url.begin();
t_iter != search_engine_for_url.end(); ++t_iter) {
if (t_iter == default_turl)
search_engines->insert(search_engines->begin(), default_turl->second);
else
search_engines->push_back(t_iter->second);
}
}
bool ReadPrefFile(const FilePath& path, std::string* content) {
if (content == NULL)
return false;
file_util::ReadFileToString(path, content);
if (content->empty()) {
LOG(WARNING) << "Firefox preference file " << path.value() << " is empty.";
return false;
}
return true;
}
std::string ReadBrowserConfigProp(const FilePath& app_path,
const std::string& pref_key) {
std::string content;
if (!ReadPrefFile(app_path.AppendASCII("browserconfig.properties"), &content))
return "";
// This file has the syntax: key=value.
size_t prop_index = content.find(pref_key + "=");
if (prop_index == std::string::npos)
return "";
size_t start = prop_index + pref_key.length();
size_t stop = std::string::npos;
if (start != std::string::npos)
stop = content.find("\n", start + 1);
if (start == std::string::npos ||
stop == std::string::npos || (start == stop)) {
LOG(WARNING) << "Firefox property " << pref_key << " could not be parsed.";
return "";
}
return content.substr(start + 1, stop - start - 1);
}
std::string ReadPrefsJsValue(const FilePath& profile_path,
const std::string& pref_key) {
std::string content;
if (!ReadPrefFile(profile_path.AppendASCII("prefs.js"), &content))
return "";
return GetPrefsJsValue(content, pref_key);
}
GURL GetHomepage(const FilePath& profile_path) {
std::string home_page_list =
ReadPrefsJsValue(profile_path, "browser.startup.homepage");
size_t seperator = home_page_list.find_first_of('|');
if (seperator == std::string::npos)
return GURL(home_page_list);
return GURL(home_page_list.substr(0, seperator));
}
bool IsDefaultHomepage(const GURL& homepage, const FilePath& app_path) {
if (!homepage.is_valid())
return false;
std::string default_homepages =
ReadBrowserConfigProp(app_path, "browser.startup.homepage");
size_t seperator = default_homepages.find_first_of('|');
if (seperator == std::string::npos)
return homepage.spec() == GURL(default_homepages).spec();
// Crack the string into separate homepage urls.
std::vector<std::string> urls;
base::SplitString(default_homepages, '|', &urls);
for (size_t i = 0; i < urls.size(); ++i) {
if (homepage.spec() == GURL(urls[i]).spec())
return true;
}
return false;
}
bool ParsePrefFile(const FilePath& pref_file, DictionaryValue* prefs) {
// The string that is before a pref key.
const std::string kUserPrefString = "user_pref(\"";
std::string contents;
if (!file_util::ReadFileToString(pref_file, &contents))
return false;
std::vector<std::string> lines;
Tokenize(contents, "\n", &lines);
for (std::vector<std::string>::const_iterator iter = lines.begin();
iter != lines.end(); ++iter) {
const std::string& line = *iter;
size_t start_key = line.find(kUserPrefString);
if (start_key == std::string::npos)
continue; // Could be a comment or a blank line.
start_key += kUserPrefString.length();
size_t stop_key = line.find('"', start_key);
if (stop_key == std::string::npos) {
LOG(ERROR) << "Invalid key found in Firefox pref file '" <<
pref_file.value() << "' line is '" << line << "'.";
continue;
}
std::string key = line.substr(start_key, stop_key - start_key);
size_t start_value = line.find(',', stop_key + 1);
if (start_value == std::string::npos) {
LOG(ERROR) << "Invalid value found in Firefox pref file '" <<
pref_file.value() << "' line is '" << line << "'.";
continue;
}
size_t stop_value = line.find(");", start_value + 1);
if (stop_value == std::string::npos) {
LOG(ERROR) << "Invalid value found in Firefox pref file '" <<
pref_file.value() << "' line is '" << line << "'.";
continue;
}
std::string value = line.substr(start_value + 1,
stop_value - start_value - 1);
TrimWhitespace(value, TRIM_ALL, &value);
// Value could be a boolean.
bool is_value_true = LowerCaseEqualsASCII(value, "true");
if (is_value_true || LowerCaseEqualsASCII(value, "false")) {
prefs->SetBoolean(key, is_value_true);
continue;
}
// Value could be a string.
if (value.size() >= 2U &&
value[0] == '"' && value[value.size() - 1] == '"') {
value = value.substr(1, value.size() - 2);
// ValueString only accept valid UTF-8. Simply ignore that entry if it is
// not UTF-8.
if (IsStringUTF8(value))
prefs->SetString(key, value);
else
VLOG(1) << "Non UTF8 value for key " << key << ", ignored.";
continue;
}
// Or value could be an integer.
int int_value = 0;
if (base::StringToInt(value, &int_value)) {
prefs->SetInteger(key, int_value);
continue;
}
LOG(ERROR) << "Invalid value found in Firefox pref file '"
<< pref_file.value() << "' value is '" << value << "'.";
}
return true;
}
std::string GetPrefsJsValue(const std::string& content,
const std::string& pref_key) {
// This file has the syntax: user_pref("key", value);
std::string search_for = std::string("user_pref(\"") + pref_key +
std::string("\", ");
size_t prop_index = content.find(search_for);
if (prop_index == std::string::npos)
return std::string();
size_t start = prop_index + search_for.length();
size_t stop = std::string::npos;
if (start != std::string::npos) {
// Stop at the last ')' on this line.
stop = content.find("\n", start + 1);
stop = content.rfind(")", stop);
}
if (start == std::string::npos || stop == std::string::npos ||
stop < start) {
LOG(WARNING) << "Firefox property " << pref_key << " could not be parsed.";
return "";
}
// String values have double quotes we don't need to return to the caller.
if (content[start] == '\"' && content[stop - 1] == '\"') {
++start;
--stop;
}
return content.substr(start, stop - start);
}
// The branding name is obtained from the application.ini file from the Firefox
// application directory. A sample application.ini file is the following:
// [App]
// Vendor=Mozilla
// Name=Iceweasel
// Profile=mozilla/firefox
// Version=3.5.16
// BuildID=20120421070307
// Copyright=Copyright (c) 1998 - 2010 mozilla.org
// ID={ec8030f7-c20a-464f-9b0e-13a3a9e97384}
// .........................................
// In this example the function returns "Iceweasel" (or a localized equivalent).
string16 GetFirefoxImporterName(const FilePath& app_path) {
const FilePath app_ini_file = app_path.AppendASCII("application.ini");
std::string branding_name;
if (file_util::PathExists(app_ini_file)) {
std::string content;
file_util::ReadFileToString(app_ini_file, &content);
std::vector<std::string> lines;
base::SplitString(content, '\n', &lines);
const std::string name_attr("Name=");
bool in_app_section = false;
for (size_t i = 0; i < lines.size(); ++i) {
TrimWhitespace(lines[i], TRIM_ALL, &lines[i]);
if (lines[i] == "[App]") {
in_app_section = true;
} else if (in_app_section) {
if (lines[i].find(name_attr) == 0) {
branding_name = lines[i].substr(name_attr.size());
break;
} else if (lines[i].length() > 0 && lines[i][0] == '[') {
// No longer in the [App] section.
break;
}
}
}
}
StringToLowerASCII(&branding_name);
if (branding_name.find("iceweasel") != std::string::npos)
return l10n_util::GetStringUTF16(IDS_IMPORT_FROM_ICEWEASEL);
return l10n_util::GetStringUTF16(IDS_IMPORT_FROM_FIREFOX);
}
|