1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_ASH_APP_LIST_SEARCH_OS_SETTINGS_PROVIDER_H_
#define CHROME_BROWSER_ASH_APP_LIST_SEARCH_OS_SETTINGS_PROVIDER_H_
#include <string>
#include <vector>
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/scoped_observation.h"
#include "base/time/time.h"
#include "chrome/browser/apps/app_service/app_service_proxy_forward.h"
#include "chrome/browser/ash/app_list/search/chrome_search_result.h"
#include "chrome/browser/ash/app_list/search/search_provider.h"
#include "chrome/browser/ui/webui/ash/settings/search/mojom/search.mojom.h"
#include "components/services/app_service/public/cpp/app_registry_cache.h"
#include "components/services/app_service/public/cpp/icon_types.h"
#include "components/session_manager/core/session_manager.h"
#include "components/session_manager/core/session_manager_observer.h"
#include "mojo/public/cpp/bindings/receiver.h"
class Profile;
namespace ash::settings {
class Hierarchy;
class SearchHandler;
} // namespace ash::settings
namespace ui {
class ImageModel;
} // namespace ui
namespace app_list {
// Search results for OS settings.
class OsSettingsResult : public ChromeSearchResult {
public:
OsSettingsResult(Profile* profile,
const ash::settings::mojom::SearchResultPtr& result,
double relevance_score,
const ui::ImageModel& icon,
const std::u16string& query);
~OsSettingsResult() override;
OsSettingsResult(const OsSettingsResult&) = delete;
OsSettingsResult& operator=(const OsSettingsResult&) = delete;
// ChromeSearchResult:
void Open(int event_flags) override;
private:
raw_ptr<Profile> profile_;
const std::string url_path_;
};
// Provider results for OS settings based on a search query. No results are
// provided for zero-state.
class OsSettingsProvider : public SearchProvider,
public apps::AppRegistryCache::Observer,
public ash::settings::mojom::SearchResultsObserver,
public session_manager::SessionManagerObserver {
public:
explicit OsSettingsProvider(Profile* profile);
~OsSettingsProvider() override;
OsSettingsProvider(const OsSettingsProvider&) = delete;
OsSettingsProvider& operator=(const OsSettingsProvider&) = delete;
// Initialize the provider. It should be called when:
// 1. User session start up tasks has completed.
// 2. User session start up tasks has not completed, but user has start to
// search in launcher.
// 3. In tests with fake search handler and hierarchy provided.
void MaybeInitialize(
ash::settings::SearchHandler* fake_search_handler = nullptr,
const ash::settings::Hierarchy* fake_hierarchy = nullptr);
// SearchProvider:
void Start(const std::u16string& query) override;
void StopQuery() override;
ash::AppListSearchResultType ResultType() const override;
// apps::AppRegistryCache::Observer:
void OnAppUpdate(const apps::AppUpdate& update) override;
void OnAppRegistryCacheWillBeDestroyed(
apps::AppRegistryCache* cache) override;
// mojom::SearchResultsObserver:
void OnSearchResultsChanged() override;
// session_manager::SessionManagerObserver:
void OnUserSessionStartUpTaskCompleted() override;
private:
void OnSearchReturned(
const std::u16string& query,
const base::TimeTicks& start_time,
std::vector<ash::settings::mojom::SearchResultPtr> results);
// Given a vector of results from the SearchHandler, filters them down to a
// display-ready vector. It:
// - returns at most |kMaxShownResults| results
// - removes results with duplicate IDs
// - removes results with relevance score below |min_score_|.
// - removes results matching alternate text unless they meet extra
// requirements.
//
// The SearchHandler's vector is ranked high-to-low with this logic:
// - compares SearchResultDefaultRank,
// - if equal, compares relevance scores
// - if equal, compares SearchResultType, favoring sections over subpages over
// settings
// - if equal, picks arbitrarily
//
// So simply iterating down the vector while being careful about duplicates
// and checking for alternate matches is enough.
std::vector<ash::settings::mojom::SearchResultPtr> FilterResults(
const std::u16string& query,
const std::vector<ash::settings::mojom::SearchResultPtr>& results,
const ash::settings::Hierarchy* hierarchy);
void OnLoadIcon(bool is_from_constructor, apps::IconValuePtr icon_value);
// Scoring and filtering parameters.
bool accept_alternate_matches_ = false;
size_t min_query_length_ = 4u;
size_t min_query_length_for_alternates_ = 4u;
float min_score_ = 0.4f;
float min_score_for_alternates_ = 0.4f;
bool has_initialized = false;
const raw_ptr<Profile> profile_;
raw_ptr<ash::settings::SearchHandler> search_handler_ = nullptr;
raw_ptr<const ash::settings::Hierarchy> hierarchy_ = nullptr;
ui::ImageModel icon_;
// Last query. It is reset when view is closed.
std::u16string last_query_;
mojo::Receiver<ash::settings::mojom::SearchResultsObserver>
search_results_observer_receiver_{this};
base::ScopedObservation<apps::AppRegistryCache,
apps::AppRegistryCache::Observer>
app_registry_cache_observer_{this};
base::ScopedObservation<session_manager::SessionManager,
session_manager::SessionManagerObserver>
session_manager_observation_{this};
base::WeakPtrFactory<OsSettingsProvider> weak_factory_{this};
};
} // namespace app_list
#endif // CHROME_BROWSER_ASH_APP_LIST_SEARCH_OS_SETTINGS_PROVIDER_H_
|