OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/extensions/extension_i18n_api.h" |
| 6 |
| 7 #include "chrome/browser/profile.h" |
| 8 #include "chrome/common/pref_names.h" |
| 9 |
| 10 namespace extension_i18n_api_functions { |
| 11 const char kGetAcceptLanguagesFunction[] = "i18n.getAcceptLanguages"; |
| 12 } // namespace extension_i18n_api_functions |
| 13 |
| 14 namespace { |
| 15 // Errors. |
| 16 const char kEmptyAcceptLanguagesError[] = "accept-languages is empty."; |
| 17 } // namespace |
| 18 |
| 19 bool GetAcceptLanguagesFunction::RunImpl() { |
| 20 std::wstring acceptLanguages = |
| 21 profile()->GetPrefs()->GetString(prefs::kAcceptLanguages); |
| 22 // Currently, there are 2 ways to set browser's accept-languages: through UI |
| 23 // or directly modify the preference file. The accept-languages set through |
| 24 // UI is guranteed to be valid, and the accept-languages string returned from |
| 25 // profile()->GetPrefs()->GetString(prefs::kAcceptLanguages) is guranteed to |
| 26 // be valid and well-formed, which means each accept-langauge is a valid |
| 27 // code, and accept-languages are seperatd by "," without surrrounding |
| 28 // spaces. But we do not do any validation (either the format or the validity |
| 29 // of the language code) on accept-languages set through editing preference |
| 30 // file directly. So, here, we're adding extra checks to be resistant to |
| 31 // crashes caused by data corruption. |
| 32 result_.reset(new ListValue()); |
| 33 if (acceptLanguages.empty()) { |
| 34 error_ = kEmptyAcceptLanguagesError; |
| 35 return false; |
| 36 } |
| 37 size_t begin = 0; |
| 38 size_t end; |
| 39 std::wstring acceptLang; |
| 40 while (1) { |
| 41 end = acceptLanguages.find(',', begin); |
| 42 if (end > begin) { |
| 43 // Guard against a malformed value with multiple "," in a row. |
| 44 acceptLang = acceptLanguages.substr(begin, end - begin); |
| 45 static_cast<ListValue*>(result_.get())-> |
| 46 Append(Value::CreateStringValue(acceptLang)); |
| 47 } |
| 48 begin = end + 1; |
| 49 // 'begin >= acceptLanguages.length()' to guard against a value |
| 50 // ending with ','. |
| 51 if (end == std::wstring::npos || begin >= acceptLanguages.length()) |
| 52 break; |
| 53 } |
| 54 if (static_cast<ListValue*>(result_.get())->GetSize() == 0) { |
| 55 error_ = kEmptyAcceptLanguagesError; |
| 56 return false; |
| 57 } |
| 58 return true; |
| 59 } |
OLD | NEW |