OLD | NEW |
| (Empty) |
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. | |
4 | |
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_ | |
7 | |
8 #include <map> | |
9 #include <vector> | |
10 | |
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" | |
16 | |
17 namespace extensions { | |
18 class WebViewInternalFindFunction; | |
19 class WebViewGuest; | |
20 | |
21 // Helper class for find requests and replies for the web_view_internal find | |
22 // API. | |
23 class WebViewFindHelper { | |
24 public: | |
25 explicit WebViewFindHelper(WebViewGuest* webview_guest); | |
26 ~WebViewFindHelper(); | |
27 | |
28 // Cancels all find requests in progress and calls their callback functions. | |
29 void CancelAllFindSessions(); | |
30 | |
31 // Dispatches the |findupdate| event. | |
32 void DispatchFindUpdateEvent(bool canceled, bool final_update); | |
33 | |
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); | |
37 | |
38 // Helper function for WebViewGuest::Find(). | |
39 void Find( | |
40 content::WebContents* guest_web_contents, | |
41 const base::string16& search_text, | |
42 const blink::WebFindOptions& options, | |
43 scoped_refptr<WebViewInternalFindFunction> find_function); | |
44 | |
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, | |
50 bool final_update); | |
51 | |
52 private: | |
53 // A wrapper to store find results. | |
54 class FindResults { | |
55 public: | |
56 FindResults(); | |
57 ~FindResults(); | |
58 | |
59 // Aggregate the find results. | |
60 void AggregateResults(int number_of_matches, | |
61 const gfx::Rect& selection_rect, | |
62 int active_match_ordinal, | |
63 bool final_update); | |
64 | |
65 // Stores find results into a DictionaryValue. | |
66 void PrepareResults(base::DictionaryValue* results); | |
67 | |
68 private: | |
69 int number_of_matches_; | |
70 int active_match_ordinal_; | |
71 gfx::Rect selection_rect_; | |
72 | |
73 friend void WebViewFindHelper::EndFindSession(int session_request_id, | |
74 bool canceled); | |
75 | |
76 DISALLOW_COPY_AND_ASSIGN(FindResults); | |
77 }; | |
78 | |
79 // Stores and processes the results for the |findupdate| event. | |
80 class FindUpdateEvent { | |
81 public: | |
82 explicit FindUpdateEvent(const base::string16& search_text); | |
83 ~FindUpdateEvent(); | |
84 | |
85 // Aggregate the find results. | |
86 void AggregateResults(int number_of_matches, | |
87 const gfx::Rect& selection_rect, | |
88 int active_match_ordinal, | |
89 bool final_update); | |
90 | |
91 // Stores find results and other event info into a DictionaryValue. | |
92 void PrepareResults(base::DictionaryValue* results); | |
93 | |
94 private: | |
95 const base::string16 search_text_; | |
96 FindResults find_results_; | |
97 | |
98 DISALLOW_COPY_AND_ASSIGN(FindUpdateEvent); | |
99 }; | |
100 | |
101 // Handles all information about a find request and its results. | |
102 class FindInfo { | |
103 public: | |
104 FindInfo( | |
105 int request_id, | |
106 const base::string16& search_text, | |
107 const blink::WebFindOptions& options, | |
108 scoped_refptr<WebViewInternalFindFunction> find_function); | |
109 ~FindInfo(); | |
110 | |
111 // Add another request to |find_next_requests_|. | |
112 void AddFindNextRequest(const base::WeakPtr<FindInfo>& request) { | |
113 find_next_requests_.push_back(request); | |
114 } | |
115 | |
116 // Aggregate the find results. | |
117 void AggregateResults(int number_of_matches, | |
118 const gfx::Rect& selection_rect, | |
119 int active_match_ordinal, | |
120 bool final_update); | |
121 | |
122 base::WeakPtr<FindInfo> AsWeakPtr(); | |
123 | |
124 blink::WebFindOptions* options() { | |
125 return &options_; | |
126 } | |
127 | |
128 bool replied() { | |
129 return replied_; | |
130 } | |
131 | |
132 int request_id() { | |
133 return request_id_; | |
134 } | |
135 | |
136 const base::string16& search_text() { | |
137 return search_text_; | |
138 } | |
139 | |
140 // Calls the callback function within |find_function_| with the find results | |
141 // from within |find_results_|. | |
142 void SendResponse(bool canceled); | |
143 | |
144 private: | |
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_; | |
150 | |
151 // A find reply has been received for this find request. | |
152 bool replied_; | |
153 | |
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_; | |
157 | |
158 // Weak pointer used to access the find info of fin. | |
159 base::WeakPtrFactory<FindInfo> weak_ptr_factory_; | |
160 | |
161 friend void WebViewFindHelper::EndFindSession(int session_request_id, | |
162 bool canceled); | |
163 | |
164 DISALLOW_COPY_AND_ASSIGN(FindInfo); | |
165 }; | |
166 | |
167 // Pointer to the webview that is being helped. | |
168 WebViewGuest* const webview_guest_; | |
169 | |
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_; | |
173 | |
174 // Stores aggregated find results and other info for the |findupdate| event. | |
175 scoped_ptr<FindUpdateEvent> find_update_event_; | |
176 | |
177 // Pointer to the first request of the current find session. | |
178 linked_ptr<FindInfo> current_find_session_; | |
179 | |
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_; | |
184 | |
185 DISALLOW_COPY_AND_ASSIGN(WebViewFindHelper); | |
186 }; | |
187 | |
188 } // namespace extensions | |
189 | |
190 #endif // EXTENSIONS_BROWSER_GUEST_VIEW_WEB_VIEW_WEB_VIEW_FIND_HELPER_H_ | |
OLD | NEW |