PageRenderTime 69ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/external/webkit/Tools/QtTestBrowser/webpage.cpp

https://gitlab.com/brian0218/rk3188_r-box_android4.2.2_sdk
C++ | 210 lines | 145 code | 34 blank | 31 comment | 16 complexity | be285b5c19541007a803c4d2490344f0 MD5 | raw file
  1. /*
  2. * Copyright (C) 2009-2010 Nokia Corporation and/or its subsidiary(-ies)
  3. * Copyright (C) 2009 Girish Ramakrishnan <girish@forwardbias.in>
  4. * Copyright (C) 2006 George Staikos <staikos@kde.org>
  5. * Copyright (C) 2006 Dirk Mueller <mueller@kde.org>
  6. * Copyright (C) 2006 Zack Rusin <zack@kde.org>
  7. * Copyright (C) 2006 Simon Hausmann <hausmann@kde.org>
  8. *
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
  21. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
  24. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  28. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #include "webpage.h"
  33. #include "launcherwindow.h"
  34. #include <QAuthenticator>
  35. #include <QDesktopServices>
  36. #include <QtGui>
  37. #include <QtNetwork/QNetworkReply>
  38. #include <QtNetwork/QNetworkRequest>
  39. #include <QtNetwork/QNetworkProxy>
  40. WebPage::WebPage(QObject* parent)
  41. : QWebPage(parent)
  42. , m_userAgent()
  43. , m_interruptingJavaScriptEnabled(false)
  44. {
  45. applyProxy();
  46. connect(networkAccessManager(), SIGNAL(authenticationRequired(QNetworkReply*, QAuthenticator*)),
  47. this, SLOT(authenticationRequired(QNetworkReply*, QAuthenticator*)));
  48. connect(this, SIGNAL(featurePermissionRequested(QWebFrame*, QWebPage::Feature)), this, SLOT(requestPermission(QWebFrame*, QWebPage::Feature)));
  49. connect(this, SIGNAL(featurePermissionRequestCanceled(QWebFrame*, QWebPage::Feature)), this, SLOT(featurePermissionRequestCanceled(QWebFrame*, QWebPage::Feature)));
  50. }
  51. void WebPage::applyProxy()
  52. {
  53. QUrl proxyUrl(qgetenv("http_proxy"));
  54. if (proxyUrl.isValid() && !proxyUrl.host().isEmpty()) {
  55. int proxyPort = (proxyUrl.port() > 0) ? proxyUrl.port() : 8080;
  56. networkAccessManager()->setProxy(QNetworkProxy(QNetworkProxy::HttpProxy, proxyUrl.host(), proxyPort));
  57. }
  58. }
  59. bool WebPage::supportsExtension(QWebPage::Extension extension) const
  60. {
  61. if (extension == QWebPage::ErrorPageExtension)
  62. return true;
  63. return false;
  64. }
  65. bool WebPage::extension(Extension extension, const ExtensionOption* option, ExtensionReturn* output)
  66. {
  67. const QWebPage::ErrorPageExtensionOption* info = static_cast<const QWebPage::ErrorPageExtensionOption*>(option);
  68. QWebPage::ErrorPageExtensionReturn* errorPage = static_cast<QWebPage::ErrorPageExtensionReturn*>(output);
  69. errorPage->content = QString("<html><head><title>Failed loading page</title></head><body>%1</body></html>")
  70. .arg(info->errorString).toUtf8();
  71. return true;
  72. }
  73. bool WebPage::acceptNavigationRequest(QWebFrame* frame, const QNetworkRequest& request, NavigationType type)
  74. {
  75. QObject* view = parent();
  76. QVariant value = view->property("keyboardModifiers");
  77. if (!value.isNull()) {
  78. Qt::KeyboardModifiers modifiers = Qt::KeyboardModifiers(value.toInt());
  79. if (modifiers & Qt::ShiftModifier) {
  80. QWebPage* page = createWindow(QWebPage::WebBrowserWindow);
  81. page->mainFrame()->load(request);
  82. return false;
  83. }
  84. if (modifiers & Qt::AltModifier) {
  85. openUrlInDefaultBrowser(request.url());
  86. return false;
  87. }
  88. }
  89. return QWebPage::acceptNavigationRequest(frame, request, type);
  90. }
  91. void WebPage::openUrlInDefaultBrowser(const QUrl& url)
  92. {
  93. #ifndef QT_NO_DESKTOPSERVICES
  94. if (QAction* action = qobject_cast<QAction*>(sender()))
  95. QDesktopServices::openUrl(action->data().toUrl());
  96. else
  97. QDesktopServices::openUrl(url);
  98. #endif
  99. }
  100. QString WebPage::userAgentForUrl(const QUrl& url) const
  101. {
  102. if (!m_userAgent.isEmpty())
  103. return m_userAgent;
  104. return QWebPage::userAgentForUrl(url);
  105. }
  106. bool WebPage::shouldInterruptJavaScript()
  107. {
  108. if (!m_interruptingJavaScriptEnabled)
  109. return false;
  110. return QWebPage::shouldInterruptJavaScript();
  111. }
  112. void WebPage::authenticationRequired(QNetworkReply* reply, QAuthenticator* authenticator)
  113. {
  114. QDialog* dialog = new QDialog(QApplication::activeWindow());
  115. dialog->setWindowTitle("HTTP Authentication");
  116. QGridLayout* layout = new QGridLayout(dialog);
  117. dialog->setLayout(layout);
  118. QLabel* messageLabel = new QLabel(dialog);
  119. messageLabel->setWordWrap(true);
  120. QString messageStr = QString("Enter with username and password for: %1");
  121. messageLabel->setText(messageStr.arg(reply->url().toString()));
  122. layout->addWidget(messageLabel, 0, 1);
  123. #ifndef QT_NO_LINEEDIT
  124. QLabel* userLabel = new QLabel("Username:", dialog);
  125. layout->addWidget(userLabel, 1, 0);
  126. QLineEdit* userInput = new QLineEdit(dialog);
  127. layout->addWidget(userInput, 1, 1);
  128. QLabel* passLabel = new QLabel("Password:", dialog);
  129. layout->addWidget(passLabel, 2, 0);
  130. QLineEdit* passInput = new QLineEdit(dialog);
  131. passInput->setEchoMode(QLineEdit::Password);
  132. layout->addWidget(passInput, 2, 1);
  133. #endif
  134. QDialogButtonBox* buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok
  135. | QDialogButtonBox::Cancel, Qt::Horizontal, dialog);
  136. connect(buttonBox, SIGNAL(accepted()), dialog, SLOT(accept()));
  137. connect(buttonBox, SIGNAL(rejected()), dialog, SLOT(reject()));
  138. layout->addWidget(buttonBox, 3, 1);
  139. if (dialog->exec() == QDialog::Accepted) {
  140. #ifndef QT_NO_LINEEDIT
  141. authenticator->setUser(userInput->text());
  142. authenticator->setPassword(passInput->text());
  143. #endif
  144. }
  145. delete dialog;
  146. }
  147. void WebPage::requestPermission(QWebFrame* frame, QWebPage::Feature feature)
  148. {
  149. setFeaturePermission(frame, feature, PermissionGrantedByUser);
  150. }
  151. void WebPage::featurePermissionRequestCanceled(QWebFrame*, QWebPage::Feature)
  152. {
  153. }
  154. QWebPage* WebPage::createWindow(QWebPage::WebWindowType type)
  155. {
  156. LauncherWindow* mw = new LauncherWindow;
  157. if (type == WebModalDialog)
  158. mw->setWindowModality(Qt::ApplicationModal);
  159. mw->show();
  160. return mw->page();
  161. }
  162. QObject* WebPage::createPlugin(const QString &classId, const QUrl&, const QStringList&, const QStringList&)
  163. {
  164. if (classId == "alien_QLabel") {
  165. QLabel* l = new QLabel;
  166. l->winId();
  167. return l;
  168. }
  169. #ifndef QT_NO_UITOOLS
  170. QUiLoader loader;
  171. return loader.createWidget(classId, view());
  172. #else
  173. Q_UNUSED(classId);
  174. return 0;
  175. #endif
  176. }