Commit cbfaf57c authored by Jia's avatar Jia Committed by Commit Bot

[cros search service] Replace string with string16

We will need to support search in multiple languages. This cl changes
the string type in the mojom interface and internal impl.

Bug: 1018613
Change-Id: I7e4064d18efd5a58acfb2574d1d79e505b27a115
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2079716Reviewed-by: default avatarColin Blundell <blundell@chromium.org>
Reviewed-by: default avatarSam McNally <sammc@chromium.org>
Commit-Queue: Jia Meng <jiameng@chromium.org>
Cr-Commit-Position: refs/heads/master@{#745824}
parent 6c497a8d
......@@ -9,7 +9,6 @@
#include "base/bind.h"
#include "base/optional.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/common/string_matching/fuzzy_tokenized_string_match.h"
#include "chrome/common/string_matching/tokenized_string.h"
......@@ -20,12 +19,11 @@ namespace {
using Hits = std::vector<mojom::RangePtr>;
void TokenizeSearchTags(
const std::vector<std::string>& search_tags,
const std::vector<base::string16>& search_tags,
std::vector<std::unique_ptr<TokenizedString>>* tokenized) {
DCHECK(tokenized);
for (const auto& tag : search_tags) {
tokenized->push_back(
std::make_unique<TokenizedString>(base::UTF8ToUTF16(tag)));
tokenized->push_back(std::make_unique<TokenizedString>(tag));
}
}
......@@ -99,7 +97,7 @@ void IndexImpl::AddOrUpdate(std::vector<mojom::DataPtr> data,
std::move(callback).Run();
}
void IndexImpl::Delete(const std::vector<std::string>& ids,
void IndexImpl::Delete(const std::vector<base::string16>& ids,
DeleteCallback callback) {
uint32_t num_deleted = 0u;
for (const auto& id : ids) {
......@@ -117,7 +115,7 @@ void IndexImpl::Delete(const std::vector<std::string>& ids,
std::move(callback).Run(num_deleted);
}
void IndexImpl::Find(const std::string& query,
void IndexImpl::Find(const base::string16& query,
int32_t max_latency_in_ms,
int32_t max_results,
FindCallback callback) {
......@@ -159,9 +157,9 @@ void IndexImpl::GetSearchParamsForTesting(double* relevance_threshold,
}
std::vector<mojom::ResultPtr> IndexImpl::GetSearchResults(
const std::string& query) const {
const base::string16& query) const {
std::vector<mojom::ResultPtr> results;
const TokenizedString tokenized_query(base::UTF8ToUTF16(query));
const TokenizedString tokenized_query(query);
for (const auto& item : data_) {
double relevance_score = 0.0;
......
......@@ -7,10 +7,10 @@
#include <map>
#include <memory>
#include <string>
#include <vector>
#include "base/macros.h"
#include "base/strings/string16.h"
#include "chrome/services/local_search_service/public/mojom/local_search_service.mojom.h"
#include "chrome/services/local_search_service/public/mojom/types.mojom.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
......@@ -40,10 +40,10 @@ class IndexImpl : public mojom::Index {
void AddOrUpdate(std::vector<mojom::DataPtr> data,
AddOrUpdateCallback callback) override;
void Delete(const std::vector<std::string>& ids,
void Delete(const std::vector<base::string16>& ids,
DeleteCallback callback) override;
void Find(const std::string& query,
void Find(const base::string16& query,
int32_t max_latency_in_ms,
int32_t max_results,
FindCallback callback) override;
......@@ -60,10 +60,10 @@ class IndexImpl : public mojom::Index {
private:
// Returns all search results for a given query.
std::vector<mojom::ResultPtr> GetSearchResults(
const std::string& query) const;
const base::string16& query) const;
// A map from key to tokenized search-tags.
std::map<std::string, std::vector<std::unique_ptr<TokenizedString>>> data_;
std::map<base::string16, std::vector<std::unique_ptr<TokenizedString>>> data_;
mojo::ReceiverSet<mojom::Index> receivers_;
......
......@@ -44,20 +44,6 @@ class IndexImplTest : public testing::Test {
index_impl_.BindReceiver(index_remote_.BindNewPipeAndPassReceiver());
}
// Creates test data to be registered to the index. |input| is a map from
// id to search-tags.
std::vector<mojom::DataPtr> CreateTestData(
const std::map<std::string, std::vector<std::string>>& input) {
std::vector<mojom::DataPtr> output;
for (const auto& item : input) {
const std::string& id = item.first;
const std::vector<std::string>& tags = item.second;
mojom::DataPtr data = mojom::Data::New(id, tags);
output.push_back(std::move(data));
}
return output;
}
protected:
base::test::SingleThreadTaskEnvironment task_environment_;
IndexImpl index_impl_;
......
......@@ -13,6 +13,8 @@
#include "base/callback.h"
#include "base/optional.h"
#include "base/run_loop.h"
#include "base/strings/string16.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/task_environment.h"
#include "chrome/services/local_search_service/local_search_service_impl.h"
#include "chrome/services/local_search_service/public/mojom/types.mojom.h"
......@@ -110,8 +112,9 @@ TEST_F(LocalSearchServiceImplTest, UpdateData) {
GetSizeAndCheck(index_remote.get(), 1u);
// Add "id3" to the index.
mojom::DataPtr data_id3 =
mojom::Data::New("id3", std::vector<std::string>({"tag3a"}));
mojom::DataPtr data_id3 = mojom::Data::New(
base::UTF8ToUTF16("id3"),
std::vector<base::string16>({base::UTF8ToUTF16("tag3a")}));
std::vector<mojom::DataPtr> data_to_update;
data_to_update.push_back(std::move(data_id3));
AddOrUpdateAndCheck(index_remote.get(), std::move(data_to_update));
......
......@@ -5,6 +5,7 @@
module local_search_service.mojom;
import "chrome/services/local_search_service/public/mojom/types.mojom";
import "mojo/public/mojom/base/string16.mojom";
// LocalSearchService will run as a singleton service so that clients
// can request Indices from it. LocalSearchService will be responsible for
......@@ -38,13 +39,13 @@ interface Index {
// Deletes data with |ids| and returns 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);
Delete(array<mojo_base.mojom.String16> ids) => (uint32 num_deleted);
// Takes an asynchronous search request call and returns results and status
// code via a callback.
// |max_latency_in_ms| = -1 means there is no maximum latency.
Find(string query, int32 max_latency_in_ms, int32 max_results)
=> (ResponseStatus status, array<Result>? results);
Find(mojo_base.mojom.String16 query, int32 max_latency_in_ms,
int32 max_results) => (ResponseStatus status, array<Result>? results);
// Sets search parameters. Clients should not need to set the parameters
// explicitly. They can use default values provided by the Index. However,
......
......@@ -5,6 +5,7 @@
module local_search_service.mojom;
import "mojo/public/mojom/base/values.mojom";
import "mojo/public/mojom/base/string16.mojom";
// A numeric range used to represent the start and end position.
struct Range {
......@@ -20,17 +21,17 @@ struct Data {
// identifiers.
// Ideally IDs should persist across sessions, but this is not strictly
// required now because data is not persisted across sessions.
string id;
mojo_base.mojom.String16 id;
// Data item will be matched between its search tags and query term.
array<string> search_tags;
array<mojo_base.mojom.String16> search_tags;
};
// Result is one item that matches a given query. It contains the id of the item
// and its matching score.
struct Result {
// Id of the data.
string id;
mojo_base.mojom.String16 id;
// Relevance score, in the range of [0,1].
double score;
// Matching ranges.
......
......@@ -4,18 +4,32 @@
#include "chrome/services/local_search_service/test_utils.h"
#include "base/strings/string16.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/services/local_search_service/public/mojom/local_search_service.mojom-test-utils.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace local_search_service {
namespace {
std::vector<base::string16> MultiUTF8ToUTF16(
const std::vector<std::string>& input) {
std::vector<base::string16> output;
for (const auto& str : input) {
output.push_back(base::UTF8ToUTF16(str));
}
return output;
}
} // namespace
std::vector<mojom::DataPtr> CreateTestData(
const std::map<std::string, std::vector<std::string>>& input) {
std::vector<mojom::DataPtr> output;
for (const auto& item : input) {
const std::string& id = item.first;
const std::vector<std::string>& tags = item.second;
mojom::DataPtr data = mojom::Data::New(id, tags);
const std::vector<base::string16> tags = MultiUTF8ToUTF16(item.second);
mojom::DataPtr data = mojom::Data::New(base::UTF8ToUTF16(item.first), tags);
output.push_back(std::move(data));
}
return output;
......@@ -39,7 +53,7 @@ void DeleteAndCheck(mojom::Index* index,
uint32_t expected_num_deleted) {
DCHECK(index);
uint32_t num_deleted = 0u;
mojom::IndexAsyncWaiter(index).Delete(ids, &num_deleted);
mojom::IndexAsyncWaiter(index).Delete(MultiUTF8ToUTF16(ids), &num_deleted);
EXPECT_EQ(num_deleted, expected_num_deleted);
}
......@@ -54,7 +68,8 @@ void FindAndCheck(mojom::Index* index,
mojom::IndexAsyncWaiter async_waiter(index);
mojom::ResponseStatus status = mojom::ResponseStatus::UNKNOWN_ERROR;
base::Optional<std::vector<::local_search_service::mojom::ResultPtr>> results;
async_waiter.Find(query, max_latency_in_ms, max_results, &status, &results);
async_waiter.Find(base::UTF8ToUTF16(query), max_latency_in_ms, max_results,
&status, &results);
EXPECT_EQ(status, expected_status);
......@@ -62,7 +77,7 @@ void FindAndCheck(mojom::Index* index,
// If results are returned, check size and values match the expected.
EXPECT_EQ(results->size(), expected_result_ids.size());
for (size_t i = 0; i < results->size(); ++i) {
EXPECT_EQ((*results)[i]->id, expected_result_ids[i]);
EXPECT_EQ((*results)[i]->id, base::UTF8ToUTF16(expected_result_ids[i]));
// Scores should be non-increasing.
if (i < results->size() - 1) {
EXPECT_GE((*results)[i]->score, (*results)[i + 1]->score);
......
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