File: web_view_find_helper.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (297 lines) | stat: -rw-r--r-- 10,871 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "extensions/browser/guest_view/web_view/web_view_find_helper.h"

#include <memory>
#include <utility>

#include "base/memory/scoped_refptr.h"
#include "components/guest_view/browser/guest_view_event.h"
#include "extensions/browser/guest_view/web_view/web_view_constants.h"
#include "extensions/browser/guest_view/web_view/web_view_guest.h"

using guest_view::GuestViewEvent;

namespace {

// Parameters to callback functions.
const char kFindNumberOfMatches[] = "numberOfMatches";
const char kFindActiveMatchOrdinal[] = "activeMatchOrdinal";
const char kFindSelectionRect[] = "selectionRect";
const char kFindRectLeft[] = "left";
const char kFindRectTop[] = "top";
const char kFindRectWidth[] = "width";
const char kFindRectHeight[] = "height";
const char kFindCanceled[] = "canceled";

}  // anonymous namespace

namespace extensions {

WebViewFindHelper::WebViewFindHelper(WebViewGuest* webview_guest)
    : webview_guest_(webview_guest), current_find_request_id_(0) {
}

WebViewFindHelper::~WebViewFindHelper() {
}

void WebViewFindHelper::CancelAllFindSessions() {
  current_find_session_ = nullptr;
  while (!find_info_map_.empty()) {
    find_info_map_.begin()->second->SendResponse(true /* canceled */);
    find_info_map_.erase(find_info_map_.begin());
  }
  if (find_update_event_)
    DispatchFindUpdateEvent(true /* canceled */, true /* final_update */);
  find_update_event_.reset();
}

void WebViewFindHelper::DispatchFindUpdateEvent(bool canceled,
                                                bool final_update) {
  CHECK(find_update_event_);
  base::Value::Dict args;
  find_update_event_->PrepareResults(args);
  args.Set(kFindCanceled, canceled);
  args.Set(webview::kFindFinalUpdate, final_update);
  CHECK(webview_guest_);
  webview_guest_->DispatchEventToView(std::make_unique<GuestViewEvent>(
      webview::kEventFindReply, std::move(args)));
}

void WebViewFindHelper::EndFindSession(int session_request_id, bool canceled) {
  auto session_iterator = find_info_map_.find(session_request_id);
  CHECK(session_iterator != find_info_map_.end());
  FindInfo* find_info = session_iterator->second.get();

  // Call the callback function of the first request of the find session.
  find_info->SendResponse(canceled);

  // For every subsequent find request of the find session.
  for (auto i = find_info->find_next_requests_.begin();
       i != find_info->find_next_requests_.end(); ++i) {
    DCHECK(i->get());

    // Do not call callbacks for subsequent find requests that have not been
    // replied to yet. These requests will get their own final updates in the
    // same order as they appear in |find_next_requests_|, i.e. the order that
    // the requests were made in. Once one request is found that has not been
    // replied to, none that follow will be replied to either, and do not need
    // to be checked.
    if (!(*i)->replied_)
      break;

    // Update the request's number of matches (if not canceled).
    if (!canceled) {
      (*i)->find_results_.number_of_matches_ =
          find_info->find_results_.number_of_matches_;
    }

    // Call the request's callback function with the find results, and then
    // delete its map entry to free the WebViewInternalFindFunction object.
    (*i)->SendResponse(canceled);
    find_info_map_.erase((*i)->request_id_);
  }

  // Erase the first find request's map entry to free the
  // WebViewInternalFindFunction
  // object.
  find_info_map_.erase(session_request_id);
}

void WebViewFindHelper::Find(content::WebContents* guest_web_contents,
                             const std::u16string& search_text,
                             blink::mojom::FindOptionsPtr options,
                             ForwardResponseCallback callback) {
  // Need a new request_id for each new find request.
  ++current_find_request_id_;

  if (current_find_session_) {
    const std::u16string& current_search_text =
        current_find_session_->search_text();
    bool current_match_case = current_find_session_->options()->match_case;
    options->new_session = current_search_text.empty() ||
                           current_search_text != search_text ||
                           current_match_case != options->match_case;
  } else {
    options->new_session = true;
  }

  // Stores the find request information by request_id so that its callback
  // function can be called when the find results are available.
  std::pair<FindInfoMap::iterator, bool> insert_result =
      find_info_map_.insert(std::make_pair(
          current_find_request_id_, base::MakeRefCounted<FindInfo>(
                                        current_find_request_id_, search_text,
                                        options.Clone(), std::move(callback))));
  // No duplicate insertions.
  CHECK(insert_result.second);

  // Link find requests that are a part of the same find session.
  if (!options->new_session && current_find_session_) {
    CHECK(current_find_request_id_ != current_find_session_->request_id());
    current_find_session_->AddFindNextRequest(
        insert_result.first->second->AsWeakPtr());
  }

  // Update the current find session, if necessary.
  if (options->new_session) {
    current_find_session_ = insert_result.first->second;
  }

  // Handle the empty |search_text| case internally.
  if (search_text.empty()) {
    guest_web_contents->StopFinding(content::STOP_FIND_ACTION_CLEAR_SELECTION);
    FindReply(current_find_request_id_, 0, gfx::Rect(), 0, true);
    return;
  }

  guest_web_contents->Find(current_find_request_id_, search_text,
                           std::move(options), /*skip_delay=*/true);
}

void WebViewFindHelper::FindReply(int request_id,
                                  int number_of_matches,
                                  const gfx::Rect& selection_rect,
                                  int active_match_ordinal,
                                  bool final_update) {
  auto find_iterator = find_info_map_.find(request_id);

  // Ignore slow replies to canceled find requests.
  if (find_iterator == find_info_map_.end())
    return;

  // This find request must be a part of an existing find session.
  CHECK(current_find_session_);

  WebViewFindHelper::FindInfo* find_info = find_iterator->second.get();
  // Handle canceled find requests.
  if (find_info->options()->new_session &&
      find_info_map_.begin()->first < request_id) {
    CHECK_NE(current_find_session_->request_id(),
             find_info_map_.begin()->first);
    if (find_update_event_)
      DispatchFindUpdateEvent(true /* canceled */, true /* final_update */);
    EndFindSession(find_info_map_.begin()->first, true /* canceled */);
  }

  // Clears the results for |findupdate| for a new find session.
  if (!find_info->replied() && find_info->options()->new_session) {
    find_update_event_ =
        std::make_unique<FindUpdateEvent>(find_info->search_text());
  }

  // Aggregate the find results.
  find_info->AggregateResults(number_of_matches, selection_rect,
                              active_match_ordinal, final_update);
  if (find_update_event_) {
    find_update_event_->AggregateResults(number_of_matches, selection_rect,
                                         active_match_ordinal, final_update);
    // Propagate incremental results to the |findupdate| event.
    DispatchFindUpdateEvent(false /* canceled */, final_update);
  }

  // Call the callback functions of completed find requests.
  if (final_update)
    EndFindSession(request_id, false /* canceled */);
}

WebViewFindHelper::FindResults::FindResults()
    : number_of_matches_(0), active_match_ordinal_(0) {
}

WebViewFindHelper::FindResults::~FindResults() {
}

void WebViewFindHelper::FindResults::AggregateResults(
    int number_of_matches,
    const gfx::Rect& selection_rect,
    int active_match_ordinal,
    bool final_update) {
  if (number_of_matches != -1)
    number_of_matches_ = number_of_matches;

  if (active_match_ordinal != -1)
    active_match_ordinal_ = active_match_ordinal;

  if (final_update && active_match_ordinal_ == 0) {
    // No match found, so the selection rectangle is empty.
    selection_rect_ = gfx::Rect();
  } else if (!selection_rect.IsEmpty()) {
    selection_rect_ = selection_rect;
  }
}

void WebViewFindHelper::FindResults::PrepareResults(
    base::Value::Dict& results) {
  results.Set(kFindNumberOfMatches, number_of_matches_);
  results.Set(kFindActiveMatchOrdinal, active_match_ordinal_);
  base::Value::Dict rect;
  rect.Set(kFindRectLeft, selection_rect_.x());
  rect.Set(kFindRectTop, selection_rect_.y());
  rect.Set(kFindRectWidth, selection_rect_.width());
  rect.Set(kFindRectHeight, selection_rect_.height());
  results.Set(kFindSelectionRect, std::move(rect));
}

WebViewFindHelper::FindUpdateEvent::FindUpdateEvent(
    const std::u16string& search_text)
    : search_text_(search_text) {}

WebViewFindHelper::FindUpdateEvent::~FindUpdateEvent() {
}

void WebViewFindHelper::FindUpdateEvent::AggregateResults(
    int number_of_matches,
    const gfx::Rect& selection_rect,
    int active_match_ordinal,
    bool final_update) {
  find_results_.AggregateResults(number_of_matches, selection_rect,
                                 active_match_ordinal, final_update);
}

void WebViewFindHelper::FindUpdateEvent::PrepareResults(
    base::Value::Dict& results) {
  results.Set(webview::kFindSearchText, search_text_);
  find_results_.PrepareResults(results);
}

WebViewFindHelper::FindInfo::FindInfo(int request_id,
                                      const std::u16string& search_text,
                                      blink::mojom::FindOptionsPtr options,
                                      ForwardResponseCallback callback)
    : request_id_(request_id),
      search_text_(search_text),
      options_(std::move(options)),
      callback_(std::move(callback)),
      replied_(false) {}

void WebViewFindHelper::FindInfo::AggregateResults(
    int number_of_matches,
    const gfx::Rect& selection_rect,
    int active_match_ordinal,
    bool final_update) {
  replied_ = true;
  find_results_.AggregateResults(number_of_matches, selection_rect,
                                 active_match_ordinal, final_update);
}

base::WeakPtr<WebViewFindHelper::FindInfo>
WebViewFindHelper::FindInfo::AsWeakPtr() {
  return weak_ptr_factory_.GetWeakPtr();
}

void WebViewFindHelper::FindInfo::SendResponse(bool canceled) {
  // Prepare the find results to pass to the callback function.
  base::Value::Dict results;
  find_results_.PrepareResults(results);
  results.Set(kFindCanceled, canceled);

  // Call the callback.
  std::move(callback_).Run(std::move(results));
}

WebViewFindHelper::FindInfo::~FindInfo() = default;

}  // namespace extensions