-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate image service to use new download status rather than unified …
…consent helper Bug: 1425071 Change-Id: I2c3295147ea755b841d96bd67030fbd34f6d195e Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4589410 Commit-Queue: Sophie Chang <sophiechang@chromium.org> Reviewed-by: Marc Treib <treib@chromium.org> Reviewed-by: Mihai Sardarescu <msarda@chromium.org> Cr-Commit-Position: refs/heads/main@{#1153862}
- Loading branch information
Sophie Chang
authored and
Chromium LUCI CQ
committed
Jun 6, 2023
1 parent
7860c6a
commit 647ee3a
Showing
13 changed files
with
421 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
components/page_image_service/image_service_consent_helper.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
// Copyright 2023 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "components/page_image_service/image_service_consent_helper.h" | ||
|
||
#include "base/feature_list.h" | ||
#include "components/page_image_service/features.h" | ||
#include "components/sync/service/sync_service.h" | ||
#include "components/unified_consent/consent_throttle.h" | ||
#include "components/unified_consent/url_keyed_data_collection_consent_helper.h" | ||
|
||
namespace page_image_service { | ||
|
||
namespace { | ||
|
||
constexpr base::TimeDelta kTimeout = base::Seconds(10); | ||
|
||
} // namespace | ||
|
||
ImageServiceConsentHelper::ImageServiceConsentHelper( | ||
syncer::SyncService* sync_service, | ||
syncer::ModelType model_type) | ||
: sync_service_(sync_service), model_type_(model_type) { | ||
if (base::FeatureList::IsEnabled(kImageServiceObserveSyncDownloadStatus)) { | ||
sync_service_observer_.Observe(sync_service); | ||
} else if (model_type == syncer::ModelType::BOOKMARKS) { | ||
consent_throttle_ = std::make_unique<unified_consent::ConsentThrottle>( | ||
unified_consent::UrlKeyedDataCollectionConsentHelper:: | ||
NewPersonalizedBookmarksDataCollectionConsentHelper(sync_service), | ||
kTimeout); | ||
} else if (model_type == syncer::ModelType::HISTORY_DELETE_DIRECTIVES) { | ||
consent_throttle_ = std::make_unique<unified_consent::ConsentThrottle>( | ||
unified_consent::UrlKeyedDataCollectionConsentHelper:: | ||
NewPersonalizedDataCollectionConsentHelper(sync_service), | ||
kTimeout); | ||
} else { | ||
NOTREACHED(); | ||
} | ||
} | ||
|
||
ImageServiceConsentHelper::~ImageServiceConsentHelper() = default; | ||
|
||
void ImageServiceConsentHelper::EnqueueRequest( | ||
base::OnceCallback<void(bool)> callback) { | ||
if (consent_throttle_) { | ||
consent_throttle_->EnqueueRequest(std::move(callback)); | ||
return; | ||
} | ||
|
||
absl::optional<bool> consent_status = GetConsentStatus(); | ||
if (consent_status.has_value()) { | ||
std::move(callback).Run(*consent_status); | ||
return; | ||
} | ||
|
||
enqueued_request_callbacks_.emplace_back(std::move(callback)); | ||
if (!request_processing_timer_.IsRunning()) { | ||
request_processing_timer_.Start( | ||
FROM_HERE, kTimeout, | ||
base::BindOnce( | ||
&ImageServiceConsentHelper::OnTimeoutExpired, | ||
// Unretained usage here okay, because this object owns the timer. | ||
base::Unretained(this))); | ||
} | ||
} | ||
|
||
void ImageServiceConsentHelper::OnStateChanged( | ||
syncer::SyncService* sync_service) { | ||
CHECK_EQ(sync_service_, sync_service); | ||
CHECK(base::FeatureList::IsEnabled(kImageServiceObserveSyncDownloadStatus)); | ||
|
||
absl::optional<bool> consent_status = GetConsentStatus(); | ||
if (!consent_status.has_value()) { | ||
return; | ||
} | ||
|
||
for (auto& request_callback : enqueued_request_callbacks_) { | ||
std::move(request_callback).Run(*consent_status); | ||
} | ||
|
||
enqueued_request_callbacks_.clear(); | ||
request_processing_timer_.Stop(); | ||
} | ||
|
||
void ImageServiceConsentHelper::OnSyncShutdown( | ||
syncer::SyncService* sync_service) { | ||
CHECK_EQ(sync_service_, sync_service); | ||
CHECK(base::FeatureList::IsEnabled(kImageServiceObserveSyncDownloadStatus)); | ||
|
||
sync_service_observer_.Reset(); | ||
} | ||
|
||
absl::optional<bool> ImageServiceConsentHelper::GetConsentStatus() { | ||
CHECK(base::FeatureList::IsEnabled(kImageServiceObserveSyncDownloadStatus)); | ||
|
||
syncer::SyncService::ModelTypeDownloadStatus download_status = | ||
sync_service_->GetDownloadStatusFor(model_type_); | ||
switch (download_status) { | ||
case syncer::SyncService::ModelTypeDownloadStatus::kWaitingForUpdates: | ||
return absl::nullopt; | ||
case syncer::SyncService::ModelTypeDownloadStatus::kUpToDate: | ||
return true; | ||
case syncer::SyncService::ModelTypeDownloadStatus::kError: | ||
return false; | ||
} | ||
} | ||
|
||
void ImageServiceConsentHelper::OnTimeoutExpired() { | ||
for (auto& request_callback : enqueued_request_callbacks_) { | ||
std::move(request_callback).Run(false); | ||
} | ||
enqueued_request_callbacks_.clear(); | ||
} | ||
|
||
} // namespace page_image_service |
75 changes: 75 additions & 0 deletions
75
components/page_image_service/image_service_consent_helper.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright 2023 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef COMPONENTS_PAGE_IMAGE_SERVICE_IMAGE_SERVICE_CONSENT_HELPER_H_ | ||
#define COMPONENTS_PAGE_IMAGE_SERVICE_IMAGE_SERVICE_CONSENT_HELPER_H_ | ||
|
||
#include <vector> | ||
|
||
#include "base/memory/raw_ptr.h" | ||
#include "base/scoped_observation.h" | ||
#include "base/time/time.h" | ||
#include "base/timer/timer.h" | ||
#include "components/sync/base/model_type.h" | ||
#include "components/sync/service/sync_service_observer.h" | ||
|
||
namespace syncer { | ||
class SyncService; | ||
} // namespace syncer | ||
|
||
namespace unified_consent { | ||
class ConsentThrottle; | ||
} // namespace unified_consent | ||
|
||
namespace page_image_service { | ||
|
||
// Helper class that observes SyncService for when it is appropriate to fetch | ||
// images for synced entities that have been viewed in the past. | ||
class ImageServiceConsentHelper : public syncer::SyncServiceObserver { | ||
public: | ||
ImageServiceConsentHelper(syncer::SyncService* sync_service, | ||
syncer::ModelType model_type); | ||
~ImageServiceConsentHelper() override; | ||
|
||
// SyncServiceObserver: | ||
void OnStateChanged(syncer::SyncService* sync_service) override; | ||
void OnSyncShutdown(syncer::SyncService* sync_service) override; | ||
|
||
// If Sync downloads for `model_type_` have already been initialized, this | ||
// method calls `callback` synchronously with the result. If not, it will hold | ||
// the request up until the timeout for the consent helper to initialize. | ||
void EnqueueRequest(base::OnceCallback<void(bool)> callback); | ||
|
||
private: | ||
// Returns whether it is appropriate to fetch images for synced entities of | ||
// `model_type_`. Will return nullopt if Sync Service is not ready yet. | ||
absl::optional<bool> GetConsentStatus(); | ||
|
||
// Run periodically to sweep away old queued requests. | ||
void OnTimeoutExpired(); | ||
|
||
// The sync service `this` is observing. | ||
raw_ptr<syncer::SyncService> sync_service_; | ||
|
||
// The model type `this` pertains to. | ||
const syncer::ModelType model_type_; | ||
|
||
// Requests waiting for the consent throttle to initialize. Requests are | ||
// stored in the queue in order of their arrival. | ||
std::vector<base::OnceCallback<void(bool)>> enqueued_request_callbacks_; | ||
|
||
// Timer used to periodically process unanswered enqueued requests, and | ||
// respond to them in the negative. | ||
base::OneShotTimer request_processing_timer_; | ||
|
||
// Consent throttle to be used if sync service is not being directly observed. | ||
std::unique_ptr<unified_consent::ConsentThrottle> consent_throttle_; | ||
|
||
base::ScopedObservation<syncer::SyncService, syncer::SyncServiceObserver> | ||
sync_service_observer_{this}; | ||
}; | ||
|
||
} // namespace page_image_service | ||
|
||
#endif // COMPONENTS_PAGE_IMAGE_SERVICE_IMAGE_SERVICE_CONSENT_HELPER_H_ |
Oops, something went wrong.