find_helper.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. #ifndef ANDROID_WEBVIEW_BROWSER_FIND_HELPER_H_
  5. #define ANDROID_WEBVIEW_BROWSER_FIND_HELPER_H_
  6. #include <string>
  7. #include "base/memory/raw_ptr.h"
  8. namespace content {
  9. class WebContents;
  10. } // namespace content
  11. namespace android_webview {
  12. // Handles the WebView find-in-page API requests.
  13. class FindHelper {
  14. public:
  15. class Listener {
  16. public:
  17. // Called when receiving a new find-in-page update.
  18. // This will be triggered when the results of FindAllSync, FindAllAsync and
  19. // FindNext are available. The value provided in active_ordinal is 0-based.
  20. virtual void OnFindResultReceived(int active_ordinal,
  21. int match_count,
  22. bool finished) = 0;
  23. virtual ~Listener() {}
  24. };
  25. explicit FindHelper(content::WebContents* web_contents);
  26. FindHelper(const FindHelper&) = delete;
  27. FindHelper& operator=(const FindHelper&) = delete;
  28. ~FindHelper();
  29. // Sets the listener to receive find result updates.
  30. // Does not own the listener and must set to nullptr when invalid.
  31. void SetListener(Listener* listener);
  32. // Asynchronous API.
  33. void FindAllAsync(const std::u16string& search_string);
  34. void HandleFindReply(int request_id,
  35. int match_count,
  36. int active_ordinal,
  37. bool finished);
  38. // Methods valid in both synchronous and asynchronous modes.
  39. void FindNext(bool forward);
  40. void ClearMatches();
  41. private:
  42. void StartNewSession(const std::u16string& search_string);
  43. bool MaybeHandleEmptySearch(const std::u16string& search_string);
  44. void NotifyResults(int active_ordinal, int match_count, bool finished);
  45. const raw_ptr<content::WebContents> web_contents_;
  46. // Listener results are reported to.
  47. raw_ptr<Listener> listener_ = nullptr;
  48. // Used to check the validity of FindNext operations.
  49. bool async_find_started_ = false;
  50. // Used to provide different IDs to each request and for result
  51. // verification in asynchronous calls.
  52. int find_request_id_counter_ = 0;
  53. int current_request_id_ = 0;
  54. // Used to mark the beginning of the current find session. This is the ID of
  55. // the first find request in the current session.
  56. int current_session_id_ = 0;
  57. // Required by FindNext and the incremental find replies.
  58. std::u16string last_search_string_;
  59. int last_match_count_ = -1;
  60. int last_active_ordinal_ = -1;
  61. };
  62. } // namespace android_webview
  63. #endif // ANDROID_WEBVIEW_BROWSER_FIND_HELPER_H_