Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(186)

Side by Side Diff: chrome/browser/android/contextualsearch/contextual_search_delegate.h

Issue 1141283003: Upstream oodles of Chrome for Android code into Chromium. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: final patch? Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #ifndef CHROME_BROWSER_ANDROID_CONTEXTUALSEARCH_CONTEXTUAL_SEARCH_DELEGATE_H_
6 #define CHROME_BROWSER_ANDROID_CONTEXTUALSEARCH_CONTEXTUAL_SEARCH_DELEGATE_H_
7
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"
15
16 namespace net {
17 class URLRequestContextGetter;
18 } // namespace net
19
20 class Profile;
21 class TemplateURLService;
22
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;
41
42 // ID used in creating URLFetcher for Contextual Search results.
43 static const int kContextualSearchURLFetcherID;
44
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;
54
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);
63
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);
69
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();
75
76 // For testing.
77 void set_context_for_testing(ContextualSearchContext* context) {
78 context_.reset(context);
79 }
80
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);
94
95 // net::URLFetcherDelegate:
96 void OnURLFetchComplete(const net::URLFetcher* source) override;
97
98 // Builds the search term resolution request URL from the current context.
99 GURL BuildRequestUrl();
100
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);
107
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);
115
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);
122
123 void SaveSurroundingText(
124 const base::string16& surrounding_text,
125 int start_offset,
126 int end_offset);
127
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);
132
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);
137
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);
145
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);
152
153 // Returns the surrounding size to use for the search term resolution
154 // request.
155 int GetSearchTermSurroundingSize();
156
157 // Returns the size of the surroundings to be sent to Icing.
158 int GetIcingSurroundingSize();
159
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);
176
177 // The current request in progress, or NULL.
178 scoped_ptr<net::URLFetcher> search_term_fetcher_;
179
180 // Holds the URL request context. Not owned.
181 net::URLRequestContextGetter* url_request_context_;
182
183 // Holds the TemplateURLService. Not owned.
184 TemplateURLService* template_url_service_;
185
186 // The callback for notifications of completed URL fetches.
187 SearchTermResolutionCallback search_term_callback_;
188
189 // The callback for notifications of surrounding text being available.
190 SurroundingTextCallback surrounding_callback_;
191
192 // The callback for notifications of Icing selection being available.
193 IcingCallback icing_callback_;
194
195 // Used to hold the context until an upcoming search term request is started.
196 scoped_ptr<ContextualSearchContext> context_;
197
198 DISALLOW_COPY_AND_ASSIGN(ContextualSearchDelegate);
199 };
200
201 #endif // CHROME_BROWSER_ANDROID_CONTEXTUALSEARCH_CONTEXTUAL_SEARCH_DELEGATE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698