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

Side by Side Diff: chrome/browser/extensions/api/i18n/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: Moved to c/b/e/api/i18n 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
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/api/i18n/i18n_api.h"
6
7 #include <algorithm>
8 #include <string>
9 #include <vector>
6 10
7 #include "base/string_piece.h" 11 #include "base/string_piece.h"
8 #include "base/utf_string_conversions.h" 12 #include "base/string_split.h"
9 #include "chrome/browser/prefs/pref_service.h" 13 #include "chrome/browser/prefs/pref_service.h"
10 #include "chrome/browser/profiles/profile.h" 14 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/common/pref_names.h" 15 #include "chrome/common/pref_names.h"
16 #include "chrome/common/extensions/api/i18n.h"
17
18 namespace GetAcceptLanguages = extensions::api::i18n::GetAcceptLanguages;
12 19
13 // Errors. 20 // Errors.
14 static const char kEmptyAcceptLanguagesError[] = "accept-languages is empty."; 21 static const char kEmptyAcceptLanguagesError[] = "accept-languages is empty.";
15 22
16 bool GetAcceptLanguagesFunction::RunImpl() { 23 bool GetAcceptLanguagesFunction::RunImpl() {
17 string16 acceptLanguages = 24 std::string accept_languages =
18 UTF8ToUTF16(profile()->GetPrefs()->GetString(prefs::kAcceptLanguages)); 25 profile()->GetPrefs()->GetString(prefs::kAcceptLanguages);
19 // Currently, there are 2 ways to set browser's accept-languages: through UI 26 // 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 27 // 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 28 // UI is guranteed to be valid, and the accept-languages string returned from
22 // profile()->GetPrefs()->GetString(prefs::kAcceptLanguages) is guranteed to 29 // profile()->GetPrefs()->GetString(prefs::kAcceptLanguages) is guranteed to
23 // be valid and well-formed, which means each accept-langauge is a valid 30 // be valid and well-formed, which means each accept-langauge is a valid
24 // code, and accept-languages are seperatd by "," without surrrounding 31 // code, and accept-languages are seperatd by "," without surrrounding
25 // spaces. But we do not do any validation (either the format or the validity 32 // 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 33 // 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 34 // file directly. So, here, we're adding extra checks to be resistant to
28 // crashes caused by data corruption. 35 // crashes caused by data corruption.
29 ListValue* result_languages = new ListValue(); 36 if (accept_languages.empty()) {
30 SetResult(result_languages);
31 if (acceptLanguages.empty()) {
32 error_ = kEmptyAcceptLanguagesError; 37 error_ = kEmptyAcceptLanguagesError;
33 return false; 38 return false;
34 } 39 }
35 size_t begin = 0; 40
36 size_t end; 41 std::vector<std::string> languages;
37 while (1) { 42 base::SplitString(accept_languages, ',', &languages);
38 end = acceptLanguages.find(',', begin); 43 languages.erase(std::remove(languages.begin(), languages.end(), ""),
39 if (end > begin) { 44 languages.end());
40 // Guard against a malformed value with multiple "," in a row. 45
41 string16 acceptLang = acceptLanguages.substr(begin, end - begin); 46 if (languages.empty()) {
42 result_languages->Append(Value::CreateStringValue(acceptLang));
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 }
50 if (result_languages->GetSize() == 0) {
51 error_ = kEmptyAcceptLanguagesError; 47 error_ = kEmptyAcceptLanguagesError;
52 return false; 48 return false;
53 } 49 }
50
51 results_ = GetAcceptLanguages::Results::Create(languages);
54 return true; 52 return true;
55 } 53 }
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/i18n/i18n_api.h ('k') | chrome/browser/extensions/api/i18n/i18n_apitest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698