From c04de4dd26dda811347a012156a50d2f6a3e22ad Mon Sep 17 00:00:00 2001 From: bridiver Date: Fri, 1 Feb 2019 12:59:42 -0700 Subject: [PATCH] lint --- .../https_everywhere_recently_used_cache.h | 90 ++++++++++--------- .../browser/https_everywhere_service.cc | 90 ++++++++++--------- 2 files changed, 96 insertions(+), 84 deletions(-) diff --git a/components/brave_shields/browser/https_everywhere_recently_used_cache.h b/components/brave_shields/browser/https_everywhere_recently_used_cache.h index 0a7a125dcfae..150e2a4d77ec 100644 --- a/components/brave_shields/browser/https_everywhere_recently_used_cache.h +++ b/components/brave_shields/browser/https_everywhere_recently_used_cache.h @@ -1,57 +1,63 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public +/* Copyright 2016 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#ifndef BRAVE_COMPONENTS_BRAVE_SHIELDS_BROWSER_HTTPS_EVERYWHERE_RECENTLY_USED_CACHE_H_ +#define BRAVE_COMPONENTS_BRAVE_SHIELDS_BROWSER_HTTPS_EVERYWHERE_RECENTLY_USED_CACHE_H_ + #include #include #include -template class RingBuffer -{ -private: - int currentIdx = 0; - int count; - std::vector data; -public: - RingBuffer(int fixedSize) : count(fixedSize), data(count) {} - - const T& at(int i) { - return data[(currentIdx - (i % count) + count) % count]; - } +template class RingBuffer { + public: + explicit RingBuffer(int fixedSize) : count(fixedSize), data(count) {} - void add(const T& newValue) { - currentIdx = (currentIdx + 1) % count; - data[currentIdx] = newValue; - } + const T& at(int i) { + return data[(currentIdx - (i % count) + count) % count]; + } - T oldest() { - return data[(currentIdx + 1) % count]; - } + void add(const T& newValue) { + currentIdx = (currentIdx + 1) % count; + data[currentIdx] = newValue; + } - void clear() { - data = std::vector(count); - } + T oldest() { + return data[(currentIdx + 1) % count]; + } + + void clear() { + data = std::vector(count); + } + + private: + int currentIdx = 0; + int count; + std::vector data; }; -template class HTTPSERecentlyUsedCache -{ -private: - RingBuffer keysByAge; -public: - std::unordered_map data; - - HTTPSERecentlyUsedCache(unsigned int size = 100) : keysByAge(size) {} - - void add(const std::string& key, const T& value) { - std::string old = keysByAge.oldest(); - if (!old.empty()) { - keysByAge.data.erase(old); - } - keysByAge[key] = value; - } +template class HTTPSERecentlyUsedCache { + public: + explicit HTTPSERecentlyUsedCache(unsigned int size = 100) : keysByAge(size) {} - void clear() { - data.clear(); - keysByAge.clear(); + void add(const std::string& key, const T& value) { + std::string old = keysByAge.oldest(); + if (!old.empty()) { + keysByAge.data.erase(old); } + keysByAge[key] = value; + } + + void clear() { + data.clear(); + keysByAge.clear(); + } + + std::unordered_map data; + + private: + RingBuffer keysByAge; }; + +#endif // BRAVE_COMPONENTS_BRAVE_SHIELDS_BROWSER_HTTPS_EVERYWHERE_RECENTLY_USED_CACHE_H_ diff --git a/components/brave_shields/browser/https_everywhere_service.cc b/components/brave_shields/browser/https_everywhere_service.cc index 961116132b4b..6e48a2a3cc92 100644 --- a/components/brave_shields/browser/https_everywhere_service.cc +++ b/components/brave_shields/browser/https_everywhere_service.cc @@ -1,6 +1,7 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this file, - * You can obtain one at http://mozilla.org/MPL/2.0/. */ +/* Copyright 2016 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "brave/components/brave_shields/browser/https_everywhere_service.h" @@ -29,57 +30,61 @@ #define HTTPSE_URL_MAX_REDIRECTS_COUNT 5 namespace { - std::vector Split(const std::string& s, char delim) { - std::stringstream ss(s); - std::string item; - std::vector result; - while (getline(ss, item, delim)) { - result.push_back(item); - } - return result; - } - // returns parts in reverse order, makes list of lookup domains like com.foo.* - std::vector ExpandDomainForLookup(const std::string& domain) { - std::vector resultDomains; - std::vector domainParts = Split(domain, '.'); - if (domainParts.empty()) { - return resultDomains; - } +std::vector Split(const std::string& s, char delim) { + std::stringstream ss(s); + std::string item; + std::vector result; + while (getline(ss, item, delim)) { + result.push_back(item); + } + return result; +} - for (size_t i = 0; i < domainParts.size() - 1; i++) { // i < size()-1 is correct: don't want 'com.*' added to resultDomains - std::string slice = ""; - std::string dot = ""; - for (int j = domainParts.size() - 1; j >= (int)i; j--) { - slice += dot + domainParts[j]; - dot = "."; - } - if (0 != i) { - // We don't want * on the top URL - resultDomains.push_back(slice + ".*"); - } else { - resultDomains.push_back(slice); - } - } +// returns parts in reverse order, makes list of lookup domains like com.foo.* +std::vector ExpandDomainForLookup(const std::string& domain) { + std::vector resultDomains; + std::vector domainParts = Split(domain, '.'); + if (domainParts.empty()) { return resultDomains; } - std::string leveldbGet(leveldb::DB* db, const std::string &key) { - if (!db) { - return ""; - } - std::string value; - leveldb::Status s = db->Get(leveldb::ReadOptions(), key, &value); - return s.ok() ? value : ""; + for (size_t i = 0; i < domainParts.size() - 1; i++) { + // i < size()-1 is correct: don't want 'com.*' added to resultDomains + std::string slice = ""; + std::string dot = ""; + for (int j = domainParts.size() - 1; j >= static_cast(i); j--) { + slice += dot + domainParts[j]; + dot = "."; + } + if (0 != i) { + // We don't want * on the top URL + resultDomains.push_back(slice + ".*"); + } else { + resultDomains.push_back(slice); + } + } + return resultDomains; +} +std::string leveldbGet(leveldb::DB* db, const std::string &key) { + if (!db) { + return ""; } + + std::string value; + leveldb::Status s = db->Get(leveldb::ReadOptions(), key, &value); + return s.ok() ? value : ""; } +} // namespace + namespace brave_shields { bool HTTPSEverywhereService::g_ignore_port_for_test_(false); std::string HTTPSEverywhereService::g_https_everywhere_component_id_( kHTTPSEverywhereComponentId); -std::string HTTPSEverywhereService::g_https_everywhere_component_base64_public_key_( +std::string +HTTPSEverywhereService::g_https_everywhere_component_base64_public_key_( kHTTPSEverywhereComponentBase64PublicKey); HTTPSEverywhereService::HTTPSEverywhereService() : level_db_(nullptr) { @@ -169,7 +174,8 @@ bool HTTPSEverywhereService::GetHTTPSURL( candidate_url = candidate_url.ReplaceComponents(replacements); } - const std::vector domains = ExpandDomainForLookup(candidate_url.host()); + const std::vector domains = + ExpandDomainForLookup(candidate_url.host()); for (auto domain : domains) { std::string value = leveldbGet(level_db_, domain); if (!value.empty()) {