diff options
Diffstat (limited to 'src/core/user_script.cpp')
-rw-r--r-- | src/core/user_script.cpp | 170 |
1 files changed, 53 insertions, 117 deletions
diff --git a/src/core/user_script.cpp b/src/core/user_script.cpp index 7ccb6e10a..c33fb9081 100644 --- a/src/core/user_script.cpp +++ b/src/core/user_script.cpp @@ -1,43 +1,9 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtWebEngine module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "common/user_script_data.h" +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +#include "base/strings/string_util.h" + +#include "qtwebengine/userscript/user_script_data.h" #include "user_script.h" #include "type_conversion.h" @@ -54,7 +20,7 @@ bool GetDeclarationValue(const base::StringPiece& line, std::string temp(line.data() + index + prefix.length(), line.length() - index - prefix.length()); - if (temp.empty() || !base::IsUnicodeWhitespace(temp[0])) + if (temp.empty() || !base::IsWhitespace(temp[0])) return false; base::TrimWhitespaceASCII(temp, base::TRIM_ALL, value); @@ -72,30 +38,19 @@ ASSERT_ENUMS_MATCH(UserScript::DocumentElementCreation, UserScriptData::Document UserScript::UserScript() : QSharedData() { + static uint64_t idCount = 0; + m_scriptData.scriptId = idCount++; } -UserScript::UserScript(const UserScript &other) - : QSharedData(other) -{ - if (other.isNull()) - return; - scriptData.reset(new UserScriptData(*other.scriptData)); - m_name = other.m_name; -} +UserScript::UserScript(const UserScript &other) = default; -UserScript::~UserScript() -{ -} +UserScript::~UserScript() = default; UserScript &UserScript::operator=(const UserScript &other) { - if (other.isNull()) { - scriptData.reset(); - m_name = QString(); - return *this; - } - scriptData.reset(new UserScriptData(*other.scriptData)); + m_scriptData = other.m_scriptData; m_name = other.m_name; + m_url = other.m_url; return *this; } @@ -107,101 +62,82 @@ QString UserScript::name() const void UserScript::setName(const QString &name) { m_name = name; - initData(); - scriptData->url = GURL(QStringLiteral("userScript:%1").arg(name).toStdString()); + m_scriptData.url = GURL(QStringLiteral("userScript:%1").arg(name).toStdString()); } QString UserScript::sourceCode() const { - if (isNull()) - return QString(); - return toQt(scriptData->source); + return toQt(m_scriptData.source); } void UserScript::setSourceCode(const QString &source) { - initData(); - scriptData->source = source.toStdString(); + m_scriptData.source = source.toStdString(); parseMetadataHeader(); } -UserScript::InjectionPoint UserScript::injectionPoint() const +QUrl UserScript::sourceUrl() const { - if (isNull()) - return UserScript::AfterLoad; - return static_cast<UserScript::InjectionPoint>(scriptData->injectionPoint); + return m_url; } -void UserScript::setInjectionPoint(UserScript::InjectionPoint p) +void UserScript::setSourceUrl(const QUrl &url) { - initData(); - scriptData->injectionPoint = p; + m_url = url; } -uint UserScript::worldId() const +UserScript::InjectionPoint UserScript::injectionPoint() const { - if (isNull()) - return 1; - return scriptData->worldId; + return static_cast<UserScript::InjectionPoint>(m_scriptData.injectionPoint); } -void UserScript::setWorldId(uint id) +void UserScript::setInjectionPoint(UserScript::InjectionPoint p) { - initData(); - scriptData->worldId = id; + m_scriptData.injectionPoint = p; } -bool UserScript::runsOnSubFrames() const +quint32 UserScript::worldId() const { - if (isNull()) - return false; - return scriptData->injectForSubframes; + return m_scriptData.worldId; } -void UserScript::setRunsOnSubFrames(bool on) +void UserScript::setWorldId(quint32 id) { - initData(); - scriptData->injectForSubframes = on; + m_scriptData.worldId = id; } -bool UserScript::operator==(const UserScript &other) const +bool UserScript::runsOnSubFrames() const { - if (isNull() != other.isNull()) - return false; - if (isNull()) // neither is valid - return true; - return worldId() == other.worldId() - && runsOnSubFrames() == other.runsOnSubFrames() - && injectionPoint() == other.injectionPoint() - && name() == other.name() && sourceCode() == other.sourceCode(); + return m_scriptData.injectForSubframes; } -void UserScript::initData() +void UserScript::setRunsOnSubFrames(bool on) { - if (scriptData.isNull()) - scriptData.reset(new UserScriptData); + m_scriptData.injectForSubframes = on; } -bool UserScript::isNull() const +const UserScriptData &UserScript::data() const { - return scriptData.isNull(); + return m_scriptData; } -UserScriptData &UserScript::data() const +bool UserScript::operator==(const UserScript &other) const { - return *(scriptData.data()); + return worldId() == other.worldId() && runsOnSubFrames() == other.runsOnSubFrames() + && injectionPoint() == other.injectionPoint() && name() == other.name() + && sourceCode() == other.sourceCode() && sourceUrl() == other.sourceUrl(); } void UserScript::parseMetadataHeader() { // Clear previous values - scriptData->globs.clear(); - scriptData->excludeGlobs.clear(); - scriptData->urlPatterns.clear(); + m_scriptData.globs.clear(); + m_scriptData.excludeGlobs.clear(); + m_scriptData.urlPatterns.clear(); // Logic taken from Chromium (extensions/browser/user_script_loader.cc) // http://wiki.greasespot.net/Metadata_block - const std::string &script_text = scriptData->source; + const std::string &script_text = m_scriptData.source; base::StringPiece line; size_t line_start = 0; size_t line_end = line_start; @@ -228,13 +164,13 @@ void UserScript::parseMetadataHeader() if (line_end == std::string::npos) line_end = script_text.length() - 1; - line.set(script_text.data() + line_start, line_end - line_start); + line = base::StringPiece(script_text.data() + line_start, line_end - line_start); if (!in_metadata) { - if (line.starts_with(kUserScriptBegin)) + if (base::StartsWith(line, kUserScriptBegin)) in_metadata = true; } else { - if (line.starts_with(kUserScriptEnd)) + if (base::StartsWith(line, kUserScriptEnd)) break; std::string value; @@ -247,7 +183,7 @@ void UserScript::parseMetadataHeader() base::ReplaceSubstringsAfterOffset(&value, 0, "\\", "\\\\"); base::ReplaceSubstringsAfterOffset(&value, 0, "?", "\\?"); } - scriptData->globs.push_back(value); + m_scriptData.globs.push_back(value); } else if (GetDeclarationValue(line, kExcludeDeclaration, &value)) { if (value.front() != '/' || value.back() != '/') { // The greasemonkey spec only allows for wildcards (*), so @@ -255,16 +191,16 @@ void UserScript::parseMetadataHeader() base::ReplaceSubstringsAfterOffset(&value, 0, "\\", "\\\\"); base::ReplaceSubstringsAfterOffset(&value, 0, "?", "\\?"); } - scriptData->excludeGlobs.push_back(value); + m_scriptData.excludeGlobs.push_back(value); } else if (GetDeclarationValue(line, kMatchDeclaration, &value)) { - scriptData->urlPatterns.push_back(value); + m_scriptData.urlPatterns.push_back(value); } else if (GetDeclarationValue(line, kRunAtDeclaration, &value)) { if (value == kRunAtDocumentStartValue) - scriptData->injectionPoint = DocumentElementCreation; + m_scriptData.injectionPoint = DocumentElementCreation; else if (value == kRunAtDocumentEndValue) - scriptData->injectionPoint = DocumentLoadFinished; + m_scriptData.injectionPoint = DocumentLoadFinished; else if (value == kRunAtDocumentIdleValue) - scriptData->injectionPoint = AfterLoad; + m_scriptData.injectionPoint = AfterLoad; } } @@ -273,8 +209,8 @@ void UserScript::parseMetadataHeader() // If no patterns were specified, default to @include *. This is what // Greasemonkey does. - if (scriptData->globs.empty() && scriptData->urlPatterns.empty()) - scriptData->globs.push_back("*"); + if (m_scriptData.globs.empty() && m_scriptData.urlPatterns.empty()) + m_scriptData.globs.push_back("*"); } } // namespace QtWebEngineCore |