OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_ANDROID_CONTEXTUALSEARCH_CONTEXTUAL_SEARCH_DELEGATE_H_ | 5 #ifndef CHROME_BROWSER_ANDROID_CONTEXTUALSEARCH_CONTEXTUAL_SEARCH_DELEGATE_H_ |
6 #define CHROME_BROWSER_ANDROID_CONTEXTUALSEARCH_CONTEXTUAL_SEARCH_DELEGATE_H_ | 6 #define CHROME_BROWSER_ANDROID_CONTEXTUALSEARCH_CONTEXTUAL_SEARCH_DELEGATE_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
(...skipping 25 matching lines...) Expand all Loading... | |
36 // way, without the complication of being connected to a Java object. | 36 // way, without the complication of being connected to a Java object. |
37 class ContextualSearchDelegate | 37 class ContextualSearchDelegate |
38 : public net::URLFetcherDelegate, | 38 : public net::URLFetcherDelegate, |
39 public base::SupportsWeakPtr<ContextualSearchDelegate> { | 39 public base::SupportsWeakPtr<ContextualSearchDelegate> { |
40 public: | 40 public: |
41 // Callback that provides the surrounding text past the selection for the UX. | 41 // Callback that provides the surrounding text past the selection for the UX. |
42 typedef base::Callback<void(const std::string&)> SurroundingTextCallback; | 42 typedef base::Callback<void(const std::string&)> SurroundingTextCallback; |
43 // Provides the Resolved Search Term, called when the Resolve Request returns. | 43 // Provides the Resolved Search Term, called when the Resolve Request returns. |
44 typedef base::Callback<void(const ResolvedSearchTerm&)> | 44 typedef base::Callback<void(const ResolvedSearchTerm&)> |
45 SearchTermResolutionCallback; | 45 SearchTermResolutionCallback; |
46 // Provides the surrounding text and selection start/end when Blink gathers | |
47 // surrounding text for the Context. | |
48 typedef base::Callback< | |
49 void(const base::string16&, int, int)> | |
50 HandleSurroundingsCallback; | |
51 // Provides limited surrounding text for icing. | 46 // Provides limited surrounding text for icing. |
52 typedef base::Callback< | 47 typedef base::Callback< |
53 void(const std::string&, const base::string16&, size_t, size_t)> | 48 void(const std::string&, const base::string16&, size_t, size_t)> |
54 IcingCallback; | 49 IcingCallback; |
55 | 50 |
56 // ID used in creating URLFetcher for Contextual Search results. | 51 // ID used in creating URLFetcher for Contextual Search results. |
57 static const int kContextualSearchURLFetcherID; | 52 static const int kContextualSearchURLFetcherID; |
58 | 53 |
59 // Constructs a delegate that will always call back to the given callbacks | 54 // Constructs a delegate that will always call back to the given callbacks |
60 // when search term resolution or surrounding text responses are available. | 55 // when search term resolution or surrounding text responses are available. |
61 ContextualSearchDelegate( | 56 ContextualSearchDelegate( |
62 net::URLRequestContextGetter* url_request_context, | 57 net::URLRequestContextGetter* url_request_context, |
63 TemplateURLService* template_url_service, | 58 TemplateURLService* template_url_service, |
64 const SearchTermResolutionCallback& search_term_callback, | 59 const SearchTermResolutionCallback& search_term_callback, |
65 const SurroundingTextCallback& surrounding_callback, | 60 const SurroundingTextCallback& surrounding_callback, |
66 const IcingCallback& icing_callback); | 61 const IcingCallback& icing_callback); |
67 ~ContextualSearchDelegate() override; | 62 ~ContextualSearchDelegate() override; |
68 | 63 |
69 // Gathers surrounding text and starts an asynchronous search term resolution | 64 // Gathers surrounding text and saves it locally in the given context. |
70 // request. The "search term" is the best query to issue for a section of text | 65 void GatherAndSaveSurroundingText( |
71 // in the context of a web page. When the response is available the callback | 66 ContextualSearchContext* contextual_search_context, |
72 // specified in the constructor is run. | 67 content::WebContents* web_contents); |
73 void StartSearchTermResolutionRequest(const std::string& selection, | |
74 const std::string& home_country, | |
75 content::WebContents* web_contents, | |
76 bool may_send_base_page_url); | |
77 | 68 |
78 // Gathers surrounding text and saves it locally for a future query. | 69 // Starts an asynchronous search term resolution request. |
79 void GatherAndSaveSurroundingText(const std::string& selection, | 70 // The given context includes some content from a web page and must be able |
80 const std::string& home_country, | 71 // to resolve. |
81 content::WebContents* web_contents, | 72 // When the response is available the callback specified in the constructor |
82 bool may_send_base_page_url); | 73 // is run. |
74 void StartSearchTermResolutionRequest( | |
75 ContextualSearchContext* contextual_search_context, | |
76 content::WebContents* web_contents); | |
83 | 77 |
84 // Continues making a Search Term Resolution request, once the surrounding | 78 // Resolves the search term specified by the current context. |
85 // text has been gathered. | 79 // Only needed for tests. TODO(donnd): make private and friend? |
Theresa
2017/03/09 22:46:37
If it's not too difficult, let's do this TODO now.
Donn Denman
2017/03/10 01:29:43
Done.
| |
86 void ContinueSearchTermResolutionRequest(); | 80 void ResolveSearchTermFromContext(); |
87 | 81 |
88 // Gets the target language for translation purposes for this user. | 82 // Gets the target language for translation purposes for this user. |
89 std::string GetTargetLanguage(); | 83 std::string GetTargetLanguage(); |
90 | 84 |
91 // Returns the accept languages preference string. | 85 // Returns the accept languages preference string. |
92 std::string GetAcceptLanguages(); | 86 std::string GetAcceptLanguages(); |
93 | 87 |
94 // For testing. | 88 // For testing. |
95 void set_context_for_testing(ContextualSearchContext* context) { | 89 void set_context_for_testing(ContextualSearchContext* context) { |
96 context_.reset(context); | 90 context_ = context; |
97 } | 91 } |
98 | 92 |
99 private: | 93 private: |
100 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, | 94 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, |
101 SurroundingTextHighMaximum); | 95 SurroundingTextHighMaximum); |
102 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, | 96 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, |
103 SurroundingTextLowMaximum); | 97 SurroundingTextLowMaximum); |
104 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, | 98 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, |
105 SurroundingTextNoBeforeText); | 99 SurroundingTextNoBeforeText); |
106 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, | 100 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, |
107 SurroundingTextNoAfterText); | 101 SurroundingTextNoAfterText); |
108 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, | 102 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, |
109 ExtractMentionsStartEnd); | 103 ExtractMentionsStartEnd); |
110 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, | 104 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, |
111 SurroundingTextForIcing); | 105 SurroundingTextForIcing); |
112 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, | 106 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, |
113 SurroundingTextForIcingNegativeLimit); | 107 SurroundingTextForIcingNegativeLimit); |
114 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, | 108 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, |
115 DecodeSearchTermFromJsonResponse); | 109 DecodeSearchTermFromJsonResponse); |
116 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, DecodeStringMentions); | 110 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, DecodeStringMentions); |
117 | 111 |
118 // net::URLFetcherDelegate: | 112 // net::URLFetcherDelegate: |
119 void OnURLFetchComplete(const net::URLFetcher* source) override; | 113 void OnURLFetchComplete(const net::URLFetcher* source) override; |
120 | 114 |
121 // Builds the ContextualSearchContext in the current context from | |
122 // the given parameters. | |
123 void BuildContext(const std::string& selection, | |
124 const std::string& home_country, | |
125 content::WebContents* web_contents, | |
126 bool may_send_base_page_url); | |
127 | |
128 // Builds and returns the search term resolution request URL. | 115 // Builds and returns the search term resolution request URL. |
129 // |selection| is used as the default query. | 116 // |selection| is used as the default query. |
130 std::string BuildRequestUrl(std::string selection); | 117 std::string BuildRequestUrl(std::string selection); |
131 | 118 |
132 // Uses the TemplateURL service to construct a search term resolution URL from | 119 // Uses the TemplateURL service to construct a search term resolution URL from |
133 // the given parameters. | 120 // the given parameters. |
134 std::string GetSearchTermResolutionUrlString( | 121 std::string GetSearchTermResolutionUrlString( |
135 const std::string& selected_text, | 122 const std::string& selected_text, |
136 const std::string& base_page_url, | 123 const std::string& base_page_url, |
137 const bool may_send_base_page_url); | 124 const bool may_send_base_page_url); |
138 | 125 |
139 // Will gather the surrounding text from the |content_view_core| and call the | 126 // Callback for GatherAndSaveSurroundingText, called when surrounding text is |
140 // |callback|. | 127 // available. |
141 void GatherSurroundingTextWithCallback(const std::string& selection, | 128 void OnTextSurroundingSelectionAvailable( |
142 const std::string& home_country, | |
143 content::WebContents* web_contents, | |
144 bool may_send_base_page_url, | |
145 HandleSurroundingsCallback callback); | |
146 | |
147 // Callback for GatherSurroundingTextWithCallback(). Will start the search | |
148 // term resolution request. | |
149 void StartSearchTermRequestFromSelection( | |
150 const base::string16& surrounding_text, | 129 const base::string16& surrounding_text, |
151 int start_offset, | 130 int start_offset, |
152 int end_offset); | 131 int end_offset); |
153 | 132 |
154 void SaveSurroundingText( | 133 void SaveSurroundingText( |
155 const base::string16& surrounding_text, | 134 const base::string16& surrounding_text, |
156 int start_offset, | 135 int start_offset, |
157 int end_offset); | 136 int end_offset); |
158 | 137 |
159 // Will call back to the manager with the proper surrounding text to be shown | 138 // Will call back to the manager with the proper surrounding text to be shown |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
236 // The callback for notifications of completed URL fetches. | 215 // The callback for notifications of completed URL fetches. |
237 SearchTermResolutionCallback search_term_callback_; | 216 SearchTermResolutionCallback search_term_callback_; |
238 | 217 |
239 // The callback for notifications of surrounding text being available. | 218 // The callback for notifications of surrounding text being available. |
240 SurroundingTextCallback surrounding_callback_; | 219 SurroundingTextCallback surrounding_callback_; |
241 | 220 |
242 // The callback for notifications of Icing selection being available. | 221 // The callback for notifications of Icing selection being available. |
243 IcingCallback icing_callback_; | 222 IcingCallback icing_callback_; |
244 | 223 |
245 // Used to hold the context until an upcoming search term request is started. | 224 // Used to hold the context until an upcoming search term request is started. |
246 std::unique_ptr<ContextualSearchContext> context_; | 225 // Owned by the Java ContextualSearchContext. |
226 ContextualSearchContext* context_; | |
247 | 227 |
248 DISALLOW_COPY_AND_ASSIGN(ContextualSearchDelegate); | 228 DISALLOW_COPY_AND_ASSIGN(ContextualSearchDelegate); |
249 }; | 229 }; |
250 | 230 |
251 #endif // CHROME_BROWSER_ANDROID_CONTEXTUALSEARCH_CONTEXTUAL_SEARCH_DELEGATE_H_ | 231 #endif // CHROME_BROWSER_ANDROID_CONTEXTUALSEARCH_CONTEXTUAL_SEARCH_DELEGATE_H_ |
OLD | NEW |