[ContentIndex] Add query methods to ContentIndexContext.
Add the virtual functions needed by the //content embedder to get the
Content Index entries and implement them.
Remove the intialization calls that notified the embedder of the
available content on start up
Bug: 973844
Change-Id: I74463f3270ea75c4846a72d6f215a5a00aa85c12
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1710646
Reviewed-by: Avi Drissman <[email protected]>
Reviewed-by: Richard Knoll <[email protected]>
Commit-Queue: Rayan Kanso <[email protected]>
Cr-Commit-Position: refs/heads/master@{#679499}
diff --git a/content/browser/content_index/content_index_database.cc b/content/browser/content_index/content_index_database.cc
index 8788c6d9..b125d300 100644
--- a/content/browser/content_index/content_index_database.cc
+++ b/content/browser/content_index/content_index_database.cc
@@ -6,6 +6,7 @@
#include <string>
+#include "base/optional.h"
#include "base/task/post_task.h"
#include "base/time/time.h"
#include "content/browser/background_fetch/storage/image_helpers.h"
@@ -79,6 +80,26 @@
return result;
}
+base::Optional<ContentIndexEntry> EntryFromSerializedProto(
+ int64_t service_worker_registration_id,
+ const std::string& serialized_proto) {
+ proto::ContentEntry entry_proto;
+ if (!entry_proto.ParseFromString(serialized_proto))
+ return base::nullopt;
+
+ GURL launch_url(entry_proto.launch_url());
+ if (!launch_url.is_valid())
+ return base::nullopt;
+
+ auto description = DescriptionFromProto(entry_proto.description());
+ base::Time registration_time = base::Time::FromDeltaSinceWindowsEpoch(
+ base::TimeDelta::FromMicroseconds(entry_proto.timestamp()));
+
+ return ContentIndexEntry(service_worker_registration_id,
+ std::move(description), std::move(launch_url),
+ registration_time);
+}
+
} // namespace
ContentIndexDatabase::ContentIndexDatabase(
@@ -232,66 +253,10 @@
std::move(descriptions));
}
-void ContentIndexDatabase::InitializeProviderWithEntries() {
- service_worker_context_->GetUserDataForAllRegistrationsByKeyPrefix(
- kEntryPrefix, base::BindOnce(&ContentIndexDatabase::DidGetAllEntries,
- weak_ptr_factory_io_.GetWeakPtr()));
-}
-
-void ContentIndexDatabase::DidGetAllEntries(
- const std::vector<std::pair<int64_t, std::string>>& user_data,
- blink::ServiceWorkerStatusCode status) {
- if (status != blink::ServiceWorkerStatusCode::kOk) {
- // TODO(crbug.com/973844): Handle or report this error.
- return;
- }
-
- if (user_data.empty())
- return;
-
- std::vector<ContentIndexEntry> entries;
- entries.reserve(user_data.size());
-
- for (const auto& ud : user_data) {
- proto::ContentEntry entry_proto;
- if (!entry_proto.ParseFromString(ud.second)) {
- // TODO(crbug.com/973844): Handle or report this error.
- return;
- }
-
- int64_t service_worker_registration_id = ud.first;
- auto description = DescriptionFromProto(entry_proto.description());
- base::Time registration_time = base::Time::FromDeltaSinceWindowsEpoch(
- base::TimeDelta::FromMicroseconds(entry_proto.timestamp()));
-
- entries.emplace_back(service_worker_registration_id, std::move(description),
- GURL(entry_proto.launch_url()), registration_time);
- }
-
- base::PostTaskWithTraits(
- FROM_HERE, {BrowserThread::UI},
- base::BindOnce(&ContentIndexDatabase::NotifyProviderContentAdded,
- weak_ptr_factory_ui_.GetWeakPtr(), std::move(entries)));
-}
-
void ContentIndexDatabase::GetIcon(
int64_t service_worker_registration_id,
const std::string& description_id,
base::OnceCallback<void(SkBitmap)> icon_callback) {
- DCHECK_CURRENTLY_ON(BrowserThread::UI);
-
- base::PostTaskWithTraits(
- FROM_HERE, {BrowserThread::IO},
- base::BindOnce(&ContentIndexDatabase::GetIconOnIO,
- weak_ptr_factory_io_.GetWeakPtr(),
- service_worker_registration_id, description_id,
- std::move(icon_callback)));
-}
-
-void ContentIndexDatabase::GetIconOnIO(
- int64_t service_worker_registration_id,
- const std::string& description_id,
- base::OnceCallback<void(SkBitmap)> icon_callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
service_worker_context_->GetRegistrationUserData(
@@ -318,6 +283,79 @@
std::move(icon_callback)));
}
+void ContentIndexDatabase::GetAllEntries(
+ ContentIndexContext::GetAllEntriesCallback callback) {
+ DCHECK_CURRENTLY_ON(BrowserThread::IO);
+
+ service_worker_context_->GetUserDataForAllRegistrationsByKeyPrefix(
+ kEntryPrefix,
+ base::BindOnce(&ContentIndexDatabase::DidGetEntries,
+ weak_ptr_factory_io_.GetWeakPtr(), std::move(callback)));
+}
+
+void ContentIndexDatabase::DidGetEntries(
+ ContentIndexContext::GetAllEntriesCallback callback,
+ const std::vector<std::pair<int64_t, std::string>>& user_data,
+ blink::ServiceWorkerStatusCode status) {
+ if (status != blink::ServiceWorkerStatusCode::kOk) {
+ // TODO(crbug.com/973844): Handle or report this error.
+ std::move(callback).Run(blink::mojom::ContentIndexError::STORAGE_ERROR,
+ /* entries= */ {});
+ return;
+ }
+
+ if (user_data.empty()) {
+ std::move(callback).Run(blink::mojom::ContentIndexError::NONE,
+ /* entries= */ {});
+ return;
+ }
+
+ std::vector<ContentIndexEntry> entries;
+ entries.reserve(user_data.size());
+
+ for (const auto& ud : user_data) {
+ auto entry = EntryFromSerializedProto(ud.first, ud.second);
+ if (!entry) {
+ // TODO(crbug.com/973844): Handle or report this error.
+ std::move(callback).Run(blink::mojom::ContentIndexError::STORAGE_ERROR,
+ /* entries= */ {});
+ return;
+ }
+
+ entries.emplace_back(std::move(*entry));
+ }
+
+ std::move(callback).Run(blink::mojom::ContentIndexError::NONE,
+ std::move(entries));
+}
+
+void ContentIndexDatabase::GetEntry(
+ int64_t service_worker_registration_id,
+ const std::string& description_id,
+ ContentIndexContext::GetEntryCallback callback) {
+ service_worker_context_->GetRegistrationUserData(
+ service_worker_registration_id, {EntryKey(description_id)},
+ base::BindOnce(&ContentIndexDatabase::DidGetEntry,
+ weak_ptr_factory_io_.GetWeakPtr(),
+ service_worker_registration_id, std::move(callback)));
+}
+
+void ContentIndexDatabase::DidGetEntry(
+ int64_t service_worker_registration_id,
+ ContentIndexContext::GetEntryCallback callback,
+ const std::vector<std::string>& data,
+ blink::ServiceWorkerStatusCode status) {
+ if (status != blink::ServiceWorkerStatusCode::kOk) {
+ // TODO(crbug.com/973844): Handle or report this error.
+ std::move(callback).Run(base::nullopt);
+ return;
+ }
+
+ DCHECK_EQ(data.size(), 1u);
+ std::move(callback).Run(
+ EntryFromSerializedProto(service_worker_registration_id, data.front()));
+}
+
void ContentIndexDatabase::Shutdown() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);