| 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/ini_parser.h" | 12 #include "base/ini_parser.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/strings/string_split.h" | 15 #include "base/strings/string_split.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" | |
| 20 #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_service.h" | |
| 23 #include "googleurl/src/gurl.h" | 19 #include "googleurl/src/gurl.h" |
| 24 #include "grit/generated_resources.h" | 20 #include "grit/generated_resources.h" |
| 25 #include "ui/base/l10n/l10n_util.h" | 21 #include "ui/base/l10n/l10n_util.h" |
| 26 | 22 |
| 27 namespace { | |
| 28 | |
| 29 // FirefoxURLParameterFilter is used to remove parameter mentioning Firefox from | |
| 30 // the search URL when importing search engines. | |
| 31 class FirefoxURLParameterFilter : public TemplateURLParser::ParameterFilter { | |
| 32 public: | |
| 33 FirefoxURLParameterFilter() {} | |
| 34 virtual ~FirefoxURLParameterFilter() {} | |
| 35 | |
| 36 // TemplateURLParser::ParameterFilter method. | |
| 37 virtual bool KeepParameter(const std::string& key, | |
| 38 const std::string& value) OVERRIDE { | |
| 39 std::string low_value = StringToLowerASCII(value); | |
| 40 if (low_value.find("mozilla") != std::string::npos || | |
| 41 low_value.find("firefox") != std::string::npos || | |
| 42 low_value.find("moz:") != std::string::npos ) | |
| 43 return false; | |
| 44 return true; | |
| 45 } | |
| 46 | |
| 47 private: | |
| 48 DISALLOW_COPY_AND_ASSIGN(FirefoxURLParameterFilter); | |
| 49 }; | |
| 50 } // namespace | |
| 51 | |
| 52 base::FilePath GetFirefoxProfilePath() { | 23 base::FilePath GetFirefoxProfilePath() { |
| 53 base::FilePath ini_file = GetProfilesINI(); | 24 base::FilePath ini_file = GetProfilesINI(); |
| 54 std::string content; | 25 std::string content; |
| 55 file_util::ReadFileToString(ini_file, &content); | 26 file_util::ReadFileToString(ini_file, &content); |
| 56 base::DictionaryValueINIParser ini_parser; | 27 base::DictionaryValueINIParser ini_parser; |
| 57 ini_parser.Parse(content); | 28 ini_parser.Parse(content); |
| 58 const DictionaryValue& root = ini_parser.root(); | 29 const DictionaryValue& root = ini_parser.root(); |
| 59 | 30 |
| 60 base::FilePath source_path; | 31 base::FilePath source_path; |
| 61 for (int i = 0; ; ++i) { | 32 for (int i = 0; ; ++i) { |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 | 113 |
| 143 // Filter out the URLs with unsupported schemes. | 114 // Filter out the URLs with unsupported schemes. |
| 144 for (size_t i = 0; i < arraysize(kInvalidSchemes); ++i) { | 115 for (size_t i = 0; i < arraysize(kInvalidSchemes); ++i) { |
| 145 if (url.SchemeIs(kInvalidSchemes[i])) | 116 if (url.SchemeIs(kInvalidSchemes[i])) |
| 146 return false; | 117 return false; |
| 147 } | 118 } |
| 148 | 119 |
| 149 return true; | 120 return true; |
| 150 } | 121 } |
| 151 | 122 |
| 152 void ParseSearchEnginesFromXMLFiles( | |
| 153 const std::vector<base::FilePath>& xml_files, | |
| 154 std::vector<TemplateURL*>* search_engines) { | |
| 155 DCHECK(search_engines); | |
| 156 | |
| 157 typedef std::map<std::string, TemplateURL*> SearchEnginesMap; | |
| 158 SearchEnginesMap search_engine_for_url; | |
| 159 std::string content; | |
| 160 // The first XML file represents the default search engine in Firefox 3, so we | |
| 161 // need to keep it on top of the list. | |
| 162 SearchEnginesMap::const_iterator default_turl = search_engine_for_url.end(); | |
| 163 for (std::vector<base::FilePath>::const_iterator file_iter = | |
| 164 xml_files.begin(); file_iter != xml_files.end(); ++file_iter) { | |
| 165 file_util::ReadFileToString(*file_iter, &content); | |
| 166 FirefoxURLParameterFilter param_filter; | |
| 167 TemplateURL* template_url = TemplateURLParser::Parse(NULL, true, | |
| 168 content.data(), content.length(), ¶m_filter); | |
| 169 if (template_url) { | |
| 170 SearchEnginesMap::iterator iter = | |
| 171 search_engine_for_url.find(template_url->url()); | |
| 172 if (iter == search_engine_for_url.end()) { | |
| 173 iter = search_engine_for_url.insert( | |
| 174 std::make_pair(template_url->url(), template_url)).first; | |
| 175 } else { | |
| 176 // We have already found a search engine with the same URL. We give | |
| 177 // priority to the latest one found, as GetSearchEnginesXMLFiles() | |
| 178 // returns a vector with first Firefox default search engines and then | |
| 179 // the user's ones. We want to give priority to the user ones. | |
| 180 delete iter->second; | |
| 181 iter->second = template_url; | |
| 182 } | |
| 183 if (default_turl == search_engine_for_url.end()) | |
| 184 default_turl = iter; | |
| 185 } | |
| 186 content.clear(); | |
| 187 } | |
| 188 | |
| 189 // Put the results in the |search_engines| vector. | |
| 190 for (SearchEnginesMap::iterator t_iter = search_engine_for_url.begin(); | |
| 191 t_iter != search_engine_for_url.end(); ++t_iter) { | |
| 192 if (t_iter == default_turl) | |
| 193 search_engines->insert(search_engines->begin(), default_turl->second); | |
| 194 else | |
| 195 search_engines->push_back(t_iter->second); | |
| 196 } | |
| 197 } | |
| 198 | |
| 199 bool ReadPrefFile(const base::FilePath& path, std::string* content) { | 123 bool ReadPrefFile(const base::FilePath& path, std::string* content) { |
| 200 if (content == NULL) | 124 if (content == NULL) |
| 201 return false; | 125 return false; |
| 202 | 126 |
| 203 file_util::ReadFileToString(path, content); | 127 file_util::ReadFileToString(path, content); |
| 204 | 128 |
| 205 if (content->empty()) { | 129 if (content->empty()) { |
| 206 LOG(WARNING) << "Firefox preference file " << path.value() << " is empty."; | 130 LOG(WARNING) << "Firefox preference file " << path.value() << " is empty."; |
| 207 return false; | 131 return false; |
| 208 } | 132 } |
| (...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 418 } | 342 } |
| 419 } | 343 } |
| 420 } | 344 } |
| 421 } | 345 } |
| 422 | 346 |
| 423 StringToLowerASCII(&branding_name); | 347 StringToLowerASCII(&branding_name); |
| 424 if (branding_name.find("iceweasel") != std::string::npos) | 348 if (branding_name.find("iceweasel") != std::string::npos) |
| 425 return l10n_util::GetStringUTF16(IDS_IMPORT_FROM_ICEWEASEL); | 349 return l10n_util::GetStringUTF16(IDS_IMPORT_FROM_ICEWEASEL); |
| 426 return l10n_util::GetStringUTF16(IDS_IMPORT_FROM_FIREFOX); | 350 return l10n_util::GetStringUTF16(IDS_IMPORT_FROM_FIREFOX); |
| 427 } | 351 } |
| OLD | NEW |