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 EXTENSIONS_BROWSER_GUEST_VIEW_WEB_VIEW_WEB_VIEW_FIND_HELPER_H_
6 #define EXTENSIONS_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().
39 void Find(content::WebContents
* guest_web_contents
,
40 const base::string16
& search_text
,
41 const blink::WebFindOptions
& options
,
42 scoped_refptr
<WebViewInternalFindFunction
> find_function
);
44 // Helper function for WeViewGuest:FindReply().
45 void FindReply(int request_id
,
46 int number_of_matches
,
47 const gfx::Rect
& selection_rect
,
48 int active_match_ordinal
,
52 // A wrapper to store find results.
58 // Aggregate the find results.
59 void AggregateResults(int number_of_matches
,
60 const gfx::Rect
& selection_rect
,
61 int active_match_ordinal
,
64 // Stores find results into a DictionaryValue.
65 void PrepareResults(base::DictionaryValue
* results
);
68 int number_of_matches_
;
69 int active_match_ordinal_
;
70 gfx::Rect selection_rect_
;
72 friend void WebViewFindHelper::EndFindSession(int session_request_id
,
75 DISALLOW_COPY_AND_ASSIGN(FindResults
);
78 // Stores and processes the results for the |findupdate| event.
79 class FindUpdateEvent
{
81 explicit FindUpdateEvent(const base::string16
& search_text
);
84 // Aggregate the find results.
85 void AggregateResults(int number_of_matches
,
86 const gfx::Rect
& selection_rect
,
87 int active_match_ordinal
,
90 // Stores find results and other event info into a DictionaryValue.
91 void PrepareResults(base::DictionaryValue
* results
);
94 const base::string16 search_text_
;
95 FindResults find_results_
;
97 DISALLOW_COPY_AND_ASSIGN(FindUpdateEvent
);
100 // Handles all information about a find request and its results.
103 FindInfo(int request_id
,
104 const base::string16
& search_text
,
105 const blink::WebFindOptions
& options
,
106 scoped_refptr
<WebViewInternalFindFunction
> find_function
);
109 // Add another request to |find_next_requests_|.
110 void AddFindNextRequest(const base::WeakPtr
<FindInfo
>& request
) {
111 find_next_requests_
.push_back(request
);
114 // Aggregate the find results.
115 void AggregateResults(int number_of_matches
,
116 const gfx::Rect
& selection_rect
,
117 int active_match_ordinal
,
120 base::WeakPtr
<FindInfo
> AsWeakPtr();
122 blink::WebFindOptions
* options() {
134 const base::string16
& search_text() {
138 // Calls the callback function within |find_function_| with the find results
139 // from within |find_results_|.
140 void SendResponse(bool canceled
);
143 const int request_id_
;
144 const base::string16 search_text_
;
145 blink::WebFindOptions options_
;
146 scoped_refptr
<WebViewInternalFindFunction
> find_function_
;
147 FindResults find_results_
;
149 // A find reply has been received for this find request.
152 // Stores pointers to all the find next requests if this is the first
153 // request of a find session.
154 std::vector
<base::WeakPtr
<FindInfo
> > find_next_requests_
;
156 friend void WebViewFindHelper::EndFindSession(int session_request_id
,
159 base::WeakPtrFactory
<FindInfo
> weak_ptr_factory_
;
161 DISALLOW_COPY_AND_ASSIGN(FindInfo
);
164 // Pointer to the webview that is being helped.
165 WebViewGuest
* const webview_guest_
;
167 // A counter to generate a unique request id for a find request.
168 // We only need the ids to be unique for a given WebViewGuest.
169 int current_find_request_id_
;
171 // Stores aggregated find results and other info for the |findupdate| event.
172 scoped_ptr
<FindUpdateEvent
> find_update_event_
;
174 // Pointer to the first request of the current find session.
175 linked_ptr
<FindInfo
> current_find_session_
;
177 // Stores each find request's information by request_id so that its callback
178 // function can be called when its find results are available.
179 typedef std::map
<int, linked_ptr
<FindInfo
> > FindInfoMap
;
180 FindInfoMap find_info_map_
;
182 DISALLOW_COPY_AND_ASSIGN(WebViewFindHelper
);
185 } // namespace extensions
187 #endif // EXTENSIONS_BROWSER_GUEST_VIEW_WEB_VIEW_WEB_VIEW_FIND_HELPER_H_