diff options
author | Kirill Burtsev <kirill.burtsev@qt.io> | 2020-02-12 16:15:34 +0100 |
---|---|---|
committer | Kirill Burtsev <kirill.burtsev@qt.io> | 2020-02-26 12:13:40 +0000 |
commit | df5d831bae99662fab43ed2628187113c18aac2c (patch) | |
tree | 62e24e3c05d6d9c1f90834523b476651536b712d /src/core/web_contents_adapter.cpp | |
parent | 08d1d866107b82525f5c1ec83f179c58ee7e6b97 (diff) |
Clear previous page text selection on new navigation unconditionally
Remove code duplication on triggering new url load, and use direct
code to clear SelectedText instead of CollapseSelection as it assumes
focused frame and might be ignored.
Fixes: QTBUG-81574
Change-Id: I01cf02967e118f407c8a3997e176d5b258478a5a
Reviewed-by: Peter Varga <pvarga@inf.u-szeged.hu>
Diffstat (limited to 'src/core/web_contents_adapter.cpp')
-rw-r--r-- | src/core/web_contents_adapter.cpp | 46 |
1 files changed, 32 insertions, 14 deletions
diff --git a/src/core/web_contents_adapter.cpp b/src/core/web_contents_adapter.cpp index 8cc8179cf..a7579f916 100644 --- a/src/core/web_contents_adapter.cpp +++ b/src/core/web_contents_adapter.cpp @@ -67,6 +67,7 @@ #include "base/task/sequence_manager/thread_controller_with_message_pump_impl.h" #include "base/values.h" #include "content/browser/renderer_host/render_view_host_impl.h" +#include "content/browser/renderer_host/text_input_manager.h" #include "content/browser/web_contents/web_contents_impl.h" #include "content/public/browser/browser_task_traits.h" #include "content/public/browser/child_process_security_policy.h" @@ -369,6 +370,23 @@ static void deserializeNavigationHistory(QDataStream &input, int *currentIndex, } } +static void Navigate(WebContentsAdapter *adapter, const content::NavigationController::LoadURLParams ¶ms) +{ + Q_ASSERT(adapter); + adapter->webContents()->GetController().LoadURLWithParams(params); + adapter->focusIfNecessary(); + adapter->resetSelection(); +} + +static void NavigateTask(QWeakPointer<WebContentsAdapter> weakAdapter, const content::NavigationController::LoadURLParams ¶ms) +{ + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); + const auto adapter = weakAdapter.toStrongRef(); + if (!adapter) + return; + Navigate(adapter.get(), params); +} + namespace { static QList<WebContentsAdapter *> recursive_guard_loading_adapters; @@ -705,21 +723,12 @@ void WebContentsAdapter::load(const QWebEngineHttpRequest &request) } } - auto navigate = [](QWeakPointer<WebContentsAdapter> weakAdapter, const content::NavigationController::LoadURLParams ¶ms) { - const auto adapter = weakAdapter.toStrongRef(); - if (!adapter) - return; - adapter->webContents()->GetController().LoadURLWithParams(params); - adapter->focusIfNecessary(); - }; - - QWeakPointer<WebContentsAdapter> weakThis(sharedFromThis()); if (resizeNeeded) { // Schedule navigation on the event loop. base::PostTaskWithTraits(FROM_HERE, {content::BrowserThread::UI}, - base::BindOnce(navigate, std::move(weakThis), std::move(params))); + base::BindOnce(&NavigateTask, sharedFromThis().toWeakRef(), std::move(params))); } else { - navigate(std::move(weakThis), params); + Navigate(this, params); } } @@ -752,9 +761,7 @@ void WebContentsAdapter::setContent(const QByteArray &data, const QString &mimeT params.can_load_local_resources = true; params.transition_type = ui::PageTransitionFromInt(ui::PAGE_TRANSITION_TYPED | ui::PAGE_TRANSITION_FROM_API); params.override_user_agent = content::NavigationController::UA_OVERRIDE_TRUE; - m_webContents->GetController().LoadURLWithParams(params); - focusIfNecessary(); - m_webContents->CollapseSelection(); + Navigate(this, params); } void WebContentsAdapter::save(const QString &filePath, int savePageFormat) @@ -1676,6 +1683,17 @@ bool WebContentsAdapter::hasFocusedFrame() const return m_webContents->GetFocusedFrame() != nullptr; } +void WebContentsAdapter::resetSelection() +{ + CHECK_INITIALIZED(); + // unconditionally clears the selection in contrast to CollapseSelection, which checks focus state first + if (auto rwhv = static_cast<RenderWidgetHostViewQt *>(m_webContents->GetRenderWidgetHostView())) { + if (auto mgr = rwhv->GetTextInputManager()) + if (auto selection = const_cast<content::TextInputManager::TextSelection *>(mgr->GetTextSelection(rwhv))) + selection->SetSelection(base::string16(), 0, gfx::Range(), false); + } +} + WebContentsAdapterClient::RenderProcessTerminationStatus WebContentsAdapterClient::renderProcessExitStatus(int terminationStatus) { auto status = WebContentsAdapterClient::RenderProcessTerminationStatus(-1); |