FreeRDP
qt/webview_impl.cpp
1 
20 #include <QApplication>
21 #include <QWebEngineView>
22 #include <QWebEngineProfile>
23 #include <QWebEngineUrlScheme>
24 #include <QWebEngineUrlSchemeHandler>
25 #include <QWebEngineUrlRequestJob>
26 
27 #include <string>
28 #include <cstdlib>
29 #include <cstdarg>
30 #include <winpr/string.h>
31 #include <winpr/assert.h>
32 #include <freerdp/log.h>
33 #include <freerdp/build-config.h>
34 
35 #include "../webview_impl.hpp"
36 
37 #define TAG CLIENT_TAG("sdl.webview")
38 
39 class SchemeHandler : public QWebEngineUrlSchemeHandler
40 {
41  public:
42  explicit SchemeHandler(QObject* parent = nullptr) : QWebEngineUrlSchemeHandler(parent)
43  {
44  }
45 
46  void requestStarted(QWebEngineUrlRequestJob* request) override
47  {
48  QUrl url = request->requestUrl();
49 
50  int rc = -1;
51  for (auto& param : url.query().split('&'))
52  {
53  QStringList pair = param.split('=');
54 
55  if (pair.size() != 2 || pair[0] != QLatin1String("code"))
56  continue;
57 
58  auto qc = pair[1];
59  m_code = qc.toStdString();
60  rc = 0;
61  break;
62  }
63  qApp->exit(rc);
64  }
65 
66  [[nodiscard]] std::string code() const
67  {
68  return m_code;
69  }
70 
71  private:
72  std::string m_code{};
73 };
74 
75 bool webview_impl_run(const std::string& title, const std::string& url, std::string& code)
76 {
77  int argc = 1;
78  const auto vendor = QLatin1String(FREERDP_VENDOR_STRING);
79  const auto product = QLatin1String(FREERDP_PRODUCT_STRING);
80  QWebEngineUrlScheme::registerScheme(QWebEngineUrlScheme("ms-appx-web"));
81 
82  std::string wtitle = title;
83  char* argv[] = { wtitle.data() };
84  QCoreApplication::setOrganizationName(vendor);
85  QCoreApplication::setApplicationName(product);
86  QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
87  QApplication app(argc, argv);
88 
89  SchemeHandler handler;
90  QWebEngineProfile::defaultProfile()->installUrlSchemeHandler("ms-appx-web", &handler);
91 
92  QWebEngineView webview;
93  webview.load(QUrl(QString::fromStdString(url)));
94  webview.show();
95 
96  if (app.exec() != 0)
97  return false;
98 
99  auto val = handler.code();
100  if (val.empty())
101  return false;
102  code = val;
103 
104  return !code.empty();
105 }