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/extensions/extension_i18n_api.h" | 5 #include "chrome/browser/extensions/extension_i18n_api.h" |
6 | 6 |
7 #include <string> | |
8 #include <vector> | |
9 | |
7 #include "base/string_piece.h" | 10 #include "base/string_piece.h" |
8 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
9 #include "chrome/browser/prefs/pref_service.h" | 12 #include "chrome/browser/prefs/pref_service.h" |
10 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
11 #include "chrome/common/pref_names.h" | 14 #include "chrome/common/pref_names.h" |
15 #include "chrome/common/extensions/api/i18n.h" | |
16 | |
17 namespace GetAcceptLanguages = extensions::api::i18n::GetAcceptLanguages; | |
12 | 18 |
13 // Errors. | 19 // Errors. |
14 static const char kEmptyAcceptLanguagesError[] = "accept-languages is empty."; | 20 static const char kEmptyAcceptLanguagesError[] = "accept-languages is empty."; |
15 | 21 |
16 bool GetAcceptLanguagesFunction::RunImpl() { | 22 bool GetAcceptLanguagesFunction::RunImpl() { |
17 string16 acceptLanguages = | 23 string16 acceptLanguages = |
not at google - send to devlin
2012/06/29 04:58:00
Style should be accept_languages not acceptLanguag
| |
18 UTF8ToUTF16(profile()->GetPrefs()->GetString(prefs::kAcceptLanguages)); | 24 UTF8ToUTF16(profile()->GetPrefs()->GetString(prefs::kAcceptLanguages)); |
19 // Currently, there are 2 ways to set browser's accept-languages: through UI | 25 // Currently, there are 2 ways to set browser's accept-languages: through UI |
20 // or directly modify the preference file. The accept-languages set through | 26 // or directly modify the preference file. The accept-languages set through |
21 // UI is guranteed to be valid, and the accept-languages string returned from | 27 // UI is guranteed to be valid, and the accept-languages string returned from |
22 // profile()->GetPrefs()->GetString(prefs::kAcceptLanguages) is guranteed to | 28 // profile()->GetPrefs()->GetString(prefs::kAcceptLanguages) is guranteed to |
23 // be valid and well-formed, which means each accept-langauge is a valid | 29 // be valid and well-formed, which means each accept-langauge is a valid |
24 // code, and accept-languages are seperatd by "," without surrrounding | 30 // code, and accept-languages are seperatd by "," without surrrounding |
25 // spaces. But we do not do any validation (either the format or the validity | 31 // spaces. But we do not do any validation (either the format or the validity |
26 // of the language code) on accept-languages set through editing preference | 32 // of the language code) on accept-languages set through editing preference |
27 // file directly. So, here, we're adding extra checks to be resistant to | 33 // file directly. So, here, we're adding extra checks to be resistant to |
28 // crashes caused by data corruption. | 34 // crashes caused by data corruption. |
29 result_.reset(new ListValue()); | |
30 if (acceptLanguages.empty()) { | 35 if (acceptLanguages.empty()) { |
31 error_ = kEmptyAcceptLanguagesError; | 36 error_ = kEmptyAcceptLanguagesError; |
32 return false; | 37 return false; |
33 } | 38 } |
34 size_t begin = 0; | 39 size_t begin = 0; |
35 size_t end; | 40 size_t end; |
41 std::vector<std::string> languages_vec; | |
not at google - send to devlin
2012/06/29 04:58:00
just "languages"?
mitchellwrosen
2012/07/27 23:51:41
Done.
| |
36 while (1) { | 42 while (1) { |
37 end = acceptLanguages.find(',', begin); | 43 end = acceptLanguages.find(',', begin); |
38 if (end > begin) { | 44 if (end > begin) { |
39 // Guard against a malformed value with multiple "," in a row. | 45 // Guard against a malformed value with multiple "," in a row. |
40 string16 acceptLang = acceptLanguages.substr(begin, end - begin); | 46 languages_vec.push_back( |
41 static_cast<ListValue*>(result_.get())-> | 47 UTF16ToUTF8(acceptLanguages.substr(begin, end - begin))); |
42 Append(Value::CreateStringValue(acceptLang)); | |
43 } | 48 } |
44 begin = end + 1; | 49 begin = end + 1; |
45 // 'begin >= acceptLanguages.length()' to guard against a value | 50 // 'begin >= acceptLanguages.length()' to guard against a value |
46 // ending with ','. | 51 // ending with ','. |
47 if (end == string16::npos || begin >= acceptLanguages.length()) | 52 if (end == string16::npos || begin >= acceptLanguages.length()) |
48 break; | 53 break; |
49 } | 54 } |
not at google - send to devlin
2012/06/29 04:58:00
This code should be using base::SplitString (base/
| |
50 if (static_cast<ListValue*>(result_.get())->GetSize() == 0) { | 55 |
56 if (languages_vec.empty()) { | |
51 error_ = kEmptyAcceptLanguagesError; | 57 error_ = kEmptyAcceptLanguagesError; |
52 return false; | 58 return false; |
53 } | 59 } |
60 | |
61 result_.reset(GetAcceptLanguages::Result::Create(languages_vec)); | |
54 return true; | 62 return true; |
55 } | 63 } |
OLD | NEW |