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

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

Issue 1354763003: [Contextual Search] Trigger the translation one-box. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed Teds comments: Changed to use LinkedHashSet to ensure unique and ordered results. Created 5 years, 2 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 "base/callback.h" 8 #include "base/callback.h"
9 #include "base/gtest_prod_util.h" 9 #include "base/gtest_prod_util.h"
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/weak_ptr.h" 11 #include "base/memory/weak_ptr.h"
12 #include "base/values.h" 12 #include "base/values.h"
13 #include "chrome/browser/android/contextualsearch/contextual_search_context.h" 13 #include "chrome/browser/android/contextualsearch/contextual_search_context.h"
14 #include "chrome/browser/android/contextualsearch/resolved_search_term.h"
14 #include "content/public/browser/android/content_view_core.h" 15 #include "content/public/browser/android/content_view_core.h"
15 #include "net/url_request/url_fetcher_delegate.h" 16 #include "net/url_request/url_fetcher_delegate.h"
16 17
17 namespace net { 18 namespace net {
18 class URLRequestContextGetter; 19 class URLRequestContextGetter;
19 } // namespace net 20 } // namespace net
20 21
21 class Profile; 22 class Profile;
22 class TemplateURLService; 23 class TemplateURLService;
23 24
24 // Handles tasks for the ContextualSearchManager in a separable and testable 25 // Handles tasks for the ContextualSearchManager in a separable and testable
25 // way, without the complication of being connected to a Java object. 26 // way, without the complication of being connected to a Java object.
26 class ContextualSearchDelegate 27 class ContextualSearchDelegate
27 : public net::URLFetcherDelegate, 28 : public net::URLFetcherDelegate,
28 public base::SupportsWeakPtr<ContextualSearchDelegate> { 29 public base::SupportsWeakPtr<ContextualSearchDelegate> {
29 public: 30 public:
30 typedef base::Callback<void(bool,
31 int,
32 const std::string&,
33 const std::string&,
34 const std::string&,
35 bool,
36 int,
37 int)> SearchTermResolutionCallback;
38 typedef base::Callback<void(const std::string&)> SurroundingTextCallback; 31 typedef base::Callback<void(const std::string&)> SurroundingTextCallback;
32 typedef base::Callback<void(const ResolvedSearchTerm&)>
33 SearchTermResolutionCallback;
39 typedef base::Callback< 34 typedef base::Callback<
40 void(const base::string16&, int, int)> 35 void(const base::string16&, int, int)>
41 HandleSurroundingsCallback; 36 HandleSurroundingsCallback;
42 typedef base::Callback< 37 typedef base::Callback<
43 void(const std::string&, const base::string16&, size_t, size_t)> 38 void(const std::string&, const base::string16&, size_t, size_t)>
44 IcingCallback; 39 IcingCallback;
45 40
46 // ID used in creating URLFetcher for Contextual Search results. 41 // ID used in creating URLFetcher for Contextual Search results.
47 static const int kContextualSearchURLFetcherID; 42 static const int kContextualSearchURLFetcherID;
48 43
(...skipping 20 matching lines...) Expand all
69 // Gathers surrounding text and saves it locally for a future query. 64 // Gathers surrounding text and saves it locally for a future query.
70 void GatherAndSaveSurroundingText(const std::string& selection, 65 void GatherAndSaveSurroundingText(const std::string& selection,
71 bool use_resolved_search_term, 66 bool use_resolved_search_term,
72 content::ContentViewCore* content_view_core, 67 content::ContentViewCore* content_view_core,
73 bool may_send_base_page_url); 68 bool may_send_base_page_url);
74 69
75 // Continues making a Search Term Resolution request, once the surrounding 70 // Continues making a Search Term Resolution request, once the surrounding
76 // text has been gathered. 71 // text has been gathered.
77 void ContinueSearchTermResolutionRequest(); 72 void ContinueSearchTermResolutionRequest();
78 73
74 // Gets the target language for translation purposes (cached).
75 std::string GetTargetLanguage();
76
79 // For testing. 77 // For testing.
80 void set_context_for_testing(ContextualSearchContext* context) { 78 void set_context_for_testing(ContextualSearchContext* context) {
81 context_.reset(context); 79 context_.reset(context);
82 } 80 }
83 81
84 private: 82 private:
85 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, 83 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest,
86 SurroundingTextHighMaximum); 84 SurroundingTextHighMaximum);
87 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, 85 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest,
88 SurroundingTextLowMaximum); 86 SurroundingTextLowMaximum);
89 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, 87 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest,
90 SurroundingTextNoBeforeText); 88 SurroundingTextNoBeforeText);
91 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, 89 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest,
92 SurroundingTextNoAfterText); 90 SurroundingTextNoAfterText);
93 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, 91 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest,
94 ExtractMentionsStartEnd); 92 ExtractMentionsStartEnd);
95 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, 93 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest,
96 SurroundingTextForIcing); 94 SurroundingTextForIcing);
97 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, 95 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest,
98 SurroundingTextForIcingNegativeLimit); 96 SurroundingTextForIcingNegativeLimit);
99 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, 97 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest,
100 DecodeSearchTermsFromJsonResponse); 98 DecodeSearchTermFromJsonResponse);
101 99
102 // net::URLFetcherDelegate: 100 // net::URLFetcherDelegate:
103 void OnURLFetchComplete(const net::URLFetcher* source) override; 101 void OnURLFetchComplete(const net::URLFetcher* source) override;
104 102
105 // Builds the search term resolution request URL from the current context. 103 // Builds the search term resolution request URL from the current context.
106 GURL BuildRequestUrl(); 104 GURL BuildRequestUrl();
107 105
108 // Uses the TemplateURL service to construct a search term resolution URL from 106 // Uses the TemplateURL service to construct a search term resolution URL from
109 // the given parameters. 107 // the given parameters.
110 std::string GetSearchTermResolutionUrlString( 108 std::string GetSearchTermResolutionUrlString(
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 143
146 // Checks if we can send the URL for this user. Several conditions are checked 144 // Checks if we can send the URL for this user. Several conditions are checked
147 // to make sure it's OK to send the URL. These fall into two categories: 145 // to make sure it's OK to send the URL. These fall into two categories:
148 // 1) check if it's allowed by our policy, and 2) ensure that the user is 146 // 1) check if it's allowed by our policy, and 2) ensure that the user is
149 // already sending their URL browsing activity to Google. 147 // already sending their URL browsing activity to Google.
150 bool CanSendPageURL(const GURL& current_page_url, 148 bool CanSendPageURL(const GURL& current_page_url,
151 Profile* profile, 149 Profile* profile,
152 TemplateURLService* template_url_service); 150 TemplateURLService* template_url_service);
153 151
154 // Decodes the given json response string and extracts parameters. 152 // Decodes the given json response string and extracts parameters.
155 void DecodeSearchTermsFromJsonResponse(const std::string& response, 153 void DecodeSearchTermFromJsonResponse(const std::string& response,
156 std::string* search_term, 154 std::string* search_term,
157 std::string* display_text, 155 std::string* display_text,
158 std::string* alternate_term, 156 std::string* alternate_term,
159 std::string* prevent_preload, 157 std::string* prevent_preload,
160 int* mention_start, 158 int* mention_start,
161 int* mention_end); 159 int* mention_end,
160 std::string* context_language);
162 161
163 void ExtractMentionsStartEnd(const base::ListValue& mentions_list, 162 void ExtractMentionsStartEnd(const base::ListValue& mentions_list,
164 int* startResult, 163 int* startResult,
165 int* endResult); 164 int* endResult);
166 165
167 // Returns the surrounding size to use for the search term resolution 166 // Returns the surrounding size to use for the search term resolution
168 // request. 167 // request.
169 int GetSearchTermSurroundingSize(); 168 int GetSearchTermSurroundingSize();
170 169
171 // Returns the size of the surroundings to be sent to Icing. 170 // Returns the size of the surroundings to be sent to Icing.
(...skipping 30 matching lines...) Expand all
202 201
203 // The callback for notifications of surrounding text being available. 202 // The callback for notifications of surrounding text being available.
204 SurroundingTextCallback surrounding_callback_; 203 SurroundingTextCallback surrounding_callback_;
205 204
206 // The callback for notifications of Icing selection being available. 205 // The callback for notifications of Icing selection being available.
207 IcingCallback icing_callback_; 206 IcingCallback icing_callback_;
208 207
209 // Used to hold the context until an upcoming search term request is started. 208 // Used to hold the context until an upcoming search term request is started.
210 scoped_ptr<ContextualSearchContext> context_; 209 scoped_ptr<ContextualSearchContext> context_;
211 210
211 // Cached Target Language string, to determine when we need to trigger
212 // translation.
213 std::string target_language_;
214
212 DISALLOW_COPY_AND_ASSIGN(ContextualSearchDelegate); 215 DISALLOW_COPY_AND_ASSIGN(ContextualSearchDelegate);
213 }; 216 };
214 217
215 #endif // CHROME_BROWSER_ANDROID_CONTEXTUALSEARCH_CONTEXTUAL_SEARCH_DELEGATE_H_ 218 #endif // CHROME_BROWSER_ANDROID_CONTEXTUALSEARCH_CONTEXTUAL_SEARCH_DELEGATE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698