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_TAB_HELPER_H_
6#define CHROME_BROWSER_UI_FIND_BAR_FIND_TAB_HELPER_H_
7#pragma once
8
9#include "chrome/browser/ui/find_bar/find_bar_controller.h"
10#include "chrome/browser/ui/find_bar/find_notification_details.h"
11#include "content/browser/tab_contents/tab_contents_observer.h"
12
13// Per-tab find manager. Handles dealing with the life cycle of find sessions.
14class FindTabHelper : public TabContentsObserver {
15 public:
16  explicit FindTabHelper(TabContents* tab_contents);
17  virtual ~FindTabHelper();
18
19  // Starts the Find operation by calling StartFinding on the Tab. This function
20  // can be called from the outside as a result of hot-keys, so it uses the
21  // last remembered search string as specified with set_find_string(). This
22  // function does not block while a search is in progress. The controller will
23  // receive the results through the notification mechanism. See Observe(...)
24  // for details.
25  void StartFinding(string16 search_string,
26                    bool forward_direction,
27                    bool case_sensitive);
28
29  // Stops the current Find operation.
30  void StopFinding(FindBarController::SelectionAction selection_action);
31
32  // Accessors/Setters for find_ui_active_.
33  bool find_ui_active() const { return find_ui_active_; }
34  void set_find_ui_active(bool find_ui_active) {
35      find_ui_active_ = find_ui_active;
36  }
37
38  // Setter for find_op_aborted_.
39  void set_find_op_aborted(bool find_op_aborted) {
40    find_op_aborted_ = find_op_aborted;
41  }
42
43  // Used _only_ by testing to get or set the current request ID.
44  int current_find_request_id() { return current_find_request_id_; }
45  void set_current_find_request_id(int current_find_request_id) {
46    current_find_request_id_ = current_find_request_id;
47  }
48
49  // Accessor for find_text_. Used to determine if this TabContents has any
50  // active searches.
51  string16 find_text() const { return find_text_; }
52
53  // Accessor for the previous search we issued.
54  string16 previous_find_text() const { return previous_find_text_; }
55
56  // Accessor for find_result_.
57  const FindNotificationDetails& find_result() const {
58    return last_search_result_;
59  }
60
61  // TabContentsObserver overrides.
62  virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
63
64 private:
65  void OnFindReply(int request_id,
66                   int number_of_matches,
67                   const gfx::Rect& selection_rect,
68                   int active_match_ordinal,
69                   bool final_update);
70
71  // Each time a search request comes in we assign it an id before passing it
72  // over the IPC so that when the results come in we can evaluate whether we
73  // still care about the results of the search (in some cases we don't because
74  // the user has issued a new search).
75  static int find_request_id_counter_;
76
77  // True if the Find UI is active for this Tab.
78  bool find_ui_active_;
79
80  // True if a Find operation was aborted. This can happen if the Find box is
81  // closed or if the search term inside the Find box is erased while a search
82  // is in progress. This can also be set if a page has been reloaded, and will
83  // on FindNext result in a full Find operation so that the highlighting for
84  // inactive matches can be repainted.
85  bool find_op_aborted_;
86
87  // This variable keeps track of what the most recent request id is.
88  int current_find_request_id_;
89
90  // The current string we are/just finished searching for. This is used to
91  // figure out if this is a Find or a FindNext operation (FindNext should not
92  // increase the request id).
93  string16 find_text_;
94
95  // The string we searched for before |find_text_|.
96  string16 previous_find_text_;
97
98  // Whether the last search was case sensitive or not.
99  bool last_search_case_sensitive_;
100
101  // The last find result. This object contains details about the number of
102  // matches, the find selection rectangle, etc. The UI can access this
103  // information to build its presentation.
104  FindNotificationDetails last_search_result_;
105
106  DISALLOW_COPY_AND_ASSIGN(FindTabHelper);
107};
108
109#endif  // CHROME_BROWSER_UI_FIND_BAR_FIND_TAB_HELPER_H_
110