Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / extensions / browser / guest_view / web_view / web_view_find_helper.h
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(content::WebContents* guest_web_contents,
40             const base::string16& search_text,
41             const blink::WebFindOptions& options,
42             scoped_refptr<WebViewInternalFindFunction> 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<WebViewInternalFindFunction> 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<WebViewInternalFindFunction> 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     friend void WebViewFindHelper::EndFindSession(int session_request_id,
157                                                   bool canceled);
158
159     base::WeakPtrFactory<FindInfo> weak_ptr_factory_;
160
161     DISALLOW_COPY_AND_ASSIGN(FindInfo);
162   };
163
164   // Pointer to the webview that is being helped.
165   WebViewGuest* const webview_guest_;
166
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_;
170
171   // Stores aggregated find results and other info for the |findupdate| event.
172   scoped_ptr<FindUpdateEvent> find_update_event_;
173
174   // Pointer to the first request of the current find session.
175   linked_ptr<FindInfo> current_find_session_;
176
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_;
181
182   DISALLOW_COPY_AND_ASSIGN(WebViewFindHelper);
183 };
184
185 } // namespace extensions
186
187 #endif  // EXTENSIONS_BROWSER_GUEST_VIEW_WEB_VIEW_WEB_VIEW_FIND_HELPER_H_