Skip to content

Instantly share code, notes, and snippets.

@lauromoura
Created April 5, 2012 21:30
Show Gist options
  • Save lauromoura/2314310 to your computer and use it in GitHub Desktop.
Save lauromoura/2314310 to your computer and use it in GitHub Desktop.
New user agent patch.
commit db1d0405a73ae9a5b70c506516380b66cbcd071b
Author: Lauro Neto <lauro.neto@openbossa.org>
Date: Wed Apr 4 21:33:35 2012 -0300
[Qt] Shared implementation of user agent for WK1 and WK2.
diff --git a/Source/WebCore/Target.pri b/Source/WebCore/Target.pri
index 96ec846..4554581 100644
--- a/Source/WebCore/Target.pri
+++ b/Source/WebCore/Target.pri
@@ -2313,6 +2313,7 @@ HEADERS += \
platform/qt/QWebPageClient.h \
platform/qt/RenderThemeQt.h \
platform/qt/RenderThemeQtMobile.h \
+ platform/qt/UserAgentQt.h \
platform/ScrollableArea.h \
platform/ScrollAnimator.h \
platform/Scrollbar.h \
@@ -2852,6 +2853,7 @@ SOURCES += \
platform/qt/RunLoopQt.cpp \
platform/qt/SharedBufferQt.cpp \
platform/qt/ThirdPartyCookiesQt.cpp \
+ platform/qt/UserAgentQt.cpp \
platform/graphics/qt/FontCacheQt.cpp \
platform/graphics/qt/FontCustomPlatformDataQt.cpp \
platform/graphics/qt/GlyphPageTreeNodeQt.cpp \
diff --git a/Source/WebCore/platform/qt/UserAgentQt.cpp b/Source/WebCore/platform/qt/UserAgentQt.cpp
new file mode 100644
index 0000000..e61595a
--- /dev/null
+++ b/Source/WebCore/platform/qt/UserAgentQt.cpp
@@ -0,0 +1,192 @@
+/*
+ * Copyright (C) 2007 Apple Inc. All rights reserved.
+ * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
+ * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
+ * Copyright (C) 2010 Sencha, Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+
+#include "UserAgentQt.h"
+
+#include <QCoreApplication>
+
+#include <wtf/text/WTFString.h>
+#if defined Q_OS_WIN32
+#include <WebCore/SystemInfo.h>
+#endif // Q_OS_WIN32
+#include <WebKitVersion.h>
+
+namespace WebCore {
+
+/*!
+ This function is called when a user agent for HTTP requests is needed.
+
+ This implementation returns the following value:
+
+ "Mozilla/5.0 (%Platform%%Security%%Subplatform%) AppleWebKit/%WebKitVersion% (KHTML, like Gecko) %AppVersion Safari/%WebKitVersion%"
+
+ In this string the following values are replaced the first time the function is called:
+ \list
+ \li %Platform% expands to the windowing system followed by "; " if it is not Windows (e.g. "X11; ").
+ \li %Security% expands to "N; " if SSL is disabled.
+ \li %Subplatform% expands to the operating system version (e.g. "Windows NT 6.1" or "Intel Mac OS X 10.5").
+ \li %WebKitVersion% is the version of WebKit the application was compiled against.
+ /endlist
+
+ The following value is replaced each time the funciton is called
+ \list
+ \li %AppVersion% expands to QCoreApplication::applicationName()/QCoreApplication::applicationVersion() if they're set; otherwise defaulting to Qt and the current Qt version.
+ \endlist
+*/
+String UserAgentQt::standardUserAgent(const String &applicationNameForUserAgent)
+{
+ static QString ua;
+
+ if (ua.isNull()) {
+
+ ua = QLatin1String("Mozilla/5.0 (%1%2%3) AppleWebKit/%4 (KHTML, like Gecko) %99 Safari/%5");
+
+ // Platform.
+ ua = ua.arg(QLatin1String(
+#ifdef Q_WS_MAC
+ "Macintosh; "
+#elif defined Q_WS_QWS
+ "QtEmbedded; "
+#elif defined Q_WS_WIN
+ // Nothing.
+#elif defined Q_WS_X11
+ "X11; "
+#else
+ "Unknown; "
+#endif
+ ));
+
+ // Security strength.
+ QString securityStrength;
+#if defined(QT_NO_OPENSSL)
+ securityStrength = QLatin1String("N; ");
+#endif
+ ua = ua.arg(securityStrength);
+
+ // Operating system.
+ ua = ua.arg(QLatin1String(
+#ifdef Q_OS_AIX
+ "AIX"
+#elif defined Q_OS_WIN32
+ windowsVersionForUAString()
+#elif defined Q_OS_DARWIN
+#ifdef __i386__ || __x86_64__
+ "Intel Mac OS X"
+#else
+ "PPC Mac OS X"
+#endif
+
+#elif defined Q_OS_BSDI
+ "BSD"
+#elif defined Q_OS_BSD4
+ "BSD Four"
+#elif defined Q_OS_CYGWIN
+ "Cygwin"
+#elif defined Q_OS_DGUX
+ "DG/UX"
+#elif defined Q_OS_DYNIX
+ "DYNIX/ptx"
+#elif defined Q_OS_FREEBSD
+ "FreeBSD"
+#elif defined Q_OS_HPUX
+ "HP-UX"
+#elif defined Q_OS_HURD
+ "GNU Hurd"
+#elif defined Q_OS_IRIX
+ "SGI Irix"
+#elif defined Q_OS_LINUX
+
+#if defined(__x86_64__)
+ "Linux x86_64"
+#elif defined(__i386__)
+ "Linux i686"
+#else
+ "Linux"
+#endif
+
+#elif defined Q_OS_LYNX
+ "LynxOS"
+#elif defined Q_OS_NETBSD
+ "NetBSD"
+#elif defined Q_OS_OS2
+ "OS/2"
+#elif defined Q_OS_OPENBSD
+ "OpenBSD"
+#elif defined Q_OS_OS2EMX
+ "OS/2"
+#elif defined Q_OS_OSF
+ "HP Tru64 UNIX"
+#elif defined Q_OS_QNX6
+ "QNX RTP Six"
+#elif defined Q_OS_QNX
+ "QNX"
+#elif defined Q_OS_RELIANT
+ "Reliant UNIX"
+#elif defined Q_OS_SCO
+ "SCO OpenServer"
+#elif defined Q_OS_SOLARIS
+ "Sun Solaris"
+#elif defined Q_OS_ULTRIX
+ "DEC Ultrix"
+#elif defined Q_OS_UNIX
+ "UNIX BSD/SYSV system"
+#elif defined Q_OS_UNIXWARE
+ "UnixWare Seven, Open UNIX Eight"
+#else
+ "Unknown"
+#endif
+ ));
+
+ // WebKit version.
+ // TODO: using macros from WebKit1 until WebKit2 Qt port versioning is sorted out.
+ QString version = QString(QLatin1String("%1.%2+")).arg(QString::number(WEBKIT_MAJOR_VERSION),
+ QString::number(WEBKIT_MINOR_VERSION));
+ ua = ua.arg(version, version);
+ }
+
+ QString appName;
+ if (applicationNameForUserAgent.isEmpty())
+ appName = QCoreApplication::applicationName();
+ else
+ appName = applicationNameForUserAgent;
+
+ if (!appName.isEmpty()) {
+ QString appVer = QCoreApplication::applicationVersion();
+ if (!appVer.isEmpty())
+ appName.append(QLatin1Char('/') + appVer);
+ } else {
+ // Qt version.
+ appName = QLatin1String("Qt/") + QLatin1String(qVersion());
+ }
+
+ return ua.arg(appName);
+}
+
+}
diff --git a/Source/WebCore/platform/qt/UserAgentQt.h b/Source/WebCore/platform/qt/UserAgentQt.h
new file mode 100644
index 0000000..c000a19
--- /dev/null
+++ b/Source/WebCore/platform/qt/UserAgentQt.h
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef UserAgentQt_h
+#define UserAgentQt_h
+
+#include <wtf/text/WTFString.h>
+
+namespace WebCore {
+
+class UserAgentQt {
+public:
+ static String standardUserAgent(const String &applicationNameForUserAgent);
+};
+
+}
+
+#endif // UserAgentQt_h
diff --git a/Source/WebKit/qt/Api/qwebpage.cpp b/Source/WebKit/qt/Api/qwebpage.cpp
index bf0c3bc..afffeae8 100644
--- a/Source/WebKit/qt/Api/qwebpage.cpp
+++ b/Source/WebKit/qt/Api/qwebpage.cpp
@@ -113,6 +113,7 @@
#include "SystemInfo.h"
#endif // Q_OS_WIN32
#include "TextIterator.h"
+#include "UserAgentQt.h"
#include "UtilsQt.h"
#include "WebEventConversion.h"
#include "WindowFeatures.h"
@@ -3753,158 +3754,7 @@ QWebPluginFactory *QWebPage::pluginFactory() const
*/
QString QWebPage::userAgentForUrl(const QUrl&) const
{
- // splitting the string in three and user QStringBuilder is better than using QString::arg()
- static QString firstPart;
- static QString secondPart;
- static QString thirdPart;
-
- if (firstPart.isNull() || secondPart.isNull() || thirdPart.isNull()) {
- QString firstPartTemp;
- firstPartTemp.reserve(150);
- firstPartTemp += QString::fromLatin1("Mozilla/5.0 ("
-
- // Platform
-#ifdef Q_WS_MAC
- "Macintosh; "
-#elif defined Q_WS_QWS
- "QtEmbedded; "
-#elif defined Q_WS_WIN
- // Nothing
-#elif defined Q_WS_X11
- "X11; "
-#else
- "Unknown; "
-#endif
- );
-
-#if defined(QT_NO_OPENSSL)
- // No SSL support
- firstPartTemp += QString::fromLatin1("N; ");
-#endif
-
- // Operating system
-#ifdef Q_OS_AIX
- firstPartTemp += QString::fromLatin1("AIX");
-#elif defined Q_OS_WIN32
- firstPartTemp += windowsVersionForUAString();
-#elif defined Q_OS_DARWIN
-#ifdef __i386__ || __x86_64__
- firstPartTemp += QString::fromLatin1("Intel Mac OS X");
-#else
- firstPartTemp += QString::fromLatin1("PPC Mac OS X");
-#endif
-
-#elif defined Q_OS_BSDI
- firstPartTemp += QString::fromLatin1("BSD");
-#elif defined Q_OS_BSD4
- firstPartTemp += QString::fromLatin1("BSD Four");
-#elif defined Q_OS_CYGWIN
- firstPartTemp += QString::fromLatin1("Cygwin");
-#elif defined Q_OS_DGUX
- firstPartTemp += QString::fromLatin1("DG/UX");
-#elif defined Q_OS_DYNIX
- firstPartTemp += QString::fromLatin1("DYNIX/ptx");
-#elif defined Q_OS_FREEBSD
- firstPartTemp += QString::fromLatin1("FreeBSD");
-#elif defined Q_OS_HPUX
- firstPartTemp += QString::fromLatin1("HP-UX");
-#elif defined Q_OS_HURD
- firstPartTemp += QString::fromLatin1("GNU Hurd");
-#elif defined Q_OS_IRIX
- firstPartTemp += QString::fromLatin1("SGI Irix");
-#elif defined Q_OS_LINUX
-
-#if defined(__x86_64__)
- firstPartTemp += QString::fromLatin1("Linux x86_64");
-#elif defined(__i386__)
- firstPartTemp += QString::fromLatin1("Linux i686");
-#else
- firstPartTemp += QString::fromLatin1("Linux");
-#endif
-
-#elif defined Q_OS_LYNX
- firstPartTemp += QString::fromLatin1("LynxOS");
-#elif defined Q_OS_NETBSD
- firstPartTemp += QString::fromLatin1("NetBSD");
-#elif defined Q_OS_OS2
- firstPartTemp += QString::fromLatin1("OS/2");
-#elif defined Q_OS_OPENBSD
- firstPartTemp += QString::fromLatin1("OpenBSD");
-#elif defined Q_OS_OS2EMX
- firstPartTemp += QString::fromLatin1("OS/2");
-#elif defined Q_OS_OSF
- firstPartTemp += QString::fromLatin1("HP Tru64 UNIX");
-#elif defined Q_OS_QNX6
- firstPartTemp += QString::fromLatin1("QNX RTP Six");
-#elif defined Q_OS_QNX
- firstPartTemp += QString::fromLatin1("QNX");
-#elif defined Q_OS_RELIANT
- firstPartTemp += QString::fromLatin1("Reliant UNIX");
-#elif defined Q_OS_SCO
- firstPartTemp += QString::fromLatin1("SCO OpenServer");
-#elif defined Q_OS_SOLARIS
- firstPartTemp += QString::fromLatin1("Sun Solaris");
-#elif defined Q_OS_ULTRIX
- firstPartTemp += QString::fromLatin1("DEC Ultrix");
-#elif defined Q_OS_UNIX
- firstPartTemp += QString::fromLatin1("UNIX BSD/SYSV system");
-#elif defined Q_OS_UNIXWARE
- firstPartTemp += QString::fromLatin1("UnixWare Seven, Open UNIX Eight");
-#else
- firstPartTemp += QString::fromLatin1("Unknown");
-#endif
-
-#if USE(QT_MOBILITY_SYSTEMINFO)
- // adding Model Number
- QtMobility::QSystemDeviceInfo systemDeviceInfo;
-
- QString model = systemDeviceInfo.model();
- if (!model.isEmpty()) {
- if (!firstPartTemp.endsWith("; "))
- firstPartTemp += QString::fromLatin1("; ");
- firstPartTemp += systemDeviceInfo.model();
- }
-#endif
- firstPartTemp.squeeze();
- firstPart = firstPartTemp;
-
- QString secondPartTemp;
- secondPartTemp.reserve(150);
- secondPartTemp += QString::fromLatin1(") ");
-
- // webkit/qt version
- secondPartTemp += QString::fromLatin1("AppleWebKit/");
- secondPartTemp += qWebKitVersion();
- secondPartTemp += QString::fromLatin1(" (KHTML, like Gecko) ");
-
-
- // Application name split the third part
- secondPartTemp.squeeze();
- secondPart = secondPartTemp;
-
- QString thirdPartTemp;
- thirdPartTemp.reserve(150);
- thirdPartTemp += QLatin1String(" Safari/");
- thirdPartTemp += qWebKitVersion();
- thirdPartTemp.squeeze();
- thirdPart = thirdPartTemp;
- Q_ASSERT(!firstPart.isNull());
- Q_ASSERT(!secondPart.isNull());
- Q_ASSERT(!thirdPart.isNull());
- }
-
- // Application name/version
- QString appName = QCoreApplication::applicationName();
- if (!appName.isEmpty()) {
- QString appVer = QCoreApplication::applicationVersion();
- if (!appVer.isEmpty())
- appName.append(QLatin1Char('/') + appVer);
- } else {
- // Qt version
- appName = QString::fromLatin1("Qt/") + QString::fromLatin1(qVersion());
- }
-
- return firstPart + secondPart + appName + thirdPart;
+ return UserAgentQt::standardUserAgent("");
}
diff --git a/Source/WebKit2/UIProcess/qt/WebPageProxyQt.cpp b/Source/WebKit2/UIProcess/qt/WebPageProxyQt.cpp
index bf86edc..d800ba4 100644
--- a/Source/WebKit2/UIProcess/qt/WebPageProxyQt.cpp
+++ b/Source/WebKit2/UIProcess/qt/WebPageProxyQt.cpp
@@ -32,6 +32,7 @@
#include "qquicknetworkreply_p.h"
#include "WebPageMessages.h"
#include "WebProcessProxy.h"
+#include "UserAgentQt.h"
#include <WebCore/Editor.h>
#include <WebCore/NotImplemented.h>
@@ -41,8 +42,7 @@ namespace WebKit {
String WebPageProxy::standardUserAgent(const String& applicationNameForUserAgent)
{
- // FIXME: This should not be hard coded.
- return "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6) AppleWebKit/531.4 (KHTML, like Gecko) Version/4.0.3 Safari/531.4";
+ return UserAgentQt::standardUserAgent(applicationNameForUserAgent);
}
void WebPageProxy::saveRecentSearches(const String&, const Vector<String>&)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment