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 27 matching lines...) Expand all Loading... | |
38 namespace { | 38 namespace { |
39 | 39 |
40 const char kContextualSearchResponseDisplayTextParam[] = "display_text"; | 40 const char kContextualSearchResponseDisplayTextParam[] = "display_text"; |
41 const char kContextualSearchResponseSelectedTextParam[] = "selected_text"; | 41 const char kContextualSearchResponseSelectedTextParam[] = "selected_text"; |
42 const char kContextualSearchResponseSearchTermParam[] = "search_term"; | 42 const char kContextualSearchResponseSearchTermParam[] = "search_term"; |
43 const char kContextualSearchResponseLanguageParam[] = "lang"; | 43 const char kContextualSearchResponseLanguageParam[] = "lang"; |
44 const char kContextualSearchResponseMidParam[] = "mid"; | 44 const char kContextualSearchResponseMidParam[] = "mid"; |
45 const char kContextualSearchResponseResolvedTermParam[] = "resolved_term"; | 45 const char kContextualSearchResponseResolvedTermParam[] = "resolved_term"; |
46 const char kContextualSearchPreventPreload[] = "prevent_preload"; | 46 const char kContextualSearchPreventPreload[] = "prevent_preload"; |
47 const char kContextualSearchMentions[] = "mentions"; | 47 const char kContextualSearchMentions[] = "mentions"; |
48 const char kContextualSearchCaption[] = "caption"; | |
49 const char kContextualSearchThumbnail[] = "thumbnail"; | |
48 const char kContextualSearchServerEndpoint[] = "_/contextualsearch?"; | 50 const char kContextualSearchServerEndpoint[] = "_/contextualsearch?"; |
49 const int kContextualSearchRequestVersion = 2; | 51 const int kContextualSearchRequestVersion = 2; |
50 const int kContextualSearchMaxSelection = 100; | 52 const int kContextualSearchMaxSelection = 100; |
51 const char kXssiEscape[] = ")]}'\n"; | 53 const char kXssiEscape[] = ")]}'\n"; |
52 const char kDiscourseContextHeaderPrefix[] = "X-Additional-Discourse-Context: "; | 54 const char kDiscourseContextHeaderPrefix[] = "X-Additional-Discourse-Context: "; |
53 const char kDoPreventPreloadValue[] = "1"; | 55 const char kDoPreventPreloadValue[] = "1"; |
54 | 56 |
55 // The number of characters that should be shown after the selected expression. | 57 // The number of characters that should be shown after the selected expression. |
56 const int kSurroundingSizeForUI = 60; | 58 const int kSurroundingSizeForUI = 60; |
57 | 59 |
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"; | |
64 // The version of the Now on Tap API that we want to invoke. | 60 // The version of the Now on Tap API that we want to invoke. |
65 const int kNowOnTapVersion = 1; | 61 const int kNowOnTapVersion = 1; |
66 | 62 |
67 } // namespace | 63 } // namespace |
68 | 64 |
69 // URLFetcher ID, only used for tests: we only have one kind of fetcher. | 65 // URLFetcher ID, only used for tests: we only have one kind of fetcher. |
70 const int ContextualSearchDelegate::kContextualSearchURLFetcherID = 1; | 66 const int ContextualSearchDelegate::kContextualSearchURLFetcherID = 1; |
71 | 67 |
72 // Handles tasks for the ContextualSearchManager in a separable, testable way. | 68 // Handles tasks for the ContextualSearchManager in a separable, testable way. |
73 ContextualSearchDelegate::ContextualSearchDelegate( | 69 ContextualSearchDelegate::ContextualSearchDelegate( |
(...skipping 421 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
495 *alternate_term = selected_text; | 491 *alternate_term = selected_text; |
496 } else { | 492 } else { |
497 std::string resolved_term; | 493 std::string resolved_term; |
498 dict->GetString(kContextualSearchResponseResolvedTermParam, &resolved_term); | 494 dict->GetString(kContextualSearchResponseResolvedTermParam, &resolved_term); |
499 if (resolved_term != *search_term) { | 495 if (resolved_term != *search_term) { |
500 *alternate_term = resolved_term; | 496 *alternate_term = resolved_term; |
501 } | 497 } |
502 } | 498 } |
503 | 499 |
504 if (field_trial_->IsNowOnTapBarIntegrationEnabled()) { | 500 if (field_trial_->IsNowOnTapBarIntegrationEnabled()) { |
505 DecodeContextualCardsResponse(*dict.get(), caption, thumbnail_url); | 501 // Get the basic Bar data for Now on Tap integration directly from the root. |
Theresa
2016/09/22 20:54:02
nit: s/Now on Tap/Contextual Cards
I plan to go t
| |
502 dict->GetString(kContextualSearchCaption, caption); | |
503 dict->GetString(kContextualSearchThumbnail, thumbnail_url); | |
506 } | 504 } |
507 } | 505 } |
508 | 506 |
509 // Extract the Start/End of the mentions in the surrounding text | 507 // Extract the Start/End of the mentions in the surrounding text |
510 // for selection-expansion. | 508 // for selection-expansion. |
511 void ContextualSearchDelegate::ExtractMentionsStartEnd( | 509 void ContextualSearchDelegate::ExtractMentionsStartEnd( |
512 const base::ListValue& mentions_list, | 510 const base::ListValue& mentions_list, |
513 int* startResult, | 511 int* startResult, |
514 int* endResult) { | 512 int* endResult) { |
515 int int_value; | 513 int int_value; |
(...skipping 23 matching lines...) Expand all Loading... | |
539 end_offset -= trim; | 537 end_offset -= trim; |
540 } | 538 } |
541 if (result_text.length() > end_offset + padding_each_side_pinned) { | 539 if (result_text.length() > end_offset + padding_each_side_pinned) { |
542 // Trim the end. | 540 // Trim the end. |
543 result_text = result_text.substr(0, end_offset + padding_each_side_pinned); | 541 result_text = result_text.substr(0, end_offset + padding_each_side_pinned); |
544 } | 542 } |
545 *start = start_offset; | 543 *start = start_offset; |
546 *end = end_offset; | 544 *end = end_offset; |
547 return result_text; | 545 return result_text; |
548 } | 546 } |
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 |