OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/find_bar/find_tab_helper.h" | 5 #include "chrome/browser/ui/find_bar/find_tab_helper.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
10 #include "chrome/browser/ui/find_bar/find_bar_state.h" | 10 #include "chrome/browser/ui/find_bar/find_bar_state.h" |
11 #include "content/browser/renderer_host/render_view_host.h" | 11 #include "content/browser/renderer_host/render_view_host.h" |
12 #include "content/browser/tab_contents/tab_contents.h" | 12 #include "content/browser/tab_contents/tab_contents.h" |
13 #include "chrome/common/chrome_notification_types.h" | 13 #include "chrome/common/chrome_notification_types.h" |
14 #include "content/public/browser/notification_service.h" | 14 #include "content/public/browser/notification_service.h" |
15 #include "content/public/common/stop_find_action.h" | 15 #include "content/public/common/stop_find_action.h" |
16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFindOptions.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFindOptions.h" |
17 | 17 |
18 using WebKit::WebFindOptions; | 18 using WebKit::WebFindOptions; |
| 19 using content::WebContents; |
19 | 20 |
20 // static | 21 // static |
21 int FindTabHelper::find_request_id_counter_ = -1; | 22 int FindTabHelper::find_request_id_counter_ = -1; |
22 | 23 |
23 FindTabHelper::FindTabHelper(TabContents* tab_contents) | 24 FindTabHelper::FindTabHelper(TabContents* tab_contents) |
24 : content::WebContentsObserver(tab_contents), | 25 : content::WebContentsObserver(tab_contents), |
25 find_ui_active_(false), | 26 find_ui_active_(false), |
26 find_op_aborted_(false), | 27 find_op_aborted_(false), |
27 current_find_request_id_(find_request_id_counter_++), | 28 current_find_request_id_(find_request_id_counter_++), |
28 last_search_case_sensitive_(false), | 29 last_search_case_sensitive_(false), |
29 last_search_result_() { | 30 last_search_result_() { |
30 } | 31 } |
31 | 32 |
32 FindTabHelper::~FindTabHelper() { | 33 FindTabHelper::~FindTabHelper() { |
33 } | 34 } |
34 | 35 |
35 void FindTabHelper::StartFinding(string16 search_string, | 36 void FindTabHelper::StartFinding(string16 search_string, |
36 bool forward_direction, | 37 bool forward_direction, |
37 bool case_sensitive) { | 38 bool case_sensitive) { |
38 // If search_string is empty, it means FindNext was pressed with a keyboard | 39 // If search_string is empty, it means FindNext was pressed with a keyboard |
39 // shortcut so unless we have something to search for we return early. | 40 // shortcut so unless we have something to search for we return early. |
40 if (search_string.empty() && find_text_.empty()) { | 41 if (search_string.empty() && find_text_.empty()) { |
41 Profile* profile = | 42 Profile* profile = |
42 Profile::FromBrowserContext(tab_contents()->GetBrowserContext()); | 43 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
43 string16 last_search_prepopulate_text = | 44 string16 last_search_prepopulate_text = |
44 FindBarState::GetLastPrepopulateText(profile); | 45 FindBarState::GetLastPrepopulateText(profile); |
45 | 46 |
46 // Try the last thing we searched for on this tab, then the last thing | 47 // Try the last thing we searched for on this tab, then the last thing |
47 // searched for on any tab. | 48 // searched for on any tab. |
48 if (!previous_find_text_.empty()) | 49 if (!previous_find_text_.empty()) |
49 search_string = previous_find_text_; | 50 search_string = previous_find_text_; |
50 else if (!last_search_prepopulate_text.empty()) | 51 else if (!last_search_prepopulate_text.empty()) |
51 search_string = last_search_prepopulate_text; | 52 search_string = last_search_prepopulate_text; |
52 else | 53 else |
(...skipping 16 matching lines...) Expand all Loading... |
69 current_find_request_id_ = find_request_id_counter_++; | 70 current_find_request_id_ = find_request_id_counter_++; |
70 | 71 |
71 if (!search_string.empty()) | 72 if (!search_string.empty()) |
72 find_text_ = search_string; | 73 find_text_ = search_string; |
73 last_search_case_sensitive_ = case_sensitive; | 74 last_search_case_sensitive_ = case_sensitive; |
74 | 75 |
75 find_op_aborted_ = false; | 76 find_op_aborted_ = false; |
76 | 77 |
77 // Keep track of what the last search was across the tabs. | 78 // Keep track of what the last search was across the tabs. |
78 Profile* profile = | 79 Profile* profile = |
79 Profile::FromBrowserContext(tab_contents()->GetBrowserContext()); | 80 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
80 FindBarState* find_bar_state = profile->GetFindBarState(); | 81 FindBarState* find_bar_state = profile->GetFindBarState(); |
81 find_bar_state->set_last_prepopulate_text(find_text_); | 82 find_bar_state->set_last_prepopulate_text(find_text_); |
82 | 83 |
83 WebFindOptions options; | 84 WebFindOptions options; |
84 options.forward = forward_direction; | 85 options.forward = forward_direction; |
85 options.matchCase = case_sensitive; | 86 options.matchCase = case_sensitive; |
86 options.findNext = find_next; | 87 options.findNext = find_next; |
87 tab_contents()->GetRenderViewHost()->Find(current_find_request_id_, | 88 web_contents()->GetRenderViewHost()->Find(current_find_request_id_, |
88 find_text_, options); | 89 find_text_, options); |
89 } | 90 } |
90 | 91 |
91 void FindTabHelper::StopFinding( | 92 void FindTabHelper::StopFinding( |
92 FindBarController::SelectionAction selection_action) { | 93 FindBarController::SelectionAction selection_action) { |
93 if (selection_action == FindBarController::kClearSelection) { | 94 if (selection_action == FindBarController::kClearSelection) { |
94 // kClearSelection means the find string has been cleared by the user, but | 95 // kClearSelection means the find string has been cleared by the user, but |
95 // the UI has not been dismissed. In that case we want to clear the | 96 // the UI has not been dismissed. In that case we want to clear the |
96 // previously remembered search (http://crbug.com/42639). | 97 // previously remembered search (http://crbug.com/42639). |
97 previous_find_text_ = string16(); | 98 previous_find_text_ = string16(); |
(...skipping 14 matching lines...) Expand all Loading... |
112 case FindBarController::kKeepSelection: | 113 case FindBarController::kKeepSelection: |
113 action = content::STOP_FIND_ACTION_KEEP_SELECTION; | 114 action = content::STOP_FIND_ACTION_KEEP_SELECTION; |
114 break; | 115 break; |
115 case FindBarController::kActivateSelection: | 116 case FindBarController::kActivateSelection: |
116 action = content::STOP_FIND_ACTION_ACTIVATE_SELECTION; | 117 action = content::STOP_FIND_ACTION_ACTIVATE_SELECTION; |
117 break; | 118 break; |
118 default: | 119 default: |
119 NOTREACHED(); | 120 NOTREACHED(); |
120 action = content::STOP_FIND_ACTION_KEEP_SELECTION; | 121 action = content::STOP_FIND_ACTION_KEEP_SELECTION; |
121 } | 122 } |
122 tab_contents()->GetRenderViewHost()->StopFinding(action); | 123 web_contents()->GetRenderViewHost()->StopFinding(action); |
123 } | 124 } |
124 | 125 |
125 void FindTabHelper::HandleFindReply(int request_id, | 126 void FindTabHelper::HandleFindReply(int request_id, |
126 int number_of_matches, | 127 int number_of_matches, |
127 const gfx::Rect& selection_rect, | 128 const gfx::Rect& selection_rect, |
128 int active_match_ordinal, | 129 int active_match_ordinal, |
129 bool final_update) { | 130 bool final_update) { |
130 // Ignore responses for requests that have been aborted. | 131 // Ignore responses for requests that have been aborted. |
131 // Ignore responses for requests other than the one we have most recently | 132 // Ignore responses for requests other than the one we have most recently |
132 // issued. That way we won't act on stale results when the user has | 133 // issued. That way we won't act on stale results when the user has |
133 // already typed in another query. | 134 // already typed in another query. |
134 if (!find_op_aborted_ && request_id == current_find_request_id_) { | 135 if (!find_op_aborted_ && request_id == current_find_request_id_) { |
135 if (number_of_matches == -1) | 136 if (number_of_matches == -1) |
136 number_of_matches = last_search_result_.number_of_matches(); | 137 number_of_matches = last_search_result_.number_of_matches(); |
137 if (active_match_ordinal == -1) | 138 if (active_match_ordinal == -1) |
138 active_match_ordinal = last_search_result_.active_match_ordinal(); | 139 active_match_ordinal = last_search_result_.active_match_ordinal(); |
139 | 140 |
140 gfx::Rect selection = selection_rect; | 141 gfx::Rect selection = selection_rect; |
141 if (selection.IsEmpty()) | 142 if (selection.IsEmpty()) |
142 selection = last_search_result_.selection_rect(); | 143 selection = last_search_result_.selection_rect(); |
143 | 144 |
144 // Notify the UI, automation and any other observers that a find result was | 145 // Notify the UI, automation and any other observers that a find result was |
145 // found. | 146 // found. |
146 last_search_result_ = FindNotificationDetails( | 147 last_search_result_ = FindNotificationDetails( |
147 request_id, number_of_matches, selection, active_match_ordinal, | 148 request_id, number_of_matches, selection, active_match_ordinal, |
148 final_update); | 149 final_update); |
149 content::NotificationService::current()->Notify( | 150 content::NotificationService::current()->Notify( |
150 chrome::NOTIFICATION_FIND_RESULT_AVAILABLE, | 151 chrome::NOTIFICATION_FIND_RESULT_AVAILABLE, |
151 content::Source<TabContents>(tab_contents()), | 152 content::Source<WebContents>(web_contents()), |
152 content::Details<FindNotificationDetails>(&last_search_result_)); | 153 content::Details<FindNotificationDetails>(&last_search_result_)); |
153 } | 154 } |
154 } | 155 } |
OLD | NEW |