Commit f36e5c27 authored by Sophie Chang's avatar Sophie Chang Committed by Commit Bot

Move proto ect conversion into hints processing util so it can be shared

Bug: 969558
Change-Id: I6b6457c4203c3d2b849f10d85e1ec2b3912ae0cb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1726762Reviewed-by: default avatarDoug Arnett <dougarnett@chromium.org>
Commit-Queue: Sophie Chang <sophiechang@chromium.org>
Cr-Commit-Position: refs/heads/master@{#682743}
parent 93689c63
......@@ -84,8 +84,7 @@ std::string HashHostForDictionary(const std::string& host) {
return base::StringPrintf("%x", base::PersistentHash(host));
}
bool ProcessHints(
google::protobuf::RepeatedPtrField<optimization_guide::proto::Hint>* hints,
bool ProcessHints(google::protobuf::RepeatedPtrField<proto::Hint>* hints,
optimization_guide::HintUpdateData* hint_update_data) {
// If there's no update data, then there's nothing to do.
if (!hint_update_data)
......@@ -101,7 +100,7 @@ bool ProcessHints(
for (auto& hint : *hints) {
// We only support host suffixes at the moment. Skip anything else.
// One |hint| applies to one host URL suffix.
if (hint.key_representation() != optimization_guide::proto::HOST_SUFFIX) {
if (hint.key_representation() != proto::HOST_SUFFIX) {
continue;
}
......@@ -134,4 +133,22 @@ bool ProcessHints(
return did_process_hints;
}
net::EffectiveConnectionType ConvertProtoEffectiveConnectionType(
proto::EffectiveConnectionType proto_ect) {
switch (proto_ect) {
case proto::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_UNKNOWN:
return net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_UNKNOWN;
case proto::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_OFFLINE:
return net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_OFFLINE;
case proto::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_SLOW_2G:
return net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_SLOW_2G;
case proto::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_2G:
return net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_2G;
case proto::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_3G:
return net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_3G;
case proto::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_4G:
return net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_4G;
}
}
} // namespace optimization_guide
......@@ -8,6 +8,7 @@
#include <string>
#include "components/optimization_guide/proto/hints.pb.h"
#include "net/nqe/effective_connection_type.h"
class GURL;
......@@ -47,6 +48,10 @@ std::string HashHostForDictionary(const std::string& host);
bool ProcessHints(google::protobuf::RepeatedPtrField<proto::Hint>* hints,
HintUpdateData* hint_update_data);
// Converts |proto_ect| into a net::EffectiveConnectionType.
net::EffectiveConnectionType ConvertProtoEffectiveConnectionType(
proto::EffectiveConnectionType proto_ect);
} // namespace optimization_guide
#endif // COMPONENTS_OPTIMIZATION_GUIDE_HINTS_PROCESSING_UTIL_H_
......@@ -12,13 +12,7 @@
namespace optimization_guide {
class HintsProcessingUtilTest : public testing::Test {
public:
HintsProcessingUtilTest() {}
~HintsProcessingUtilTest() override {}
};
TEST_F(HintsProcessingUtilTest, FindPageHintForSubstringPagePattern) {
TEST(HintsProcessingUtilTest, FindPageHintForSubstringPagePattern) {
proto::Hint hint1;
// Page hint for "/one/"
......@@ -64,7 +58,7 @@ TEST_F(HintsProcessingUtilTest, FindPageHintForSubstringPagePattern) {
GURL("https://www.foo.org/bar/three.jpg"), &hint1));
}
TEST_F(HintsProcessingUtilTest, ProcessHintsNoUpdateData) {
TEST(HintsProcessingUtilTest, ProcessHintsNoUpdateData) {
proto::Hint hint;
hint.set_key("whatever.com");
hint.set_key_representation(proto::HOST_SUFFIX);
......@@ -77,7 +71,7 @@ TEST_F(HintsProcessingUtilTest, ProcessHintsNoUpdateData) {
EXPECT_FALSE(ProcessHints(&hints, nullptr));
}
TEST_F(HintsProcessingUtilTest, ProcessHintsWithNoPageHintsAndUpdateData) {
TEST(HintsProcessingUtilTest, ProcessHintsWithNoPageHintsAndUpdateData) {
proto::Hint hint;
hint.set_key("whatever.com");
hint.set_key_representation(proto::HOST_SUFFIX);
......@@ -92,7 +86,7 @@ TEST_F(HintsProcessingUtilTest, ProcessHintsWithNoPageHintsAndUpdateData) {
EXPECT_EQ(1ul, update_data->TakeUpdateEntries()->size());
}
TEST_F(HintsProcessingUtilTest, ProcessHintsWithPageHintsAndUpdateData) {
TEST(HintsProcessingUtilTest, ProcessHintsWithPageHintsAndUpdateData) {
google::protobuf::RepeatedPtrField<proto::Hint> hints;
proto::Hint hint;
......@@ -121,4 +115,28 @@ TEST_F(HintsProcessingUtilTest, ProcessHintsWithPageHintsAndUpdateData) {
EXPECT_EQ(2ul, update_data->TakeUpdateEntries()->size());
}
TEST(HintsProcessingUtilTest, ConvertProtoEffectiveConnectionType) {
EXPECT_EQ(
ConvertProtoEffectiveConnectionType(
proto::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_UNKNOWN),
net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_UNKNOWN);
EXPECT_EQ(
ConvertProtoEffectiveConnectionType(
proto::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_OFFLINE),
net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_OFFLINE);
EXPECT_EQ(
ConvertProtoEffectiveConnectionType(
proto::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_SLOW_2G),
net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_SLOW_2G);
EXPECT_EQ(ConvertProtoEffectiveConnectionType(
proto::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_2G),
net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_2G);
EXPECT_EQ(ConvertProtoEffectiveConnectionType(
proto::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_3G),
net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_3G);
EXPECT_EQ(ConvertProtoEffectiveConnectionType(
proto::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_4G),
net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_4G);
}
} // namespace optimization_guide
......@@ -122,30 +122,6 @@ bool IsEnabledOptimizationType(
}
}
net::EffectiveConnectionType ConvertProtoEffectiveConnectionType(
optimization_guide::proto::EffectiveConnectionType proto_ect) {
switch (proto_ect) {
case optimization_guide::proto::EffectiveConnectionType::
EFFECTIVE_CONNECTION_TYPE_UNKNOWN:
return net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_UNKNOWN;
case optimization_guide::proto::EffectiveConnectionType::
EFFECTIVE_CONNECTION_TYPE_OFFLINE:
return net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_OFFLINE;
case optimization_guide::proto::EffectiveConnectionType::
EFFECTIVE_CONNECTION_TYPE_SLOW_2G:
return net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_SLOW_2G;
case optimization_guide::proto::EffectiveConnectionType::
EFFECTIVE_CONNECTION_TYPE_2G:
return net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_2G;
case optimization_guide::proto::EffectiveConnectionType::
EFFECTIVE_CONNECTION_TYPE_3G:
return net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_3G;
case optimization_guide::proto::EffectiveConnectionType::
EFFECTIVE_CONNECTION_TYPE_4G:
return net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_4G;
}
}
} // namespace
PreviewsHints::PreviewsHints(
......@@ -353,12 +329,14 @@ bool PreviewsHints::IsWhitelisted(
if (optimization.has_previews_metadata()) {
*out_inflation_percent =
optimization.previews_metadata().inflation_percent();
*out_ect_threshold = ConvertProtoEffectiveConnectionType(
*out_ect_threshold =
optimization_guide::ConvertProtoEffectiveConnectionType(
optimization.previews_metadata().max_ect_trigger());
} else {
*out_inflation_percent = optimization.inflation_percent();
if (matched_page_hint->has_max_ect_trigger()) {
*out_ect_threshold = ConvertProtoEffectiveConnectionType(
*out_ect_threshold =
optimization_guide::ConvertProtoEffectiveConnectionType(
matched_page_hint->max_ect_trigger());
}
}
......
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