| 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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 const char kContextualSearchServerEndpoint[] = "_/contextualsearch?"; | 48 const char kContextualSearchServerEndpoint[] = "_/contextualsearch?"; |
| 49 const int kContextualSearchRequestVersion = 2; | 49 const int kContextualSearchRequestVersion = 2; |
| 50 const int kContextualSearchMaxSelection = 100; | 50 const int kContextualSearchMaxSelection = 100; |
| 51 const char kXssiEscape[] = ")]}'\n"; | 51 const char kXssiEscape[] = ")]}'\n"; |
| 52 const char kDiscourseContextHeaderPrefix[] = "X-Additional-Discourse-Context: "; | 52 const char kDiscourseContextHeaderPrefix[] = "X-Additional-Discourse-Context: "; |
| 53 const char kDoPreventPreloadValue[] = "1"; | 53 const char kDoPreventPreloadValue[] = "1"; |
| 54 | 54 |
| 55 // The number of characters that should be shown after the selected expression. | 55 // The number of characters that should be shown after the selected expression. |
| 56 const int kSurroundingSizeForUI = 60; | 56 const int kSurroundingSizeForUI = 60; |
| 57 | 57 |
| 58 // Contextual Cards (aka Now on Tap) integration. |
| 59 const char kContextualSearchContextualCards[] = "contextual_cards"; |
| 60 const char kContextualSearchCards[] = "cards"; |
| 61 const char kContextualSearchSingleCard[] = "singleCard"; |
| 62 const char kContextualSearchSubTitle[] = "subtitle"; |
| 63 const char kContextualSearchThumbnailUri[] = "thumbnail.uri"; |
| 58 // The version of the Now on Tap API that we want to invoke. | 64 // The version of the Now on Tap API that we want to invoke. |
| 59 const int kNowOnTapVersion = 1; | 65 const int kNowOnTapVersion = 1; |
| 60 | 66 |
| 61 } // namespace | 67 } // namespace |
| 62 | 68 |
| 63 // URLFetcher ID, only used for tests: we only have one kind of fetcher. | 69 // URLFetcher ID, only used for tests: we only have one kind of fetcher. |
| 64 const int ContextualSearchDelegate::kContextualSearchURLFetcherID = 1; | 70 const int ContextualSearchDelegate::kContextualSearchURLFetcherID = 1; |
| 65 | 71 |
| 66 // Handles tasks for the ContextualSearchManager in a separable, testable way. | 72 // Handles tasks for the ContextualSearchManager in a separable, testable way. |
| 67 ContextualSearchDelegate::ContextualSearchDelegate( | 73 ContextualSearchDelegate::ContextualSearchDelegate( |
| (...skipping 382 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 450 std::string* thumbnail_url, | 456 std::string* thumbnail_url, |
| 451 std::string* caption) { | 457 std::string* caption) { |
| 452 bool contains_xssi_escape = | 458 bool contains_xssi_escape = |
| 453 base::StartsWith(response, kXssiEscape, base::CompareCase::SENSITIVE); | 459 base::StartsWith(response, kXssiEscape, base::CompareCase::SENSITIVE); |
| 454 const std::string& proper_json = | 460 const std::string& proper_json = |
| 455 contains_xssi_escape ? response.substr(sizeof(kXssiEscape) - 1) | 461 contains_xssi_escape ? response.substr(sizeof(kXssiEscape) - 1) |
| 456 : response; | 462 : response; |
| 457 JSONStringValueDeserializer deserializer(proper_json); | 463 JSONStringValueDeserializer deserializer(proper_json); |
| 458 std::unique_ptr<base::Value> root = | 464 std::unique_ptr<base::Value> root = |
| 459 deserializer.Deserialize(nullptr, nullptr); | 465 deserializer.Deserialize(nullptr, nullptr); |
| 460 std::unique_ptr<base::DictionaryValue> dict = | 466 const std::unique_ptr<base::DictionaryValue> dict = |
| 461 base::DictionaryValue::From(std::move(root)); | 467 base::DictionaryValue::From(std::move(root)); |
| 462 if (!dict) | 468 if (!dict) |
| 463 return; | 469 return; |
| 464 | 470 |
| 465 dict->GetString(kContextualSearchPreventPreload, prevent_preload); | 471 dict->GetString(kContextualSearchPreventPreload, prevent_preload); |
| 466 dict->GetString(kContextualSearchResponseSearchTermParam, search_term); | 472 dict->GetString(kContextualSearchResponseSearchTermParam, search_term); |
| 467 dict->GetString(kContextualSearchResponseLanguageParam, lang); | 473 dict->GetString(kContextualSearchResponseLanguageParam, lang); |
| 468 | 474 |
| 469 // For the display_text, if not present fall back to the "search_term". | 475 // For the display_text, if not present fall back to the "search_term". |
| 470 if (!dict->GetString(kContextualSearchResponseDisplayTextParam, | 476 if (!dict->GetString(kContextualSearchResponseDisplayTextParam, |
| (...skipping 18 matching lines...) Expand all Loading... |
| 489 *alternate_term = selected_text; | 495 *alternate_term = selected_text; |
| 490 } else { | 496 } else { |
| 491 std::string resolved_term; | 497 std::string resolved_term; |
| 492 dict->GetString(kContextualSearchResponseResolvedTermParam, &resolved_term); | 498 dict->GetString(kContextualSearchResponseResolvedTermParam, &resolved_term); |
| 493 if (resolved_term != *search_term) { | 499 if (resolved_term != *search_term) { |
| 494 *alternate_term = resolved_term; | 500 *alternate_term = resolved_term; |
| 495 } | 501 } |
| 496 } | 502 } |
| 497 | 503 |
| 498 if (field_trial_->IsNowOnTapBarIntegrationEnabled()) { | 504 if (field_trial_->IsNowOnTapBarIntegrationEnabled()) { |
| 499 // TODO(donnd): extract thumbnail_url and caption. | 505 DecodeContextualCardsResponse(*dict.get(), caption, thumbnail_url); |
| 500 } | 506 } |
| 501 } | 507 } |
| 502 | 508 |
| 503 // Extract the Start/End of the mentions in the surrounding text | 509 // Extract the Start/End of the mentions in the surrounding text |
| 504 // for selection-expansion. | 510 // for selection-expansion. |
| 505 void ContextualSearchDelegate::ExtractMentionsStartEnd( | 511 void ContextualSearchDelegate::ExtractMentionsStartEnd( |
| 506 const base::ListValue& mentions_list, | 512 const base::ListValue& mentions_list, |
| 507 int* startResult, | 513 int* startResult, |
| 508 int* endResult) { | 514 int* endResult) { |
| 509 int int_value; | 515 int int_value; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 533 end_offset -= trim; | 539 end_offset -= trim; |
| 534 } | 540 } |
| 535 if (result_text.length() > end_offset + padding_each_side_pinned) { | 541 if (result_text.length() > end_offset + padding_each_side_pinned) { |
| 536 // Trim the end. | 542 // Trim the end. |
| 537 result_text = result_text.substr(0, end_offset + padding_each_side_pinned); | 543 result_text = result_text.substr(0, end_offset + padding_each_side_pinned); |
| 538 } | 544 } |
| 539 *start = start_offset; | 545 *start = start_offset; |
| 540 *end = end_offset; | 546 *end = end_offset; |
| 541 return result_text; | 547 return result_text; |
| 542 } | 548 } |
| 549 |
| 550 void ContextualSearchDelegate::DecodeContextualCardsResponse( |
| 551 const base::DictionaryValue& dict, |
| 552 std::string* subtitle, |
| 553 std::string* thumbnail) { |
| 554 const base::DictionaryValue* contextual_cards_dict = nullptr; |
| 555 if (!dict.GetDictionary(kContextualSearchContextualCards, |
| 556 &contextual_cards_dict)) |
| 557 return; |
| 558 |
| 559 const base::ListValue* card_list = nullptr; |
| 560 if (!contextual_cards_dict->GetList(kContextualSearchCards, &card_list)) |
| 561 return; |
| 562 |
| 563 // TODO(donnd): remove these DCHECKS. They are not needed since we never |
| 564 // dereference if the values are missing. |
| 565 DCHECK(card_list); |
| 566 for (const auto& card : *card_list) { |
| 567 const base::DictionaryValue* card_dict = nullptr; |
| 568 if (!card->GetAsDictionary(&card_dict)) |
| 569 continue; |
| 570 |
| 571 DCHECK(card_dict); |
| 572 const base::DictionaryValue* single_card = nullptr; |
| 573 if (!card_dict->GetDictionary(kContextualSearchSingleCard, &single_card)) |
| 574 continue; |
| 575 |
| 576 DCHECK(single_card); |
| 577 single_card->GetString(kContextualSearchSubTitle, subtitle); |
| 578 single_card->GetString(kContextualSearchThumbnailUri, thumbnail); |
| 579 return; |
| 580 } |
| 581 } |
| OLD | NEW |