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 #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" |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 std::string search_term; | 157 std::string search_term; |
158 std::string display_text; | 158 std::string display_text; |
159 std::string alternate_term; | 159 std::string alternate_term; |
160 std::string mid; | 160 std::string mid; |
161 std::string prevent_preload; | 161 std::string prevent_preload; |
162 int mention_start = 0; | 162 int mention_start = 0; |
163 int mention_end = 0; | 163 int mention_end = 0; |
164 int start_adjust = 0; | 164 int start_adjust = 0; |
165 int end_adjust = 0; | 165 int end_adjust = 0; |
166 std::string context_language; | 166 std::string context_language; |
| 167 std::string thumbnail_url = ""; |
167 | 168 |
168 DecodeSearchTermFromJsonResponse( | 169 DecodeSearchTermFromJsonResponse( |
169 json_string, &search_term, &display_text, &alternate_term, &mid, | 170 json_string, &search_term, &display_text, &alternate_term, &mid, |
170 &prevent_preload, &mention_start, &mention_end, &context_language); | 171 &prevent_preload, &mention_start, &mention_end, &context_language, |
| 172 &thumbnail_url); |
171 if (mention_start != 0 || mention_end != 0) { | 173 if (mention_start != 0 || mention_end != 0) { |
172 // Sanity check that our selection is non-zero and it is less than | 174 // Sanity check that our selection is non-zero and it is less than |
173 // 100 characters as that would make contextual search bar hide. | 175 // 100 characters as that would make contextual search bar hide. |
174 // We also check that there is at least one character overlap between | 176 // We also check that there is at least one character overlap between |
175 // the new and old selection. | 177 // the new and old selection. |
176 if (mention_start >= mention_end || | 178 if (mention_start >= mention_end || |
177 (mention_end - mention_start) > kContextualSearchMaxSelection || | 179 (mention_end - mention_start) > kContextualSearchMaxSelection || |
178 mention_end <= context_->start_offset || | 180 mention_end <= context_->start_offset || |
179 mention_start >= context_->end_offset) { | 181 mention_start >= context_->end_offset) { |
180 start_adjust = 0; | 182 start_adjust = 0; |
181 end_adjust = 0; | 183 end_adjust = 0; |
182 } else { | 184 } else { |
183 start_adjust = mention_start - context_->start_offset; | 185 start_adjust = mention_start - context_->start_offset; |
184 end_adjust = mention_end - context_->end_offset; | 186 end_adjust = mention_end - context_->end_offset; |
185 } | 187 } |
186 } | 188 } |
187 bool is_invalid = response_code == net::URLFetcher::RESPONSE_CODE_INVALID; | 189 bool is_invalid = response_code == net::URLFetcher::RESPONSE_CODE_INVALID; |
188 return std::unique_ptr<ResolvedSearchTerm>(new ResolvedSearchTerm( | 190 return std::unique_ptr<ResolvedSearchTerm>(new ResolvedSearchTerm( |
189 is_invalid, response_code, search_term, display_text, alternate_term, mid, | 191 is_invalid, response_code, search_term, display_text, alternate_term, mid, |
190 prevent_preload == kDoPreventPreloadValue, start_adjust, end_adjust, | 192 prevent_preload == kDoPreventPreloadValue, start_adjust, end_adjust, |
191 context_language)); | 193 context_language, thumbnail_url)); |
192 } | 194 } |
193 | 195 |
194 std::string ContextualSearchDelegate::BuildRequestUrl(std::string selection) { | 196 std::string ContextualSearchDelegate::BuildRequestUrl(std::string selection) { |
195 // TODO(donnd): Confirm this is the right way to handle TemplateURL fails. | 197 // TODO(donnd): Confirm this is the right way to handle TemplateURL fails. |
196 if (!template_url_service_ || | 198 if (!template_url_service_ || |
197 !template_url_service_->GetDefaultSearchProvider()) { | 199 !template_url_service_->GetDefaultSearchProvider()) { |
198 return std::string(); | 200 return std::string(); |
199 } | 201 } |
200 | 202 |
201 std::string selected_text(net::EscapeQueryParamValue(selection, true)); | 203 std::string selected_text(net::EscapeQueryParamValue(selection, true)); |
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
429 // the value of the given parameters. | 431 // the value of the given parameters. |
430 void ContextualSearchDelegate::DecodeSearchTermFromJsonResponse( | 432 void ContextualSearchDelegate::DecodeSearchTermFromJsonResponse( |
431 const std::string& response, | 433 const std::string& response, |
432 std::string* search_term, | 434 std::string* search_term, |
433 std::string* display_text, | 435 std::string* display_text, |
434 std::string* alternate_term, | 436 std::string* alternate_term, |
435 std::string* mid, | 437 std::string* mid, |
436 std::string* prevent_preload, | 438 std::string* prevent_preload, |
437 int* mention_start, | 439 int* mention_start, |
438 int* mention_end, | 440 int* mention_end, |
439 std::string* lang) { | 441 std::string* lang, |
| 442 std::string* thumbnail_url) { |
440 bool contains_xssi_escape = | 443 bool contains_xssi_escape = |
441 base::StartsWith(response, kXssiEscape, base::CompareCase::SENSITIVE); | 444 base::StartsWith(response, kXssiEscape, base::CompareCase::SENSITIVE); |
442 const std::string& proper_json = | 445 const std::string& proper_json = |
443 contains_xssi_escape ? response.substr(sizeof(kXssiEscape) - 1) | 446 contains_xssi_escape ? response.substr(sizeof(kXssiEscape) - 1) |
444 : response; | 447 : response; |
445 JSONStringValueDeserializer deserializer(proper_json); | 448 JSONStringValueDeserializer deserializer(proper_json); |
446 std::unique_ptr<base::Value> root = | 449 std::unique_ptr<base::Value> root = |
447 deserializer.Deserialize(nullptr, nullptr); | 450 deserializer.Deserialize(nullptr, nullptr); |
448 std::unique_ptr<base::DictionaryValue> dict = | 451 std::unique_ptr<base::DictionaryValue> dict = |
449 base::DictionaryValue::From(std::move(root)); | 452 base::DictionaryValue::From(std::move(root)); |
(...skipping 25 matching lines...) Expand all Loading... |
475 dict->GetString(kContextualSearchResponseSelectedTextParam, &selected_text); | 478 dict->GetString(kContextualSearchResponseSelectedTextParam, &selected_text); |
476 if (selected_text != *search_term) { | 479 if (selected_text != *search_term) { |
477 *alternate_term = selected_text; | 480 *alternate_term = selected_text; |
478 } else { | 481 } else { |
479 std::string resolved_term; | 482 std::string resolved_term; |
480 dict->GetString(kContextualSearchResponseResolvedTermParam, &resolved_term); | 483 dict->GetString(kContextualSearchResponseResolvedTermParam, &resolved_term); |
481 if (resolved_term != *search_term) { | 484 if (resolved_term != *search_term) { |
482 *alternate_term = resolved_term; | 485 *alternate_term = resolved_term; |
483 } | 486 } |
484 } | 487 } |
| 488 |
| 489 // TODO(donnd): extract thumbnail_url. Also extract caption and pipe through. |
485 } | 490 } |
486 | 491 |
487 // Extract the Start/End of the mentions in the surrounding text | 492 // Extract the Start/End of the mentions in the surrounding text |
488 // for selection-expansion. | 493 // for selection-expansion. |
489 void ContextualSearchDelegate::ExtractMentionsStartEnd( | 494 void ContextualSearchDelegate::ExtractMentionsStartEnd( |
490 const base::ListValue& mentions_list, | 495 const base::ListValue& mentions_list, |
491 int* startResult, | 496 int* startResult, |
492 int* endResult) { | 497 int* endResult) { |
493 int int_value; | 498 int int_value; |
494 if (mentions_list.GetInteger(0, &int_value)) | 499 if (mentions_list.GetInteger(0, &int_value)) |
(...skipping 22 matching lines...) Expand all Loading... |
517 end_offset -= trim; | 522 end_offset -= trim; |
518 } | 523 } |
519 if (result_text.length() > end_offset + padding_each_side_pinned) { | 524 if (result_text.length() > end_offset + padding_each_side_pinned) { |
520 // Trim the end. | 525 // Trim the end. |
521 result_text = result_text.substr(0, end_offset + padding_each_side_pinned); | 526 result_text = result_text.substr(0, end_offset + padding_each_side_pinned); |
522 } | 527 } |
523 *start = start_offset; | 528 *start = start_offset; |
524 *end = end_offset; | 529 *end = end_offset; |
525 return result_text; | 530 return result_text; |
526 } | 531 } |
OLD | NEW |