Commit f88dac0a authored by Sylvain Defresne's avatar Sylvain Defresne Committed by Commit Bot

Remove unused free function GetIOSChromeParseJSONCallback()

This function is never used except in the function unit tests.
Remove it and the corresponding unit tests.

Bug: 842655, 964232, 714018
Change-Id: Ia0e2f85dd86019db776dfed9980b4800def49580
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1627370
Auto-Submit: Sylvain Defresne <sdefresne@chromium.org>
Reviewed-by: default avatarOlivier Robin <olivierrobin@chromium.org>
Commit-Queue: Sylvain Defresne <sdefresne@chromium.org>
Cr-Commit-Position: refs/heads/master@{#662572}
parent 825eb0aa
......@@ -217,7 +217,6 @@ source_set("browser_impl") {
"//ios/chrome/browser/translate",
"//ios/chrome/browser/ui:feature_flags",
"//ios/chrome/browser/update_client",
"//ios/chrome/browser/web_resource",
"//ios/chrome/common",
"//ios/chrome/common/app_group",
"//ios/public/provider/chrome/browser",
......
......@@ -50,7 +50,6 @@
#include "ios/chrome/browser/prefs/browser_prefs.h"
#include "ios/chrome/browser/prefs/ios_chrome_pref_service_factory.h"
#include "ios/chrome/browser/update_client/ios_chrome_update_query_params_delegate.h"
#include "ios/chrome/browser/web_resource/web_resource_util.h"
#include "ios/chrome/common/channel_info.h"
#include "ios/web/public/web_task_traits.h"
#include "ios/web/public/web_thread.h"
......
# Copyright 2016 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.
source_set("web_resource") {
sources = [
"web_resource_util.cc",
"web_resource_util.h",
]
deps = [
"//base",
"//components/web_resource",
"//ios/web",
]
}
source_set("unit_tests") {
testonly = true
sources = [
"web_resource_util_unittest.cc",
]
deps = [
":web_resource",
"//base",
"//base/test:test_support",
"//ios/web",
"//testing/gtest",
]
}
achuith@chromium.org
rsesek@chromium.org
# TEAM: ios-directory-owners@chromium.org
# OS: iOS
// Copyright 2015 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 "ios/chrome/browser/web_resource/web_resource_util.h"
#include "base/bind.h"
#include "base/json/json_reader.h"
#include "base/location.h"
#include "base/task/post_task.h"
#include "base/task_runner.h"
#include "base/task_runner_util.h"
#include "base/threading/thread_task_runner_handle.h"
#include "base/values.h"
namespace web_resource {
namespace {
const char kInvalidDataTypeError[] =
"Data from web resource server is missing or not valid JSON.";
const char kUnexpectedJSONFormatError[] =
"Data from web resource server does not have expected format.";
// Parses |data| as a JSON string and calls back on |task_runner|.
// Must not be called on the UI thread, for performance reasons.
void ParseJSONOnBackgroundThread(
base::TaskRunner* task_runner,
const std::string& data,
const WebResourceService::SuccessCallback& success_callback,
const WebResourceService::ErrorCallback& error_callback) {
if (data.empty()) {
task_runner->PostTask(
FROM_HERE, base::BindOnce(error_callback, kInvalidDataTypeError));
return;
}
base::Optional<base::Value> value(base::JSONReader::Read(data));
if (!value) {
// Page information not properly read, or corrupted.
task_runner->PostTask(
FROM_HERE, base::BindOnce(error_callback, kInvalidDataTypeError));
return;
}
if (!value->is_dict()) {
task_runner->PostTask(
FROM_HERE, base::BindOnce(error_callback, kUnexpectedJSONFormatError));
return;
}
task_runner->PostTask(FROM_HERE,
base::BindOnce(success_callback, std::move(*value)));
}
// Starts the parsing of |data| as a JSON string asynchronously on a background
// thread. Can be called from the UI thread.
void StartParseJSONAsync(
const std::string& data,
const WebResourceService::SuccessCallback& success_callback,
const WebResourceService::ErrorCallback& error_callback) {
base::PostTaskWithTraits(
FROM_HERE, {base::MayBlock(), base::TaskPriority::BEST_EFFORT},
base::BindOnce(&ParseJSONOnBackgroundThread,
base::RetainedRef(base::ThreadTaskRunnerHandle::Get()),
data, success_callback, error_callback));
}
} // namespace
WebResourceService::ParseJSONCallback GetIOSChromeParseJSONCallback() {
return base::Bind(&StartParseJSONAsync);
}
} // namespace web_resource
// Copyright 2015 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.
#ifndef IOS_CHROME_BROWSER_WEB_RESOURCE_WEB_RESOURCE_UTIL_H_
#define IOS_CHROME_BROWSER_WEB_RESOURCE_WEB_RESOURCE_UTIL_H_
#include "components/web_resource/web_resource_service.h"
namespace web_resource {
// Returns a ParseJSONCallback that parses JSON on a background thread.
// Generates an error if the data is not a dictionary.
WebResourceService::ParseJSONCallback GetIOSChromeParseJSONCallback();
} // namespace web_resource
#endif // IOS_CHROME_BROWSER_WEB_RESOURCE_WEB_RESOURCE_UTIL_H_
// Copyright 2015 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 "ios/chrome/browser/web_resource/web_resource_util.h"
#include <string>
#include <utility>
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/run_loop.h"
#include "base/strings/stringprintf.h"
#include "base/test/scoped_task_environment.h"
#include "base/values.h"
#include "ios/web/public/web_thread.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
namespace web_resource {
class WebResourceUtilTest : public PlatformTest {
protected:
WebResourceUtilTest() : error_called_(false), success_called_(false) {}
~WebResourceUtilTest() override {}
WebResourceService::SuccessCallback GetSuccessCallback() {
return base::Bind(&WebResourceUtilTest::OnParseSuccess,
base::Unretained(this));
}
WebResourceService::ErrorCallback GetErrorCallback() {
return base::Bind(&WebResourceUtilTest::OnParseError,
base::Unretained(this));
}
// Called on success.
void OnParseSuccess(base::Value value) {
success_called_ = true;
value_ = std::move(value);
}
// Called on error.
void OnParseError(const std::string& error) {
error_called_ = true;
error_ = error;
}
void FlushBackgroundTasks() {
// The function is not synchronous, callbacks are not called before flushing
// the tasks.
EXPECT_FALSE(success_called_);
EXPECT_FALSE(error_called_);
scoped_task_environment_.RunUntilIdle();
}
base::test::ScopedTaskEnvironment scoped_task_environment_;
std::string error_;
base::Optional<base::Value> value_;
bool error_called_;
bool success_called_;
};
TEST_F(WebResourceUtilTest, Success) {
const std::string kExpectedKey("foo");
const std::string kExpectedValue("bar");
std::string json = base::StringPrintf("{\"%s\":\"%s\"}", kExpectedKey.c_str(),
kExpectedValue.c_str());
GetIOSChromeParseJSONCallback().Run(json, GetSuccessCallback(),
GetErrorCallback());
FlushBackgroundTasks();
// The success callback is called with the reference value.
EXPECT_FALSE(error_called_);
EXPECT_TRUE(success_called_);
base::DictionaryValue* dictionary = nullptr;
ASSERT_TRUE(value_->GetAsDictionary(&dictionary));
EXPECT_EQ(1u, dictionary->size());
base::Value* actual_value = nullptr;
ASSERT_TRUE(dictionary->Get(kExpectedKey, &actual_value));
std::string actual_value_as_string;
EXPECT_TRUE(actual_value->GetAsString(&actual_value_as_string));
EXPECT_EQ(kExpectedValue, actual_value_as_string);
}
// Only DictionartValues are expected.
TEST_F(WebResourceUtilTest, UnexpectedValue) {
GetIOSChromeParseJSONCallback().Run("foo", GetSuccessCallback(),
GetErrorCallback());
FlushBackgroundTasks();
// The error callback is called.
EXPECT_TRUE(error_called_);
EXPECT_FALSE(success_called_);
EXPECT_FALSE(error_.empty());
}
// Empty data is not expected.
TEST_F(WebResourceUtilTest, EmptyValue) {
GetIOSChromeParseJSONCallback().Run(std::string(), GetSuccessCallback(),
GetErrorCallback());
FlushBackgroundTasks();
// The error callback is called.
EXPECT_TRUE(error_called_);
EXPECT_FALSE(success_called_);
EXPECT_FALSE(error_.empty());
}
// Wrong syntax.
TEST_F(WebResourceUtilTest, SyntaxError) {
GetIOSChromeParseJSONCallback().Run("%$[", GetSuccessCallback(),
GetErrorCallback());
FlushBackgroundTasks();
// The error callback is called.
EXPECT_TRUE(error_called_);
EXPECT_FALSE(success_called_);
EXPECT_FALSE(error_.empty());
}
} // namespace web_resource
......@@ -258,7 +258,6 @@ test("ios_chrome_unittests") {
"//ios/chrome/browser/voice:unit_tests",
"//ios/chrome/browser/web:unit_tests",
"//ios/chrome/browser/web:unit_tests_internal",
"//ios/chrome/browser/web_resource:unit_tests",
"//ios/chrome/browser/web_state_list:unit_tests",
"//ios/chrome/browser/web_state_list/web_usage_enabler:unit_tests",
"//ios/chrome/browser/webui:unit_tests",
......
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