Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
bridiver committed Feb 1, 2019
1 parent e60738d commit c04de4d
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 84 deletions.
Original file line number Diff line number Diff line change
@@ -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 <unordered_map>
#include <string>
#include <vector>

template <class T> class RingBuffer
{
private:
int currentIdx = 0;
int count;
std::vector<T> data;
public:
RingBuffer(int fixedSize) : count(fixedSize), data(count) {}

const T& at(int i) {
return data[(currentIdx - (i % count) + count) % count];
}
template <class T> 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<T>(count);
}
T oldest() {
return data[(currentIdx + 1) % count];
}

void clear() {
data = std::vector<T>(count);
}

private:
int currentIdx = 0;
int count;
std::vector<T> data;
};

template <class T> class HTTPSERecentlyUsedCache
{
private:
RingBuffer<T> keysByAge;
public:
std::unordered_map<std::string, T> 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 T> 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<std::string, T> data;

private:
RingBuffer<T> keysByAge;
};

#endif // BRAVE_COMPONENTS_BRAVE_SHIELDS_BROWSER_HTTPS_EVERYWHERE_RECENTLY_USED_CACHE_H_
90 changes: 48 additions & 42 deletions components/brave_shields/browser/https_everywhere_service.cc
Original file line number Diff line number Diff line change
@@ -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"

Expand Down Expand Up @@ -29,57 +30,61 @@
#define HTTPSE_URL_MAX_REDIRECTS_COUNT 5

namespace {
std::vector<std::string> Split(const std::string& s, char delim) {
std::stringstream ss(s);
std::string item;
std::vector<std::string> 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<std::string> ExpandDomainForLookup(const std::string& domain) {
std::vector<std::string> resultDomains;
std::vector<std::string> domainParts = Split(domain, '.');
if (domainParts.empty()) {
return resultDomains;
}
std::vector<std::string> Split(const std::string& s, char delim) {
std::stringstream ss(s);
std::string item;
std::vector<std::string> 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<std::string> ExpandDomainForLookup(const std::string& domain) {
std::vector<std::string> resultDomains;
std::vector<std::string> 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<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);
}
}
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) {
Expand Down Expand Up @@ -169,7 +174,8 @@ bool HTTPSEverywhereService::GetHTTPSURL(
candidate_url = candidate_url.ReplaceComponents(replacements);
}

const std::vector<std::string> domains = ExpandDomainForLookup(candidate_url.host());
const std::vector<std::string> domains =
ExpandDomainForLookup(candidate_url.host());
for (auto domain : domains) {
std::string value = leveldbGet(level_db_, domain);
if (!value.empty()) {
Expand Down

0 comments on commit c04de4d

Please sign in to comment.