foreseen

A text-based gemini browser with prefetching
git clone git://git.laack.co/foreseen.git
Log | Files | Refs | README | LICENSE

browser.hpp (2056B)


      1 #pragma once
      2 
      3 #include <atomic>
      4 #include <cstddef>
      5 #include <thread>
      6 #include <unordered_map>
      7 #include <vector>
      8 
      9 #include "cache.hpp"
     10 #include "identity-manager.hpp"
     11 #include "line.hpp"
     12 #include "link.hpp"
     13 #include "site.hpp"
     14 #include "utils.hpp"
     15 
     16 const std::string OPENED_EXT_TXT = "Externally opened";
     17 
     18 enum SiteLoadResult {
     19     SITE_LOADED,  // These are valid status codes so 1X, 2X, and 3e
     20     SITE_TEMPORARY_FAILURE,
     21     SITE_PERMANENT_FAILURE,
     22     SITE_REQUIRES_CERTIFICATE,
     23     SITE_REJECTED_CERTIFICATE,
     24     SITE_INVALID_CERTIFICATE,
     25     SITE_UNEXPECTED_STATUS_CODE,
     26     LINK_DOES_NOT_EXIST  // only for link following
     27 };
     28 
     29 struct SiteLoadPair {  // TODO: Set this up.
     30     SiteLoadResult result;
     31     std::string metaLine;
     32 };
     33 
     34 class Browser {
     35    private:
     36     IdentityManager identityManager = IdentityManager{};
     37     std::vector<std::thread> threads;
     38     std::vector<std::atomic<bool>> done;
     39     std::unordered_map<std::string, int> previousStatusCodes;
     40     std::vector<Link*> siteHistory;
     41     std::thread openThread;
     42     int previousIdx = -1;
     43     Cache* visitedCache;
     44     Cache* preFetchCache;
     45     Site* currentSite;
     46     std::vector<Line*> lines;
     47     std::vector<std::size_t> links;  // these point to line indices
     48     void tryCacheTargets();
     49     Site* findInCacheAndPromoteIfRelevant(std::string& urlString);
     50 
     51    public:
     52     Browser();
     53     void setLinksOfCurrentLines();
     54     ~Browser();
     55     SiteLoadPair goToSite(std::string url, bool addToHistory = true,
     56                           bool refresh = false);
     57     void setDone(int threadIdx);
     58     void refresh();
     59     Site* getCurrentSite();
     60     std::string tryDownloadPage(std::string destinationDir = "") noexcept;
     61     Link* getCurrentLink();
     62     std::optional<uri> getPriorUri();
     63     std::vector<std::pair<std::string, TextRender>> renderSite();
     64     std::vector<Line*> toLines(Site* site);
     65     SiteLoadPair followLinkNumber(int linkToFollow);
     66     std::vector<Link>* getLinkLines();
     67     Identity getIdentity(uri uriInput);
     68     void justCacheSite(Link link);
     69     void goBack();
     70     void goForward();
     71 };