OLD | NEW |
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/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 |
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
275 | 275 |
276 return content.substr(start + 1, stop - start - 1); | 276 return content.substr(start + 1, stop - start - 1); |
277 } | 277 } |
278 | 278 |
279 std::string ReadPrefsJsValue(const FilePath& profile_path, | 279 std::string ReadPrefsJsValue(const FilePath& profile_path, |
280 const std::string& pref_key) { | 280 const std::string& pref_key) { |
281 std::string content; | 281 std::string content; |
282 if (!ReadPrefFile(profile_path.AppendASCII("prefs.js"), &content)) | 282 if (!ReadPrefFile(profile_path.AppendASCII("prefs.js"), &content)) |
283 return ""; | 283 return ""; |
284 | 284 |
285 // This file has the syntax: user_pref("key", value); | 285 return GetPrefsJsValue(content, pref_key); |
286 std::string search_for = std::string("user_pref(\"") + pref_key + | |
287 std::string("\", "); | |
288 size_t prop_index = content.find(search_for); | |
289 if (prop_index == std::string::npos) | |
290 return ""; | |
291 | |
292 size_t start = prop_index + search_for.length(); | |
293 size_t stop = std::string::npos; | |
294 if (start != std::string::npos) | |
295 stop = content.find(")", start + 1); | |
296 | |
297 if (start == std::string::npos || stop == std::string::npos) { | |
298 NOTREACHED() << "Firefox property " << pref_key << " could not be parsed."; | |
299 return ""; | |
300 } | |
301 | |
302 // String values have double quotes we don't need to return to the caller. | |
303 if (content[start] == '\"' && content[stop - 1] == '\"') { | |
304 ++start; | |
305 --stop; | |
306 } | |
307 | |
308 return content.substr(start, stop - start); | |
309 } | 286 } |
310 | 287 |
311 int GetFirefoxDefaultSearchEngineIndex( | 288 int GetFirefoxDefaultSearchEngineIndex( |
312 const std::vector<TemplateURL*>& search_engines, | 289 const std::vector<TemplateURL*>& search_engines, |
313 const FilePath& profile_path) { | 290 const FilePath& profile_path) { |
314 // The default search engine is contained in the file prefs.js found in the | 291 // The default search engine is contained in the file prefs.js found in the |
315 // profile directory. | 292 // profile directory. |
316 // It is the "browser.search.selectedEngine" property. | 293 // It is the "browser.search.selectedEngine" property. |
317 if (search_engines.empty()) | 294 if (search_engines.empty()) |
318 return -1; | 295 return -1; |
319 | 296 |
320 std::wstring default_se_name = UTF8ToWide( | 297 std::string default_se_name = |
321 ReadPrefsJsValue(profile_path, "browser.search.selectedEngine")); | 298 ReadPrefsJsValue(profile_path, "browser.search.selectedEngine"); |
322 | 299 |
323 if (default_se_name.empty()) { | 300 if (default_se_name.empty()) { |
324 // browser.search.selectedEngine does not exist if the user has not changed | 301 // browser.search.selectedEngine does not exist if the user has not changed |
325 // from the default (or has selected the default). | 302 // from the default (or has selected the default). |
326 // TODO: should fallback to 'browser.search.defaultengine' if selectedEngine | 303 // TODO: should fallback to 'browser.search.defaultengine' if selectedEngine |
327 // is empty. | 304 // is empty. |
328 return -1; | 305 return -1; |
329 } | 306 } |
330 | 307 |
331 int default_se_index = -1; | 308 int default_se_index = -1; |
332 for (std::vector<TemplateURL*>::const_iterator iter = search_engines.begin(); | 309 for (std::vector<TemplateURL*>::const_iterator iter = search_engines.begin(); |
333 iter != search_engines.end(); ++iter) { | 310 iter != search_engines.end(); ++iter) { |
334 if (default_se_name == (*iter)->short_name()) { | 311 if (default_se_name == WideToUTF8((*iter)->short_name())) { |
335 default_se_index = static_cast<int>(iter - search_engines.begin()); | 312 default_se_index = static_cast<int>(iter - search_engines.begin()); |
336 break; | 313 break; |
337 } | 314 } |
338 } | 315 } |
339 if (default_se_index == -1) { | 316 if (default_se_index == -1) { |
340 NOTREACHED() << | 317 NOTREACHED() << |
341 "Firefox default search engine not found in search engine list"; | 318 "Firefox default search engine not found in search engine list"; |
342 } | 319 } |
343 | 320 |
344 return default_se_index; | 321 return default_se_index; |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
442 if (base::StringToInt(value, &int_value)) { | 419 if (base::StringToInt(value, &int_value)) { |
443 prefs->SetInteger(key, int_value); | 420 prefs->SetInteger(key, int_value); |
444 continue; | 421 continue; |
445 } | 422 } |
446 | 423 |
447 LOG(ERROR) << "Invalid value found in Firefox pref file '" << | 424 LOG(ERROR) << "Invalid value found in Firefox pref file '" << |
448 pref_file.value() << "' value is '" << value << "'."; | 425 pref_file.value() << "' value is '" << value << "'."; |
449 } | 426 } |
450 return true; | 427 return true; |
451 } | 428 } |
| 429 |
| 430 std::string GetPrefsJsValue(const std::string& content, |
| 431 const std::string& pref_key) { |
| 432 // This file has the syntax: user_pref("key", value); |
| 433 std::string search_for = std::string("user_pref(\"") + pref_key + |
| 434 std::string("\", "); |
| 435 size_t prop_index = content.find(search_for); |
| 436 if (prop_index == std::string::npos) |
| 437 return ""; |
| 438 |
| 439 size_t start = prop_index + search_for.length(); |
| 440 size_t stop = std::string::npos; |
| 441 if (start != std::string::npos) { |
| 442 // Stop at the last ')' on this line. |
| 443 stop = content.find("\n", start + 1); |
| 444 stop = content.rfind(")", stop); |
| 445 } |
| 446 |
| 447 if (start == std::string::npos || stop == std::string::npos || |
| 448 stop < start) { |
| 449 LOG(WARNING) << "Firefox property " << pref_key << " could not be parsed."; |
| 450 return ""; |
| 451 } |
| 452 |
| 453 // String values have double quotes we don't need to return to the caller. |
| 454 if (content[start] == '\"' && content[stop - 1] == '\"') { |
| 455 ++start; |
| 456 --stop; |
| 457 } |
| 458 |
| 459 return content.substr(start, stop - start); |
| 460 } |
OLD | NEW |