1 /****************************************************************************
2 **
3 ** Copyright (C) 2019 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtWebEngine module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39
40 #include "find_text_helper.h"
41 #include "qwebenginefindtextresult.h"
42 #include "type_conversion.h"
43 #include "web_contents_adapter_client.h"
44
45 #include "content/public/browser/web_contents.h"
46 #include "third_party/blink/public/mojom/frame/find_in_page.mojom.h"
47
48 namespace QtWebEngineCore {
49
50 // static
51 int FindTextHelper::m_findRequestIdCounter = -1;
52
FindTextHelper(content::WebContents * webContents,WebContentsAdapterClient * viewClient)53 FindTextHelper::FindTextHelper(content::WebContents *webContents, WebContentsAdapterClient *viewClient)
54 : m_webContents(webContents)
55 , m_viewClient(viewClient)
56 , m_currentFindRequestId(m_findRequestIdCounter++)
57 , m_lastCompletedFindRequestId(m_currentFindRequestId)
58 {
59 }
60
~FindTextHelper()61 FindTextHelper::~FindTextHelper()
62 {
63 if (isFindTextInProgress())
64 stopFinding();
65 }
66
startFinding(const QString & findText,bool caseSensitively,bool findBackward,const QWebEngineCallback<bool> resultCallback)67 void FindTextHelper::startFinding(const QString &findText, bool caseSensitively, bool findBackward, const QWebEngineCallback<bool> resultCallback)
68 {
69 if (findText.isEmpty()) {
70 stopFinding();
71 m_viewClient->findTextFinished(QWebEngineFindTextResult());
72 m_widgetCallbacks.invokeEmpty(resultCallback);
73 return;
74 }
75
76 startFinding(findText, caseSensitively, findBackward);
77 m_widgetCallbacks.registerCallback(m_currentFindRequestId, resultCallback);
78 }
79
startFinding(const QString & findText,bool caseSensitively,bool findBackward,const QJSValue & resultCallback)80 void FindTextHelper::startFinding(const QString &findText, bool caseSensitively, bool findBackward, const QJSValue &resultCallback)
81 {
82 if (findText.isEmpty()) {
83 stopFinding();
84 m_viewClient->findTextFinished(QWebEngineFindTextResult());
85 if (!resultCallback.isUndefined()) {
86 QJSValueList args;
87 args.append(QJSValue(0));
88 const_cast<QJSValue&>(resultCallback).call(args);
89 }
90 return;
91 }
92
93 startFinding(findText, caseSensitively, findBackward);
94 if (!resultCallback.isUndefined())
95 m_quickCallbacks.insert(m_currentFindRequestId, resultCallback);
96 }
97
startFinding(const QString & findText,bool caseSensitively,bool findBackward)98 void FindTextHelper::startFinding(const QString &findText, bool caseSensitively, bool findBackward)
99 {
100 if (findText.isEmpty()) {
101 stopFinding();
102 return;
103 }
104
105 if (m_currentFindRequestId > m_lastCompletedFindRequestId) {
106 // There are cases where the render process will overwrite a previous request
107 // with the new search and we'll have a dangling callback, leaving the application
108 // waiting for it forever.
109 // Assume that any unfinished find has been unsuccessful when a new one is started
110 // to cover that case.
111 m_lastCompletedFindRequestId = m_currentFindRequestId;
112 m_viewClient->findTextFinished(QWebEngineFindTextResult());
113 invokeResultCallback(m_currentFindRequestId, 0);
114 }
115
116 blink::mojom::FindOptionsPtr options = blink::mojom::FindOptions::New();
117 options->forward = !findBackward;
118 options->match_case = caseSensitively;
119 options->find_next = findText == m_previousFindText;
120 m_previousFindText = findText;
121
122 m_currentFindRequestId = m_findRequestIdCounter++;
123 m_webContents->Find(m_currentFindRequestId, toString16(findText), std::move(options));
124 }
125
stopFinding()126 void FindTextHelper::stopFinding()
127 {
128 m_lastCompletedFindRequestId = m_currentFindRequestId;
129 m_previousFindText = QString();
130 m_webContents->StopFinding(content::STOP_FIND_ACTION_KEEP_SELECTION);
131 }
132
isFindTextInProgress() const133 bool FindTextHelper::isFindTextInProgress() const
134 {
135 return m_currentFindRequestId != m_lastCompletedFindRequestId;
136 }
137
handleFindReply(content::WebContents * source,int requestId,int numberOfMatches,const gfx::Rect & selectionRect,int activeMatch,bool finalUpdate)138 void FindTextHelper::handleFindReply(content::WebContents *source, int requestId, int numberOfMatches,
139 const gfx::Rect &selectionRect, int activeMatch, bool finalUpdate)
140 {
141 Q_UNUSED(selectionRect);
142
143 Q_ASSERT(source == m_webContents);
144
145 if (!finalUpdate || requestId <= m_lastCompletedFindRequestId)
146 return;
147
148 Q_ASSERT(m_currentFindRequestId == requestId);
149 m_lastCompletedFindRequestId = requestId;
150 m_viewClient->findTextFinished(QWebEngineFindTextResult(numberOfMatches, activeMatch));
151 invokeResultCallback(requestId, numberOfMatches);
152 }
153
handleLoadCommitted()154 void FindTextHelper::handleLoadCommitted()
155 {
156 // Make sure that we don't set the findNext WebFindOptions on a new frame.
157 m_previousFindText = QString();
158 }
159
invokeResultCallback(int requestId,int numberOfMatches)160 void FindTextHelper::invokeResultCallback(int requestId, int numberOfMatches)
161 {
162 if (m_quickCallbacks.contains(requestId)) {
163 QJSValue resultCallback = m_quickCallbacks.take(requestId);
164 QJSValueList args;
165 args.append(QJSValue(numberOfMatches));
166 resultCallback.call(args);
167 } else {
168 m_widgetCallbacks.invoke(requestId, numberOfMatches > 0);
169 }
170 }
171
172 } // namespace QtWebEngineCore
173