Commit f986a092 authored by Rachel Wong's avatar Rachel Wong Committed by Commit Bot

[search service] Add and implement proxy interface.

This CL defines and implements the search service proxy API.
- IndexProxy implements an interface that is a wrapper around an Index;
- LocalSearchServiceProxy implements an interface that is a wrapper
  around LocalSearchService and owns various IndexProxies;
- LocalSearchServiceProxyFactory is responsible for the creation of
  LocalSearchServiceProxies.

Bug: 1092767
Change-Id: Icb2fa359fe9d05bc4ae5b5bc977550ed659e8262
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2249428
Commit-Queue: Rachel Wong <wrong@chromium.org>
Reviewed-by: default avatarSam McNally <sammc@chromium.org>
Reviewed-by: default avatarJia Meng <jiameng@chromium.org>
Reviewed-by: default avatarTony Yeoman <tby@chromium.org>
Cr-Commit-Position: refs/heads/master@{#780210}
parent d00acbad
......@@ -1371,6 +1371,12 @@ source_set("chromeos") {
"local_search_service/local_search_service.h",
"local_search_service/local_search_service_factory.cc",
"local_search_service/local_search_service_factory.h",
"local_search_service/proxy/index_proxy.cc",
"local_search_service/proxy/index_proxy.h",
"local_search_service/proxy/local_search_service_proxy.cc",
"local_search_service/proxy/local_search_service_proxy.h",
"local_search_service/proxy/local_search_service_proxy_factory.cc",
"local_search_service/proxy/local_search_service_proxy_factory.h",
"local_search_service/shared_structs.cc",
"local_search_service/shared_structs.h",
"locale_change_guard.cc",
......@@ -3102,6 +3108,7 @@ source_set("unit_tests") {
"local_search_service/index_unittest.cc",
"local_search_service/inverted_index_unittest.cc",
"local_search_service/local_search_service_unittest.cc",
"local_search_service/proxy/local_search_service_proxy_unittest.cc",
"local_search_service/proxy/types_mojom_traits_unittest.cc",
"locale_change_guard_unittest.cc",
"lock_screen_apps/app_manager_impl_unittest.cc",
......
......@@ -15,7 +15,7 @@ namespace local_search_service {
class Index;
enum class IndexId { kInvalid = -1, kCrosSettings = 0 };
enum class IndexId { kCrosSettings = 0 };
// LocalSearchService creates and owns content-specific Indices. Clients can
// call it |GetIndex| method to get an Index for a given index id.
......
......@@ -5,7 +5,10 @@
import("//mojo/public/tools/bindings/mojom.gni")
mojom("mojom") {
sources = [ "types.mojom" ]
sources = [
"local_search_service_proxy.mojom",
"types.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.
#include "chrome/browser/chromeos/local_search_service/proxy/index_proxy.h"
#include "base/optional.h"
namespace local_search_service {
IndexProxy::IndexProxy(Index* index) : index_(index) {
DCHECK(index_);
}
IndexProxy::~IndexProxy() = default;
void IndexProxy::BindReceiver(
mojo::PendingReceiver<mojom::IndexProxy> receiver) {
receivers_.Add(this, std::move(receiver));
}
void IndexProxy::GetSize(GetSizeCallback callback) {
const uint64_t num_items = index_->GetSize();
std::move(callback).Run(num_items);
}
void IndexProxy::AddOrUpdate(const std::vector<Data>& data,
AddOrUpdateCallback callback) {
index_->AddOrUpdate(data);
std::move(callback).Run();
}
void IndexProxy::Delete(const std::vector<std::string>& ids,
DeleteCallback callback) {
const uint64_t num_deleted = index_->Delete(ids);
std::move(callback).Run(num_deleted);
}
void IndexProxy::Find(const base::string16& query,
uint32_t max_results,
FindCallback callback) {
std::vector<Result> results;
ResponseStatus status = index_->Find(query, max_results, &results);
if (status != ResponseStatus::kSuccess) {
std::move(callback).Run(status, base::nullopt);
} else {
std::move(callback).Run(status, std::move(results));
}
}
} // namespace local_search_service
// 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_CHROMEOS_LOCAL_SEARCH_SERVICE_PROXY_INDEX_PROXY_H_
#define CHROME_BROWSER_CHROMEOS_LOCAL_SEARCH_SERVICE_PROXY_INDEX_PROXY_H_
#include <vector>
#include "base/strings/string16.h"
#include "chrome/browser/chromeos/local_search_service/proxy/local_search_service_proxy.mojom.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/receiver_set.h"
namespace local_search_service {
class Index;
class IndexProxy : public mojom::IndexProxy {
public:
explicit IndexProxy(Index* index);
~IndexProxy() override;
void BindReceiver(mojo::PendingReceiver<mojom::IndexProxy> receiver);
// mojom::IndexProxy:
void GetSize(GetSizeCallback callback) override;
void AddOrUpdate(const std::vector<Data>& data,
AddOrUpdateCallback callback) override;
void Delete(const std::vector<std::string>& ids,
DeleteCallback callback) override;
void Find(const base::string16& query,
uint32_t max_results,
FindCallback callback) override;
private:
Index* const index_;
mojo::ReceiverSet<mojom::IndexProxy> receivers_;
};
} // namespace local_search_service
#endif // CHROME_BROWSER_CHROMEOS_LOCAL_SEARCH_SERVICE_PROXY_INDEX_PROXY_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/chromeos/local_search_service/proxy/local_search_service_proxy.h"
#include "chrome/browser/chromeos/local_search_service/local_search_service.h"
#include "chrome/browser/chromeos/local_search_service/proxy/index_proxy.h"
namespace local_search_service {
LocalSearchServiceProxy::LocalSearchServiceProxy(
local_search_service::LocalSearchService* local_search_service)
: service_(local_search_service) {
DCHECK(service_);
}
LocalSearchServiceProxy::~LocalSearchServiceProxy() = default;
void LocalSearchServiceProxy::GetIndex(
IndexId index_id,
mojo::PendingReceiver<mojom::IndexProxy> index_receiver) {
auto it = indices_.find(index_id);
if (it == indices_.end()) {
Index* index = service_->GetIndex(index_id);
it = indices_.emplace(index_id, std::make_unique<IndexProxy>(index)).first;
}
it->second->BindReceiver(std::move(index_receiver));
}
void LocalSearchServiceProxy::BindReceiver(
mojo::PendingReceiver<mojom::LocalSearchServiceProxy> receiver) {
receivers_.Add(this, std::move(receiver));
}
} // namespace local_search_service
// 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_CHROMEOS_LOCAL_SEARCH_SERVICE_PROXY_LOCAL_SEARCH_SERVICE_PROXY_H_
#define CHROME_BROWSER_CHROMEOS_LOCAL_SEARCH_SERVICE_PROXY_LOCAL_SEARCH_SERVICE_PROXY_H_
#include <map>
#include "chrome/browser/chromeos/local_search_service/proxy/local_search_service_proxy.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"
namespace local_search_service {
class LocalSearchService;
class IndexProxy;
class LocalSearchServiceProxy : public mojom::LocalSearchServiceProxy,
public KeyedService {
public:
explicit LocalSearchServiceProxy(LocalSearchService* local_search_service);
~LocalSearchServiceProxy() override;
LocalSearchServiceProxy(const LocalSearchServiceProxy&) = delete;
LocalSearchServiceProxy& operator=(const LocalSearchServiceProxy) = delete;
// mojom::LocalSearchServiceProxy:
void GetIndex(
IndexId index_id,
mojo::PendingReceiver<mojom::IndexProxy> index_receiver) override;
void BindReceiver(
mojo::PendingReceiver<mojom::LocalSearchServiceProxy> receiver);
private:
LocalSearchService* const service_;
mojo::ReceiverSet<mojom::LocalSearchServiceProxy> receivers_;
std::map<IndexId, std::unique_ptr<IndexProxy>> indices_;
};
} // namespace local_search_service
#endif // CHROME_BROWSER_CHROMEOS_LOCAL_SEARCH_SERVICE_PROXY_LOCAL_SEARCH_SERVICE_PROXY_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.
module local_search_service.mojom;
import "chrome/browser/chromeos/local_search_service/proxy/types.mojom";
import "mojo/public/mojom/base/string16.mojom";
enum IndexId {
kCrosSettings = 0
// Add new client IDs here.
};
// LocalSearchServiceProxy creates and owns content-specific IndexProxies.
// Clients can call |GetIndex| to get an IndexProxy for a given index id.
interface LocalSearchServiceProxy {
// A client can call this function to bind an IndexProxy for |index_id|. If
// the Index isn't created when this function is called, LocalSearchService
// will create one.
// Note, there should be one primary client that is the owner of the data and
// can read/write the data to the Index. The other clients should only use
// the Index for query search.
GetIndex(IndexId index_id, pending_receiver<IndexProxy> index_receiver);
};
// A proxy to a local search service Index.
// An Index has a registry of searchable data, which can be updated. It also
// runs a search function to find matching items for a given query.
// Each Index can serve multiple clients, but only one client (the primary
// client) that owns the data should be allowed to modify the Index.
interface IndexProxy {
// Returns number of data items.
GetSize() => (uint64 num_items);
// Adds or updates data and callbacks upon completion.
// Only the primary client should be allowed to do this operation.
AddOrUpdate(array<Data> data) => ();
// Deletes data with |ids| and returns the number of items deleted.
// If an id doesn't exist in the Index, no operation will be done.
// Only the primary client should be allowed to do this operation.
Delete(array<string> ids) => (uint32 num_deleted);
// Takes an asynchronous search request call and returns results and status
// code via a callback. |results| will be null if there is an error.
Find(mojo_base.mojom.String16 query, uint32 max_results)
=> (ResponseStatus status, array<Result>? 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/chromeos/local_search_service/proxy/local_search_service_proxy_factory.h"
#include "chrome/browser/chromeos/local_search_service/local_search_service_factory.h"
#include "chrome/browser/chromeos/local_search_service/proxy/local_search_service_proxy.h"
#include "chrome/browser/profiles/profile.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
namespace local_search_service {
// static
LocalSearchServiceProxy* LocalSearchServiceProxyFactory::GetForProfile(
Profile* profile) {
return static_cast<LocalSearchServiceProxy*>(
LocalSearchServiceProxyFactory::GetInstance()
->GetServiceForBrowserContext(profile, /*create=*/true));
}
// static
LocalSearchServiceProxyFactory* LocalSearchServiceProxyFactory::GetInstance() {
static base::NoDestructor<LocalSearchServiceProxyFactory> instance;
return instance.get();
}
LocalSearchServiceProxyFactory::LocalSearchServiceProxyFactory()
: BrowserContextKeyedServiceFactory(
"LocalSearchServiceProxy",
BrowserContextDependencyManager::GetInstance()) {
DependsOn(local_search_service::LocalSearchServiceFactory::GetInstance());
}
LocalSearchServiceProxyFactory::~LocalSearchServiceProxyFactory() = default;
KeyedService* LocalSearchServiceProxyFactory::BuildServiceInstanceFor(
content::BrowserContext* context) const {
DCHECK(context);
Profile* profile = Profile::FromBrowserContext(context);
return new LocalSearchServiceProxy(
local_search_service::LocalSearchServiceFactory::GetForProfile(profile));
}
} // namespace local_search_service
// 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_CHROMEOS_LOCAL_SEARCH_SERVICE_PROXY_LOCAL_SEARCH_SERVICE_PROXY_FACTORY_H_
#define CHROME_BROWSER_CHROMEOS_LOCAL_SEARCH_SERVICE_PROXY_LOCAL_SEARCH_SERVICE_PROXY_FACTORY_H_
#include "base/no_destructor.h"
#include "components/keyed_service/content/browser_context_keyed_service_factory.h"
class Profile;
namespace local_search_service {
class LocalSearchServiceProxy;
class LocalSearchServiceProxyFactory
: public BrowserContextKeyedServiceFactory {
public:
static LocalSearchServiceProxy* GetForProfile(Profile* profile);
static LocalSearchServiceProxyFactory* GetInstance();
LocalSearchServiceProxyFactory(const LocalSearchServiceProxyFactory&) =
delete;
LocalSearchServiceProxyFactory& operator=(
const LocalSearchServiceProxyFactory&) = delete;
private:
friend class base::NoDestructor<LocalSearchServiceProxyFactory>;
LocalSearchServiceProxyFactory();
~LocalSearchServiceProxyFactory() override;
// BrowserContextKeyedServiceFactory:
KeyedService* BuildServiceInstanceFor(
content::BrowserContext* context) const override;
};
} // namespace local_search_service
#endif // CHROME_BROWSER_CHROMEOS_LOCAL_SEARCH_SERVICE_PROXY_LOCAL_SEARCH_SERVICE_PROXY_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/chromeos/local_search_service/proxy/local_search_service_proxy.h"
#include "base/bind.h"
#include "base/run_loop.h"
#include "base/test/task_environment.h"
#include "chrome/browser/chromeos/local_search_service/proxy/index_proxy.h"
#include "chrome/browser/chromeos/local_search_service/proxy/types.mojom.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace local_search_service {
class LocalSearchServiceProxyTest : public testing::Test {
public:
LocalSearchServiceProxyTest() {
service_proxy_ = std::make_unique<LocalSearchServiceProxy>(&service_);
service_proxy_->BindReceiver(service_remote_.BindNewPipeAndPassReceiver());
}
protected:
mojo::Remote<mojom::LocalSearchServiceProxy> service_remote_;
private:
base::test::TaskEnvironment task_environment_;
LocalSearchService service_;
std::unique_ptr<LocalSearchServiceProxy> service_proxy_;
};
TEST_F(LocalSearchServiceProxyTest, GetIndex) {
mojo::Remote<mojom::IndexProxy> index_remote;
service_remote_->GetIndex(IndexId::kCrosSettings,
index_remote.BindNewPipeAndPassReceiver());
base::RunLoop().RunUntilIdle();
// Check that IndexRemote is bound.
bool callback_done = false;
uint64_t num_items;
index_remote->GetSize(base::BindOnce(
[](bool* callback_done, uint64_t* num_items, uint64_t size) {
*callback_done = true;
*num_items = size;
},
&callback_done, &num_items));
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(callback_done);
EXPECT_EQ(num_items, 0U);
}
} // namespace local_search_service
......@@ -6,6 +6,33 @@
namespace mojo {
// static
local_search_service::mojom::IndexId
EnumTraits<local_search_service::mojom::IndexId,
local_search_service::IndexId>::ToMojom(local_search_service::IndexId
input) {
switch (input) {
case local_search_service::IndexId::kCrosSettings:
return local_search_service::mojom::IndexId::kCrosSettings;
}
NOTREACHED();
return local_search_service::mojom::IndexId::kCrosSettings;
}
// static
bool EnumTraits<local_search_service::mojom::IndexId,
local_search_service::IndexId>::
FromMojom(local_search_service::mojom::IndexId input,
local_search_service::IndexId* output) {
switch (input) {
case local_search_service::mojom::IndexId::kCrosSettings:
*output = local_search_service::IndexId::kCrosSettings;
return true;
}
NOTREACHED();
return false;
}
// static
bool StructTraits<local_search_service::mojom::ContentDataView,
local_search_service::Content>::
......
......@@ -7,11 +7,24 @@
#include "chrome/browser/chromeos/local_search_service/index.h"
#include "chrome/browser/chromeos/local_search_service/local_search_service.h"
#include "chrome/browser/chromeos/local_search_service/proxy/local_search_service_proxy.mojom.h"
#include "chrome/browser/chromeos/local_search_service/proxy/types.mojom.h"
#include "mojo/public/cpp/bindings/struct_traits.h"
namespace mojo {
// TODO(crbug/1092767): Consolidate the API to use mojo enums instead of
// EnumTraits.
template <>
struct EnumTraits<local_search_service::mojom::IndexId,
local_search_service::IndexId> {
static local_search_service::mojom::IndexId ToMojom(
local_search_service::IndexId input);
static bool FromMojom(local_search_service::mojom::IndexId input,
local_search_service::IndexId* output);
};
template <>
struct StructTraits<local_search_service::mojom::ContentDataView,
local_search_service::Content> {
......
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