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 5 #ifndef CHROME_BROWSER_UI_FIND_BAR_FIND_TAB_HELPER_H_ 6 #define CHROME_BROWSER_UI_FIND_BAR_FIND_TAB_HELPER_H_ 7 8 #include "chrome/browser/ui/find_bar/find_bar_controller.h" 9 #include "chrome/browser/ui/find_bar/find_notification_details.h" 10 #include "content/public/browser/web_contents_observer.h" 11 #include "content/public/browser/web_contents_user_data.h" 12 #include "ui/gfx/range/range.h" 13 14 namespace gfx { 15 class RectF; 16 } 17 18 // Per-tab find manager. Handles dealing with the life cycle of find sessions. 19 class FindTabHelper : public content::WebContentsObserver, 20 public content::WebContentsUserData<FindTabHelper> { 21 public: 22 virtual ~FindTabHelper(); 23 24 // Starts the Find operation by calling StartFinding on the Tab. This function 25 // can be called from the outside as a result of hot-keys, so it uses the 26 // last remembered search string as specified with set_find_string(). This 27 // function does not block while a search is in progress. The controller will 28 // receive the results through the notification mechanism. See Observe(...) 29 // for details. 30 void StartFinding(base::string16 search_string, 31 bool forward_direction, 32 bool case_sensitive); 33 34 // Stops the current Find operation. 35 void StopFinding(FindBarController::SelectionAction selection_action); 36 37 // Accessors/Setters for find_ui_active_. 38 bool find_ui_active() const { return find_ui_active_; } 39 void set_find_ui_active(bool find_ui_active) { 40 find_ui_active_ = find_ui_active; 41 } 42 43 // Setter for find_op_aborted_. 44 void set_find_op_aborted(bool find_op_aborted) { 45 find_op_aborted_ = find_op_aborted; 46 } 47 48 // Used _only_ by testing to get or set the current request ID. 49 int current_find_request_id() { return current_find_request_id_; } 50 void set_current_find_request_id(int current_find_request_id) { 51 current_find_request_id_ = current_find_request_id; 52 } 53 54 // Accessor for find_text_. Used to determine if this WebContents has any 55 // active searches. 56 base::string16 find_text() const { return find_text_; } 57 58 // Accessor for the previous search we issued. 59 base::string16 previous_find_text() const { return previous_find_text_; } 60 61 gfx::Range selected_range() const { return selected_range_; } 62 void set_selected_range(const gfx::Range& selected_range) { 63 selected_range_ = selected_range; 64 } 65 66 // Accessor for find_result_. 67 const FindNotificationDetails& find_result() const { 68 return last_search_result_; 69 } 70 71 #if defined(OS_ANDROID) 72 // Selects and zooms to the find result nearest to the point (x,y) 73 // defined in find-in-page coordinates. 74 void ActivateNearestFindResult(float x, float y); 75 76 // Asks the renderer to send the rects of the current find matches. 77 void RequestFindMatchRects(int current_version); 78 #endif 79 80 void HandleFindReply(int request_id, 81 int number_of_matches, 82 const gfx::Rect& selection_rect, 83 int active_match_ordinal, 84 bool final_update); 85 86 private: 87 explicit FindTabHelper(content::WebContents* web_contents); 88 friend class content::WebContentsUserData<FindTabHelper>; 89 90 // Each time a search request comes in we assign it an id before passing it 91 // over the IPC so that when the results come in we can evaluate whether we 92 // still care about the results of the search (in some cases we don't because 93 // the user has issued a new search). 94 static int find_request_id_counter_; 95 96 // True if the Find UI is active for this Tab. 97 bool find_ui_active_; 98 99 // True if a Find operation was aborted. This can happen if the Find box is 100 // closed or if the search term inside the Find box is erased while a search 101 // is in progress. This can also be set if a page has been reloaded, and will 102 // on FindNext result in a full Find operation so that the highlighting for 103 // inactive matches can be repainted. 104 bool find_op_aborted_; 105 106 // This variable keeps track of what the most recent request id is. 107 int current_find_request_id_; 108 109 // The current string we are/just finished searching for. This is used to 110 // figure out if this is a Find or a FindNext operation (FindNext should not 111 // increase the request id). 112 base::string16 find_text_; 113 114 // The string we searched for before |find_text_|. 115 base::string16 previous_find_text_; 116 117 // The selection within the text. 118 gfx::Range selected_range_; 119 120 // Whether the last search was case sensitive or not. 121 bool last_search_case_sensitive_; 122 123 // The last find result. This object contains details about the number of 124 // matches, the find selection rectangle, etc. The UI can access this 125 // information to build its presentation. 126 FindNotificationDetails last_search_result_; 127 128 DISALLOW_COPY_AND_ASSIGN(FindTabHelper); 129 }; 130 131 #endif // CHROME_BROWSER_UI_FIND_BAR_FIND_TAB_HELPER_H_ 132