summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/find_bar/find_bar_controller.h
blob: c73809f59c6b136bf8ca21746fb46f0277a7feea (plain)
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
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// 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 "base/basictypes.h"
#include "base/memory/scoped_ptr.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"

class FindBar;

namespace content {
class WebContents;
}

namespace gfx {
class Rect;
}

class FindBarController : public content::NotificationObserver {
 public:
  // An enum listing the possible actions to take on a find-in-page selection
  // in the page when ending the find session.
  enum SelectionAction {
    kKeepSelectionOnPage,     // Translate the find selection into a normal
                              // selection.
    kClearSelectionOnPage,    // Clear the find selection.
    kActivateSelectionOnPage  // Focus and click the selected node (for links).
  };

  // An enum listing the possible actions to take on a find-in-page results in
  // the Find box when ending the find session.
  enum ResultAction {
    kClearResultsInFindBox,  // Clear search string, ordinal and match count.
    kKeepResultsInFindBox,   // Leave the results untouched.
  };

  // FindBar takes ownership of |find_bar_view|.
  explicit FindBarController(FindBar* find_bar);

  virtual ~FindBarController();

  // Shows the find bar. Any previous search string will again be visible.
  void Show();

  // Ends the current session. |selection_action| specifies what to do with the
  // selection on the page created by the find operation. |results_action|
  // specifies what to do with the contents of the Find box (after ending).
  void EndFindSession(SelectionAction selection_action,
                      ResultAction results_action);

  // Accessor for the attached WebContents.
  content::WebContents* web_contents() const { return web_contents_; }

  // 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);

  // Overridden from content::NotificationObserver:
  virtual void Observe(int type,
                       const content::NotificationSource& source,
                       const content::NotificationDetails& details) OVERRIDE;

  FindBar* find_bar() const { return find_bar_.get(); }

  // Reposition |view_location| such that it avoids |avoid_overlapping_rect|,
  // and return the new location.
  static gfx::Rect GetLocationForFindbarView(
      gfx::Rect view_location,
      const gfx::Rect& dialog_bounds,
      const gfx::Rect& avoid_overlapping_rect);

 private:
  // Sents an update to the find bar with the tab contents' current result. The
  // web_contents_ must be non-NULL before this call. Theis 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();

  content::NotificationRegistrar registrar_;

  scoped_ptr<FindBar> find_bar_;

  // The WebContents we are currently associated with.  Can be NULL.
  content::WebContents* web_contents_;

  // The last match count we reported to the user. This is used by
  // UpdateFindBarForCurrentResult to avoid flickering.
  int last_reported_matchcount_;

  DISALLOW_COPY_AND_ASSIGN(FindBarController);
};

#endif  // CHROME_BROWSER_UI_FIND_BAR_FIND_BAR_CONTROLLER_H_