diff options
author | Michal Klocek <michal.klocek@qt.io> | 2020-08-26 16:25:00 +0200 |
---|---|---|
committer | Michal Klocek <michal.klocek@qt.io> | 2020-09-28 13:59:08 +0200 |
commit | 3bf3d3fe7bcfa7933ecc2ec2f1607f586b2b00d8 (patch) | |
tree | 85efe06d6dd9a6afb997b016007376a86c4a2aaf /src/core/user_script.cpp | |
parent | b598974f9cbde2688a75488d6920e474343c84d2 (diff) | |
download | qtwebengine-3bf3d3fe7bcfa7933ecc2ec2f1607f586b2b00d8.tar.gz |
Cleanup qwebenginescript and qwebenginescriptcollection api
This patch cleans up script and collection apis:
* do not allocate user_script on heap, there is no need
for that.
* remove isNull(), which was used by collection.findScript(name)
* remove collection.size(), there is already collection.count()
* remove collection.findScript(name), user can use findScripts(name)
which returns list of scripts or empty list if not found
* collection.findScripts(name) is simply collection.find(name)
[ChangeLog] Removed QWebEngineScriptCollection::findScript(name),
use QWebEngineScriptCollection::find(name) instead.
Change-Id: Iecf8f1d7c26275b9ce3a1ea97cf4bd74b17f681e
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Diffstat (limited to 'src/core/user_script.cpp')
-rw-r--r-- | src/core/user_script.cpp | 110 |
1 files changed, 32 insertions, 78 deletions
diff --git a/src/core/user_script.cpp b/src/core/user_script.cpp index 6f5828cbf..f4f320d4c 100644 --- a/src/core/user_script.cpp +++ b/src/core/user_script.cpp @@ -74,28 +74,13 @@ UserScript::UserScript() { } -UserScript::UserScript(const UserScript &other) - : QSharedData(other) -{ - if (other.isNull()) - return; - scriptData.reset(new UserScriptData(*other.scriptData)); - m_name = other.m_name; - m_url = other.m_url; -} +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; @@ -109,21 +94,17 @@ 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(); } @@ -139,83 +120,56 @@ void UserScript::setSourceUrl(const QUrl &url) UserScript::InjectionPoint UserScript::injectionPoint() const { - if (isNull()) - return UserScript::AfterLoad; - return static_cast<UserScript::InjectionPoint>(scriptData->injectionPoint); + return static_cast<UserScript::InjectionPoint>(m_scriptData.injectionPoint); } void UserScript::setInjectionPoint(UserScript::InjectionPoint p) { - initData(); - scriptData->injectionPoint = p; + m_scriptData.injectionPoint = p; } -uint UserScript::worldId() const +quint32 UserScript::worldId() const { - if (isNull()) - return 1; - return scriptData->worldId; + return m_scriptData.worldId; } -void UserScript::setWorldId(uint id) +void UserScript::setWorldId(quint32 id) { - initData(); - scriptData->worldId = id; + m_scriptData.worldId = id; } bool UserScript::runsOnSubFrames() const { - if (isNull()) - return false; - return scriptData->injectForSubframes; + return m_scriptData.injectForSubframes; } void UserScript::setRunsOnSubFrames(bool on) { - initData(); - scriptData->injectForSubframes = on; -} - -bool UserScript::operator==(const UserScript &other) 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() - && sourceUrl() == other.sourceUrl(); + m_scriptData.injectForSubframes = on; } -void UserScript::initData() +const UserScriptData &UserScript::data() const { - if (scriptData.isNull()) - scriptData.reset(new UserScriptData); + return m_scriptData; } -bool UserScript::isNull() const -{ - return scriptData.isNull(); -} - -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; @@ -261,7 +215,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 @@ -269,16 +223,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; } } @@ -287,8 +241,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 |