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

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

Issue 1354763003: [Contextual Search] Trigger the translation one-box. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 3 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 #include "chrome/browser/android/contextualsearch/contextual_search_delegate.h" 5 #include "chrome/browser/android/contextualsearch/contextual_search_delegate.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/base64.h" 9 #include "base/base64.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
11 #include "base/json/json_string_value_serializer.h" 11 #include "base/json/json_string_value_serializer.h"
12 #include "base/strings/string_number_conversions.h" 12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_util.h" 13 #include "base/strings/string_util.h"
14 #include "base/strings/utf_string_conversions.h" 14 #include "base/strings/utf_string_conversions.h"
15 #include "chrome/browser/android/contextualsearch/resolved_search_term.h"
15 #include "chrome/browser/android/proto/client_discourse_context.pb.h" 16 #include "chrome/browser/android/proto/client_discourse_context.pb.h"
16 #include "chrome/browser/profiles/profile.h" 17 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/profiles/profile_manager.h" 18 #include "chrome/browser/profiles/profile_manager.h"
18 #include "chrome/browser/sync/profile_sync_service.h" 19 #include "chrome/browser/sync/profile_sync_service.h"
19 #include "chrome/browser/sync/profile_sync_service_factory.h" 20 #include "chrome/browser/sync/profile_sync_service_factory.h"
21 #include "chrome/browser/translate/translate_service.h"
20 #include "components/search_engines/template_url_service.h" 22 #include "components/search_engines/template_url_service.h"
21 #include "components/variations/net/variations_http_header_provider.h" 23 #include "components/variations/net/variations_http_header_provider.h"
22 #include "components/variations/variations_associated_data.h" 24 #include "components/variations/variations_associated_data.h"
23 #include "content/public/browser/android/content_view_core.h" 25 #include "content/public/browser/android/content_view_core.h"
24 #include "content/public/browser/web_contents.h" 26 #include "content/public/browser/web_contents.h"
25 #include "net/base/escape.h" 27 #include "net/base/escape.h"
26 #include "net/url_request/url_fetcher.h" 28 #include "net/url_request/url_fetcher.h"
27 #include "url/gurl.h" 29 #include "url/gurl.h"
28 30
29 using content::ContentViewCore; 31 using content::ContentViewCore;
30 32
31 namespace { 33 namespace {
32 34
33 const char kContextualSearchFieldTrialName[] = "ContextualSearch"; 35 const char kContextualSearchFieldTrialName[] = "ContextualSearch";
34 const char kContextualSearchSurroundingSizeParamName[] = "surrounding_size"; 36 const char kContextualSearchSurroundingSizeParamName[] = "surrounding_size";
35 const char kContextualSearchIcingSurroundingSizeParamName[] = 37 const char kContextualSearchIcingSurroundingSizeParamName[] =
36 "icing_surrounding_size"; 38 "icing_surrounding_size";
37 const char kContextualSearchResolverURLParamName[] = "resolver_url"; 39 const char kContextualSearchResolverURLParamName[] = "resolver_url";
38 const char kContextualSearchDoNotSendURLParamName[] = "do_not_send_url"; 40 const char kContextualSearchDoNotSendURLParamName[] = "do_not_send_url";
39 const char kContextualSearchResponseDisplayTextParam[] = "display_text"; 41 const char kContextualSearchResponseDisplayTextParam[] = "display_text";
40 const char kContextualSearchResponseSelectedTextParam[] = "selected_text"; 42 const char kContextualSearchResponseSelectedTextParam[] = "selected_text";
41 const char kContextualSearchResponseSearchTermParam[] = "search_term"; 43 const char kContextualSearchResponseSearchTermParam[] = "search_term";
44 const char kContextualSearchResponseLanguageParam[] = "lang";
42 const char kContextualSearchResponseResolvedTermParam[] = "resolved_term"; 45 const char kContextualSearchResponseResolvedTermParam[] = "resolved_term";
43 const char kContextualSearchPreventPreload[] = "prevent_preload"; 46 const char kContextualSearchPreventPreload[] = "prevent_preload";
44 const char kContextualSearchMentions[] = "mentions"; 47 const char kContextualSearchMentions[] = "mentions";
45 const char kContextualSearchServerEndpoint[] = "_/contextualsearch?"; 48 const char kContextualSearchServerEndpoint[] = "_/contextualsearch?";
46 const int kContextualSearchRequestVersion = 2; 49 const int kContextualSearchRequestVersion = 2;
47 const char kContextualSearchResolverUrl[] = 50 const char kContextualSearchResolverUrl[] =
48 "contextual-search-resolver-url"; 51 "contextual-search-resolver-url";
49 // The default size of the content surrounding the selection to gather, allowing 52 // The default size of the content surrounding the selection to gather, allowing
50 // room for other parameters. 53 // room for other parameters.
51 const int kContextualSearchDefaultContentSize = 1536; 54 const int kContextualSearchDefaultContentSize = 1536;
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 DCHECK(source == search_term_fetcher_.get()); 144 DCHECK(source == search_term_fetcher_.get());
142 int response_code = source->GetResponseCode(); 145 int response_code = source->GetResponseCode();
143 std::string search_term; 146 std::string search_term;
144 std::string display_text; 147 std::string display_text;
145 std::string alternate_term; 148 std::string alternate_term;
146 std::string prevent_preload; 149 std::string prevent_preload;
147 int mention_start = 0; 150 int mention_start = 0;
148 int mention_end = 0; 151 int mention_end = 0;
149 int start_adjust = 0; 152 int start_adjust = 0;
150 int end_adjust = 0; 153 int end_adjust = 0;
154 std::string language;
155 std::string target_language;
151 156
152 if (source->GetStatus().is_success() && response_code == 200) { 157 if (source->GetStatus().is_success() && response_code == 200) {
153 std::string response; 158 std::string response;
154 bool has_string_response = source->GetResponseAsString(&response); 159 bool has_string_response = source->GetResponseAsString(&response);
155 DCHECK(has_string_response); 160 DCHECK(has_string_response);
156 if (has_string_response) { 161 if (has_string_response) {
157 DecodeSearchTermsFromJsonResponse(response, &search_term, &display_text, 162 DecodeSearchTermFromJsonResponse(response, &search_term, &display_text,
158 &alternate_term, &prevent_preload, 163 &alternate_term, &prevent_preload,
159 &mention_start, &mention_end); 164 &mention_start, &mention_end, &language);
160 if (mention_start != 0 || mention_end != 0) { 165 if (mention_start != 0 || mention_end != 0) {
161 // Sanity check that our selection is non-zero and it is less than 166 // Sanity check that our selection is non-zero and it is less than
162 // 100 characters as that would make contextual search bar hide. 167 // 100 characters as that would make contextual search bar hide.
163 // We also check that there is at least one character overlap between 168 // We also check that there is at least one character overlap between
164 // the new and old selection. 169 // the new and old selection.
165 if (mention_start >= mention_end 170 if (mention_start >= mention_end
166 || (mention_end - mention_start) > kContextualSearchMaxSelection 171 || (mention_end - mention_start) > kContextualSearchMaxSelection
167 || mention_end <= context_->start_offset 172 || mention_end <= context_->start_offset
168 || mention_start >= context_->end_offset) { 173 || mention_start >= context_->end_offset) {
169 start_adjust = 0; 174 start_adjust = 0;
170 end_adjust = 0; 175 end_adjust = 0;
171 } else { 176 } else {
172 start_adjust = mention_start - context_->start_offset; 177 start_adjust = mention_start - context_->start_offset;
173 end_adjust = mention_end - context_->end_offset; 178 end_adjust = mention_end - context_->end_offset;
174 } 179 }
175 } 180 }
176 } 181 }
177 } 182 }
178 bool is_invalid = response_code == net::URLFetcher::RESPONSE_CODE_INVALID; 183 bool is_invalid = response_code == net::URLFetcher::RESPONSE_CODE_INVALID;
179 search_term_callback_.Run( 184 ResolvedSearchTerm resolved_search_term(
180 is_invalid, response_code, search_term, display_text, alternate_term, 185 is_invalid, response_code, search_term, display_text, alternate_term,
181 prevent_preload == kDoPreventPreloadValue, start_adjust, end_adjust); 186 prevent_preload == kDoPreventPreloadValue, start_adjust, end_adjust,
187 language);
188 search_term_callback_.Run(resolved_search_term);
182 189
183 // The ContextualSearchContext is consumed once the request has completed. 190 // The ContextualSearchContext is consumed once the request has completed.
184 context_.reset(); 191 context_.reset();
185 } 192 }
186 193
187 // TODO(jeremycho): Remove selected_text and base_page_url CGI parameters. 194 // TODO(jeremycho): Remove selected_text and base_page_url CGI parameters.
188 GURL ContextualSearchDelegate::BuildRequestUrl() { 195 GURL ContextualSearchDelegate::BuildRequestUrl() {
189 // TODO(jeremycho): Confirm this is the right way to handle TemplateURL fails. 196 // TODO(jeremycho): Confirm this is the right way to handle TemplateURL fails.
190 if (!template_url_service_ || 197 if (!template_url_service_ ||
191 !template_url_service_->GetDefaultSearchProvider()) { 198 !template_url_service_->GetDefaultSearchProvider()) {
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after
417 if (service == NULL || !service->CanSyncStart() || 424 if (service == NULL || !service->CanSyncStart() ||
418 !sync_prefs.GetPreferredDataTypes(syncer::UserTypes()) 425 !sync_prefs.GetPreferredDataTypes(syncer::UserTypes())
419 .Has(syncer::PROXY_TABS) || 426 .Has(syncer::PROXY_TABS) ||
420 !service->GetActiveDataTypes().Has(syncer::HISTORY_DELETE_DIRECTIVES)) { 427 !service->GetActiveDataTypes().Has(syncer::HISTORY_DELETE_DIRECTIVES)) {
421 return false; 428 return false;
422 } 429 }
423 430
424 return true; 431 return true;
425 } 432 }
426 433
434 // Gets the target language from the user's profile, and caches
435 // if for subsequent requests.
436 std::string ContextualSearchDelegate::GetTargetLanguage() {
437 if (target_language_.empty()) {
438 Profile* profile = ProfileManager::GetActiveUserProfile();
439 PrefService* prefs = profile->GetPrefs();
440 target_language_ = TranslateService::GetTargetLanguage(prefs);
441 DCHECK(!target_language_.empty());
442 }
443 return target_language_;
pedro (no code reviews) 2015/09/23 18:33:55 We should probably get a list of languages, and no
Donn Denman 2015/10/09 22:08:23 Done.
444 }
445
427 // Decodes the given response from the search term resolution request and sets 446 // Decodes the given response from the search term resolution request and sets
428 // the value of the given parameters. 447 // the value of the given parameters.
429 void ContextualSearchDelegate::DecodeSearchTermsFromJsonResponse( 448 void ContextualSearchDelegate::DecodeSearchTermFromJsonResponse(
430 const std::string& response, 449 const std::string& response,
431 std::string* search_term, 450 std::string* search_term,
432 std::string* display_text, 451 std::string* display_text,
433 std::string* alternate_term, 452 std::string* alternate_term,
434 std::string* prevent_preload, 453 std::string* prevent_preload,
435 int* mention_start, 454 int* mention_start,
436 int* mention_end) { 455 int* mention_end,
456 std::string* lang) {
437 bool contains_xssi_escape = response.find(kXssiEscape) == 0; 457 bool contains_xssi_escape = response.find(kXssiEscape) == 0;
438 const std::string& proper_json = 458 const std::string& proper_json =
439 contains_xssi_escape ? response.substr(strlen(kXssiEscape)) : response; 459 contains_xssi_escape ? response.substr(strlen(kXssiEscape)) : response;
440 JSONStringValueDeserializer deserializer(proper_json); 460 JSONStringValueDeserializer deserializer(proper_json);
441 scoped_ptr<base::Value> root(deserializer.Deserialize(NULL, NULL)); 461 scoped_ptr<base::Value> root(deserializer.Deserialize(NULL, NULL));
442 462
443 if (root.get() != NULL && root->IsType(base::Value::TYPE_DICTIONARY)) { 463 if (root.get() != NULL && root->IsType(base::Value::TYPE_DICTIONARY)) {
444 base::DictionaryValue* dict = 464 base::DictionaryValue* dict =
445 static_cast<base::DictionaryValue*>(root.get()); 465 static_cast<base::DictionaryValue*>(root.get());
446 dict->GetString(kContextualSearchPreventPreload, prevent_preload); 466 dict->GetString(kContextualSearchPreventPreload, prevent_preload);
447 dict->GetString(kContextualSearchResponseSearchTermParam, search_term); 467 dict->GetString(kContextualSearchResponseSearchTermParam, search_term);
468 dict->GetString(kContextualSearchResponseLanguageParam, lang);
448 // For the display_text, if not present fall back to the "search_term". 469 // For the display_text, if not present fall back to the "search_term".
449 if (!dict->GetString(kContextualSearchResponseDisplayTextParam, 470 if (!dict->GetString(kContextualSearchResponseDisplayTextParam,
450 display_text)) { 471 display_text)) {
451 *display_text = *search_term; 472 *display_text = *search_term;
452 } 473 }
453 // Extract mentions for selection expansion. 474 // Extract mentions for selection expansion.
454 base::ListValue* mentions_list; 475 base::ListValue* mentions_list;
455 dict->GetList(kContextualSearchMentions, &mentions_list); 476 dict->GetList(kContextualSearchMentions, &mentions_list);
456 if (mentions_list != NULL && mentions_list->GetSize() >= 2) 477 if (mentions_list != NULL && mentions_list->GetSize() >= 2)
457 ExtractMentionsStartEnd(*mentions_list, mention_start, mention_end); 478 ExtractMentionsStartEnd(*mentions_list, mention_start, mention_end);
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
533 end_offset -= trim; 554 end_offset -= trim;
534 } 555 }
535 if (result_text.length() > end_offset + padding_each_side_pinned) { 556 if (result_text.length() > end_offset + padding_each_side_pinned) {
536 // Trim the end. 557 // Trim the end.
537 result_text = result_text.substr(0, end_offset + padding_each_side_pinned); 558 result_text = result_text.substr(0, end_offset + padding_each_side_pinned);
538 } 559 }
539 *start = start_offset; 560 *start = start_offset;
540 *end = end_offset; 561 *end = end_offset;
541 return result_text; 562 return result_text;
542 } 563 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698