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

Unified 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 side-by-side diff with in-line comments
Download patch
« 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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/extensions/extension_i18n_api.cc
===================================================================
--- chrome/browser/extensions/extension_i18n_api.cc (revision 0)
+++ chrome/browser/extensions/extension_i18n_api.cc (revision 0)
@@ -0,0 +1,59 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/extensions/extension_i18n_api.h"
+
+#include "chrome/browser/profile.h"
+#include "chrome/common/pref_names.h"
+
+namespace extension_i18n_api_functions {
+ const char kGetAcceptLanguagesFunction[] = "i18n.getAcceptLanguages";
+} // namespace extension_i18n_api_functions
+
+namespace {
+ // Errors.
+ const char kEmptyAcceptLanguagesError[] = "accept-languages is empty.";
+} // namespace
+
+bool GetAcceptLanguagesFunction::RunImpl() {
+ std::wstring acceptLanguages =
+ profile()->GetPrefs()->GetString(prefs::kAcceptLanguages);
+ // Currently, there are 2 ways to set browser's accept-languages: through UI
+ // or directly modify the preference file. The accept-languages set through
+ // UI is guranteed to be valid, and the accept-languages string returned from
+ // profile()->GetPrefs()->GetString(prefs::kAcceptLanguages) is guranteed to
+ // be valid and well-formed, which means each accept-langauge is a valid
+ // code, and accept-languages are seperatd by "," without surrrounding
+ // spaces. But we do not do any validation (either the format or the validity
+ // of the language code) on accept-languages set through editing preference
+ // file directly. So, here, we're adding extra checks to be resistant to
+ // crashes caused by data corruption.
+ result_.reset(new ListValue());
+ if (acceptLanguages.empty()) {
+ error_ = kEmptyAcceptLanguagesError;
+ return false;
+ }
+ size_t begin = 0;
+ size_t end;
+ std::wstring acceptLang;
+ while (1) {
+ end = acceptLanguages.find(',', begin);
+ if (end > begin) {
+ // Guard against a malformed value with multiple "," in a row.
+ acceptLang = acceptLanguages.substr(begin, end - begin);
+ static_cast<ListValue*>(result_.get())->
+ Append(Value::CreateStringValue(acceptLang));
+ }
+ begin = end + 1;
+ // 'begin >= acceptLanguages.length()' to guard against a value
+ // ending with ','.
+ if (end == std::wstring::npos || begin >= acceptLanguages.length())
+ break;
+ }
+ if (static_cast<ListValue*>(result_.get())->GetSize() == 0) {
+ error_ = kEmptyAcceptLanguagesError;
+ return false;
+ }
+ return true;
+}
Property changes on: chrome\browser\extensions\extension_i18n_api.cc
___________________________________________________________________
Added: svn:eol-style
+ LF
« 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