Commit 408b01b6 authored by michaelpg's avatar michaelpg Committed by Commit bot

WebUIDataSource::AddInteger (like AddBoolean/AddString)

Convenience function to add integers to a WebUIDataSource
without using AddLocalizedStrings with a base::DictionaryValue.

Limits width to 32 bits to prevent integers larger than can
be represented in ES6 (2^53 - 1). (As before, larger numbers
could still be set via AddLocalizedStrings.)

BUG=none
R=tommycli@chromium.org,creis@chromium.org
TEST=content_unittests:WebUIDataSourceTest

Review-Url: https://codereview.chromium.org/2391073005
Cr-Commit-Position: refs/heads/master@{#423816}
parent d9143c64
......@@ -56,10 +56,8 @@ DateTimeHandler* DateTimeHandler::Create(
html_source->AddBoolean("systemTimeZoneDetectionManaged",
system_time_zone_automatic_detection_managed);
if (system_time_zone_automatic_detection_managed) {
base::DictionaryValue dict;
dict.SetInteger("systemTimeZoneDetectionPolicyValue",
html_source->AddInteger("systemTimeZoneDetectionPolicyValue",
GetSystemTimezoneAutomaticDetectionPolicyValue());
html_source->AddLocalizedStrings(dict);
}
if (base::CommandLine::ForCurrentProcess()->HasSwitch(
......
......@@ -5,6 +5,7 @@
#include "content/browser/webui/web_ui_data_source_impl.h"
#include <stddef.h>
#include <stdint.h>
#include <string>
......@@ -141,6 +142,10 @@ void WebUIDataSourceImpl::AddBoolean(const std::string& name, bool value) {
// replacements.
}
void WebUIDataSourceImpl::AddInteger(const std::string& name, int32_t value) {
localized_strings_.SetInteger(name, value);
}
void WebUIDataSourceImpl::SetJsonPath(const std::string& path) {
json_path_ = path;
}
......
......@@ -5,6 +5,8 @@
#ifndef CONTENT_BROWSER_WEBUI_WEB_UI_DATA_SOURCE_IMPL_H_
#define CONTENT_BROWSER_WEBUI_WEB_UI_DATA_SOURCE_IMPL_H_
#include <stdint.h>
#include <map>
#include <string>
......@@ -34,6 +36,7 @@ class CONTENT_EXPORT WebUIDataSourceImpl
void AddLocalizedStrings(
const base::DictionaryValue& localized_strings) override;
void AddBoolean(const std::string& name, bool value) override;
void AddInteger(const std::string& name, int32_t value) override;
void SetJsonPath(const std::string& path) override;
void AddResourcePath(const std::string& path, int resource_id) override;
void SetDefaultResource(int resource_id) override;
......
......@@ -103,17 +103,23 @@ TEST_F(WebUIDataSourceTest, EmptyStrings) {
StartDataRequest("strings.js", base::Bind(&EmptyStringsCallback));
}
void SomeStringsCallback(scoped_refptr<base::RefCountedMemory> data) {
void SomeValuesCallback(scoped_refptr<base::RefCountedMemory> data) {
std::string result(data->front_as<char>(), data->size());
EXPECT_NE(result.find("\"flag\":true"), std::string::npos);
EXPECT_NE(result.find("\"counter\":10"), std::string::npos);
EXPECT_NE(result.find("\"debt\":-456"), std::string::npos);
EXPECT_NE(result.find("\"planet\":\"pluto\""), std::string::npos);
EXPECT_NE(result.find("\"button\":\"foo\""), std::string::npos);
}
TEST_F(WebUIDataSourceTest, SomeStrings) {
TEST_F(WebUIDataSourceTest, SomeValues) {
source()->SetJsonPath("strings.js");
source()->AddBoolean("flag", true);
source()->AddInteger("counter", 10);
source()->AddInteger("debt", -456);
source()->AddString("planet", base::ASCIIToUTF16("pluto"));
source()->AddLocalizedString("button", kDummyStringId);
StartDataRequest("strings.js", base::Bind(&SomeStringsCallback));
StartDataRequest("strings.js", base::Bind(&SomeValuesCallback));
}
void DefaultResourceFoobarCallback(scoped_refptr<base::RefCountedMemory> data) {
......
......@@ -5,6 +5,8 @@
#ifndef CONTENT_PUBLIC_BROWSER_WEB_UI_DATA_SOURCE_H_
#define CONTENT_PUBLIC_BROWSER_WEB_UI_DATA_SOURCE_H_
#include <stdint.h>
#include "base/callback.h"
#include "base/strings/string16.h"
#include "content/common/content_export.h"
......@@ -47,6 +49,11 @@ class WebUIDataSource {
// Adds a boolean keyed to its name to our dictionary.
virtual void AddBoolean(const std::string& name, bool value) = 0;
// Adds a signed 32-bit integer keyed to its name to our dictionary. Larger
// integers may not be exactly representable in JavaScript. See
// MAX_SAFE_INTEGER in /v8/src/globals.h.
virtual void AddInteger(const std::string& name, int32_t value) = 0;
// Sets the path which will return the JSON strings.
virtual void SetJsonPath(const std::string& path) = 0;
......
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