Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(49)

Side by Side Diff: chrome/browser/extensions/extension_i18n_api.cc

Issue 10701026: Use JSON schema compiler for i18n api (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Cleaned up code Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | chrome/common/extensions/api/api.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/extensions/extension_i18n_api.h" 5 #include "chrome/browser/extensions/extension_i18n_api.h"
6 6
7 #include <algorithm>
8 #include <string>
9 #include <vector>
10
7 #include "base/string_piece.h" 11 #include "base/string_piece.h"
12 #include "base/string_split.h"
8 #include "base/utf_string_conversions.h" 13 #include "base/utf_string_conversions.h"
9 #include "chrome/browser/prefs/pref_service.h" 14 #include "chrome/browser/prefs/pref_service.h"
10 #include "chrome/browser/profiles/profile.h" 15 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/common/pref_names.h" 16 #include "chrome/common/pref_names.h"
17 #include "chrome/common/extensions/api/i18n.h"
18
19 namespace GetAcceptLanguages = extensions::api::i18n::GetAcceptLanguages;
12 20
13 // Errors. 21 // Errors.
14 static const char kEmptyAcceptLanguagesError[] = "accept-languages is empty."; 22 static const char kEmptyAcceptLanguagesError[] = "accept-languages is empty.";
15 23
16 bool GetAcceptLanguagesFunction::RunImpl() { 24 bool GetAcceptLanguagesFunction::RunImpl() {
17 string16 acceptLanguages = 25 string16 accept_languages =
18 UTF8ToUTF16(profile()->GetPrefs()->GetString(prefs::kAcceptLanguages)); 26 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.
19 // Currently, there are 2 ways to set browser's accept-languages: through UI 27 // 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 28 // 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 29 // UI is guranteed to be valid, and the accept-languages string returned from
22 // profile()->GetPrefs()->GetString(prefs::kAcceptLanguages) is guranteed to 30 // profile()->GetPrefs()->GetString(prefs::kAcceptLanguages) is guranteed to
23 // be valid and well-formed, which means each accept-langauge is a valid 31 // be valid and well-formed, which means each accept-langauge is a valid
24 // code, and accept-languages are seperatd by "," without surrrounding 32 // code, and accept-languages are seperatd by "," without surrrounding
25 // spaces. But we do not do any validation (either the format or the validity 33 // 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 34 // 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 35 // file directly. So, here, we're adding extra checks to be resistant to
28 // crashes caused by data corruption. 36 // crashes caused by data corruption.
29 ListValue* result_languages = new ListValue(); 37 if (accept_languages.empty()) {
30 SetResult(result_languages);
31 if (acceptLanguages.empty()) {
32 error_ = kEmptyAcceptLanguagesError; 38 error_ = kEmptyAcceptLanguagesError;
33 return false; 39 return false;
34 } 40 }
35 size_t begin = 0; 41
36 size_t end; 42 std::vector<string16> languages_utf16;
37 while (1) { 43 base::SplitString(accept_languages, ',', &languages_utf16);
38 end = acceptLanguages.find(',', begin); 44
39 if (end > begin) { 45 std::vector<std::string> languages_utf8;
40 // Guard against a malformed value with multiple "," in a row. 46 for (std::vector<string16>::iterator iter = languages_utf16.begin();
41 string16 acceptLang = acceptLanguages.substr(begin, end - begin); 47 iter != languages_utf16.end(); ++iter) {
42 result_languages->Append(Value::CreateStringValue(acceptLang)); 48 languages_utf8.push_back(UTF16ToUTF8(*iter));
43 }
44 begin = end + 1;
45 // 'begin >= acceptLanguages.length()' to guard against a value
46 // ending with ','.
47 if (end == string16::npos || begin >= acceptLanguages.length())
48 break;
49 } 49 }
50 if (result_languages->GetSize() == 0) { 50
51 std::vector<std::string>::iterator iter =
52 std::remove(languages_utf8.begin(), languages_utf8.end(), "");
53 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.
54
55 if (languages_utf8.empty()) {
51 error_ = kEmptyAcceptLanguagesError; 56 error_ = kEmptyAcceptLanguagesError;
52 return false; 57 return false;
53 } 58 }
59
60 results_ = GetAcceptLanguages::Results::Create(languages_utf8);
54 return true; 61 return true;
55 } 62 }
OLDNEW
« no previous file with comments | « no previous file | chrome/common/extensions/api/api.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698