1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_UI_FIND_BAR_FIND_BAR_CONTROLLER_H_
#define CHROME_BROWSER_UI_FIND_BAR_FIND_BAR_CONTROLLER_H_
#include <memory>
#include <string_view>
#include "base/scoped_observation.h"
#include "components/find_in_page/find_result_observer.h"
#include "components/find_in_page/find_tab_helper.h"
#include "content/public/browser/web_contents_observer.h"
class FindBar;
class FindBarPlatformHelper;
namespace content {
class WebContents;
}
namespace gfx {
class Rect;
}
namespace find_in_page {
enum class SelectionAction;
enum class ResultAction;
} // namespace find_in_page
class FindBarController : public content::WebContentsObserver,
public find_in_page::FindResultObserver {
public:
explicit FindBarController(std::unique_ptr<FindBar> find_bar);
FindBarController(const FindBarController&) = delete;
FindBarController& operator=(const FindBarController&) = delete;
~FindBarController() override;
// Shows the find bar. Any previous search string will again be visible.
// The find operation will also be started depending on |find_next| and
// if there is currently a text selection. |find_next| means the user
// used a command to advance the search and |forward_direction| indicates if
// the find should be forward or backwards.
void Show(bool find_next = false, bool forward_direction = true);
// Ends the current session. |selection_action| specifies what to do with the
// selection on the page created by the find operation. |result_action|
// specifies what to do with the contents of the Find box (after ending).
void EndFindSession(find_in_page::SelectionAction selection_action,
find_in_page::ResultAction result_action);
// Changes the WebContents that this FindBar is attached to. This
// occurs when the user switches tabs in the Browser window. |contents| can be
// NULL.
void ChangeWebContents(content::WebContents* contents);
// content::WebContentsObserver:
void NavigationEntryCommitted(
const content::LoadCommittedDetails& load_details) override;
// find_in_page::FindResultObserver:
void OnFindEmptyText(content::WebContents* web_contents) override;
void OnFindResultAvailable(content::WebContents* web_contents) override;
void SetText(std::u16string text);
// Called when the find text is updated in response to a user action.
void OnUserChangedFindText(std::u16string_view text);
// Will be called only from `Browser`.
void HandleActiveTabChanged(content::WebContents* new_contents);
FindBar* find_bar() const { return find_bar_.get(); }
private:
// Sends an update to the find bar with the tab contents' current result. The
// `web_contents()` must be non-NULL before this call. This handles
// de-flickering in addition to just calling the update function.
void UpdateFindBarForCurrentResult();
// For Windows and Linux this function sets the prepopulate text for the
// Find text box. The propopulate value is the last value the user searched
// for in the current tab, or (if blank) the last value searched for in any
// tab. Mac has a global value for search, so this function does nothing on
// Mac.
void MaybeSetPrepopulateText();
// Gets the text that is selected in the current tab, or an empty string.
std::u16string GetSelectedText();
std::unique_ptr<FindBar> find_bar_;
std::unique_ptr<FindBarPlatformHelper> find_bar_platform_helper_;
// The last match count and ordinal we reported to the user. This is used
// by UpdateFindBarForCurrentResult to avoid flickering.
int last_reported_matchcount_ = 0;
int last_reported_ordinal_ = 0;
// If the user has changed the text in the find bar. Used to avoid
// replacing user-entered text with selection.
bool has_user_modified_text_ = false;
base::ScopedObservation<find_in_page::FindTabHelper,
find_in_page::FindResultObserver>
find_tab_observation_{this};
};
#endif // CHROME_BROWSER_UI_FIND_BAR_FIND_BAR_CONTROLLER_H_
|