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

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: Added a method to get all locales by checking IME. Put translation behind a flag. 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 #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 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
140 DCHECK(source == search_term_fetcher_.get()); 143 DCHECK(source == search_term_fetcher_.get());
141 int response_code = source->GetResponseCode(); 144 int response_code = source->GetResponseCode();
142 std::string search_term; 145 std::string search_term;
143 std::string display_text; 146 std::string display_text;
144 std::string alternate_term; 147 std::string alternate_term;
145 std::string prevent_preload; 148 std::string prevent_preload;
146 int mention_start = 0; 149 int mention_start = 0;
147 int mention_end = 0; 150 int mention_end = 0;
148 int start_adjust = 0; 151 int start_adjust = 0;
149 int end_adjust = 0; 152 int end_adjust = 0;
153 std::string context_language;
154 std::string target_language;
150 155
151 if (source->GetStatus().is_success() && response_code == 200) { 156 if (source->GetStatus().is_success() && response_code == 200) {
152 std::string response; 157 std::string response;
153 bool has_string_response = source->GetResponseAsString(&response); 158 bool has_string_response = source->GetResponseAsString(&response);
154 DCHECK(has_string_response); 159 DCHECK(has_string_response);
155 if (has_string_response) { 160 if (has_string_response) {
156 DecodeSearchTermsFromJsonResponse(response, &search_term, &display_text, 161 DecodeSearchTermFromJsonResponse(
157 &alternate_term, &prevent_preload, 162 response, &search_term, &display_text, &alternate_term,
158 &mention_start, &mention_end); 163 &prevent_preload, &mention_start, &mention_end, &context_language);
159 if (mention_start != 0 || mention_end != 0) { 164 if (mention_start != 0 || mention_end != 0) {
160 // Sanity check that our selection is non-zero and it is less than 165 // Sanity check that our selection is non-zero and it is less than
161 // 100 characters as that would make contextual search bar hide. 166 // 100 characters as that would make contextual search bar hide.
162 // We also check that there is at least one character overlap between 167 // We also check that there is at least one character overlap between
163 // the new and old selection. 168 // the new and old selection.
164 if (mention_start >= mention_end 169 if (mention_start >= mention_end
165 || (mention_end - mention_start) > kContextualSearchMaxSelection 170 || (mention_end - mention_start) > kContextualSearchMaxSelection
166 || mention_end <= context_->start_offset 171 || mention_end <= context_->start_offset
167 || mention_start >= context_->end_offset) { 172 || mention_start >= context_->end_offset) {
168 start_adjust = 0; 173 start_adjust = 0;
169 end_adjust = 0; 174 end_adjust = 0;
170 } else { 175 } else {
171 start_adjust = mention_start - context_->start_offset; 176 start_adjust = mention_start - context_->start_offset;
172 end_adjust = mention_end - context_->end_offset; 177 end_adjust = mention_end - context_->end_offset;
173 } 178 }
174 } 179 }
175 } 180 }
176 } 181 }
177 bool is_invalid = response_code == net::URLFetcher::RESPONSE_CODE_INVALID; 182 bool is_invalid = response_code == net::URLFetcher::RESPONSE_CODE_INVALID;
178 search_term_callback_.Run( 183 ResolvedSearchTerm resolved_search_term(
179 is_invalid, response_code, search_term, display_text, alternate_term, 184 is_invalid, response_code, search_term, display_text, alternate_term,
180 prevent_preload == kDoPreventPreloadValue, start_adjust, end_adjust); 185 prevent_preload == kDoPreventPreloadValue, start_adjust, end_adjust,
186 context_language);
187 search_term_callback_.Run(resolved_search_term);
181 188
182 // The ContextualSearchContext is consumed once the request has completed. 189 // The ContextualSearchContext is consumed once the request has completed.
183 context_.reset(); 190 context_.reset();
184 } 191 }
185 192
186 // TODO(jeremycho): Remove selected_text and base_page_url CGI parameters. 193 // TODO(jeremycho): Remove selected_text and base_page_url CGI parameters.
187 GURL ContextualSearchDelegate::BuildRequestUrl() { 194 GURL ContextualSearchDelegate::BuildRequestUrl() {
188 // TODO(jeremycho): Confirm this is the right way to handle TemplateURL fails. 195 // TODO(jeremycho): Confirm this is the right way to handle TemplateURL fails.
189 if (!template_url_service_ || 196 if (!template_url_service_ ||
190 !template_url_service_->GetDefaultSearchProvider()) { 197 !template_url_service_->GetDefaultSearchProvider()) {
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after
408 if (service == NULL || !service->CanSyncStart() || 415 if (service == NULL || !service->CanSyncStart() ||
409 !sync_prefs.GetPreferredDataTypes(syncer::UserTypes()) 416 !sync_prefs.GetPreferredDataTypes(syncer::UserTypes())
410 .Has(syncer::PROXY_TABS) || 417 .Has(syncer::PROXY_TABS) ||
411 !service->GetActiveDataTypes().Has(syncer::HISTORY_DELETE_DIRECTIVES)) { 418 !service->GetActiveDataTypes().Has(syncer::HISTORY_DELETE_DIRECTIVES)) {
412 return false; 419 return false;
413 } 420 }
414 421
415 return true; 422 return true;
416 } 423 }
417 424
425 // Gets the target language from the user's profile, and caches
426 // if for subsequent requests.
427 std::string ContextualSearchDelegate::GetTargetLanguage() {
428 if (target_language_.empty()) {
429 Profile* profile = ProfileManager::GetActiveUserProfile();
430 PrefService* prefs = profile->GetPrefs();
431 target_language_ = TranslateService::GetTargetLanguage(prefs);
432 DCHECK(!target_language_.empty());
433 }
434 return target_language_;
435 }
436
418 // Decodes the given response from the search term resolution request and sets 437 // Decodes the given response from the search term resolution request and sets
419 // the value of the given parameters. 438 // the value of the given parameters.
420 void ContextualSearchDelegate::DecodeSearchTermsFromJsonResponse( 439 void ContextualSearchDelegate::DecodeSearchTermFromJsonResponse(
421 const std::string& response, 440 const std::string& response,
422 std::string* search_term, 441 std::string* search_term,
423 std::string* display_text, 442 std::string* display_text,
424 std::string* alternate_term, 443 std::string* alternate_term,
425 std::string* prevent_preload, 444 std::string* prevent_preload,
426 int* mention_start, 445 int* mention_start,
427 int* mention_end) { 446 int* mention_end,
447 std::string* lang) {
428 bool contains_xssi_escape = response.find(kXssiEscape) == 0; 448 bool contains_xssi_escape = response.find(kXssiEscape) == 0;
429 const std::string& proper_json = 449 const std::string& proper_json =
430 contains_xssi_escape ? response.substr(strlen(kXssiEscape)) : response; 450 contains_xssi_escape ? response.substr(strlen(kXssiEscape)) : response;
431 JSONStringValueDeserializer deserializer(proper_json); 451 JSONStringValueDeserializer deserializer(proper_json);
432 scoped_ptr<base::Value> root(deserializer.Deserialize(NULL, NULL)); 452 scoped_ptr<base::Value> root(deserializer.Deserialize(NULL, NULL));
433 453
434 if (root.get() != NULL && root->IsType(base::Value::TYPE_DICTIONARY)) { 454 if (root.get() != NULL && root->IsType(base::Value::TYPE_DICTIONARY)) {
435 base::DictionaryValue* dict = 455 base::DictionaryValue* dict =
436 static_cast<base::DictionaryValue*>(root.get()); 456 static_cast<base::DictionaryValue*>(root.get());
437 dict->GetString(kContextualSearchPreventPreload, prevent_preload); 457 dict->GetString(kContextualSearchPreventPreload, prevent_preload);
438 dict->GetString(kContextualSearchResponseSearchTermParam, search_term); 458 dict->GetString(kContextualSearchResponseSearchTermParam, search_term);
459 dict->GetString(kContextualSearchResponseLanguageParam, lang);
439 // For the display_text, if not present fall back to the "search_term". 460 // For the display_text, if not present fall back to the "search_term".
440 if (!dict->GetString(kContextualSearchResponseDisplayTextParam, 461 if (!dict->GetString(kContextualSearchResponseDisplayTextParam,
441 display_text)) { 462 display_text)) {
442 *display_text = *search_term; 463 *display_text = *search_term;
443 } 464 }
444 // Extract mentions for selection expansion. 465 // Extract mentions for selection expansion.
445 base::ListValue* mentions_list; 466 base::ListValue* mentions_list;
446 dict->GetList(kContextualSearchMentions, &mentions_list); 467 dict->GetList(kContextualSearchMentions, &mentions_list);
447 if (mentions_list != NULL && mentions_list->GetSize() >= 2) 468 if (mentions_list != NULL && mentions_list->GetSize() >= 2)
448 ExtractMentionsStartEnd(*mentions_list, mention_start, mention_end); 469 ExtractMentionsStartEnd(*mentions_list, mention_start, mention_end);
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
524 end_offset -= trim; 545 end_offset -= trim;
525 } 546 }
526 if (result_text.length() > end_offset + padding_each_side_pinned) { 547 if (result_text.length() > end_offset + padding_each_side_pinned) {
527 // Trim the end. 548 // Trim the end.
528 result_text = result_text.substr(0, end_offset + padding_each_side_pinned); 549 result_text = result_text.substr(0, end_offset + padding_each_side_pinned);
529 } 550 }
530 *start = start_offset; 551 *start = start_offset;
531 *end = end_offset; 552 *end = end_offset;
532 return result_text; 553 return result_text;
533 } 554 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698