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 5 #include "chrome/browser/ui/find_bar/find_bar_controller.h" 6 7 #include <algorithm> 8 9 #include "base/i18n/rtl.h" 10 #include "base/logging.h" 11 #include "build/build_config.h" 12 #include "chrome/browser/chrome_notification_types.h" 13 #include "chrome/browser/profiles/profile.h" 14 #include "chrome/browser/ui/find_bar/find_bar.h" 15 #include "chrome/browser/ui/find_bar/find_bar_state.h" 16 #include "chrome/browser/ui/find_bar/find_bar_state_factory.h" 17 #include "chrome/browser/ui/find_bar/find_tab_helper.h" 18 #include "content/public/browser/navigation_details.h" 19 #include "content/public/browser/navigation_entry.h" 20 #include "content/public/browser/notification_details.h" 21 #include "content/public/browser/notification_source.h" 22 #include "content/public/browser/web_contents.h" 23 #include "ui/gfx/rect.h" 24 25 using content::NavigationController; 26 using content::WebContents; 27 28 // The minimum space between the FindInPage window and the search result. 29 static const int kMinFindWndDistanceFromSelection = 5; 30 31 FindBarController::FindBarController(FindBar* find_bar) 32 : find_bar_(find_bar), 33 web_contents_(NULL), 34 last_reported_matchcount_(0) { 35 } 36 37 FindBarController::~FindBarController() { 38 DCHECK(!web_contents_); 39 } 40 41 void FindBarController::Show() { 42 FindTabHelper* find_tab_helper = 43 FindTabHelper::FromWebContents(web_contents_); 44 45 // Only show the animation if we're not already showing a find bar for the 46 // selected WebContents. 47 if (!find_tab_helper->find_ui_active()) { 48 MaybeSetPrepopulateText(); 49 50 find_tab_helper->set_find_ui_active(true); 51 find_bar_->Show(true); 52 } 53 find_bar_->SetFocusAndSelection(); 54 } 55 56 void FindBarController::EndFindSession(SelectionAction selection_action, 57 ResultAction result_action) { 58 find_bar_->Hide(true); 59 60 // |web_contents_| can be NULL for a number of reasons, for example when the 61 // tab is closing. We must guard against that case. See issue 8030. 62 if (web_contents_) { 63 FindTabHelper* find_tab_helper = 64 FindTabHelper::FromWebContents(web_contents_); 65 66 // When we hide the window, we need to notify the renderer that we are done 67 // for now, so that we can abort the scoping effort and clear all the 68 // tickmarks and highlighting. 69 find_tab_helper->StopFinding(selection_action); 70 71 if (result_action == kClearResultsInFindBox) 72 find_bar_->ClearResults(find_tab_helper->find_result()); 73 74 // When we get dismissed we restore the focus to where it belongs. 75 find_bar_->RestoreSavedFocus(); 76 } 77 } 78 79 void FindBarController::ChangeWebContents(WebContents* contents) { 80 if (web_contents_) { 81 registrar_.RemoveAll(); 82 find_bar_->StopAnimation(); 83 } 84 85 web_contents_ = contents; 86 FindTabHelper* find_tab_helper = 87 web_contents_ ? FindTabHelper::FromWebContents(web_contents_) 88 : NULL; 89 90 // Hide any visible find window from the previous tab if a NULL tab contents 91 // is passed in or if the find UI is not active in the new tab. 92 if (find_bar_->IsFindBarVisible() && 93 (!find_tab_helper || !find_tab_helper->find_ui_active())) { 94 find_bar_->Hide(false); 95 } 96 97 if (!web_contents_) 98 return; 99 100 registrar_.Add(this, 101 chrome::NOTIFICATION_FIND_RESULT_AVAILABLE, 102 content::Source<WebContents>(web_contents_)); 103 registrar_.Add( 104 this, 105 content::NOTIFICATION_NAV_ENTRY_COMMITTED, 106 content::Source<NavigationController>(&web_contents_->GetController())); 107 108 MaybeSetPrepopulateText(); 109 110 if (find_tab_helper && find_tab_helper->find_ui_active()) { 111 // A tab with a visible find bar just got selected and we need to show the 112 // find bar but without animation since it was already animated into its 113 // visible state. We also want to reset the window location so that 114 // we don't surprise the user by popping up to the left for no apparent 115 // reason. 116 find_bar_->Show(false); 117 } 118 119 UpdateFindBarForCurrentResult(); 120 find_bar_->UpdateFindBarForChangedWebContents(); 121 } 122 123 //////////////////////////////////////////////////////////////////////////////// 124 // FindBarHost, content::NotificationObserver implementation: 125 126 void FindBarController::Observe(int type, 127 const content::NotificationSource& source, 128 const content::NotificationDetails& details) { 129 FindTabHelper* find_tab_helper = 130 FindTabHelper::FromWebContents(web_contents_); 131 if (type == chrome::NOTIFICATION_FIND_RESULT_AVAILABLE) { 132 // Don't update for notifications from WebContentses other than the one we 133 // are actively tracking. 134 if (content::Source<WebContents>(source).ptr() == web_contents_) { 135 UpdateFindBarForCurrentResult(); 136 if (find_tab_helper->find_result().final_update() && 137 find_tab_helper->find_result().number_of_matches() == 0) { 138 const string16& last_search = find_tab_helper->previous_find_text(); 139 const string16& current_search = find_tab_helper->find_text(); 140 if (last_search.find(current_search) != 0) 141 find_bar_->AudibleAlert(); 142 } 143 } 144 } else if (type == content::NOTIFICATION_NAV_ENTRY_COMMITTED) { 145 NavigationController* source_controller = 146 content::Source<NavigationController>(source).ptr(); 147 if (source_controller == &web_contents_->GetController()) { 148 content::LoadCommittedDetails* commit_details = 149 content::Details<content::LoadCommittedDetails>(details).ptr(); 150 content::PageTransition transition_type = 151 commit_details->entry->GetTransitionType(); 152 // We hide the FindInPage window when the user navigates away, except on 153 // reload (and when clicking on anchors within web pages). 154 if (find_bar_->IsFindBarVisible()) { 155 if (content::PageTransitionStripQualifier(transition_type) != 156 content::PAGE_TRANSITION_RELOAD) { 157 // This is a new navigation (not reload), but we still don't want the 158 // Find box to disappear if the navigation is just to a fragment 159 // within the page. 160 if (commit_details->is_navigation_to_different_page()) 161 EndFindSession(kKeepSelectionOnPage, kClearResultsInFindBox); 162 } else { 163 // On Reload we want to make sure FindNext is converted to a full Find 164 // to make sure highlights for inactive matches are repainted. 165 find_tab_helper->set_find_op_aborted(true); 166 } 167 } 168 } 169 } 170 } 171 172 // static 173 gfx::Rect FindBarController::GetLocationForFindbarView( 174 gfx::Rect view_location, 175 const gfx::Rect& dialog_bounds, 176 const gfx::Rect& avoid_overlapping_rect) { 177 if (base::i18n::IsRTL()) { 178 int boundary = dialog_bounds.width() - view_location.width(); 179 view_location.set_x(std::min(view_location.x(), boundary)); 180 } else { 181 view_location.set_x(std::max(view_location.x(), dialog_bounds.x())); 182 } 183 184 gfx::Rect new_pos = view_location; 185 186 // If the selection rectangle intersects the current position on screen then 187 // we try to move our dialog to the left (right for RTL) of the selection 188 // rectangle. 189 if (!avoid_overlapping_rect.IsEmpty() && 190 avoid_overlapping_rect.Intersects(new_pos)) { 191 if (base::i18n::IsRTL()) { 192 new_pos.set_x(avoid_overlapping_rect.x() + 193 avoid_overlapping_rect.width() + 194 (2 * kMinFindWndDistanceFromSelection)); 195 196 // If we moved it off-screen to the right, we won't move it at all. 197 if (new_pos.x() + new_pos.width() > dialog_bounds.width()) 198 new_pos = view_location; // Reset. 199 } else { 200 new_pos.set_x(avoid_overlapping_rect.x() - new_pos.width() - 201 kMinFindWndDistanceFromSelection); 202 203 // If we moved it off-screen to the left, we won't move it at all. 204 if (new_pos.x() < 0) 205 new_pos = view_location; // Reset. 206 } 207 } 208 209 return new_pos; 210 } 211 212 void FindBarController::UpdateFindBarForCurrentResult() { 213 FindTabHelper* find_tab_helper = 214 FindTabHelper::FromWebContents(web_contents_); 215 const FindNotificationDetails& find_result = find_tab_helper->find_result(); 216 217 // Avoid bug 894389: When a new search starts (and finds something) it reports 218 // an interim match count result of 1 before the scoping effort starts. This 219 // is to provide feedback as early as possible that we will find something. 220 // As you add letters to the search term, this creates a flashing effect when 221 // we briefly show "1 of 1" matches because there is a slight delay until 222 // the scoping effort starts updating the match count. We avoid this flash by 223 // ignoring interim results of 1 if we already have a positive number. 224 if (find_result.number_of_matches() > -1) { 225 if (last_reported_matchcount_ > 0 && 226 find_result.number_of_matches() == 1 && 227 !find_result.final_update()) 228 return; // Don't let interim result override match count. 229 last_reported_matchcount_ = find_result.number_of_matches(); 230 } 231 232 find_bar_->UpdateUIForFindResult(find_result, find_tab_helper->find_text()); 233 } 234 235 void FindBarController::MaybeSetPrepopulateText() { 236 // Having a per-tab find_string is not compatible with a global find 237 // pasteboard, so we always have the same find text in all find bars. This is 238 // done through the find pasteboard mechanism, so don't set the text here. 239 if (find_bar_->HasGlobalFindPasteboard()) 240 return; 241 242 // Find out what we should show in the find text box. Usually, this will be 243 // the last search in this tab, but if no search has been issued in this tab 244 // we use the last search string (from any tab). 245 FindTabHelper* find_tab_helper = 246 FindTabHelper::FromWebContents(web_contents_); 247 string16 find_string = find_tab_helper->find_text(); 248 if (find_string.empty()) 249 find_string = find_tab_helper->previous_find_text(); 250 if (find_string.empty()) { 251 Profile* profile = 252 Profile::FromBrowserContext(web_contents_->GetBrowserContext()); 253 find_string = FindBarStateFactory::GetLastPrepopulateText(profile); 254 } 255 256 // Update the find bar with existing results and search text, regardless of 257 // whether or not the find bar is visible, so that if it's subsequently 258 // shown it is showing the right state for this tab. We update the find text 259 // _first_ since the FindBarView checks its emptiness to see if it should 260 // clear the result count display when there's nothing in the box. 261 find_bar_->SetFindText(find_string); 262 } 263