Update broken references to image assets
[chromium-blink-merge.git] / chrome / browser / android / contextualsearch / contextual_search_delegate.h
blob8714afe5da0cd33b116b2c57ca0be7ae678592a4
1 // Copyright 2015 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 CHROME_BROWSER_ANDROID_CONTEXTUALSEARCH_CONTEXTUAL_SEARCH_DELEGATE_H_
6 #define CHROME_BROWSER_ANDROID_CONTEXTUALSEARCH_CONTEXTUAL_SEARCH_DELEGATE_H_
8 #include "base/callback.h"
9 #include "base/gtest_prod_util.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/values.h"
13 #include "chrome/browser/android/contextualsearch/contextual_search_context.h"
14 #include "content/public/browser/android/content_view_core.h"
15 #include "net/url_request/url_fetcher_delegate.h"
17 namespace net {
18 class URLRequestContextGetter;
19 } // namespace net
21 class Profile;
22 class TemplateURLService;
24 // Handles tasks for the ContextualSearchManager in a separable and testable
25 // way, without the complication of being connected to a Java object.
26 class ContextualSearchDelegate
27 : public net::URLFetcherDelegate,
28 public base::SupportsWeakPtr<ContextualSearchDelegate> {
29 public:
30 typedef base::Callback<void(bool,
31 int,
32 const std::string&,
33 const std::string&,
34 const std::string&,
35 bool,
36 int,
37 int)> SearchTermResolutionCallback;
38 typedef base::Callback<
39 void(const std::string&, const std::string&)> SurroundingTextCallback;
40 typedef base::Callback<
41 void(const base::string16&, int, int)>
42 HandleSurroundingsCallback;
43 typedef base::Callback<
44 void(const std::string&, const base::string16&, size_t, size_t)>
45 IcingCallback;
47 // ID used in creating URLFetcher for Contextual Search results.
48 static const int kContextualSearchURLFetcherID;
50 // Constructs a delegate that will always call back to the given callbacks
51 // when search term resolution or surrounding text responses are available.
52 ContextualSearchDelegate(
53 net::URLRequestContextGetter* url_request_context,
54 TemplateURLService* template_url_service,
55 const SearchTermResolutionCallback& search_term_callback,
56 const SurroundingTextCallback& surrounding_callback,
57 const IcingCallback& icing_callback);
58 ~ContextualSearchDelegate() override;
60 // Gathers surrounding text and starts an asynchronous search term resolution
61 // request. The "search term" is the best query to issue for a section of text
62 // in the context of a web page. When the response is available the callback
63 // specified in the constructor is run.
64 void StartSearchTermResolutionRequest(
65 const std::string& selection,
66 bool use_resolved_search_term,
67 content::ContentViewCore* content_view_core,
68 bool may_send_base_page_url);
70 // Gathers surrounding text and saves it locally for a future query.
71 void GatherAndSaveSurroundingText(const std::string& selection,
72 bool use_resolved_search_term,
73 content::ContentViewCore* content_view_core,
74 bool may_send_base_page_url);
76 // Continues making a Search Term Resolution request, once the surrounding
77 // text has been gathered.
78 void ContinueSearchTermResolutionRequest();
80 // For testing.
81 void set_context_for_testing(ContextualSearchContext* context) {
82 context_.reset(context);
85 private:
86 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest,
87 SurroundingTextHighMaximum);
88 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest,
89 SurroundingTextLowMaximum);
90 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest,
91 SurroundingTextNoBeforeText);
92 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest,
93 ExtractMentionsStartEnd);
94 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest,
95 SurroundingTextForIcing);
96 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest,
97 SurroundingTextForIcingNegativeLimit);
98 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest,
99 DecodeSearchTermsFromJsonResponse);
101 // net::URLFetcherDelegate:
102 void OnURLFetchComplete(const net::URLFetcher* source) override;
104 // Builds the search term resolution request URL from the current context.
105 GURL BuildRequestUrl();
107 // Uses the TemplateURL service to construct a search term resolution URL from
108 // the given parameters.
109 std::string GetSearchTermResolutionUrlString(
110 const std::string& selected_text,
111 const std::string& base_page_url,
112 const bool may_send_base_page_url);
114 // Will gather the surrounding text from the |content_view_core| and call the
115 // |callback|.
116 void GatherSurroundingTextWithCallback(
117 const std::string& selection,
118 bool use_resolved_search_term,
119 content::ContentViewCore* content_view_core,
120 bool may_send_base_page_url,
121 HandleSurroundingsCallback callback);
123 // Callback for GatherSurroundingTextWithCallback(). Will start the search
124 // term resolution request.
125 void StartSearchTermRequestFromSelection(
126 const base::string16& surrounding_text,
127 int start_offset,
128 int end_offset);
130 void SaveSurroundingText(
131 const base::string16& surrounding_text,
132 int start_offset,
133 int end_offset);
135 // Will call back to the manager with the proper surrounding text to be shown
136 // in the UI. Will return a maximum of |max_surrounding_chars| characters for
137 // each of the segments.
138 void SendSurroundingText(int max_surrounding_chars);
140 // Populates the discourse context and adds it to the HTTP header of the
141 // search term resolution request.
142 void SetDiscourseContextAndAddToHeader(
143 const ContextualSearchContext& context);
145 // Checks if we can send the URL for this user. Several conditions are checked
146 // to make sure it's OK to send the URL. These fall into two categories:
147 // 1) check if it's allowed by our policy, and 2) ensure that the user is
148 // already sending their URL browsing activity to Google.
149 bool CanSendPageURL(const GURL& current_page_url,
150 Profile* profile,
151 TemplateURLService* template_url_service);
153 // Decodes the given json response string and extracts parameters.
154 void DecodeSearchTermsFromJsonResponse(const std::string& response,
155 std::string* search_term,
156 std::string* display_text,
157 std::string* alternate_term,
158 std::string* prevent_preload,
159 int* mention_start,
160 int* mention_end);
162 void ExtractMentionsStartEnd(const base::ListValue& mentions_list,
163 int* startResult,
164 int* endResult);
166 // Returns the surrounding size to use for the search term resolution
167 // request.
168 int GetSearchTermSurroundingSize();
170 // Returns the size of the surroundings to be sent to Icing.
171 int GetIcingSurroundingSize();
173 // Generates a subset of the given surrounding_text string, for Icing
174 // integration.
175 // |surrounding_text| the entire text context that contains the selection.
176 // |padding_each_side| the number of characters of padding desired on each
177 // side of the selection (negative values treated as 0).
178 // |start| the start offset of the selection, updated to reflect the new
179 // position
180 // of the selection in the function result.
181 // |end| the end offset of the selection, updated to reflect the new position
182 // of the selection in the function result.
183 // |return| the trimmed surrounding text with selection at the
184 // updated start/end offsets.
185 base::string16 SurroundingTextForIcing(const base::string16& surrounding_text,
186 int padding_each_side,
187 size_t* start,
188 size_t* end);
190 // The current request in progress, or NULL.
191 scoped_ptr<net::URLFetcher> search_term_fetcher_;
193 // Holds the URL request context. Not owned.
194 net::URLRequestContextGetter* url_request_context_;
196 // Holds the TemplateURLService. Not owned.
197 TemplateURLService* template_url_service_;
199 // The callback for notifications of completed URL fetches.
200 SearchTermResolutionCallback search_term_callback_;
202 // The callback for notifications of surrounding text being available.
203 SurroundingTextCallback surrounding_callback_;
205 // The callback for notifications of Icing selection being available.
206 IcingCallback icing_callback_;
208 // Used to hold the context until an upcoming search term request is started.
209 scoped_ptr<ContextualSearchContext> context_;
211 DISALLOW_COPY_AND_ASSIGN(ContextualSearchDelegate);
214 #endif // CHROME_BROWSER_ANDROID_CONTEXTUALSEARCH_CONTEXTUAL_SEARCH_DELEGATE_H_