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