Use JSON schema compiler in i18n API code

TBR=ben@chromium.org

BUG=121174

Review URL: https://chromiumcodereview.appspot.com/10701026

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@149045 0039d316-1c4b-4281-b951-d872f2087c98
parent 9f8394d8
...@@ -2,20 +2,27 @@ ...@@ -2,20 +2,27 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "chrome/browser/extensions/extension_i18n_api.h" #include "chrome/browser/extensions/api/i18n/i18n_api.h"
#include <algorithm>
#include <string>
#include <vector>
#include "base/string_piece.h" #include "base/string_piece.h"
#include "base/utf_string_conversions.h" #include "base/string_split.h"
#include "chrome/browser/prefs/pref_service.h" #include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/common/pref_names.h" #include "chrome/common/pref_names.h"
#include "chrome/common/extensions/api/i18n.h"
namespace GetAcceptLanguages = extensions::api::i18n::GetAcceptLanguages;
// Errors. // Errors.
static const char kEmptyAcceptLanguagesError[] = "accept-languages is empty."; static const char kEmptyAcceptLanguagesError[] = "accept-languages is empty.";
bool GetAcceptLanguagesFunction::RunImpl() { bool GetAcceptLanguagesFunction::RunImpl() {
string16 acceptLanguages = std::string accept_languages =
UTF8ToUTF16(profile()->GetPrefs()->GetString(prefs::kAcceptLanguages)); profile()->GetPrefs()->GetString(prefs::kAcceptLanguages);
// Currently, there are 2 ways to set browser's accept-languages: through UI // Currently, there are 2 ways to set browser's accept-languages: through UI
// or directly modify the preference file. The accept-languages set through // or directly modify the preference file. The accept-languages set through
// UI is guranteed to be valid, and the accept-languages string returned from // UI is guranteed to be valid, and the accept-languages string returned from
...@@ -26,30 +33,21 @@ bool GetAcceptLanguagesFunction::RunImpl() { ...@@ -26,30 +33,21 @@ bool GetAcceptLanguagesFunction::RunImpl() {
// of the language code) on accept-languages set through editing preference // of the language code) on accept-languages set through editing preference
// file directly. So, here, we're adding extra checks to be resistant to // file directly. So, here, we're adding extra checks to be resistant to
// crashes caused by data corruption. // crashes caused by data corruption.
ListValue* result_languages = new ListValue(); if (accept_languages.empty()) {
SetResult(result_languages);
if (acceptLanguages.empty()) {
error_ = kEmptyAcceptLanguagesError; error_ = kEmptyAcceptLanguagesError;
return false; return false;
} }
size_t begin = 0;
size_t end; std::vector<std::string> languages;
while (1) { base::SplitString(accept_languages, ',', &languages);
end = acceptLanguages.find(',', begin); languages.erase(std::remove(languages.begin(), languages.end(), ""),
if (end > begin) { languages.end());
// Guard against a malformed value with multiple "," in a row.
string16 acceptLang = acceptLanguages.substr(begin, end - begin); if (languages.empty()) {
result_languages->Append(Value::CreateStringValue(acceptLang));
}
begin = end + 1;
// 'begin >= acceptLanguages.length()' to guard against a value
// ending with ','.
if (end == string16::npos || begin >= acceptLanguages.length())
break;
}
if (result_languages->GetSize() == 0) {
error_ = kEmptyAcceptLanguagesError; error_ = kEmptyAcceptLanguagesError;
return false; return false;
} }
results_ = GetAcceptLanguages::Results::Create(languages);
return true; return true;
} }
// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_I18N_API_H__ #ifndef CHROME_BROWSER_EXTENSIONS_API_I18N_I18N_API_H__
#define CHROME_BROWSER_EXTENSIONS_EXTENSION_I18N_API_H__ #define CHROME_BROWSER_EXTENSIONS_API_I18N_I18N_API_H__
#include "chrome/browser/extensions/extension_function.h" #include "chrome/browser/extensions/extension_function.h"
...@@ -13,4 +13,4 @@ class GetAcceptLanguagesFunction : public SyncExtensionFunction { ...@@ -13,4 +13,4 @@ class GetAcceptLanguagesFunction : public SyncExtensionFunction {
DECLARE_EXTENSION_FUNCTION_NAME("i18n.getAcceptLanguages") DECLARE_EXTENSION_FUNCTION_NAME("i18n.getAcceptLanguages")
}; };
#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_I18N_API_H__ #endif // CHROME_BROWSER_EXTENSIONS_API_I18N_I18N_API_H__
// Copyright (c) 2009 The Chromium Authors. All rights reserved. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
......
...@@ -36,7 +36,7 @@ ...@@ -36,7 +36,7 @@
#include "chrome/browser/extensions/api/web_socket_proxy_private/web_socket_proxy_private_api.h" #include "chrome/browser/extensions/api/web_socket_proxy_private/web_socket_proxy_private_api.h"
#include "chrome/browser/extensions/api/webstore_private/webstore_private_api.h" #include "chrome/browser/extensions/api/webstore_private/webstore_private_api.h"
#include "chrome/browser/extensions/extension_font_settings_api.h" #include "chrome/browser/extensions/extension_font_settings_api.h"
#include "chrome/browser/extensions/extension_i18n_api.h" #include "chrome/browser/extensions/api/i18n/i18n_api.h"
#include "chrome/browser/extensions/extension_idle_api.h" #include "chrome/browser/extensions/extension_idle_api.h"
#include "chrome/browser/extensions/extension_management_api.h" #include "chrome/browser/extensions/extension_management_api.h"
#include "chrome/browser/extensions/extension_module.h" #include "chrome/browser/extensions/extension_module.h"
......
...@@ -553,6 +553,8 @@ ...@@ -553,6 +553,8 @@
'browser/extensions/api/page_capture/page_capture_api.h', 'browser/extensions/api/page_capture/page_capture_api.h',
'browser/extensions/api/debugger/debugger_api.cc', 'browser/extensions/api/debugger/debugger_api.cc',
'browser/extensions/api/debugger/debugger_api.h', 'browser/extensions/api/debugger/debugger_api.h',
'browser/extensions/api/i18n/i18n_api.cc',
'browser/extensions/api/i18n/i18n_api.h',
'browser/extensions/api/input_ime/input_ime_api.cc', 'browser/extensions/api/input_ime/input_ime_api.cc',
'browser/extensions/api/input_ime/input_ime_api.h', 'browser/extensions/api/input_ime/input_ime_api.h',
'browser/extensions/api/managed_mode/managed_mode_api.cc', 'browser/extensions/api/managed_mode/managed_mode_api.cc',
...@@ -571,8 +573,6 @@ ...@@ -571,8 +573,6 @@
'browser/extensions/api/webstore_private/webstore_private_api.h', 'browser/extensions/api/webstore_private/webstore_private_api.h',
'browser/extensions/extension_font_settings_api.cc', 'browser/extensions/extension_font_settings_api.cc',
'browser/extensions/extension_font_settings_api.h', 'browser/extensions/extension_font_settings_api.h',
'browser/extensions/extension_i18n_api.cc',
'browser/extensions/extension_i18n_api.h',
'browser/extensions/extension_idle_api.cc', 'browser/extensions/extension_idle_api.cc',
'browser/extensions/extension_idle_api.h', 'browser/extensions/extension_idle_api.h',
'browser/extensions/extension_input_api.cc', 'browser/extensions/extension_input_api.cc',
......
...@@ -2710,6 +2710,7 @@ ...@@ -2710,6 +2710,7 @@
'browser/extensions/api/extension_action/page_as_browser_action_apitest.cc', 'browser/extensions/api/extension_action/page_as_browser_action_apitest.cc',
'browser/extensions/api/extension_action/script_badge_apitest.cc', 'browser/extensions/api/extension_action/script_badge_apitest.cc',
'browser/extensions/api/file_system/file_system_apitest.cc', 'browser/extensions/api/file_system/file_system_apitest.cc',
'browser/extensions/api/i18n/i18n_apitest.cc',
'browser/extensions/api/identity/identity_apitest.cc', 'browser/extensions/api/identity/identity_apitest.cc',
'browser/extensions/api/idltest/idltest_apitest.cc', 'browser/extensions/api/idltest/idltest_apitest.cc',
'browser/extensions/api/input_ime/input_ime_apitest_chromeos.cc', 'browser/extensions/api/input_ime/input_ime_apitest_chromeos.cc',
...@@ -2766,7 +2767,6 @@ ...@@ -2766,7 +2767,6 @@
'browser/extensions/extension_function_test_utils.h', 'browser/extensions/extension_function_test_utils.h',
'browser/extensions/extension_geolocation_apitest.cc', 'browser/extensions/extension_geolocation_apitest.cc',
'browser/extensions/extension_get_views_apitest.cc', 'browser/extensions/extension_get_views_apitest.cc',
'browser/extensions/extension_i18n_apitest.cc',
'browser/extensions/extension_icon_source_apitest.cc', 'browser/extensions/extension_icon_source_apitest.cc',
'browser/extensions/extension_idle_apitest.cc', 'browser/extensions/extension_idle_apitest.cc',
'browser/extensions/extension_incognito_apitest.cc', 'browser/extensions/extension_incognito_apitest.cc',
......
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
'events.json', 'events.json',
'experimental_record.json', 'experimental_record.json',
'file_browser_handler_internal.json', 'file_browser_handler_internal.json',
'i18n.json',
'font_settings.json', 'font_settings.json',
'history.json', 'history.json',
'permissions.json', 'permissions.json',
......
...@@ -27,7 +27,8 @@ ...@@ -27,7 +27,8 @@
"unprivileged": true, "unprivileged": true,
"description": "Gets the localized string for the specified message. If the message is missing, this method returns an empty string (''). If the format of the <code>getMessage()</code> call is wrong &mdash; for example, <em>messageName</em> is not a string or the <em>substitutions</em> array has more than 9 elements &mdash; this method returns <code>undefined</code>.", "description": "Gets the localized string for the specified message. If the message is missing, this method returns an empty string (''). If the format of the <code>getMessage()</code> call is wrong &mdash; for example, <em>messageName</em> is not a string or the <em>substitutions</em> array has more than 9 elements &mdash; this method returns <code>undefined</code>.",
"parameters": [ "parameters": [
{ "type": "string", {
"type": "string",
"name": "messageName", "name": "messageName",
"description": "The name of the message, as specified in the <a href='i18n-messages.html'><code>messages.json</code></a> file." "description": "The name of the message, as specified in the <a href='i18n-messages.html'><code>messages.json</code></a> file."
}, },
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment