Commit e1e5e454 authored by Tarun Bansal's avatar Tarun Bansal Committed by Commit Bot

Add a struct to store network properties in data saver

The struct would be used to store different network
properties related to data saver proxy. The properties
include (but are not limited to) state of different proxy
server schemes.

Bug: 779219
Change-Id: Idd82c2035cf047feaa8134e57a1bacda2eafb1b0
Reviewed-on: https://chromium-review.googlesource.com/742485Reviewed-by: default avatarScott Little <sclittle@chromium.org>
Commit-Queue: Tarun Bansal <tbansal@chromium.org>
Cr-Commit-Position: refs/heads/master@{#512987}
parent e0a92a11
...@@ -48,6 +48,8 @@ browser_sources = [ ...@@ -48,6 +48,8 @@ browser_sources = [
"data_usage_store.h", "data_usage_store.h",
"db_data_owner.cc", "db_data_owner.cc",
"db_data_owner.h", "db_data_owner.h",
"network_property.cc",
"network_property.h",
"secure_proxy_checker.cc", "secure_proxy_checker.cc",
"secure_proxy_checker.h", "secure_proxy_checker.h",
"warmup_url_fetcher.cc", "warmup_url_fetcher.cc",
...@@ -178,6 +180,7 @@ source_set("unit_tests") { ...@@ -178,6 +180,7 @@ source_set("unit_tests") {
"data_reduction_proxy_request_options_unittest.cc", "data_reduction_proxy_request_options_unittest.cc",
"data_reduction_proxy_settings_unittest.cc", "data_reduction_proxy_settings_unittest.cc",
"data_usage_store_unittest.cc", "data_usage_store_unittest.cc",
"network_property_unittest.cc",
"warmup_url_fetcher_unittest.cc", "warmup_url_fetcher_unittest.cc",
] ]
......
// Copyright 2017 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 "components/data_reduction_proxy/core/browser/network_property.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
namespace {
static const char kSecureProxiesState[] = "secure_proxies_state=";
static const char kInsecureProxiesState[] = "insecure_proxies_state=";
} // namespace
namespace data_reduction_proxy {
NetworkProperty::NetworkProperty(ProxyState secure_proxies_state,
ProxyState insecure_proxies_state)
: secure_proxies_state_(secure_proxies_state),
insecure_proxies_state_(insecure_proxies_state) {}
NetworkProperty::NetworkProperty(const std::string& serialized) {
const std::vector<std::string> tokens = base::SplitString(
serialized, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
// Initialize with the default values.
secure_proxies_state_ = PROXY_STATE_ALLOWED;
insecure_proxies_state_ = PROXY_STATE_ALLOWED;
DCHECK_EQ(2u, tokens.size());
for (const std::string& token : tokens) {
// Check if |token| begins with kSecureProxiesState.
size_t idx = token.find(kSecureProxiesState);
if (idx == 0) {
int value = -1;
base::StringToInt(token.substr(arraysize(kSecureProxiesState) - 1),
&value);
DCHECK(value >= 0 && value <= PROXY_STATE_LAST);
if (value >= 0 && value <= PROXY_STATE_LAST)
secure_proxies_state_ = static_cast<ProxyState>(value);
}
// Check if |token| begins with kInsecureProxiesState.
idx = token.find(kInsecureProxiesState);
if (idx == 0) {
int value = -1;
base::StringToInt(token.substr(arraysize(kInsecureProxiesState) - 1),
&value);
DCHECK(value >= 0 && value <= PROXY_STATE_LAST);
if (value >= 0 && value <= PROXY_STATE_LAST)
insecure_proxies_state_ = static_cast<ProxyState>(value);
}
}
}
NetworkProperty::NetworkProperty(const NetworkProperty& other) = default;
NetworkProperty& NetworkProperty::operator=(const NetworkProperty& other) =
default;
std::string NetworkProperty::ToString() const {
// Store the properties as comma separated tokens.
return kSecureProxiesState + base::IntToString(secure_proxies_state_) + "," +
kInsecureProxiesState + base::IntToString(insecure_proxies_state_);
}
ProxyState NetworkProperty::secure_proxies_state() const {
return secure_proxies_state_;
}
ProxyState NetworkProperty::insecure_proxies_state() const {
return insecure_proxies_state_;
}
} // namespace data_reduction_proxy
\ No newline at end of file
// Copyright 2017 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 COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_NETWORK_PROPERTY_H_
#define COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_NETWORK_PROPERTY_H_
#include <string>
namespace data_reduction_proxy {
// State of a proxy server scheme. Tracks if the proxy is allowed or not, and
// why it is not allowed.
enum ProxyState {
PROXY_STATE_ALLOWED = 0,
PROXY_STATE_DISALLOWED_BY_CARRIER = 1 << 0,
PROXY_STATE_DISALLOWED_DUE_TO_CAPTIVE_PORTAL = 1 << 1,
PROXY_STATE_DISALLOWED_DUE_TO_WARMUP_PROBE_FAILURE = 1 << 2,
PROXY_STATE_LAST = PROXY_STATE_DISALLOWED_DUE_TO_WARMUP_PROBE_FAILURE
};
// Stores the properties of a single network.
class NetworkProperty {
public:
NetworkProperty(ProxyState secure_proxies_state,
ProxyState insecure_proxies_state);
NetworkProperty(const NetworkProperty& other);
NetworkProperty& operator=(const NetworkProperty& other);
// Creates NetworkProperty from the serialized string.
explicit NetworkProperty(const std::string& serialized);
// Serializes |this| to string which can be persisted on the disk.
std::string ToString() const;
// Most recent state of the secure and insecure proxies, respectively.
ProxyState secure_proxies_state() const;
ProxyState insecure_proxies_state() const;
private:
ProxyState secure_proxies_state_;
ProxyState insecure_proxies_state_;
};
} // namespace data_reduction_proxy
#endif // COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_NETWORK_PROPERTY_H_
\ No newline at end of file
// Copyright 2017 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 "components/data_reduction_proxy/core/browser/network_property.h"
#include <string>
#include "base/macros.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace data_reduction_proxy {
namespace {
// Test that the network property is properly serialized and unserialized back.
TEST(NetworkPropertyTest, TestSerializeUnserialize) {
for (int secure_proxy_state = 0; secure_proxy_state <= PROXY_STATE_LAST;
++secure_proxy_state) {
for (int insecure_proxy_state = 0; insecure_proxy_state <= PROXY_STATE_LAST;
++insecure_proxy_state) {
NetworkProperty network_property(
static_cast<ProxyState>(secure_proxy_state),
static_cast<ProxyState>(insecure_proxy_state));
EXPECT_EQ(static_cast<ProxyState>(secure_proxy_state),
network_property.secure_proxies_state());
EXPECT_EQ(static_cast<ProxyState>(insecure_proxy_state),
network_property.insecure_proxies_state());
std::string network_property_string = network_property.ToString();
EXPECT_FALSE(network_property_string.empty());
// Generate network property from the string and compare it with
// |network_property|.
NetworkProperty network_property_reversed(network_property_string);
EXPECT_EQ(network_property.secure_proxies_state(),
network_property_reversed.secure_proxies_state());
EXPECT_EQ(network_property.insecure_proxies_state(),
network_property_reversed.insecure_proxies_state());
EXPECT_EQ(network_property.ToString(),
network_property_reversed.ToString());
}
}
}
} // namespace
} // namespace data_reduction_proxy
\ No newline at end of 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