find_tab_helper.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // Copyright (c) 2011 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 COMPONENTS_FIND_IN_PAGE_FIND_TAB_HELPER_H_
  5. #define COMPONENTS_FIND_IN_PAGE_FIND_TAB_HELPER_H_
  6. #include <string>
  7. #include "base/memory/raw_ptr.h"
  8. #include "base/observer_list.h"
  9. #include "build/build_config.h"
  10. #include "components/find_in_page/find_notification_details.h"
  11. #include "content/public/browser/web_contents_user_data.h"
  12. #include "ui/gfx/range/range.h"
  13. namespace find_in_page {
  14. class FindResultObserver;
  15. enum class SelectionAction;
  16. // Per-tab find manager. Handles dealing with the life cycle of find sessions.
  17. class FindTabHelper : public content::WebContentsUserData<FindTabHelper> {
  18. public:
  19. // The delegate tracks search text state.
  20. class Delegate {
  21. public:
  22. // Informs the delegate when the user searches.
  23. virtual void SetLastSearchText(const std::u16string& text) = 0;
  24. // Gets the text to prepopulate into the search field for new searches. May
  25. // return an empty string.
  26. virtual std::u16string GetSearchPrepopulateText() = 0;
  27. protected:
  28. virtual ~Delegate() = default;
  29. };
  30. FindTabHelper(const FindTabHelper&) = delete;
  31. FindTabHelper& operator=(const FindTabHelper&) = delete;
  32. ~FindTabHelper() override;
  33. void AddObserver(FindResultObserver* observer);
  34. void RemoveObserver(FindResultObserver* observer);
  35. // Starts the Find operation by calling StartFinding on the Tab. This function
  36. // can be called from the outside as a result of hot-keys, so it uses the
  37. // last remembered search string as specified with set_find_string(). This
  38. // function does not block while a search is in progress. The controller will
  39. // receive the results through the notification mechanism. See Observe(...)
  40. // for details.
  41. //
  42. // |find_match| controls whether to find the first match or to only do match
  43. // counts and highlighting.
  44. void StartFinding(std::u16string search_string,
  45. bool forward_direction,
  46. bool case_sensitive,
  47. bool find_match,
  48. bool run_synchronously_for_testing = false);
  49. // Stops the current Find operation.
  50. void StopFinding(SelectionAction selection_action);
  51. // When the user commits to a search query or jumps from one result
  52. // to the next, move accessibility focus to the next find result.
  53. void ActivateFindInPageResultForAccessibility();
  54. // Retrieves the starting text for searching in the tab.
  55. std::u16string GetInitialSearchText();
  56. // Accessors/Setters for find_ui_active_.
  57. bool find_ui_active() const { return find_ui_active_; }
  58. void set_find_ui_active(bool find_ui_active) {
  59. find_ui_active_ = find_ui_active;
  60. }
  61. // Used _only_ by testing to get the current request ID.
  62. int current_find_request_id() { return current_find_request_id_; }
  63. // Accessor for find_text_. Used to determine if this WebContents has any
  64. // active searches.
  65. std::u16string find_text() const { return find_text_; }
  66. // Accessor for the previous search we issued.
  67. std::u16string previous_find_text() const { return previous_find_text_; }
  68. // Accessor for the last completed search (i.e., where |find_match| was true
  69. // and we got a final_update result).
  70. std::u16string last_completed_find_text() const {
  71. return last_completed_find_text_;
  72. }
  73. void set_last_completed_find_text(
  74. const std::u16string& last_completed_find_text) {
  75. last_completed_find_text_ = last_completed_find_text;
  76. }
  77. gfx::Range selected_range() const { return selected_range_; }
  78. void set_selected_range(const gfx::Range& selected_range) {
  79. selected_range_ = selected_range;
  80. }
  81. // Accessor for find_result_.
  82. const FindNotificationDetails& find_result() const {
  83. return last_search_result_;
  84. }
  85. bool should_find_match() const { return should_find_match_; }
  86. #if BUILDFLAG(IS_ANDROID)
  87. // Selects and zooms to the find result nearest to the point (x,y)
  88. // defined in find-in-page coordinates.
  89. void ActivateNearestFindResult(float x, float y);
  90. // Asks the renderer to send the rects of the current find matches.
  91. void RequestFindMatchRects(int current_version);
  92. #endif
  93. void HandleFindReply(int request_id,
  94. int number_of_matches,
  95. const gfx::Rect& selection_rect,
  96. int active_match_ordinal,
  97. bool final_update);
  98. void set_delegate(Delegate* delegate) { delegate_ = delegate; }
  99. private:
  100. explicit FindTabHelper(content::WebContents* web_contents);
  101. friend class content::WebContentsUserData<FindTabHelper>;
  102. // Each time a search request comes in we assign it an id before passing it
  103. // over the IPC so that when the results come in we can evaluate whether we
  104. // still care about the results of the search (in some cases we don't because
  105. // the user has issued a new search).
  106. static int find_request_id_counter_;
  107. // True if the Find UI is active for this Tab.
  108. bool find_ui_active_ = false;
  109. // True if a Find operation was aborted. This can happen if the Find box is
  110. // closed or if the search term inside the Find box is erased while a search
  111. // is in progress. This can also be set if a page has been reloaded, and will
  112. // on FindNext result in a full Find operation so that the highlighting for
  113. // inactive matches can be repainted.
  114. bool find_op_aborted_ = false;
  115. // This variable keeps track of what the most recent request ID is.
  116. int current_find_request_id_;
  117. // This variable keeps track of the ID of the first find request in the
  118. // current session, which also uniquely identifies the session.
  119. int current_find_session_id_;
  120. // The current string we are/just finished searching for. This is used to
  121. // figure out if this is a Find or a FindNext operation (FindNext should not
  122. // increase the request id).
  123. std::u16string find_text_;
  124. // The string we searched for before |find_text_|.
  125. std::u16string previous_find_text_;
  126. // Used to keep track the last completed search (i.e., where |find_match|
  127. // was true and we got a final_update result). A single find session can
  128. // result in multiple final updates, if the document contents change
  129. // dynamically. It's a nuisance to notify the user more than once that a
  130. // search came up empty, and we never want to notify the user that a
  131. // previously successful search's results were removed because,
  132. // for instance, the page is being torn down during navigation.
  133. std::u16string last_completed_find_text_;
  134. // The selection within the text.
  135. gfx::Range selected_range_;
  136. // Whether the last search was case sensitive or not.
  137. bool last_search_case_sensitive_ = false;
  138. // The last find result. This object contains details about the number of
  139. // matches, the find selection rectangle, etc. The UI can access this
  140. // information to build its presentation.
  141. FindNotificationDetails last_search_result_;
  142. // The value of the |find_match| option for the active search, or false if
  143. // there is no active search.
  144. bool should_find_match_ = false;
  145. // The optional delegate that remembers recent search text state.
  146. raw_ptr<Delegate> delegate_ = nullptr;
  147. base::ObserverList<FindResultObserver> observers_;
  148. WEB_CONTENTS_USER_DATA_KEY_DECL();
  149. };
  150. } // namespace find_in_page
  151. #endif // COMPONENTS_FIND_IN_PAGE_FIND_TAB_HELPER_H_