Chromium Code Reviews| 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 starts an asynchronous search term resolution |
| 70 // request. The "search term" is the best query to issue for a section of text | 65 // request. The given context includes some content from a web page. |
| 71 // in the context of a web page. When the response is available the callback | 66 // When the response is available the callback specified in the constructor |
| 72 // specified in the constructor is run. | 67 // is run. |
| 73 void StartSearchTermResolutionRequest(const std::string& selection, | 68 void StartSearchTermResolutionRequest( |
| 74 const std::string& home_country, | 69 ContextualSearchContext* contextual_search_context, |
| 75 content::WebContents* web_contents, | 70 content::WebContents* web_contents); |
| 76 bool may_send_base_page_url); | |
| 77 | 71 |
| 78 // Gathers surrounding text and saves it locally for a future query. | 72 // Gathers surrounding text and saves it locally in the given context. |
| 79 void GatherAndSaveSurroundingText(const std::string& selection, | 73 void GatherAndSaveSurroundingText( |
| 80 const std::string& home_country, | 74 ContextualSearchContext* contextual_search_context, |
| 81 content::WebContents* web_contents, | 75 content::WebContents* web_contents); |
| 82 bool may_send_base_page_url); | |
| 83 | 76 |
| 84 // Continues making a Search Term Resolution request, once the surrounding | 77 // Resolves the search term specified by the current context. |
| 85 // text has been gathered. | 78 // Only needed for tests. TODO(donnd): make private and friend? |
| 86 void ContinueSearchTermResolutionRequest(); | 79 void ResolveSearchTermFromContext(); |
| 87 | 80 |
| 88 // Gets the target language for translation purposes for this user. | 81 // Gets the target language for translation purposes for this user. |
| 89 std::string GetTargetLanguage(); | 82 std::string GetTargetLanguage(); |
| 90 | 83 |
| 91 // Returns the accept languages preference string. | 84 // Returns the accept languages preference string. |
| 92 std::string GetAcceptLanguages(); | 85 std::string GetAcceptLanguages(); |
| 93 | 86 |
| 94 // For testing. | 87 // For testing. |
| 95 void set_context_for_testing(ContextualSearchContext* context) { | 88 void set_context_for_testing(ContextualSearchContext* context) { |
| 96 context_.reset(context); | 89 context_ = context; |
| 97 } | 90 } |
| 98 | 91 |
| 99 private: | 92 private: |
| 100 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, | 93 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, |
| 101 SurroundingTextHighMaximum); | 94 SurroundingTextHighMaximum); |
| 102 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, | 95 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, |
| 103 SurroundingTextLowMaximum); | 96 SurroundingTextLowMaximum); |
| 104 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, | 97 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, |
| 105 SurroundingTextNoBeforeText); | 98 SurroundingTextNoBeforeText); |
| 106 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, | 99 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, |
| 107 SurroundingTextNoAfterText); | 100 SurroundingTextNoAfterText); |
| 108 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, | 101 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, |
| 109 ExtractMentionsStartEnd); | 102 ExtractMentionsStartEnd); |
| 110 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, | 103 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, |
| 111 SurroundingTextForIcing); | 104 SurroundingTextForIcing); |
| 112 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, | 105 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, |
| 113 SurroundingTextForIcingNegativeLimit); | 106 SurroundingTextForIcingNegativeLimit); |
| 114 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, | 107 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, |
| 115 DecodeSearchTermFromJsonResponse); | 108 DecodeSearchTermFromJsonResponse); |
| 116 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, DecodeStringMentions); | 109 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, DecodeStringMentions); |
| 117 | 110 |
| 118 // net::URLFetcherDelegate: | 111 // net::URLFetcherDelegate: |
| 119 void OnURLFetchComplete(const net::URLFetcher* source) override; | 112 void OnURLFetchComplete(const net::URLFetcher* source) override; |
| 120 | 113 |
| 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. | 114 // Builds and returns the search term resolution request URL. |
| 129 // |selection| is used as the default query. | 115 // |selection| is used as the default query. |
| 130 std::string BuildRequestUrl(std::string selection); | 116 std::string BuildRequestUrl(std::string selection); |
| 131 | 117 |
| 132 // Uses the TemplateURL service to construct a search term resolution URL from | 118 // Uses the TemplateURL service to construct a search term resolution URL from |
| 133 // the given parameters. | 119 // the given parameters. |
| 134 std::string GetSearchTermResolutionUrlString( | 120 std::string GetSearchTermResolutionUrlString( |
| 135 const std::string& selected_text, | 121 const std::string& selected_text, |
| 136 const std::string& base_page_url, | 122 const std::string& base_page_url, |
| 137 const bool may_send_base_page_url); | 123 const bool may_send_base_page_url); |
| 138 | 124 |
| 139 // Will gather the surrounding text from the |content_view_core| and call the | 125 // Callback for GatherAndSaveSurroundingText, called when surrounding text is |
| 140 // |callback|. | 126 // available. |
| 141 void GatherSurroundingTextWithCallback(const std::string& selection, | 127 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, | 128 const base::string16& surrounding_text, |
| 151 int start_offset, | 129 int start_offset, |
| 152 int end_offset); | 130 int end_offset); |
| 153 | 131 |
| 154 void SaveSurroundingText( | 132 void SaveSurroundingText( |
| 155 const base::string16& surrounding_text, | 133 const base::string16& surrounding_text, |
| 156 int start_offset, | 134 int start_offset, |
| 157 int end_offset); | 135 int end_offset); |
| 158 | 136 |
| 159 // Will call back to the manager with the proper surrounding text to be shown | 137 // 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. | 214 // The callback for notifications of completed URL fetches. |
| 237 SearchTermResolutionCallback search_term_callback_; | 215 SearchTermResolutionCallback search_term_callback_; |
| 238 | 216 |
| 239 // The callback for notifications of surrounding text being available. | 217 // The callback for notifications of surrounding text being available. |
| 240 SurroundingTextCallback surrounding_callback_; | 218 SurroundingTextCallback surrounding_callback_; |
| 241 | 219 |
| 242 // The callback for notifications of Icing selection being available. | 220 // The callback for notifications of Icing selection being available. |
| 243 IcingCallback icing_callback_; | 221 IcingCallback icing_callback_; |
| 244 | 222 |
| 245 // Used to hold the context until an upcoming search term request is started. | 223 // Used to hold the context until an upcoming search term request is started. |
| 246 std::unique_ptr<ContextualSearchContext> context_; | 224 ContextualSearchContext* context_; |
|
Theresa
2017/03/08 01:54:38
Just to check my understanding - the ContextualSea
Donn Denman
2017/03/09 17:35:05
That's right, it's now owned by the Java object.
| |
| 247 | 225 |
| 248 DISALLOW_COPY_AND_ASSIGN(ContextualSearchDelegate); | 226 DISALLOW_COPY_AND_ASSIGN(ContextualSearchDelegate); |
| 249 }; | 227 }; |
| 250 | 228 |
| 251 #endif // CHROME_BROWSER_ANDROID_CONTEXTUALSEARCH_CONTEXTUAL_SEARCH_DELEGATE_H_ | 229 #endif // CHROME_BROWSER_ANDROID_CONTEXTUALSEARCH_CONTEXTUAL_SEARCH_DELEGATE_H_ |
| OLD | NEW |