This source file includes following definitions.
- last_reported_matchcount_
- Show
- EndFindSession
- ChangeWebContents
- Observe
- GetLocationForFindbarView
- UpdateFindBarForCurrentResult
- MaybeSetPrepopulateText
#include "chrome/browser/ui/find_bar/find_bar_controller.h"
#include <algorithm>
#include "base/i18n/rtl.h"
#include "base/logging.h"
#include "build/build_config.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/find_bar/find_bar.h"
#include "chrome/browser/ui/find_bar/find_bar_state.h"
#include "chrome/browser/ui/find_bar/find_bar_state_factory.h"
#include "chrome/browser/ui/find_bar/find_tab_helper.h"
#include "content/public/browser/navigation_details.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_source.h"
#include "content/public/browser/web_contents.h"
#include "ui/gfx/rect.h"
using content::NavigationController;
using content::WebContents;
static const int kMinFindWndDistanceFromSelection = 5;
FindBarController::FindBarController(FindBar* find_bar)
: find_bar_(find_bar),
web_contents_(NULL),
last_reported_matchcount_(0) {
}
FindBarController::~FindBarController() {
DCHECK(!web_contents_);
}
void FindBarController::Show() {
FindTabHelper* find_tab_helper =
FindTabHelper::FromWebContents(web_contents_);
if (!find_tab_helper->find_ui_active()) {
MaybeSetPrepopulateText();
find_tab_helper->set_find_ui_active(true);
find_bar_->Show(true);
}
find_bar_->SetFocusAndSelection();
}
void FindBarController::EndFindSession(SelectionAction selection_action,
ResultAction result_action) {
find_bar_->Hide(true);
if (web_contents_) {
FindTabHelper* find_tab_helper =
FindTabHelper::FromWebContents(web_contents_);
find_tab_helper->StopFinding(selection_action);
if (result_action == kClearResultsInFindBox)
find_bar_->ClearResults(find_tab_helper->find_result());
find_bar_->RestoreSavedFocus();
}
}
void FindBarController::ChangeWebContents(WebContents* contents) {
if (web_contents_) {
registrar_.RemoveAll();
find_bar_->StopAnimation();
FindTabHelper* find_tab_helper =
FindTabHelper::FromWebContents(web_contents_);
if (find_tab_helper)
find_tab_helper->set_selected_range(find_bar_->GetSelectedRange());
}
web_contents_ = contents;
FindTabHelper* find_tab_helper =
web_contents_ ? FindTabHelper::FromWebContents(web_contents_)
: NULL;
if (find_bar_->IsFindBarVisible() &&
(!find_tab_helper || !find_tab_helper->find_ui_active())) {
find_bar_->Hide(false);
}
if (!web_contents_)
return;
registrar_.Add(this,
chrome::NOTIFICATION_FIND_RESULT_AVAILABLE,
content::Source<WebContents>(web_contents_));
registrar_.Add(
this,
content::NOTIFICATION_NAV_ENTRY_COMMITTED,
content::Source<NavigationController>(&web_contents_->GetController()));
MaybeSetPrepopulateText();
if (find_tab_helper && find_tab_helper->find_ui_active()) {
find_bar_->Show(false);
}
UpdateFindBarForCurrentResult();
find_bar_->UpdateFindBarForChangedWebContents();
}
void FindBarController::Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
FindTabHelper* find_tab_helper =
FindTabHelper::FromWebContents(web_contents_);
if (type == chrome::NOTIFICATION_FIND_RESULT_AVAILABLE) {
if (content::Source<WebContents>(source).ptr() == web_contents_) {
UpdateFindBarForCurrentResult();
if (find_tab_helper->find_result().final_update() &&
find_tab_helper->find_result().number_of_matches() == 0) {
const base::string16& last_search =
find_tab_helper->previous_find_text();
const base::string16& current_search = find_tab_helper->find_text();
if (last_search.find(current_search) != 0)
find_bar_->AudibleAlert();
}
}
} else if (type == content::NOTIFICATION_NAV_ENTRY_COMMITTED) {
NavigationController* source_controller =
content::Source<NavigationController>(source).ptr();
if (source_controller == &web_contents_->GetController()) {
content::LoadCommittedDetails* commit_details =
content::Details<content::LoadCommittedDetails>(details).ptr();
content::PageTransition transition_type =
commit_details->entry->GetTransitionType();
if (find_bar_->IsFindBarVisible()) {
if (content::PageTransitionStripQualifier(transition_type) !=
content::PAGE_TRANSITION_RELOAD) {
if (commit_details->is_navigation_to_different_page())
EndFindSession(kKeepSelectionOnPage, kClearResultsInFindBox);
} else {
find_tab_helper->set_find_op_aborted(true);
}
}
}
}
}
gfx::Rect FindBarController::GetLocationForFindbarView(
gfx::Rect view_location,
const gfx::Rect& dialog_bounds,
const gfx::Rect& avoid_overlapping_rect) {
if (base::i18n::IsRTL()) {
int boundary = dialog_bounds.width() - view_location.width();
view_location.set_x(std::min(view_location.x(), boundary));
} else {
view_location.set_x(std::max(view_location.x(), dialog_bounds.x()));
}
gfx::Rect new_pos = view_location;
if (!avoid_overlapping_rect.IsEmpty() &&
avoid_overlapping_rect.Intersects(new_pos)) {
if (base::i18n::IsRTL()) {
new_pos.set_x(avoid_overlapping_rect.x() +
avoid_overlapping_rect.width() +
(2 * kMinFindWndDistanceFromSelection));
if (new_pos.x() + new_pos.width() > dialog_bounds.width())
new_pos = view_location;
} else {
new_pos.set_x(avoid_overlapping_rect.x() - new_pos.width() -
kMinFindWndDistanceFromSelection);
if (new_pos.x() < 0)
new_pos = view_location;
}
}
return new_pos;
}
void FindBarController::UpdateFindBarForCurrentResult() {
FindTabHelper* find_tab_helper =
FindTabHelper::FromWebContents(web_contents_);
const FindNotificationDetails& find_result = find_tab_helper->find_result();
if (find_result.number_of_matches() > -1) {
if (last_reported_matchcount_ > 0 &&
find_result.number_of_matches() == 1 &&
!find_result.final_update())
return;
last_reported_matchcount_ = find_result.number_of_matches();
}
find_bar_->UpdateUIForFindResult(find_result, find_tab_helper->find_text());
}
void FindBarController::MaybeSetPrepopulateText() {
if (find_bar_->HasGlobalFindPasteboard())
return;
FindTabHelper* find_tab_helper =
FindTabHelper::FromWebContents(web_contents_);
base::string16 find_string = find_tab_helper->find_text();
if (find_string.empty())
find_string = find_tab_helper->previous_find_text();
if (find_string.empty()) {
Profile* profile =
Profile::FromBrowserContext(web_contents_->GetBrowserContext());
find_string = FindBarStateFactory::GetLastPrepopulateText(profile);
}
find_bar_->SetFindTextAndSelectedRange(find_string,
find_tab_helper->selected_range());
}