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
3// found in the LICENSE file.
4
5#ifndef CHROME_BROWSER_UI_FIND_BAR_FIND_BAR_CONTROLLER_H_
6#define CHROME_BROWSER_UI_FIND_BAR_FIND_BAR_CONTROLLER_H_
7#pragma once
8
9#include "base/basictypes.h"
10#include "base/memory/scoped_ptr.h"
11#include "content/common/notification_observer.h"
12#include "content/common/notification_registrar.h"
13
14namespace gfx {
15class Rect;
16}
17
18class FindBar;
19class TabContentsWrapper;
20
21class FindBarController : public NotificationObserver {
22 public:
23  // An enum listing the possible actions to take on a find-in-page selection.
24  enum SelectionAction {
25    kKeepSelection,  // Translate the find selection into a normal selection.
26    kClearSelection,  // Clear the find selection.
27    kActivateSelection  // Focus and click the selected node (for links).
28  };
29
30  // FindBar takes ownership of |find_bar_view|.
31  explicit FindBarController(FindBar* find_bar);
32
33  virtual ~FindBarController();
34
35  // Shows the find bar. Any previous search string will again be visible.
36  void Show();
37
38  // Ends the current session.
39  void EndFindSession(SelectionAction action);
40
41  // Accessor for the attached TabContentsWrapper.
42  TabContentsWrapper* tab_contents() const { return tab_contents_; }
43
44  // Changes the TabContents that this FindBar is attached to. This occurs when
45  // the user switches tabs in the Browser window. |contents| can be NULL.
46  void ChangeTabContents(TabContentsWrapper* contents);
47
48  // Overridden from NotificationObserver:
49  virtual void Observe(NotificationType type,
50                       const NotificationSource& source,
51                       const NotificationDetails& details);
52
53  FindBar* find_bar() const { return find_bar_.get(); }
54
55  // Reposition |view_location| such that it avoids |avoid_overlapping_rect|,
56  // and return the new location.
57  static gfx::Rect GetLocationForFindbarView(
58      gfx::Rect view_location,
59      const gfx::Rect& dialog_bounds,
60      const gfx::Rect& avoid_overlapping_rect);
61
62 private:
63  // Sents an update to the find bar with the tab contents' current result. The
64  // tab_contents_ must be non-NULL before this call. Theis handles
65  // de-flickering in addition to just calling the update function.
66  void UpdateFindBarForCurrentResult();
67
68  // For Windows and Linux this function sets the prepopulate text for the
69  // Find text box. The propopulate value is the last value the user searched
70  // for in the current tab, or (if blank) the last value searched for in any
71  // tab. Mac has a global value for search, so this function does nothing on
72  // Mac.
73  void MaybeSetPrepopulateText();
74
75  NotificationRegistrar registrar_;
76
77  scoped_ptr<FindBar> find_bar_;
78
79  // The TabContentsWrapper we are currently associated with.  Can be NULL.
80  TabContentsWrapper* tab_contents_;
81
82  // The last match count we reported to the user. This is used by
83  // UpdateFindBarForCurrentResult to avoid flickering.
84  int last_reported_matchcount_;
85
86  DISALLOW_COPY_AND_ASSIGN(FindBarController);
87};
88
89#endif  // CHROME_BROWSER_UI_FIND_BAR_FIND_BAR_CONTROLLER_H_
90