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

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

Issue 2706333002: [TTS] Add a Java Context linked to existing native (Closed)
Patch Set: Nothing, I think. Created 3 years, 9 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
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
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.
83 74 void StartSearchTermResolutionRequest(
84 // Continues making a Search Term Resolution request, once the surrounding 75 ContextualSearchContext* contextual_search_context,
85 // text has been gathered. 76 content::WebContents* web_contents);
86 void ContinueSearchTermResolutionRequest();
87 77
88 // Gets the target language for translation purposes for this user. 78 // Gets the target language for translation purposes for this user.
89 std::string GetTargetLanguage(); 79 std::string GetTargetLanguage();
90 80
91 // Returns the accept languages preference string. 81 // Returns the accept languages preference string.
92 std::string GetAcceptLanguages(); 82 std::string GetAcceptLanguages();
93 83
94 // For testing. 84 // For testing.
95 void set_context_for_testing(ContextualSearchContext* context) { 85 void set_context_for_testing(ContextualSearchContext* context) {
96 context_.reset(context); 86 context_ = context;
97 } 87 }
98 88
99 private: 89 private:
90 // Friend our test which allows our private methods to be used in helper
91 // functions. FRIEND_TEST_ALL_PREFIXES just friends individual prefixes.
92 // Needed for |ResolveSearchTermFromContext|.
93 friend class ContextualSearchDelegateTest;
94 // TODO(donnd): consider removing the following since the above covers this.
100 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, 95 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest,
101 SurroundingTextHighMaximum); 96 SurroundingTextHighMaximum);
102 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, 97 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest,
103 SurroundingTextLowMaximum); 98 SurroundingTextLowMaximum);
104 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, 99 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest,
105 SurroundingTextNoBeforeText); 100 SurroundingTextNoBeforeText);
106 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, 101 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest,
107 SurroundingTextNoAfterText); 102 SurroundingTextNoAfterText);
108 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, 103 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest,
109 ExtractMentionsStartEnd); 104 ExtractMentionsStartEnd);
110 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, 105 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest,
111 SurroundingTextForIcing); 106 SurroundingTextForIcing);
112 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, 107 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest,
113 SurroundingTextForIcingNegativeLimit); 108 SurroundingTextForIcingNegativeLimit);
114 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, 109 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest,
115 DecodeSearchTermFromJsonResponse); 110 DecodeSearchTermFromJsonResponse);
116 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, DecodeStringMentions); 111 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, DecodeStringMentions);
117 112
118 // net::URLFetcherDelegate: 113 // net::URLFetcherDelegate:
119 void OnURLFetchComplete(const net::URLFetcher* source) override; 114 void OnURLFetchComplete(const net::URLFetcher* source) override;
120 115
121 // Builds the ContextualSearchContext in the current context from 116 // Resolves the search term specified by the current context.
122 // the given parameters. 117 // Only needed for tests. TODO(donnd): make private and friend?
123 void BuildContext(const std::string& selection, 118 void ResolveSearchTermFromContext();
124 const std::string& home_country,
125 content::WebContents* web_contents,
126 bool may_send_base_page_url);
127 119
128 // Builds and returns the search term resolution request URL. 120 // Builds and returns the search term resolution request URL.
129 // |selection| is used as the default query. 121 // |selection| is used as the default query.
130 std::string BuildRequestUrl(std::string selection); 122 std::string BuildRequestUrl(std::string selection);
131 123
132 // Uses the TemplateURL service to construct a search term resolution URL from 124 // Uses the TemplateURL service to construct a search term resolution URL from
133 // the given parameters. 125 // the given parameters.
134 std::string GetSearchTermResolutionUrlString( 126 std::string GetSearchTermResolutionUrlString(
135 const std::string& selected_text, 127 const std::string& selected_text,
136 const std::string& base_page_url, 128 const std::string& base_page_url,
137 const bool may_send_base_page_url); 129 const bool may_send_base_page_url);
138 130
139 // Will gather the surrounding text from the |content_view_core| and call the 131 // Callback for GatherAndSaveSurroundingText, called when surrounding text is
140 // |callback|. 132 // available.
141 void GatherSurroundingTextWithCallback(const std::string& selection, 133 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, 134 const base::string16& surrounding_text,
151 int start_offset, 135 int start_offset,
152 int end_offset); 136 int end_offset);
153 137
154 void SaveSurroundingText( 138 void SaveSurroundingText(
155 const base::string16& surrounding_text, 139 const base::string16& surrounding_text,
156 int start_offset, 140 int start_offset,
157 int end_offset); 141 int end_offset);
158 142
159 // Will call back to the manager with the proper surrounding text to be shown 143 // 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
236 // The callback for notifications of completed URL fetches. 220 // The callback for notifications of completed URL fetches.
237 SearchTermResolutionCallback search_term_callback_; 221 SearchTermResolutionCallback search_term_callback_;
238 222
239 // The callback for notifications of surrounding text being available. 223 // The callback for notifications of surrounding text being available.
240 SurroundingTextCallback surrounding_callback_; 224 SurroundingTextCallback surrounding_callback_;
241 225
242 // The callback for notifications of Icing selection being available. 226 // The callback for notifications of Icing selection being available.
243 IcingCallback icing_callback_; 227 IcingCallback icing_callback_;
244 228
245 // Used to hold the context until an upcoming search term request is started. 229 // Used to hold the context until an upcoming search term request is started.
246 std::unique_ptr<ContextualSearchContext> context_; 230 // Owned by the Java ContextualSearchContext.
231 ContextualSearchContext* context_;
247 232
248 DISALLOW_COPY_AND_ASSIGN(ContextualSearchDelegate); 233 DISALLOW_COPY_AND_ASSIGN(ContextualSearchDelegate);
249 }; 234 };
250 235
251 #endif // CHROME_BROWSER_ANDROID_CONTEXTUALSEARCH_CONTEXTUAL_SEARCH_DELEGATE_H_ 236 #endif // CHROME_BROWSER_ANDROID_CONTEXTUALSEARCH_CONTEXTUAL_SEARCH_DELEGATE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698