diff options
author | xji@chromium.org <xji@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-09-09 22:56:28 +0000 |
---|---|---|
committer | xji@chromium.org <xji@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-09-09 22:56:28 +0000 |
commit | 198bcfe93dd75609fd50caca76f702b28934f48c (patch) | |
tree | 5fb68a530c89540fd43ecfed4bf8b69bf75ad3d9 /chrome/browser/extensions/extension_i18n_api.cc | |
parent | bcc9ae958d2f957a8deb766f994b3039758e4ee6 (diff) | |
download | chromium_src-198bcfe93dd75609fd50caca76f702b28934f48c.zip chromium_src-198bcfe93dd75609fd50caca76f702b28934f48c.tar.gz chromium_src-198bcfe93dd75609fd50caca76f702b28934f48c.tar.bz2 |
Added chrome.i18n extension API.
The API proposal doc is
http://sites.google.com/a/chromium.org/dev/developers/design-documents/extensions/i18n-api
This CL only adds the getter of accept languages:
chrome.i18n.getAcceptLanguages().
The setter of accept languages will be added in a separate CL.
BUG=http://crbug.com/14574
TEST=TEST=There is an extension in chrome\test\data\extensions\samples\i18n.
Load this extension. It creates a toolstrip button. Click this button, you
should get chrome's accept languages.
Review URL: http://codereview.chromium.org/174116
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@25810 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/extensions/extension_i18n_api.cc')
-rw-r--r-- | chrome/browser/extensions/extension_i18n_api.cc | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/chrome/browser/extensions/extension_i18n_api.cc b/chrome/browser/extensions/extension_i18n_api.cc new file mode 100644 index 0000000..01048e9 --- /dev/null +++ b/chrome/browser/extensions/extension_i18n_api.cc @@ -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; +} |