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

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

Issue 174116: chrome.i18n API (accept languages part 1) (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 3 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 | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(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 }
OLDNEW
« no previous file with comments | « chrome/browser/extensions/extension_i18n_api.h ('k') | chrome/browser/extensions/extension_i18n_apitest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698