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"
18 class URLRequestContextGetter
;
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
> {
30 typedef base::Callback
<void(bool,
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)>
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();
81 void set_context_for_testing(ContextualSearchContext
* context
) {
82 context_
.reset(context
);
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
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
,
130 void SaveSurroundingText(
131 const base::string16
& surrounding_text
,
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
,
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
,
162 void ExtractMentionsStartEnd(const base::ListValue
& mentions_list
,
166 // Returns the surrounding size to use for the search term resolution
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
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
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
,
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_