FreeRDP
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Modules Pages
wrapper/webview_impl.cpp
1
20#include "webview.h"
21
22#include <cassert>
23#include <string>
24#include <vector>
25#include <map>
26#include <regex>
27#include <sstream>
28#include "../webview_impl.hpp"
29
30static std::vector<std::string> split(const std::string& input, const std::string& regex)
31{
32 // passing -1 as the submatch index parameter performs splitting
33 std::regex re(regex);
34 std::sregex_token_iterator first{ input.begin(), input.end(), re, -1 };
35 std::sregex_token_iterator last;
36 return { first, last };
37}
38
39static std::map<std::string, std::string> urlsplit(const std::string& url)
40{
41 auto pos = url.find('?');
42 if (pos == std::string::npos)
43 return {};
44 auto surl = url.substr(pos);
45 auto args = split(surl, "&");
46
47 std::map<std::string, std::string> argmap;
48 for (const auto& arg : args)
49 {
50 auto kv = split(arg, "=");
51 if (kv.size() == 2)
52 argmap.insert({ kv[0], kv[1] });
53 }
54 return argmap;
55}
56
57static void fkt(const std::string& url, void* arg)
58{
59 auto args = urlsplit(url);
60 auto val = args.find("code");
61 if (val == args.end())
62 return;
63
64 assert(arg);
65 auto rcode = static_cast<std::string*>(arg);
66 *rcode = val->second;
67}
68
69bool webview_impl_run(const std::string& title, const std::string& url, std::string& code)
70{
71 webview::webview w(false, nullptr);
72
73 w.set_title(title);
74 w.set_size(640, 480, WEBVIEW_HINT_NONE);
75
76 std::string scheme;
77 w.add_scheme_handler("ms-appx-web", fkt, &scheme);
78 w.add_navigate_listener(fkt, &code);
79 w.navigate(url);
80 w.run();
81 return !code.empty();
82}