mattreynolds | 5afc0169 | 2016-08-19 22:09:56 | [diff] [blame] | 1 | // Copyright (c) 2016 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef COMPONENTS_OMNIBOX_BROWSER_PHYSICAL_WEB_PROVIDER_H_ |
| 6 | #define COMPONENTS_OMNIBOX_BROWSER_PHYSICAL_WEB_PROVIDER_H_ |
| 7 | |
mattreynolds | 1d38d116 | 2017-01-19 02:45:24 | [diff] [blame] | 8 | #include <vector> |
| 9 | |
mattreynolds | 2b530de | 2016-09-02 18:20:16 | [diff] [blame] | 10 | #include "base/macros.h" |
mattreynolds | a04fff0 | 2017-01-06 23:46:18 | [diff] [blame] | 11 | #include "components/bookmarks/browser/titled_url_match.h" |
mattreynolds | 5afc0169 | 2016-08-19 22:09:56 | [diff] [blame] | 12 | #include "components/omnibox/browser/autocomplete_input.h" |
| 13 | #include "components/omnibox/browser/autocomplete_provider.h" |
| 14 | |
| 15 | class AutocompleteProviderClient; |
mattreynolds | 2b530de | 2016-09-02 18:20:16 | [diff] [blame] | 16 | class HistoryURLProvider; |
mattreynolds | 5afc0169 | 2016-08-19 22:09:56 | [diff] [blame] | 17 | |
mattreynolds | 1d38d116 | 2017-01-19 02:45:24 | [diff] [blame] | 18 | namespace physical_web { |
| 19 | struct Metadata; |
| 20 | using MetadataList = std::vector<Metadata>; |
mattreynolds | 5afc0169 | 2016-08-19 22:09:56 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | class PhysicalWebProvider : public AutocompleteProvider { |
mattreynolds | 2b530de | 2016-09-02 18:20:16 | [diff] [blame] | 24 | public: |
mattreynolds | 08c7909 | 2016-09-14 22:00:08 | [diff] [blame] | 25 | // The maximum number of match results to provide. If the number of nearby |
| 26 | // URLs exceeds this limit, an overflow item is created. Tapping the overflow |
| 27 | // item navigates to a page with the full list of nearby URLs. The overflow |
| 28 | // item is counted as a match result for the purposes of the match limit. |
| 29 | // |
| 30 | // ex: With kPhysicalWebMaxMatches == 1, there should be at most one |
| 31 | // suggestion created by this provider. If there is a single nearby URL, then |
| 32 | // the suggestion will be for that URL. If there are multiple nearby URLs, the |
| 33 | // suggestion will be the overflow item which navigates to the WebUI when |
| 34 | // tapped. |
| 35 | static const size_t kPhysicalWebMaxMatches; |
| 36 | |
mattreynolds | 2b530de | 2016-09-02 18:20:16 | [diff] [blame] | 37 | static PhysicalWebProvider* Create(AutocompleteProviderClient* client, |
| 38 | HistoryURLProvider* history_url_provider); |
mattreynolds | 5afc0169 | 2016-08-19 22:09:56 | [diff] [blame] | 39 | |
| 40 | // AutocompleteProvider: |
| 41 | void Start(const AutocompleteInput& input, bool minimal_changes) override; |
| 42 | void Stop(bool clear_cached_results, bool due_to_user_inactivity) override; |
mattreynolds | e2e018a4 | 2016-11-04 18:42:45 | [diff] [blame] | 43 | void AddProviderInfo(ProvidersInfo* provider_info) const override; |
mattreynolds | 5afc0169 | 2016-08-19 22:09:56 | [diff] [blame] | 44 | |
mattreynolds | 2b530de | 2016-09-02 18:20:16 | [diff] [blame] | 45 | private: |
| 46 | PhysicalWebProvider(AutocompleteProviderClient* client, |
| 47 | HistoryURLProvider* history_url_provider); |
mattreynolds | 5afc0169 | 2016-08-19 22:09:56 | [diff] [blame] | 48 | ~PhysicalWebProvider() override; |
| 49 | |
mattreynolds | a04fff0 | 2017-01-06 23:46:18 | [diff] [blame] | 50 | // When the user has focused the omnibox but not yet entered any text (i.e., |
| 51 | // the Zero Suggest case), calling this method adds a separate match item to |
| 52 | // |matches_| for each nearby URL in |metadata_list|, up to the maximum number |
| 53 | // of matches allowed. If the total number of nearby URLs exceeds this limit, |
| 54 | // one match is used for an overflow item. |
| 55 | void ConstructZeroSuggestMatches( |
mattreynolds | 1d38d116 | 2017-01-19 02:45:24 | [diff] [blame] | 56 | std::unique_ptr<physical_web::MetadataList> metadata_list); |
mattreynolds | a04fff0 | 2017-01-06 23:46:18 | [diff] [blame] | 57 | |
| 58 | // When the user has entered text into the omnibox (i.e., the Query Suggest |
| 59 | // case), calling this method adds a separate match item to |matches_| for |
| 60 | // each nearby URL in |metadata_list| that matches all of the query terms in |
| 61 | // |input|, up to the maximum number of matches allowed. |
| 62 | void ConstructQuerySuggestMatches( |
mattreynolds | 1d38d116 | 2017-01-19 02:45:24 | [diff] [blame] | 63 | std::unique_ptr<physical_web::MetadataList> metadata_list, |
mattreynolds | a04fff0 | 2017-01-06 23:46:18 | [diff] [blame] | 64 | const AutocompleteInput& input); |
mattreynolds | 5afc0169 | 2016-08-19 22:09:56 | [diff] [blame] | 65 | |
| 66 | // Adds an overflow match item to |matches_| with a relevance score equal to |
| 67 | // |relevance| and a label indicating there are |additional_url_count| more |
mattreynolds | 08c7909 | 2016-09-14 22:00:08 | [diff] [blame] | 68 | // nearby URLs. The page |title| of one of the additional nearby URLs will be |
| 69 | // included in the label, truncating if necessary. Selecting the overflow item |
| 70 | // navigates to the Physical Web WebUI, which displays the full list of nearby |
| 71 | // URLs. |
| 72 | void AppendOverflowItem(int additional_url_count, |
| 73 | int relevance, |
| 74 | const base::string16& title); |
mattreynolds | 5afc0169 | 2016-08-19 22:09:56 | [diff] [blame] | 75 | |
| 76 | AutocompleteProviderClient* client_; |
mattreynolds | 2b530de | 2016-09-02 18:20:16 | [diff] [blame] | 77 | |
| 78 | // Used for efficiency when creating the verbatim match. Can be null. |
| 79 | HistoryURLProvider* history_url_provider_; |
| 80 | |
mattreynolds | e2e018a4 | 2016-11-04 18:42:45 | [diff] [blame] | 81 | // The number of nearby Physical Web URLs when the provider last constructed |
mattreynolds | 6b89f92 | 2017-02-23 20:53:29 | [diff] [blame] | 82 | // matches. Initialized to string::npos. |
mattreynolds | e2e018a4 | 2016-11-04 18:42:45 | [diff] [blame] | 83 | size_t nearby_url_count_; |
| 84 | |
mattreynolds | 6b89f92 | 2017-02-23 20:53:29 | [diff] [blame] | 85 | // The number of nearby Physical Web URLs when the omnibox input was last |
| 86 | // focused. Initialized to string::npos. |
| 87 | // This value is set when the omnibox is focused and recorded when the user |
| 88 | // selects an omnibox suggestion. If the value is still string::npos when the |
| 89 | // user makes a selection, it indicates the omnibox was never focused. |
| 90 | size_t nearby_url_count_at_focus_; |
| 91 | |
mattreynolds | a04fff0 | 2017-01-06 23:46:18 | [diff] [blame] | 92 | // If true, provide suggestions when the user has focused the omnibox but has |
| 93 | // not typed anything. |
| 94 | bool zero_suggest_enabled_; |
| 95 | |
| 96 | // If true, provide suggestions when the user has entered a query into the |
| 97 | // omnibox. |
| 98 | bool after_typing_enabled_; |
| 99 | |
| 100 | // The base relevance score for Physical Web URL suggestions when the user has |
| 101 | // not typed anything into the omnibox. |
| 102 | int zero_suggest_base_relevance_; |
| 103 | |
| 104 | // The base relevance score for Physical Web URL suggestions after the user |
| 105 | // has typed a query into the omnibox. |
| 106 | int after_typing_base_relevance_; |
| 107 | |
| 108 | // Set to true if at least one suggestion was created the last time the |
| 109 | // provider was started, even if the suggestion could not be displayed due to |
| 110 | // a field trial. |
| 111 | bool had_physical_web_suggestions_; |
| 112 | |
| 113 | // Set to true if at least one suggestion was created since the last time the |
| 114 | // omnibox was focused, even if the suggestion could not be displayed due to |
| 115 | // a field trial. |
| 116 | bool had_physical_web_suggestions_at_focus_or_later_; |
| 117 | |
mattreynolds | 2b530de | 2016-09-02 18:20:16 | [diff] [blame] | 118 | DISALLOW_COPY_AND_ASSIGN(PhysicalWebProvider); |
mattreynolds | 5afc0169 | 2016-08-19 22:09:56 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | #endif // COMPONENTS_OMNIBOX_BROWSER_PHYSICAL_WEB_PROVIDER_H_ |