Commit 9ee7bbea authored by Kyle Horimoto's avatar Kyle Horimoto Committed by Commit Bot

[CrOS Settings] Add SearchHandler interface and implementation.

This CL:
(1) Adds a SearchHandler Mojo interface, then implements it via a
    KeyedService.
(2) Hooks up this interface to the OS settings UI.

After this CL, it's possible for the settings JS code to make direct
calls into the service.

Bug: 1059099
Change-Id: I51fbdabdcd227a6004e4319038383cdf4ada9519
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2103523
Commit-Queue: Kyle Horimoto <khorimoto@chromium.org>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Reviewed-by: default avatarJames Cook <jamescook@chromium.org>
Reviewed-by: default avatardpapad <dpapad@chromium.org>
Auto-Submit: Kyle Horimoto <khorimoto@chromium.org>
Cr-Commit-Position: refs/heads/master@{#752731}
parent 4a22af3d
...@@ -447,6 +447,8 @@ ...@@ -447,6 +447,8 @@
<include name="IDR_ADD_SUPERVISION_MOJOM_LITE_JS" file="${root_gen_dir}\chrome\browser\ui\webui\chromeos\add_supervision\add_supervision.mojom-lite.js" compress="gzip" use_base_dir="false" type="BINDATA" /> <include name="IDR_ADD_SUPERVISION_MOJOM_LITE_JS" file="${root_gen_dir}\chrome\browser\ui\webui\chromeos\add_supervision\add_supervision.mojom-lite.js" compress="gzip" use_base_dir="false" type="BINDATA" />
</if> </if>
<if expr="chromeos"> <if expr="chromeos">
<include name="IDR_OS_SETTINGS_SEARCH_MOJOM_LITE_JS" file="${root_gen_dir}\chrome\browser\ui\webui\settings\chromeos\search\search.mojom-lite.js" compress="gzip" use_base_dir="false" type="BINDATA" />
<include name="IDR_OS_SETTINGS_SEARCH_RESULT_ICON_MOJOM_LITE_JS" file="${root_gen_dir}\chrome\browser\ui\webui\settings\chromeos\search\search_result_icon.mojom-lite.js" compress="gzip" use_base_dir="false" type="BINDATA" />
<include name="IDR_OS_SETTINGS_USER_ACTION_RECORDER_MOJOM_LITE_JS" file="${root_gen_dir}\chrome\browser\ui\webui\settings\chromeos\search\user_action_recorder.mojom-lite.js" compress="gzip" use_base_dir="false" type="BINDATA" /> <include name="IDR_OS_SETTINGS_USER_ACTION_RECORDER_MOJOM_LITE_JS" file="${root_gen_dir}\chrome\browser\ui\webui\settings\chromeos\search\user_action_recorder.mojom-lite.js" compress="gzip" use_base_dir="false" type="BINDATA" />
</if> </if>
<if expr="chromeos"> <if expr="chromeos">
......
...@@ -130,6 +130,7 @@ ...@@ -130,6 +130,7 @@
#include "chrome/browser/ui/webui/chromeos/multidevice_setup/multidevice_setup_dialog.h" #include "chrome/browser/ui/webui/chromeos/multidevice_setup/multidevice_setup_dialog.h"
#include "chrome/browser/ui/webui/chromeos/network_ui.h" #include "chrome/browser/ui/webui/chromeos/network_ui.h"
#include "chrome/browser/ui/webui/settings/chromeos/os_settings_ui.h" #include "chrome/browser/ui/webui/settings/chromeos/os_settings_ui.h"
#include "chrome/browser/ui/webui/settings/chromeos/search/search.mojom.h"
#include "chrome/browser/ui/webui/settings/chromeos/search/user_action_recorder.mojom.h" #include "chrome/browser/ui/webui/settings/chromeos/search/user_action_recorder.mojom.h"
#include "chromeos/components/help_app_ui/help_app_ui.h" #include "chromeos/components/help_app_ui/help_app_ui.h"
#include "chromeos/components/help_app_ui/help_app_ui.mojom.h" #include "chromeos/components/help_app_ui/help_app_ui.mojom.h"
...@@ -486,6 +487,10 @@ void PopulateChromeWebUIFrameBinders( ...@@ -486,6 +487,10 @@ void PopulateChromeWebUIFrameBinders(
chromeos::settings::mojom::UserActionRecorder, chromeos::settings::mojom::UserActionRecorder,
chromeos::settings::OSSettingsUI>(map); chromeos::settings::OSSettingsUI>(map);
RegisterWebUIControllerInterfaceBinder<
chromeos::settings::mojom::SearchHandler,
chromeos::settings::OSSettingsUI>(map);
RegisterWebUIControllerInterfaceBinder< RegisterWebUIControllerInterfaceBinder<
chromeos::cellular_setup::mojom::CellularSetup, chromeos::cellular_setup::mojom::CellularSetup,
chromeos::cellular_setup::CellularSetupDialogUI>(map); chromeos::cellular_setup::CellularSetupDialogUI>(map);
......
...@@ -3175,6 +3175,7 @@ source_set("unit_tests") { ...@@ -3175,6 +3175,7 @@ source_set("unit_tests") {
"../ui/webui/settings/chromeos/internet_handler_unittest.cc", "../ui/webui/settings/chromeos/internet_handler_unittest.cc",
"../ui/webui/settings/chromeos/multidevice_handler_unittest.cc", "../ui/webui/settings/chromeos/multidevice_handler_unittest.cc",
"../ui/webui/settings/chromeos/os_settings_localized_strings_provider_unittest.cc", "../ui/webui/settings/chromeos/os_settings_localized_strings_provider_unittest.cc",
"../ui/webui/settings/chromeos/search/search_handler_unittest.cc",
"../ui/webui/settings/chromeos/search/settings_user_action_tracker_unittest.cc", "../ui/webui/settings/chromeos/search/settings_user_action_tracker_unittest.cc",
] ]
if (use_cups) { if (use_cups) {
......
...@@ -37,8 +37,11 @@ if (optimize_webui) { ...@@ -37,8 +37,11 @@ if (optimize_webui) {
"chrome://os-settings/app-management/image_info.mojom-lite.js", "chrome://os-settings/app-management/image_info.mojom-lite.js",
"chrome://os-settings/app-management/image.mojom-lite.js", "chrome://os-settings/app-management/image.mojom-lite.js",
"chrome://os-settings/app-management/types.mojom-lite.js", "chrome://os-settings/app-management/types.mojom-lite.js",
"chrome://os-settings/search/search.mojom-lite.js",
"chrome://os-settings/search/search_result_icon.mojom-lite.js",
"chrome://os-settings/search/user_action_recorder.mojom-lite.js", "chrome://os-settings/search/user_action_recorder.mojom-lite.js",
"chrome://resources/mojo/mojo/public/mojom/base/big_buffer.mojom.html", "chrome://resources/mojo/mojo/public/mojom/base/big_buffer.mojom.html",
"chrome://resources/mojo/mojo/public/mojom/base/string16.mojom.html",
"chrome://resources/mojo/mojo/public/mojom/base/time.mojom.html", "chrome://resources/mojo/mojo/public/mojom/base/time.mojom.html",
] ]
......
...@@ -2034,6 +2034,10 @@ jumbo_static_library("ui") { ...@@ -2034,6 +2034,10 @@ jumbo_static_library("ui") {
"webui/settings/chromeos/quick_unlock_handler.cc", "webui/settings/chromeos/quick_unlock_handler.cc",
"webui/settings/chromeos/quick_unlock_handler.h", "webui/settings/chromeos/quick_unlock_handler.h",
"webui/settings/chromeos/search/search_concept.h", "webui/settings/chromeos/search/search_concept.h",
"webui/settings/chromeos/search/search_handler.cc",
"webui/settings/chromeos/search/search_handler.h",
"webui/settings/chromeos/search/search_handler_factory.cc",
"webui/settings/chromeos/search/search_handler_factory.h",
"webui/settings/chromeos/search/settings_user_action_tracker.cc", "webui/settings/chromeos/search/settings_user_action_tracker.cc",
"webui/settings/chromeos/search/settings_user_action_tracker.h", "webui/settings/chromeos/search/settings_user_action_tracker.h",
"webui/settings/chromeos/server_printer_url_util.cc", "webui/settings/chromeos/server_printer_url_util.cc",
......
...@@ -67,6 +67,8 @@ ...@@ -67,6 +67,8 @@
#include "chrome/browser/ui/webui/settings/chromeos/plugin_vm_handler.h" #include "chrome/browser/ui/webui/settings/chromeos/plugin_vm_handler.h"
#include "chrome/browser/ui/webui/settings/chromeos/pref_names.h" #include "chrome/browser/ui/webui/settings/chromeos/pref_names.h"
#include "chrome/browser/ui/webui/settings/chromeos/quick_unlock_handler.h" #include "chrome/browser/ui/webui/settings/chromeos/quick_unlock_handler.h"
#include "chrome/browser/ui/webui/settings/chromeos/search/search_handler.h"
#include "chrome/browser/ui/webui/settings/chromeos/search/search_handler_factory.h"
#include "chrome/browser/ui/webui/settings/chromeos/search/settings_user_action_tracker.h" #include "chrome/browser/ui/webui/settings/chromeos/search/settings_user_action_tracker.h"
#include "chrome/browser/ui/webui/settings/chromeos/wallpaper_handler.h" #include "chrome/browser/ui/webui/settings/chromeos/wallpaper_handler.h"
#include "chrome/browser/ui/webui/settings/downloads_handler.h" #include "chrome/browser/ui/webui/settings/downloads_handler.h"
...@@ -241,6 +243,11 @@ OSSettingsUI::OSSettingsUI(content::WebUI* web_ui) ...@@ -241,6 +243,11 @@ OSSettingsUI::OSSettingsUI(content::WebUI* web_ui)
html_source->AddResourcePath( html_source->AddResourcePath(
"search/user_action_recorder.mojom-lite.js", "search/user_action_recorder.mojom-lite.js",
IDR_OS_SETTINGS_USER_ACTION_RECORDER_MOJOM_LITE_JS); IDR_OS_SETTINGS_USER_ACTION_RECORDER_MOJOM_LITE_JS);
html_source->AddResourcePath(
"search/search_result_icon.mojom-lite.js",
IDR_OS_SETTINGS_SEARCH_RESULT_ICON_MOJOM_LITE_JS);
html_source->AddResourcePath("search/search.mojom-lite.js",
IDR_OS_SETTINGS_SEARCH_MOJOM_LITE_JS);
// AddOsLocalizedStrings must be added after AddBrowserLocalizedStrings // AddOsLocalizedStrings must be added after AddBrowserLocalizedStrings
// as repeated keys used by the OS strings should override the same keys // as repeated keys used by the OS strings should override the same keys
...@@ -474,6 +481,12 @@ void OSSettingsUI::BindInterface( ...@@ -474,6 +481,12 @@ void OSSettingsUI::BindInterface(
std::make_unique<SettingsUserActionTracker>(std::move(receiver)); std::make_unique<SettingsUserActionTracker>(std::move(receiver));
} }
void OSSettingsUI::BindInterface(
mojo::PendingReceiver<mojom::SearchHandler> receiver) {
SearchHandlerFactory::GetForProfile(Profile::FromWebUI(web_ui()))
->BindInterface(std::move(receiver));
}
void OSSettingsUI::BindInterface( void OSSettingsUI::BindInterface(
mojo::PendingReceiver<app_management::mojom::PageHandlerFactory> receiver) { mojo::PendingReceiver<app_management::mojom::PageHandlerFactory> receiver) {
if (!app_management_page_handler_factory_) { if (!app_management_page_handler_factory_) {
......
...@@ -29,6 +29,10 @@ class PrefRegistrySyncable; ...@@ -29,6 +29,10 @@ class PrefRegistrySyncable;
namespace chromeos { namespace chromeos {
namespace settings { namespace settings {
namespace mojom {
class SearchHandler;
} // namespace mojom
// The WebUI handler for chrome://os-settings. // The WebUI handler for chrome://os-settings.
class OSSettingsUI : public ui::MojoWebUIController { class OSSettingsUI : public ui::MojoWebUIController {
public: public:
...@@ -49,6 +53,10 @@ class OSSettingsUI : public ui::MojoWebUIController { ...@@ -49,6 +53,10 @@ class OSSettingsUI : public ui::MojoWebUIController {
// passing the pending receiver that will be internally bound. // passing the pending receiver that will be internally bound.
void BindInterface(mojo::PendingReceiver<mojom::UserActionRecorder> receiver); void BindInterface(mojo::PendingReceiver<mojom::UserActionRecorder> receiver);
// Instantiates implementor of the mojom::SearchHandler mojo interface
// passing the pending receiver that will be internally bound.
void BindInterface(mojo::PendingReceiver<mojom::SearchHandler> receiver);
// Instantiates implementor of the mojom::PageHandlerFactory mojo interface // Instantiates implementor of the mojom::PageHandlerFactory mojo interface
// passing the pending receiver that will be internally bound. // passing the pending receiver that will be internally bound.
void BindInterface( void BindInterface(
......
...@@ -8,7 +8,10 @@ assert(is_chromeos) ...@@ -8,7 +8,10 @@ assert(is_chromeos)
mojom("mojo_bindings") { mojom("mojo_bindings") {
sources = [ sources = [
"search.mojom",
"search_result_icon.mojom", "search_result_icon.mojom",
"user_action_recorder.mojom", "user_action_recorder.mojom",
] ]
public_deps = [ "//mojo/public/mojom/base" ]
} }
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
module chromeos.settings.mojom;
import "chrome/browser/ui/webui/settings/chromeos/search/search_result_icon.mojom";
import "mojo/public/mojom/base/string16.mojom";
// Search result metadata.
struct SearchResult {
// String to be displayed as a result in the UI. Meant to be displayed
// directly (i.e., not an ID but rather the actual text).
mojo_base.mojom.String16 result_text;
// The URL path containing the relevant setting, which may or may not contain
// URL parameters. For example, the Wi-Fi list settings page is
// chrome://os-settings/networks?type=WiFi, so the field would be
// "networks?type=WiFi" for this page.
string url_path_with_parameters;
// Icon to display for the search result.
SearchResultIcon icon;
};
// Provides settings search results. Implemented in the browser process;
// intended to be called from settings JS and Launcher C++.
interface SearchHandler {
// Searches settings for the given query and returns a list of results, sorted
// from most relevant to least relevant. An empty array indicates no relevant
// results.
Search(mojo_base.mojom.String16 query) => (array<SearchResult> results);
};
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/webui/settings/chromeos/search/search_handler.h"
#include "base/strings/string_number_conversions.h"
#include "chrome/browser/ui/webui/settings/chromeos/os_settings_localized_strings_provider.h"
#include "chrome/browser/ui/webui/settings/chromeos/search/search_concept.h"
#include "chrome/browser/ui/webui/settings/chromeos/search/search_result_icon.mojom.h"
#include "chrome/services/local_search_service/public/mojom/types.mojom.h"
#include "ui/base/l10n/l10n_util.h"
namespace chromeos {
namespace settings {
namespace {
const int32_t kLocalSearchServiceMaxLatencyMs = 3000;
const int32_t kLocalSearchServiceMaxResults = 10;
} // namespace
// static
const size_t SearchHandler::kNumMaxResults = 5;
SearchHandler::SearchHandler(
OsSettingsLocalizedStringsProvider* strings_provider,
local_search_service::mojom::LocalSearchService* local_search_service)
: strings_provider_(strings_provider) {
DCHECK(strings_provider_);
local_search_service->GetIndex(
local_search_service::mojom::LocalSearchService::IndexId::CROS_SETTINGS,
index_remote_.BindNewPipeAndPassReceiver());
}
SearchHandler::~SearchHandler() = default;
void SearchHandler::BindInterface(
mojo::PendingReceiver<mojom::SearchHandler> pending_receiver) {
receivers_.Add(this, std::move(pending_receiver));
}
void SearchHandler::Search(const base::string16& query,
SearchCallback callback) {
index_remote_->Find(
query, kLocalSearchServiceMaxLatencyMs, kLocalSearchServiceMaxResults,
base::BindOnce(&SearchHandler::OnLocalSearchServiceResults,
base::Unretained(this), std::move(callback)));
}
void SearchHandler::OnLocalSearchServiceResults(
SearchCallback callback,
local_search_service::mojom::ResponseStatus response_status,
base::Optional<std::vector<local_search_service::mojom::ResultPtr>>
results) {
switch (response_status) {
case local_search_service::mojom::ResponseStatus::UNKNOWN_ERROR:
case local_search_service::mojom::ResponseStatus::EMPTY_QUERY:
case local_search_service::mojom::ResponseStatus::EMPTY_INDEX:
LOG(ERROR) << "Cannot search; LocalSearchService returned "
<< response_status << ". Returning empty results array.";
std::move(callback).Run({});
return;
case local_search_service::mojom::ResponseStatus::SUCCESS:
DCHECK(results);
ReturnSuccessfulResults(std::move(callback), std::move(results));
return;
}
NOTREACHED() << "Invalid response status: " << response_status << ".";
}
void SearchHandler::ReturnSuccessfulResults(
SearchCallback callback,
base::Optional<std::vector<local_search_service::mojom::ResultPtr>>
results) {
std::vector<mojom::SearchResultPtr> search_results;
for (const auto& result : *results) {
mojom::SearchResultPtr result_ptr = ResultToSearchResult(*result);
if (result_ptr)
search_results.push_back(std::move(result_ptr));
// Limit the number of results to return.
if (search_results.size() == kNumMaxResults)
break;
}
std::move(callback).Run(std::move(search_results));
}
mojom::SearchResultPtr SearchHandler::ResultToSearchResult(
const local_search_service::mojom::Result& result) {
int message_id;
// The result's ID is expected to be a stringified int.
if (!base::StringToInt(result.id, &message_id))
return nullptr;
const SearchConcept* concept =
strings_provider_->GetCanonicalTagMetadata(message_id);
// If the concept was not registered, no metadata is available. This can occur
// if the search tag was dynamically unregistered during the asynchronous
// Find() call.
if (!concept)
return nullptr;
return mojom::SearchResult::New(l10n_util::GetStringUTF16(message_id),
concept->url_path_with_parameters,
concept->icon);
}
void SearchHandler::Shutdown() {
strings_provider_ = nullptr;
receivers_.Clear();
index_remote_.reset();
}
} // namespace settings
} // namespace chromeos
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_UI_WEBUI_SETTINGS_CHROMEOS_SEARCH_SEARCH_HANDLER_H_
#define CHROME_BROWSER_UI_WEBUI_SETTINGS_CHROMEOS_SEARCH_SEARCH_HANDLER_H_
#include <vector>
#include "base/optional.h"
#include "chrome/browser/ui/webui/settings/chromeos/search/search.mojom.h"
#include "chrome/services/local_search_service/public/mojom/local_search_service.mojom.h"
#include "components/keyed_service/core/keyed_service.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/receiver_set.h"
#include "mojo/public/cpp/bindings/remote.h"
namespace chromeos {
namespace settings {
class OsSettingsLocalizedStringsProvider;
// Handles search queries for Chrome OS settings. Search() is expected to be
// invoked by the settings UI as well as the the Launcher search UI. Search
// results are obtained by matching the provided query against search tags
// indexed in the LocalSearchService and cross-referencing results with
// OsSettingsLocalizedStringsProvider.
//
// SearchHandler returns at most |kNumMaxResults| results; searches which do not
// provide any matches result in an empty results array.
class SearchHandler : public mojom::SearchHandler, public KeyedService {
public:
// Maximum number of results returned by a Search() call.
static const size_t kNumMaxResults;
SearchHandler(
OsSettingsLocalizedStringsProvider* strings_provider,
local_search_service::mojom::LocalSearchService* local_search_service);
~SearchHandler() override;
SearchHandler(const SearchHandler& other) = delete;
SearchHandler& operator=(const SearchHandler& other) = delete;
void BindInterface(
mojo::PendingReceiver<mojom::SearchHandler> pending_receiver);
// mojom::SearchHandler:
void Search(const base::string16& query, SearchCallback callback) override;
private:
// KeyedService:
void Shutdown() override;
void OnLocalSearchServiceResults(
SearchCallback callback,
local_search_service::mojom::ResponseStatus response_status,
base::Optional<std::vector<local_search_service::mojom::ResultPtr>>
results);
void ReturnSuccessfulResults(
SearchCallback callback,
base::Optional<std::vector<local_search_service::mojom::ResultPtr>>
results);
mojom::SearchResultPtr ResultToSearchResult(
const local_search_service::mojom::Result& result);
OsSettingsLocalizedStringsProvider* strings_provider_;
// Note: Expected to have multiple clients, so a ReceiverSet is used.
mojo::ReceiverSet<mojom::SearchHandler> receivers_;
mojo::Remote<local_search_service::mojom::Index> index_remote_;
};
} // namespace settings
} // namespace chromeos
#endif // CHROME_BROWSER_UI_WEBUI_SETTINGS_CHROMEOS_SEARCH_SEARCH_HANDLER_H_
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/webui/settings/chromeos/search/search_handler_factory.h"
#include "chrome/browser/local_search_service/local_search_service_proxy.h"
#include "chrome/browser/local_search_service/local_search_service_proxy_factory.h"
#include "chrome/browser/profiles/incognito_helpers.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/webui/settings/chromeos/os_settings_localized_strings_provider_factory.h"
#include "chrome/browser/ui/webui/settings/chromeos/search/search_handler.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
namespace chromeos {
namespace settings {
// static
SearchHandler* SearchHandlerFactory::GetForProfile(Profile* profile) {
return static_cast<SearchHandler*>(
SearchHandlerFactory::GetInstance()->GetServiceForBrowserContext(
profile, /*create=*/true));
}
// static
SearchHandlerFactory* SearchHandlerFactory::GetInstance() {
return base::Singleton<SearchHandlerFactory>::get();
}
SearchHandlerFactory::SearchHandlerFactory()
: BrowserContextKeyedServiceFactory(
"SearchHandler",
BrowserContextDependencyManager::GetInstance()) {
DependsOn(
local_search_service::LocalSearchServiceProxyFactory::GetInstance());
DependsOn(OsSettingsLocalizedStringsProviderFactory::GetInstance());
}
SearchHandlerFactory::~SearchHandlerFactory() = default;
KeyedService* SearchHandlerFactory::BuildServiceInstanceFor(
content::BrowserContext* context) const {
Profile* profile = Profile::FromBrowserContext(context);
return new SearchHandler(
OsSettingsLocalizedStringsProviderFactory::GetForProfile(profile),
local_search_service::LocalSearchServiceProxyFactory::GetForProfile(
Profile::FromBrowserContext(profile))
->GetLocalSearchService());
}
bool SearchHandlerFactory::ServiceIsNULLWhileTesting() const {
return true;
}
content::BrowserContext* SearchHandlerFactory::GetBrowserContextToUse(
content::BrowserContext* context) const {
return chrome::GetBrowserContextOwnInstanceInIncognito(context);
}
} // namespace settings
} // namespace chromeos
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_UI_WEBUI_SETTINGS_CHROMEOS_SEARCH_SEARCH_HANDLER_FACTORY_H_
#define CHROME_BROWSER_UI_WEBUI_SETTINGS_CHROMEOS_SEARCH_SEARCH_HANDLER_FACTORY_H_
#include "base/memory/singleton.h"
#include "components/keyed_service/content/browser_context_keyed_service_factory.h"
class Profile;
namespace chromeos {
namespace settings {
class SearchHandler;
// Factory for SearchHandler; available for incognito and multi-profile cases to
// support settings search in those contexts.
class SearchHandlerFactory : public BrowserContextKeyedServiceFactory {
public:
static SearchHandler* GetForProfile(Profile* profile);
static SearchHandlerFactory* GetInstance();
private:
friend struct base::DefaultSingletonTraits<SearchHandlerFactory>;
SearchHandlerFactory();
~SearchHandlerFactory() override;
SearchHandlerFactory(const SearchHandlerFactory&) = delete;
SearchHandlerFactory& operator=(const SearchHandlerFactory&) = delete;
// BrowserContextKeyedServiceFactory:
KeyedService* BuildServiceInstanceFor(
content::BrowserContext* context) const override;
bool ServiceIsNULLWhileTesting() const override;
content::BrowserContext* GetBrowserContextToUse(
content::BrowserContext* context) const override;
};
} // namespace settings
} // namespace chromeos
#endif // CHROME_BROWSER_UI_WEBUI_SETTINGS_CHROMEOS_SEARCH_SEARCH_HANDLER_FACTORY_H_
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/webui/settings/chromeos/search/search_handler.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/ui/webui/settings/chromeos/os_settings_localized_strings_provider.h"
#include "chrome/browser/ui/webui/settings/chromeos/search/search.mojom-test-utils.h"
#include "chrome/common/webui_url_constants.h"
#include "chrome/services/local_search_service/local_search_service_impl.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "chromeos/services/network_config/public/cpp/cros_network_config_test_helper.h"
#include "content/public/test/browser_task_environment.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace chromeos {
namespace settings {
class SearchHandlerTest : public testing::Test {
protected:
SearchHandlerTest() : profile_manager_(TestingBrowserProcess::GetGlobal()) {}
~SearchHandlerTest() override = default;
// testing::Test:
void SetUp() override {
ASSERT_TRUE(profile_manager_.SetUp());
provider_ = std::make_unique<OsSettingsLocalizedStringsProvider>(
profile_manager_.CreateTestingProfile("TestingProfile"),
&local_search_service_);
handler_ = std::make_unique<SearchHandler>(provider_.get(),
&local_search_service_);
handler_->BindInterface(handler_remote_.BindNewPipeAndPassReceiver());
// Allow asynchronous networking code to complete; specifically, let code
// which adds Wi-Fi to the system run so that Wi-Fi search tags can be
// queried below.
base::RunLoop().RunUntilIdle();
}
content::BrowserTaskEnvironment task_environment_;
TestingProfileManager profile_manager_;
chromeos::network_config::CrosNetworkConfigTestHelper network_config_helper_;
mojo::Remote<mojom::SearchHandler> handler_remote_;
local_search_service::LocalSearchServiceImpl local_search_service_;
std::unique_ptr<OsSettingsLocalizedStringsProvider> provider_;
std::unique_ptr<SearchHandler> handler_;
};
TEST_F(SearchHandlerTest, Success) {
// Search for "Wi-Fi".
std::vector<mojom::SearchResultPtr> search_results;
mojom::SearchHandlerAsyncWaiter(handler_remote_.get())
.Search(base::ASCIIToUTF16("Wi-Fi"), &search_results);
// Multiple results should be available, and they should have Wi-Fi metadata.
EXPECT_GT(search_results.size(), 0u);
for (const auto& result : search_results) {
EXPECT_EQ(chrome::kWiFiSettingsSubPage, result->url_path_with_parameters);
EXPECT_EQ(mojom::SearchResultIcon::kWifi, result->icon);
}
}
TEST_F(SearchHandlerTest, NoResults) {
std::vector<mojom::SearchResultPtr> search_results;
mojom::SearchHandlerAsyncWaiter(handler_remote_.get())
.Search(base::ASCIIToUTF16("QueryWithNoResults"), &search_results);
EXPECT_TRUE(search_results.empty());
}
} // namespace settings
} // namespace chromeos
...@@ -4,7 +4,8 @@ ...@@ -4,7 +4,8 @@
module chromeos.settings.mojom; module chromeos.settings.mojom;
// Records user actions within OS settings. // Records user actions within OS settings. Implemented in the browser process;
// intended to be called from settings JS.
interface UserActionRecorder { interface UserActionRecorder {
// Records that the settings window has been focused. // Records that the settings window has been focused.
RecordPageFocus(); RecordPageFocus();
......
...@@ -10,7 +10,7 @@ import "mojo/public/mojom/base/string16.mojom"; ...@@ -10,7 +10,7 @@ import "mojo/public/mojom/base/string16.mojom";
// LocalSearchService will run as a singleton service so that clients // LocalSearchService will run as a singleton service so that clients
// can request Indices from it. LocalSearchService will be responsible for // can request Indices from it. LocalSearchService will be responsible for
// creating Indices, but data update will be the responsibility of // creating Indices, but data update will be the responsibility of
// clients. // clients. Runs in the browser process as a KeyedService.
interface LocalSearchService { interface LocalSearchService {
// Identifies a globally unique Index of related search data. // Identifies a globally unique Index of related search data.
enum IndexId { enum IndexId {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment