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

Side by Side Diff: chrome/browser/importer/firefox_importer_utils.cc

Issue 16982004: Move Firefox importer's INI parser to c/browser/common. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove extended initializer list for linux_chromeos build. Created 7 years, 6 months 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
« no previous file with comments | « base/ini_parser_unittest.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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/browser/importer/firefox_importer_utils.h" 5 #include "chrome/browser/importer/firefox_importer_utils.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <map> 8 #include <map>
9 #include <string> 9 #include <string>
10 10
11 #include "base/file_util.h" 11 #include "base/file_util.h"
12 #include "base/ini_parser.h"
12 #include "base/logging.h" 13 #include "base/logging.h"
13 #include "base/strings/string_number_conversions.h" 14 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_split.h" 15 #include "base/strings/string_split.h"
15 #include "base/strings/string_util.h"
16 #include "base/strings/stringprintf.h" 16 #include "base/strings/stringprintf.h"
17 #include "base/strings/utf_string_conversions.h" 17 #include "base/strings/utf_string_conversions.h"
18 #include "base/values.h" 18 #include "base/values.h"
19 #include "chrome/browser/search_engines/template_url.h" 19 #include "chrome/browser/search_engines/template_url.h"
20 #include "chrome/browser/search_engines/template_url_parser.h" 20 #include "chrome/browser/search_engines/template_url_parser.h"
21 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" 21 #include "chrome/browser/search_engines/template_url_prepopulate_data.h"
22 #include "chrome/browser/search_engines/template_url_service.h" 22 #include "chrome/browser/search_engines/template_url_service.h"
23 #include "googleurl/src/gurl.h" 23 #include "googleurl/src/gurl.h"
24 #include "grit/generated_resources.h" 24 #include "grit/generated_resources.h"
25 #include "ui/base/l10n/l10n_util.h" 25 #include "ui/base/l10n/l10n_util.h"
(...skipping 17 matching lines...) Expand all
43 return false; 43 return false;
44 return true; 44 return true;
45 } 45 }
46 46
47 private: 47 private:
48 DISALLOW_COPY_AND_ASSIGN(FirefoxURLParameterFilter); 48 DISALLOW_COPY_AND_ASSIGN(FirefoxURLParameterFilter);
49 }; 49 };
50 } // namespace 50 } // namespace
51 51
52 base::FilePath GetFirefoxProfilePath() { 52 base::FilePath GetFirefoxProfilePath() {
53 DictionaryValue root;
54 base::FilePath ini_file = GetProfilesINI(); 53 base::FilePath ini_file = GetProfilesINI();
55 ParseProfileINI(ini_file, &root); 54 std::string content;
55 file_util::ReadFileToString(ini_file, &content);
56 base::DictionaryValueINIParser ini_parser;
57 ini_parser.Parse(content);
58 const DictionaryValue& root = ini_parser.root();
56 59
57 base::FilePath source_path; 60 base::FilePath source_path;
58 for (int i = 0; ; ++i) { 61 for (int i = 0; ; ++i) {
59 std::string current_profile = base::StringPrintf("Profile%d", i); 62 std::string current_profile = base::StringPrintf("Profile%d", i);
60 if (!root.HasKey(current_profile)) { 63 if (!root.HasKey(current_profile)) {
61 // Profiles are continuously numbered. So we exit when we can't 64 // Profiles are continuously numbered. So we exit when we can't
62 // find the i-th one. 65 // find the i-th one.
63 break; 66 break;
64 } 67 }
65 std::string is_relative; 68 std::string is_relative;
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 // file, we could go straight from bytes -> filepath; 126 // file, we could go straight from bytes -> filepath;
124 // otherwise, we're out of luck here. 127 // otherwise, we're out of luck here.
125 *app_path = base::FilePath::FromWStringHack( 128 *app_path = base::FilePath::FromWStringHack(
126 UTF8ToWide(line.substr(equal + 1))); 129 UTF8ToWide(line.substr(equal + 1)));
127 } 130 }
128 } 131 }
129 } 132 }
130 return ret; 133 return ret;
131 } 134 }
132 135
133 void ParseProfileINI(const base::FilePath& file, DictionaryValue* root) {
134 // Reads the whole INI file.
135 std::string content;
136 file_util::ReadFileToString(file, &content);
137 ReplaceSubstringsAfterOffset(&content, 0, "\r\n", "\n");
138 std::vector<std::string> lines;
139 base::SplitString(content, '\n', &lines);
140
141 // Parses the file.
142 root->Clear();
143 std::string current_section;
144 for (size_t i = 0; i < lines.size(); ++i) {
145 std::string line = lines[i];
146 if (line.empty()) {
147 // Skips the empty line.
148 continue;
149 }
150 if (line[0] == '#' || line[0] == ';') {
151 // This line is a comment.
152 continue;
153 }
154 if (line[0] == '[') {
155 // It is a section header.
156 current_section = line.substr(1);
157 size_t end = current_section.rfind(']');
158 if (end != std::string::npos)
159 current_section.erase(end);
160 } else {
161 std::string key, value;
162 size_t equal = line.find('=');
163 if (equal != std::string::npos) {
164 key = line.substr(0, equal);
165 value = line.substr(equal + 1);
166 // Checks whether the section and key contain a '.' character.
167 // Those sections and keys break DictionaryValue's path format,
168 // so we discard them.
169 if (current_section.find('.') == std::string::npos &&
170 key.find('.') == std::string::npos)
171 root->SetString(current_section + "." + key, value);
172 }
173 }
174 }
175 }
176
177 bool CanImportURL(const GURL& url) { 136 bool CanImportURL(const GURL& url) {
178 const char* kInvalidSchemes[] = {"wyciwyg", "place", "about", "chrome"}; 137 const char* kInvalidSchemes[] = {"wyciwyg", "place", "about", "chrome"};
179 138
180 // The URL is not valid. 139 // The URL is not valid.
181 if (!url.is_valid()) 140 if (!url.is_valid())
182 return false; 141 return false;
183 142
184 // Filter out the URLs with unsupported schemes. 143 // Filter out the URLs with unsupported schemes.
185 for (size_t i = 0; i < arraysize(kInvalidSchemes); ++i) { 144 for (size_t i = 0; i < arraysize(kInvalidSchemes); ++i) {
186 if (url.SchemeIs(kInvalidSchemes[i])) 145 if (url.SchemeIs(kInvalidSchemes[i]))
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after
459 } 418 }
460 } 419 }
461 } 420 }
462 } 421 }
463 422
464 StringToLowerASCII(&branding_name); 423 StringToLowerASCII(&branding_name);
465 if (branding_name.find("iceweasel") != std::string::npos) 424 if (branding_name.find("iceweasel") != std::string::npos)
466 return l10n_util::GetStringUTF16(IDS_IMPORT_FROM_ICEWEASEL); 425 return l10n_util::GetStringUTF16(IDS_IMPORT_FROM_ICEWEASEL);
467 return l10n_util::GetStringUTF16(IDS_IMPORT_FROM_FIREFOX); 426 return l10n_util::GetStringUTF16(IDS_IMPORT_FROM_FIREFOX);
468 } 427 }
OLDNEW
« no previous file with comments | « base/ini_parser_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698