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 CHROME_BROWSER_GUEST_VIEW_WEB_VIEW_WEB_VIEW_FIND_HELPER_H_ | |
6 #define CHROME_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 WebviewFindFunction; | |
19 } // namespace extensions | |
20 class WebViewGuest; | |
21 | |
22 // Helper class for find requests and replies for the webview find 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(content::WebContents* guest_web_contents, | |
40 const base::string16& search_text, | |
41 const blink::WebFindOptions& options, | |
42 scoped_refptr<extensions::WebviewFindFunction> find_function); | |
43 | |
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, | |
49 bool final_update); | |
50 | |
51 private: | |
52 // A wrapper to store find results. | |
53 class FindResults { | |
54 public: | |
55 FindResults(); | |
56 ~FindResults(); | |
57 | |
58 // Aggregate the find results. | |
59 void AggregateResults(int number_of_matches, | |
60 const gfx::Rect& selection_rect, | |
61 int active_match_ordinal, | |
62 bool final_update); | |
63 | |
64 // Stores find results into a DictionaryValue. | |
65 void PrepareResults(base::DictionaryValue* results); | |
66 | |
67 private: | |
68 int number_of_matches_; | |
69 int active_match_ordinal_; | |
70 gfx::Rect selection_rect_; | |
71 | |
72 friend void WebviewFindHelper::EndFindSession(int session_request_id, | |
73 bool canceled); | |
74 | |
75 DISALLOW_COPY_AND_ASSIGN(FindResults); | |
76 }; | |
77 | |
78 // Stores and processes the results for the |findupdate| event. | |
79 class FindUpdateEvent { | |
80 public: | |
81 explicit FindUpdateEvent(const base::string16& search_text); | |
82 ~FindUpdateEvent(); | |
83 | |
84 // Aggregate the find results. | |
85 void AggregateResults(int number_of_matches, | |
86 const gfx::Rect& selection_rect, | |
87 int active_match_ordinal, | |
88 bool final_update); | |
89 | |
90 // Stores find results and other event info into a DictionaryValue. | |
91 void PrepareResults(base::DictionaryValue* results); | |
92 | |
93 private: | |
94 const base::string16 search_text_; | |
95 FindResults find_results_; | |
96 | |
97 DISALLOW_COPY_AND_ASSIGN(FindUpdateEvent); | |
98 }; | |
99 | |
100 // Handles all information about a find request and its results. | |
101 class FindInfo { | |
102 public: | |
103 FindInfo(int request_id, | |
104 const base::string16& search_text, | |
105 const blink::WebFindOptions& options, | |
106 scoped_refptr<extensions::WebviewFindFunction> find_function); | |
107 ~FindInfo(); | |
108 | |
109 // Add another request to |find_next_requests_|. | |
110 void AddFindNextRequest(const base::WeakPtr<FindInfo>& request) { | |
111 find_next_requests_.push_back(request); | |
112 } | |
113 | |
114 // Aggregate the find results. | |
115 void AggregateResults(int number_of_matches, | |
116 const gfx::Rect& selection_rect, | |
117 int active_match_ordinal, | |
118 bool final_update); | |
119 | |
120 base::WeakPtr<FindInfo> AsWeakPtr(); | |
121 | |
122 blink::WebFindOptions* options() { | |
123 return &options_; | |
124 } | |
125 | |
126 bool replied() { | |
127 return replied_; | |
128 } | |
129 | |
130 int request_id() { | |
131 return request_id_; | |
132 } | |
133 | |
134 const base::string16& search_text() { | |
135 return search_text_; | |
136 } | |
137 | |
138 // Calls the callback function within |find_function_| with the find results | |
139 // from within |find_results_|. | |
140 void SendResponse(bool canceled); | |
141 | |
142 private: | |
143 const int request_id_; | |
144 const base::string16 search_text_; | |
145 blink::WebFindOptions options_; | |
146 scoped_refptr<extensions::WebviewFindFunction> find_function_; | |
147 FindResults find_results_; | |
148 | |
149 // A find reply has been received for this find request. | |
150 bool replied_; | |
151 | |
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_; | |
155 | |
156 // Weak pointer used to access the find info of fin. | |
157 base::WeakPtrFactory<FindInfo> weak_ptr_factory_; | |
158 | |
159 friend void WebviewFindHelper::EndFindSession(int session_request_id, | |
160 bool canceled); | |
161 | |
162 DISALLOW_COPY_AND_ASSIGN(FindInfo); | |
163 }; | |
164 | |
165 // Pointer to the webview that is being helped. | |
166 WebViewGuest* const webview_guest_; | |
167 | |
168 // A counter to generate a unique request id for a find request. | |
169 // We only need the ids to be unique for a given WebViewGuest. | |
170 int current_find_request_id_; | |
171 | |
172 // Stores aggregated find results and other info for the |findupdate| event. | |
173 scoped_ptr<FindUpdateEvent> find_update_event_; | |
174 | |
175 // Pointer to the first request of the current find session. | |
176 linked_ptr<FindInfo> current_find_session_; | |
177 | |
178 // Stores each find request's information by request_id so that its callback | |
179 // function can be called when its find results are available. | |
180 typedef std::map<int, linked_ptr<FindInfo> > FindInfoMap; | |
181 FindInfoMap find_info_map_; | |
182 | |
183 DISALLOW_COPY_AND_ASSIGN(WebviewFindHelper); | |
184 }; | |
185 | |
186 #endif // CHROME_BROWSER_GUEST_VIEW_WEB_VIEW_WEB_VIEW_FIND_HELPER_H_ | |
OLD | NEW |