find_helper.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "android_webview/browser/find_helper.h"
  5. #include "content/public/browser/render_view_host.h"
  6. #include "content/public/browser/web_contents.h"
  7. #include "content/public/common/stop_find_action.h"
  8. #include "third_party/blink/public/mojom/frame/find_in_page.mojom.h"
  9. using content::WebContents;
  10. namespace android_webview {
  11. FindHelper::FindHelper(WebContents* web_contents)
  12. : web_contents_(web_contents) {}
  13. FindHelper::~FindHelper() {
  14. }
  15. void FindHelper::SetListener(Listener* listener) {
  16. listener_ = listener;
  17. }
  18. void FindHelper::FindAllAsync(const std::u16string& search_string) {
  19. // Stop any ongoing asynchronous request.
  20. web_contents_->StopFinding(content::STOP_FIND_ACTION_KEEP_SELECTION);
  21. async_find_started_ = true;
  22. StartNewSession(search_string);
  23. if (MaybeHandleEmptySearch(search_string))
  24. return;
  25. auto options = blink::mojom::FindOptions::New();
  26. options->forward = true;
  27. options->match_case = false;
  28. options->new_session = true;
  29. web_contents_->Find(current_request_id_, search_string, std::move(options));
  30. }
  31. void FindHelper::HandleFindReply(int request_id,
  32. int match_count,
  33. int active_ordinal,
  34. bool finished) {
  35. if (!async_find_started_ || request_id < current_session_id_)
  36. return;
  37. NotifyResults(active_ordinal, match_count, finished);
  38. }
  39. void FindHelper::FindNext(bool forward) {
  40. if (!async_find_started_)
  41. return;
  42. current_request_id_ = find_request_id_counter_++;
  43. if (MaybeHandleEmptySearch(last_search_string_))
  44. return;
  45. auto options = blink::mojom::FindOptions::New();
  46. options->forward = forward;
  47. options->match_case = false;
  48. options->new_session = false;
  49. web_contents_->Find(current_request_id_, last_search_string_,
  50. std::move(options));
  51. }
  52. void FindHelper::ClearMatches() {
  53. web_contents_->StopFinding(content::STOP_FIND_ACTION_CLEAR_SELECTION);
  54. async_find_started_ = false;
  55. last_search_string_.clear();
  56. last_match_count_ = -1;
  57. last_active_ordinal_ = -1;
  58. }
  59. bool FindHelper::MaybeHandleEmptySearch(const std::u16string& search_string) {
  60. if (!search_string.empty())
  61. return false;
  62. web_contents_->StopFinding(content::STOP_FIND_ACTION_CLEAR_SELECTION);
  63. NotifyResults(0, 0, true);
  64. return true;
  65. }
  66. void FindHelper::StartNewSession(const std::u16string& search_string) {
  67. current_request_id_ = find_request_id_counter_++;
  68. current_session_id_ = current_request_id_;
  69. last_search_string_ = search_string;
  70. last_match_count_ = -1;
  71. last_active_ordinal_ = -1;
  72. }
  73. void FindHelper::NotifyResults(int active_ordinal,
  74. int match_count,
  75. bool finished) {
  76. // Match count or ordinal values set to -1 refer to received replies.
  77. if (match_count == -1)
  78. match_count = last_match_count_;
  79. else
  80. last_match_count_ = match_count;
  81. if (active_ordinal == -1)
  82. active_ordinal = last_active_ordinal_;
  83. else
  84. last_active_ordinal_ = active_ordinal;
  85. // Skip the update if we don't still have a valid ordinal.
  86. // The next update, final or not, should have this information.
  87. if (!finished && active_ordinal == -1)
  88. return;
  89. // Safeguard in case of errors to prevent reporting -1 to the API listeners.
  90. if (match_count == -1) {
  91. NOTREACHED();
  92. match_count = 0;
  93. }
  94. if (active_ordinal == -1) {
  95. NOTREACHED();
  96. active_ordinal = 0;
  97. }
  98. // WebView.FindListener active match ordinals are 0-based while WebKit sends
  99. // 1-based ordinals. Still we can receive 0 ordinal in case of no results.
  100. active_ordinal = std::max(active_ordinal - 1, 0);
  101. if (listener_)
  102. listener_->OnFindResultReceived(active_ordinal, match_count, finished);
  103. }
  104. } // namespace android_webview