/src/cpp/desktop/DesktopWebPage.cpp

https://github.com/nlhartman/rstudio · C++ · 87 lines · 59 code · 15 blank · 13 comment · 15 complexity · 38b96b648690ddafd5ca26e77f76fb56 MD5 · raw file

  1. /*
  2. * DesktopWebPage.cpp
  3. *
  4. * Copyright (C) 2009-11 by RStudio, Inc.
  5. *
  6. * This program is licensed to you under the terms of version 3 of the
  7. * GNU Affero General Public License. This program is distributed WITHOUT
  8. * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
  9. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
  10. * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
  11. *
  12. */
  13. #include "DesktopWebPage.hpp"
  14. #include <QWidget>
  15. #include <QtDebug>
  16. #include "DesktopNetworkAccessManager.hpp"
  17. extern QString sharedSecret;
  18. namespace desktop {
  19. WebPage::WebPage(QUrl baseUrl, QWidget *parent) :
  20. QWebPage(parent),
  21. baseUrl_(baseUrl),
  22. navigated_(false)
  23. {
  24. //settings()->setAttribute(QWebSettings::DeveloperExtrasEnabled, true);
  25. setNetworkAccessManager(new NetworkAccessManager(sharedSecret, parent));
  26. }
  27. void WebPage::setBaseUrl(const QUrl& baseUrl)
  28. {
  29. baseUrl_ = baseUrl;
  30. }
  31. bool WebPage::shouldInterruptJavaScript()
  32. {
  33. return false;
  34. }
  35. void WebPage::javaScriptConsoleMessage(const QString& message, int /*lineNumber*/, const QString& /*sourceID*/)
  36. {
  37. qDebug() << message;
  38. }
  39. QString WebPage::userAgentForUrl(const QUrl &url) const
  40. {
  41. return this->QWebPage::userAgentForUrl(url) + QString::fromAscii(" Qt/") + QString::fromAscii(qVersion());
  42. }
  43. bool WebPage::acceptNavigationRequest(QWebFrame*,
  44. const QNetworkRequest& request,
  45. NavigationType navType)
  46. {
  47. QUrl url = request.url();
  48. if (url.toString() == QString::fromAscii("about:blank"))
  49. return true;
  50. if (url.scheme() != QString::fromAscii("http")
  51. && url.scheme() != QString::fromAscii("https")
  52. && url.scheme() != QString::fromAscii("mailto"))
  53. {
  54. return false;
  55. }
  56. if (baseUrl_.isEmpty() ||
  57. (url.scheme() == baseUrl_.scheme()
  58. && url.host() == baseUrl_.host()
  59. && url.port() == baseUrl_.port()))
  60. {
  61. navigated_ = true;
  62. return true;
  63. }
  64. else
  65. {
  66. QDesktopServices::openUrl(url);
  67. if (!navigated_)
  68. this->view()->window()->deleteLater();
  69. return false;
  70. }
  71. }
  72. }