Index: chrome/browser/extensions/extension_i18n_api.cc |
diff --git a/chrome/browser/extensions/extension_i18n_api.cc b/chrome/browser/extensions/extension_i18n_api.cc |
index d1b87080ccc92ff54045cb65acac7978e0363ca9..8a30216df706922bd4418a56b833f6ee1811dd42 100644 |
--- a/chrome/browser/extensions/extension_i18n_api.cc |
+++ b/chrome/browser/extensions/extension_i18n_api.cc |
@@ -4,17 +4,25 @@ |
#include "chrome/browser/extensions/extension_i18n_api.h" |
+#include <algorithm> |
+#include <string> |
+#include <vector> |
+ |
#include "base/string_piece.h" |
+#include "base/string_split.h" |
#include "base/utf_string_conversions.h" |
#include "chrome/browser/prefs/pref_service.h" |
#include "chrome/browser/profiles/profile.h" |
#include "chrome/common/pref_names.h" |
+#include "chrome/common/extensions/api/i18n.h" |
+ |
+namespace GetAcceptLanguages = extensions::api::i18n::GetAcceptLanguages; |
// Errors. |
static const char kEmptyAcceptLanguagesError[] = "accept-languages is empty."; |
bool GetAcceptLanguagesFunction::RunImpl() { |
- string16 acceptLanguages = |
+ string16 accept_languages = |
UTF8ToUTF16(profile()->GetPrefs()->GetString(prefs::kAcceptLanguages)); |
not at google - send to devlin
2012/07/30 11:35:47
This is weird that we convert from utf8 to utf16 t
mitchellwrosen
2012/07/30 17:56:05
Done.
|
// Currently, there are 2 ways to set browser's accept-languages: through UI |
// or directly modify the preference file. The accept-languages set through |
@@ -26,30 +34,29 @@ bool GetAcceptLanguagesFunction::RunImpl() { |
// of the language code) on accept-languages set through editing preference |
// file directly. So, here, we're adding extra checks to be resistant to |
// crashes caused by data corruption. |
- ListValue* result_languages = new ListValue(); |
- SetResult(result_languages); |
- if (acceptLanguages.empty()) { |
+ if (accept_languages.empty()) { |
error_ = kEmptyAcceptLanguagesError; |
return false; |
} |
- size_t begin = 0; |
- size_t end; |
- while (1) { |
- end = acceptLanguages.find(',', begin); |
- if (end > begin) { |
- // Guard against a malformed value with multiple "," in a row. |
- string16 acceptLang = acceptLanguages.substr(begin, end - begin); |
- result_languages->Append(Value::CreateStringValue(acceptLang)); |
- } |
- begin = end + 1; |
- // 'begin >= acceptLanguages.length()' to guard against a value |
- // ending with ','. |
- if (end == string16::npos || begin >= acceptLanguages.length()) |
- break; |
+ |
+ std::vector<string16> languages_utf16; |
+ base::SplitString(accept_languages, ',', &languages_utf16); |
+ |
+ std::vector<std::string> languages_utf8; |
+ for (std::vector<string16>::iterator iter = languages_utf16.begin(); |
+ iter != languages_utf16.end(); ++iter) { |
+ languages_utf8.push_back(UTF16ToUTF8(*iter)); |
} |
- if (result_languages->GetSize() == 0) { |
+ |
+ std::vector<std::string>::iterator iter = |
+ std::remove(languages_utf8.begin(), languages_utf8.end(), ""); |
+ languages_utf8.erase(iter, languages_utf8.end()); |
not at google - send to devlin
2012/07/30 11:35:47
perhaps inline iter? Unless there's some strange C
mitchellwrosen
2012/07/30 17:56:05
Done.
|
+ |
+ if (languages_utf8.empty()) { |
error_ = kEmptyAcceptLanguagesError; |
return false; |
} |
+ |
+ results_ = GetAcceptLanguages::Results::Create(languages_utf8); |
return true; |
} |