Skip to content

Commit

Permalink
Add bookmark support.
Browse files Browse the repository at this point in the history
  • Loading branch information
ttalvitie committed Sep 21, 2021
1 parent 7dc7b19 commit 0ec02f2
Show file tree
Hide file tree
Showing 16 changed files with 1,069 additions and 15 deletions.
415 changes: 415 additions & 0 deletions src/bookmarks.cpp

Large diffs are not rendered by default.

42 changes: 42 additions & 0 deletions src/bookmarks.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#pragma once

#include "common.hpp"

class CefRequest;

namespace browservice {

struct Bookmark {
string url;
string title;
uint64_t time;
};

class Bookmarks {
SHARED_ONLY_CLASS(Bookmarks);
public:
Bookmarks(CKey);

// Returns empty pointer and writes to log if loading failed
static shared_ptr<Bookmarks> load();

// Returns false and writes to log if saving failed
bool save();

const map<uint64_t, Bookmark>& getData() const;

uint64_t putBookmark(Bookmark bookmark);
void removeBookmark(uint64_t id);

private:
map<uint64_t, Bookmark> data_;
};

// Returns the bookmark ID if given url is bookmarked in the last state saved
// using Bookmarks::save, or if no bookmarks have yet been saved, the state
// loaded on the first call to this function. Safe to call from any thread.
optional<uint64_t> getCachedBookmarkIDByURL(string url);

string handleBookmarksRequest(CefRefPtr<CefRequest> request);

}
37 changes: 33 additions & 4 deletions src/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@ namespace browservice {

thread_local mt19937 rng(random_device{}());

string sanitizeUTF8String(string str) {
string ret;
namespace {

template <typename A, typename B>
void sanitizeUTF8StringImpl(const string& str, A byteHandler, B pointHandler) {
for(size_t i = 0; i < str.size(); ++i) {
int ch = (int)(uint8_t)str[i];
if(ch == 0) {
continue;
}
if(ch < 128) {
ret.push_back((char)ch);
byteHandler(str.data() + i, 1);
pointHandler(ch);
continue;
}

Expand Down Expand Up @@ -55,10 +58,36 @@ string sanitizeUTF8String(string str) {
)) ||
(length == (size_t)4 && point >= 0x10000 && point <= 0x10FFFF)
) {
ret.append(str.substr(i, length));
byteHandler(str.data() + i, length);
pointHandler(point);
i += length - 1;
}
}
}

}

string sanitizeUTF8String(string str) {
string ret;
sanitizeUTF8StringImpl(
str,
[&](const char* bytes, size_t count) {
str.insert(str.end(), bytes, bytes + count);
},
[](int) {}
);
return ret;
}

vector<int> sanitizeUTF8StringToCodePoints(string str) {
vector<int> ret;
sanitizeUTF8StringImpl(
str,
[](const char*, size_t) {},
[&](int point) {
ret.push_back(point);
}
);
return ret;
}

Expand Down
1 change: 1 addition & 0 deletions src/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ string toString(const T& obj) {
}

string sanitizeUTF8String(string str);
vector<int> sanitizeUTF8StringToCodePoints(string str);

// Logging macros that log given message along with severity, source file and
// line information to stderr. Message is formed by calling toString for each
Expand Down
Loading

0 comments on commit 0ec02f2

Please sign in to comment.