| OLD | NEW |
| 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/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/strings/string_split.h" | 14 #include "base/strings/string_split.h" |
| 15 #include "base/strings/string_util.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/common/ini_parser.h" |
| 19 #include "chrome/browser/search_engines/template_url.h" | 20 #include "chrome/browser/search_engines/template_url.h" |
| 20 #include "chrome/browser/search_engines/template_url_parser.h" | 21 #include "chrome/browser/search_engines/template_url_parser.h" |
| 21 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" | 22 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" |
| 22 #include "chrome/browser/search_engines/template_url_service.h" | 23 #include "chrome/browser/search_engines/template_url_service.h" |
| 23 #include "googleurl/src/gurl.h" | 24 #include "googleurl/src/gurl.h" |
| 24 #include "grit/generated_resources.h" | 25 #include "grit/generated_resources.h" |
| 25 #include "ui/base/l10n/l10n_util.h" | 26 #include "ui/base/l10n/l10n_util.h" |
| 26 | 27 |
| 27 namespace { | 28 namespace { |
| 28 | 29 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 43 return false; | 44 return false; |
| 44 return true; | 45 return true; |
| 45 } | 46 } |
| 46 | 47 |
| 47 private: | 48 private: |
| 48 DISALLOW_COPY_AND_ASSIGN(FirefoxURLParameterFilter); | 49 DISALLOW_COPY_AND_ASSIGN(FirefoxURLParameterFilter); |
| 49 }; | 50 }; |
| 50 } // namespace | 51 } // namespace |
| 51 | 52 |
| 52 base::FilePath GetFirefoxProfilePath() { | 53 base::FilePath GetFirefoxProfilePath() { |
| 53 DictionaryValue root; | |
| 54 base::FilePath ini_file = GetProfilesINI(); | 54 base::FilePath ini_file = GetProfilesINI(); |
| 55 ParseProfileINI(ini_file, &root); | 55 std::string content; |
| 56 file_util::ReadFileToString(ini_file, &content); |
| 57 DictionaryValueINIParser ini_parser; |
| 58 ini_parser.Parse(content); |
| 59 const DictionaryValue& root = ini_parser.root(); |
| 56 | 60 |
| 57 base::FilePath source_path; | 61 base::FilePath source_path; |
| 58 for (int i = 0; ; ++i) { | 62 for (int i = 0; ; ++i) { |
| 59 std::string current_profile = base::StringPrintf("Profile%d", i); | 63 std::string current_profile = base::StringPrintf("Profile%d", i); |
| 60 if (!root.HasKey(current_profile)) { | 64 if (!root.HasKey(current_profile)) { |
| 61 // Profiles are continuously numbered. So we exit when we can't | 65 // Profiles are continuously numbered. So we exit when we can't |
| 62 // find the i-th one. | 66 // find the i-th one. |
| 63 break; | 67 break; |
| 64 } | 68 } |
| 65 std::string is_relative; | 69 std::string is_relative; |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 // file, we could go straight from bytes -> filepath; | 127 // file, we could go straight from bytes -> filepath; |
| 124 // otherwise, we're out of luck here. | 128 // otherwise, we're out of luck here. |
| 125 *app_path = base::FilePath::FromWStringHack( | 129 *app_path = base::FilePath::FromWStringHack( |
| 126 UTF8ToWide(line.substr(equal + 1))); | 130 UTF8ToWide(line.substr(equal + 1))); |
| 127 } | 131 } |
| 128 } | 132 } |
| 129 } | 133 } |
| 130 return ret; | 134 return ret; |
| 131 } | 135 } |
| 132 | 136 |
| 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) { | 137 bool CanImportURL(const GURL& url) { |
| 178 const char* kInvalidSchemes[] = {"wyciwyg", "place", "about", "chrome"}; | 138 const char* kInvalidSchemes[] = {"wyciwyg", "place", "about", "chrome"}; |
| 179 | 139 |
| 180 // The URL is not valid. | 140 // The URL is not valid. |
| 181 if (!url.is_valid()) | 141 if (!url.is_valid()) |
| 182 return false; | 142 return false; |
| 183 | 143 |
| 184 // Filter out the URLs with unsupported schemes. | 144 // Filter out the URLs with unsupported schemes. |
| 185 for (size_t i = 0; i < arraysize(kInvalidSchemes); ++i) { | 145 for (size_t i = 0; i < arraysize(kInvalidSchemes); ++i) { |
| 186 if (url.SchemeIs(kInvalidSchemes[i])) | 146 if (url.SchemeIs(kInvalidSchemes[i])) |
| (...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 459 } | 419 } |
| 460 } | 420 } |
| 461 } | 421 } |
| 462 } | 422 } |
| 463 | 423 |
| 464 StringToLowerASCII(&branding_name); | 424 StringToLowerASCII(&branding_name); |
| 465 if (branding_name.find("iceweasel") != std::string::npos) | 425 if (branding_name.find("iceweasel") != std::string::npos) |
| 466 return l10n_util::GetStringUTF16(IDS_IMPORT_FROM_ICEWEASEL); | 426 return l10n_util::GetStringUTF16(IDS_IMPORT_FROM_ICEWEASEL); |
| 467 return l10n_util::GetStringUTF16(IDS_IMPORT_FROM_FIREFOX); | 427 return l10n_util::GetStringUTF16(IDS_IMPORT_FROM_FIREFOX); |
| 468 } | 428 } |
| OLD | NEW |