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.
5 #include "chrome/browser/ui/find_bar/find_tab_helper.h"
9 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/ui/find_bar/find_bar_state.h"
12 #include "chrome/browser/ui/find_bar/find_bar_state_factory.h"
13 #include "content/public/browser/notification_service.h"
14 #include "content/public/browser/render_view_host.h"
15 #include "content/public/browser/web_contents.h"
16 #include "content/public/common/stop_find_action.h"
17 #include "third_party/WebKit/public/web/WebFindOptions.h"
18 #include "ui/gfx/rect_f.h"
20 using WebKit::WebFindOptions;
21 using content::WebContents;
23 DEFINE_WEB_CONTENTS_USER_DATA_KEY(FindTabHelper);
26 int FindTabHelper::find_request_id_counter_ = -1;
28 FindTabHelper::FindTabHelper(WebContents* web_contents)
29 : content::WebContentsObserver(web_contents),
30 find_ui_active_(false),
31 find_op_aborted_(false),
32 current_find_request_id_(find_request_id_counter_++),
33 last_search_case_sensitive_(false),
34 last_search_result_() {
37 FindTabHelper::~FindTabHelper() {
40 void FindTabHelper::StartFinding(string16 search_string,
41 bool forward_direction,
42 bool case_sensitive) {
43 // If search_string is empty, it means FindNext was pressed with a keyboard
44 // shortcut so unless we have something to search for we return early.
45 if (search_string.empty() && find_text_.empty()) {
47 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
48 string16 last_search_prepopulate_text =
49 FindBarStateFactory::GetLastPrepopulateText(profile);
51 // Try the last thing we searched for on this tab, then the last thing
52 // searched for on any tab.
53 if (!previous_find_text_.empty())
54 search_string = previous_find_text_;
55 else if (!last_search_prepopulate_text.empty())
56 search_string = last_search_prepopulate_text;
61 // Keep track of the previous search.
62 previous_find_text_ = find_text_;
64 // This is a FindNext operation if we are searching for the same text again,
65 // or if the passed in search text is empty (FindNext keyboard shortcut). The
66 // exception to this is if the Find was aborted (then we don't want FindNext
67 // because the highlighting has been cleared and we need it to reappear). We
68 // therefore treat FindNext after an aborted Find operation as a full fledged
70 bool find_next = (find_text_ == search_string || search_string.empty()) &&
71 (last_search_case_sensitive_ == case_sensitive) &&
74 current_find_request_id_ = find_request_id_counter_++;
76 if (!search_string.empty())
77 find_text_ = search_string;
78 last_search_case_sensitive_ = case_sensitive;
80 find_op_aborted_ = false;
82 // Keep track of what the last search was across the tabs.
84 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
85 FindBarState* find_bar_state = FindBarStateFactory::GetForProfile(profile);
86 find_bar_state->set_last_prepopulate_text(find_text_);
88 WebFindOptions options;
89 options.forward = forward_direction;
90 options.matchCase = case_sensitive;
91 options.findNext = find_next;
92 web_contents()->GetRenderViewHost()->Find(current_find_request_id_,
96 void FindTabHelper::StopFinding(
97 FindBarController::SelectionAction selection_action) {
98 if (selection_action == FindBarController::kClearSelectionOnPage) {
99 // kClearSelection means the find string has been cleared by the user, but
100 // the UI has not been dismissed. In that case we want to clear the
101 // previously remembered search (http://crbug.com/42639).
102 previous_find_text_ = string16();
104 find_ui_active_ = false;
105 if (!find_text_.empty())
106 previous_find_text_ = find_text_;
109 find_op_aborted_ = true;
110 last_search_result_ = FindNotificationDetails();
112 content::StopFindAction action;
113 switch (selection_action) {
114 case FindBarController::kClearSelectionOnPage:
115 action = content::STOP_FIND_ACTION_CLEAR_SELECTION;
117 case FindBarController::kKeepSelectionOnPage:
118 action = content::STOP_FIND_ACTION_KEEP_SELECTION;
120 case FindBarController::kActivateSelectionOnPage:
121 action = content::STOP_FIND_ACTION_ACTIVATE_SELECTION;
125 action = content::STOP_FIND_ACTION_KEEP_SELECTION;
127 web_contents()->GetRenderViewHost()->StopFinding(action);
130 #if defined(OS_ANDROID)
131 void FindTabHelper::ActivateNearestFindResult(float x, float y) {
132 if (!find_op_aborted_ && !find_text_.empty()) {
133 web_contents()->GetRenderViewHost()->ActivateNearestFindResult(
134 current_find_request_id_, x, y);
138 void FindTabHelper::RequestFindMatchRects(int current_version) {
139 if (!find_op_aborted_ && !find_text_.empty())
140 web_contents()->GetRenderViewHost()->RequestFindMatchRects(current_version);
144 void FindTabHelper::HandleFindReply(int request_id,
145 int number_of_matches,
146 const gfx::Rect& selection_rect,
147 int active_match_ordinal,
149 // Ignore responses for requests that have been aborted.
150 // Ignore responses for requests other than the one we have most recently
151 // issued. That way we won't act on stale results when the user has
152 // already typed in another query.
153 if (!find_op_aborted_ && request_id == current_find_request_id_) {
154 if (number_of_matches == -1)
155 number_of_matches = last_search_result_.number_of_matches();
156 if (active_match_ordinal == -1)
157 active_match_ordinal = last_search_result_.active_match_ordinal();
159 gfx::Rect selection = selection_rect;
160 if (final_update && active_match_ordinal == 0)
161 selection = gfx::Rect();
162 else if (selection_rect.IsEmpty())
163 selection = last_search_result_.selection_rect();
165 // Notify the UI, automation and any other observers that a find result was
167 last_search_result_ = FindNotificationDetails(
168 request_id, number_of_matches, selection, active_match_ordinal,
170 content::NotificationService::current()->Notify(
171 chrome::NOTIFICATION_FIND_RESULT_AVAILABLE,
172 content::Source<WebContents>(web_contents()),
173 content::Details<FindNotificationDetails>(&last_search_result_));