- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / find_bar / find_tab_helper.cc
1 // Copyright (c) 2012 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 #include "chrome/browser/ui/find_bar/find_tab_helper.h"
6
7 #include <vector>
8
9 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/ui/find_bar/find_bar_state.h"
12 #include "chrome/browser/ui/find_bar/find_bar_state_factory.h"
13 #include "content/public/browser/notification_service.h"
14 #include "content/public/browser/render_view_host.h"
15 #include "content/public/browser/web_contents.h"
16 #include "content/public/common/stop_find_action.h"
17 #include "third_party/WebKit/public/web/WebFindOptions.h"
18 #include "ui/gfx/rect_f.h"
19
20 using WebKit::WebFindOptions;
21 using content::WebContents;
22
23 DEFINE_WEB_CONTENTS_USER_DATA_KEY(FindTabHelper);
24
25 // static
26 int FindTabHelper::find_request_id_counter_ = -1;
27
28 FindTabHelper::FindTabHelper(WebContents* web_contents)
29     : content::WebContentsObserver(web_contents),
30       find_ui_active_(false),
31       find_op_aborted_(false),
32       current_find_request_id_(find_request_id_counter_++),
33       last_search_case_sensitive_(false),
34       last_search_result_() {
35 }
36
37 FindTabHelper::~FindTabHelper() {
38 }
39
40 void FindTabHelper::StartFinding(string16 search_string,
41                                  bool forward_direction,
42                                  bool case_sensitive) {
43   // If search_string is empty, it means FindNext was pressed with a keyboard
44   // shortcut so unless we have something to search for we return early.
45   if (search_string.empty() && find_text_.empty()) {
46     Profile* profile =
47         Profile::FromBrowserContext(web_contents()->GetBrowserContext());
48     string16 last_search_prepopulate_text =
49         FindBarStateFactory::GetLastPrepopulateText(profile);
50
51     // Try the last thing we searched for on this tab, then the last thing
52     // searched for on any tab.
53     if (!previous_find_text_.empty())
54       search_string = previous_find_text_;
55     else if (!last_search_prepopulate_text.empty())
56       search_string = last_search_prepopulate_text;
57     else
58       return;
59   }
60
61   // Keep track of the previous search.
62   previous_find_text_ = find_text_;
63
64   // This is a FindNext operation if we are searching for the same text again,
65   // or if the passed in search text is empty (FindNext keyboard shortcut). The
66   // exception to this is if the Find was aborted (then we don't want FindNext
67   // because the highlighting has been cleared and we need it to reappear). We
68   // therefore treat FindNext after an aborted Find operation as a full fledged
69   // Find.
70   bool find_next = (find_text_ == search_string || search_string.empty()) &&
71                    (last_search_case_sensitive_ == case_sensitive) &&
72                    !find_op_aborted_;
73   if (!find_next)
74     current_find_request_id_ = find_request_id_counter_++;
75
76   if (!search_string.empty())
77     find_text_ = search_string;
78   last_search_case_sensitive_ = case_sensitive;
79
80   find_op_aborted_ = false;
81
82   // Keep track of what the last search was across the tabs.
83   Profile* profile =
84       Profile::FromBrowserContext(web_contents()->GetBrowserContext());
85   FindBarState* find_bar_state = FindBarStateFactory::GetForProfile(profile);
86   find_bar_state->set_last_prepopulate_text(find_text_);
87
88   WebFindOptions options;
89   options.forward = forward_direction;
90   options.matchCase = case_sensitive;
91   options.findNext = find_next;
92   web_contents()->GetRenderViewHost()->Find(current_find_request_id_,
93                                             find_text_, options);
94 }
95
96 void FindTabHelper::StopFinding(
97     FindBarController::SelectionAction selection_action) {
98   if (selection_action == FindBarController::kClearSelectionOnPage) {
99     // kClearSelection means the find string has been cleared by the user, but
100     // the UI has not been dismissed. In that case we want to clear the
101     // previously remembered search (http://crbug.com/42639).
102     previous_find_text_ = string16();
103   } else {
104     find_ui_active_ = false;
105     if (!find_text_.empty())
106       previous_find_text_ = find_text_;
107   }
108   find_text_.clear();
109   find_op_aborted_ = true;
110   last_search_result_ = FindNotificationDetails();
111
112   content::StopFindAction action;
113   switch (selection_action) {
114     case FindBarController::kClearSelectionOnPage:
115       action = content::STOP_FIND_ACTION_CLEAR_SELECTION;
116       break;
117     case FindBarController::kKeepSelectionOnPage:
118       action = content::STOP_FIND_ACTION_KEEP_SELECTION;
119       break;
120     case FindBarController::kActivateSelectionOnPage:
121       action = content::STOP_FIND_ACTION_ACTIVATE_SELECTION;
122       break;
123     default:
124       NOTREACHED();
125       action = content::STOP_FIND_ACTION_KEEP_SELECTION;
126   }
127   web_contents()->GetRenderViewHost()->StopFinding(action);
128 }
129
130 #if defined(OS_ANDROID)
131 void FindTabHelper::ActivateNearestFindResult(float x, float y) {
132   if (!find_op_aborted_ && !find_text_.empty()) {
133     web_contents()->GetRenderViewHost()->ActivateNearestFindResult(
134         current_find_request_id_, x, y);
135   }
136 }
137
138 void FindTabHelper::RequestFindMatchRects(int current_version) {
139   if (!find_op_aborted_ && !find_text_.empty())
140     web_contents()->GetRenderViewHost()->RequestFindMatchRects(current_version);
141 }
142 #endif
143
144 void FindTabHelper::HandleFindReply(int request_id,
145                                     int number_of_matches,
146                                     const gfx::Rect& selection_rect,
147                                     int active_match_ordinal,
148                                     bool final_update) {
149   // Ignore responses for requests that have been aborted.
150   // Ignore responses for requests other than the one we have most recently
151   // issued. That way we won't act on stale results when the user has
152   // already typed in another query.
153   if (!find_op_aborted_ && request_id == current_find_request_id_) {
154     if (number_of_matches == -1)
155       number_of_matches = last_search_result_.number_of_matches();
156     if (active_match_ordinal == -1)
157       active_match_ordinal = last_search_result_.active_match_ordinal();
158
159     gfx::Rect selection = selection_rect;
160     if (final_update && active_match_ordinal == 0)
161       selection = gfx::Rect();
162     else if (selection_rect.IsEmpty())
163       selection = last_search_result_.selection_rect();
164
165     // Notify the UI, automation and any other observers that a find result was
166     // found.
167     last_search_result_ = FindNotificationDetails(
168         request_id, number_of_matches, selection, active_match_ordinal,
169         final_update);
170     content::NotificationService::current()->Notify(
171         chrome::NOTIFICATION_FIND_RESULT_AVAILABLE,
172         content::Source<WebContents>(web_contents()),
173         content::Details<FindNotificationDetails>(&last_search_result_));
174   }
175 }