1 // Copyright 2014 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.
5 #ifndef CHROME_BROWSER_GUEST_VIEW_WEB_VIEW_WEB_VIEW_FIND_HELPER_H_
6 #define CHROME_BROWSER_GUEST_VIEW_WEB_VIEW_WEB_VIEW_FIND_HELPER_H_
11 #include "base/memory/weak_ptr.h"
12 #include "base/values.h"
13 #include "content/public/browser/web_contents.h"
14 #include "third_party/WebKit/public/web/WebFindOptions.h"
15 #include "ui/gfx/geometry/rect.h"
17 namespace extensions {
18 class WebViewInternalFindFunction;
21 // Helper class for find requests and replies for the web_view_internal find
23 class WebViewFindHelper {
25 explicit WebViewFindHelper(WebViewGuest* webview_guest);
28 // Cancels all find requests in progress and calls their callback functions.
29 void CancelAllFindSessions();
31 // Dispatches the |findupdate| event.
32 void DispatchFindUpdateEvent(bool canceled, bool final_update);
34 // Ends the find session with id |session_request_id| and calls the
35 // appropriate callbacks.
36 void EndFindSession(int session_request_id, bool canceled);
38 // Helper function for WebViewGuest::Find().
40 content::WebContents* guest_web_contents,
41 const base::string16& search_text,
42 const blink::WebFindOptions& options,
43 scoped_refptr<WebViewInternalFindFunction> find_function);
45 // Helper function for WeViewGuest:FindReply().
46 void FindReply(int request_id,
47 int number_of_matches,
48 const gfx::Rect& selection_rect,
49 int active_match_ordinal,
53 // A wrapper to store find results.
59 // Aggregate the find results.
60 void AggregateResults(int number_of_matches,
61 const gfx::Rect& selection_rect,
62 int active_match_ordinal,
65 // Stores find results into a DictionaryValue.
66 void PrepareResults(base::DictionaryValue* results);
69 int number_of_matches_;
70 int active_match_ordinal_;
71 gfx::Rect selection_rect_;
73 friend void WebViewFindHelper::EndFindSession(int session_request_id,
76 DISALLOW_COPY_AND_ASSIGN(FindResults);
79 // Stores and processes the results for the |findupdate| event.
80 class FindUpdateEvent {
82 explicit FindUpdateEvent(const base::string16& search_text);
85 // Aggregate the find results.
86 void AggregateResults(int number_of_matches,
87 const gfx::Rect& selection_rect,
88 int active_match_ordinal,
91 // Stores find results and other event info into a DictionaryValue.
92 void PrepareResults(base::DictionaryValue* results);
95 const base::string16 search_text_;
96 FindResults find_results_;
98 DISALLOW_COPY_AND_ASSIGN(FindUpdateEvent);
101 // Handles all information about a find request and its results.
106 const base::string16& search_text,
107 const blink::WebFindOptions& options,
108 scoped_refptr<WebViewInternalFindFunction> find_function);
111 // Add another request to |find_next_requests_|.
112 void AddFindNextRequest(const base::WeakPtr<FindInfo>& request) {
113 find_next_requests_.push_back(request);
116 // Aggregate the find results.
117 void AggregateResults(int number_of_matches,
118 const gfx::Rect& selection_rect,
119 int active_match_ordinal,
122 base::WeakPtr<FindInfo> AsWeakPtr();
124 blink::WebFindOptions* options() {
136 const base::string16& search_text() {
140 // Calls the callback function within |find_function_| with the find results
141 // from within |find_results_|.
142 void SendResponse(bool canceled);
145 const int request_id_;
146 const base::string16 search_text_;
147 blink::WebFindOptions options_;
148 scoped_refptr<WebViewInternalFindFunction> find_function_;
149 FindResults find_results_;
151 // A find reply has been received for this find request.
154 // Stores pointers to all the find next requests if this is the first
155 // request of a find session.
156 std::vector<base::WeakPtr<FindInfo> > find_next_requests_;
158 // Weak pointer used to access the find info of fin.
159 base::WeakPtrFactory<FindInfo> weak_ptr_factory_;
161 friend void WebViewFindHelper::EndFindSession(int session_request_id,
164 DISALLOW_COPY_AND_ASSIGN(FindInfo);
167 // Pointer to the webview that is being helped.
168 WebViewGuest* const webview_guest_;
170 // A counter to generate a unique request id for a find request.
171 // We only need the ids to be unique for a given WebViewGuest.
172 int current_find_request_id_;
174 // Stores aggregated find results and other info for the |findupdate| event.
175 scoped_ptr<FindUpdateEvent> find_update_event_;
177 // Pointer to the first request of the current find session.
178 linked_ptr<FindInfo> current_find_session_;
180 // Stores each find request's information by request_id so that its callback
181 // function can be called when its find results are available.
182 typedef std::map<int, linked_ptr<FindInfo> > FindInfoMap;
183 FindInfoMap find_info_map_;
185 DISALLOW_COPY_AND_ASSIGN(WebViewFindHelper);
188 } // namespace extensions
190 #endif // CHROME_BROWSER_GUEST_VIEW_WEB_VIEW_WEB_VIEW_FIND_HELPER_H_