Revert "Fix broken channel icon in chrome://help on CrOS" and try again
[chromium-blink-merge.git] / extensions / browser / guest_view / web_view / web_view_find_helper.h
blob2ed0d0006d3dc96c25e4752612ce2b2d5c6edb87
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_
8 #include <map>
9 #include <vector>
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;
19 class WebViewGuest;
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();
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,
49 bool final_update);
51 private:
52 // A wrapper to store find results.
53 class FindResults {
54 public:
55 FindResults();
56 ~FindResults();
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);
64 // Stores find results into a DictionaryValue.
65 void PrepareResults(base::DictionaryValue* results);
67 private:
68 int number_of_matches_;
69 int active_match_ordinal_;
70 gfx::Rect selection_rect_;
72 friend void WebViewFindHelper::EndFindSession(int session_request_id,
73 bool canceled);
75 DISALLOW_COPY_AND_ASSIGN(FindResults);
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();
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);
90 // Stores find results and other event info into a DictionaryValue.
91 void PrepareResults(base::DictionaryValue* results);
93 private:
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.
101 class FindInfo {
102 public:
103 FindInfo(int request_id,
104 const base::string16& search_text,
105 const blink::WebFindOptions& options,
106 scoped_refptr<WebViewInternalFindFunction> find_function);
107 ~FindInfo();
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,
118 bool final_update);
120 base::WeakPtr<FindInfo> AsWeakPtr();
122 blink::WebFindOptions* options() {
123 return &options_;
126 bool replied() {
127 return replied_;
130 int request_id() {
131 return request_id_;
134 const base::string16& search_text() {
135 return search_text_;
138 // Calls the callback function within |find_function_| with the find results
139 // from within |find_results_|.
140 void SendResponse(bool canceled);
142 private:
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.
150 bool replied_;
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,
157 bool canceled);
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_