Commit 0c3d0392 authored by David Black's avatar David Black Committed by Commit Bot

Restrict proactive suggestions feature based on sync settings.

The proactive suggestions feature will not make requests to the backend
if either:
- User history sync is disabled, or
- User has a sync password

Bug: b:141025289
Change-Id: Iba8548e1f873a63c721a71bf8135b9a744da882d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1804066Reviewed-by: default avatarXiaohui Chen <xiaohuic@chromium.org>
Commit-Queue: David Black <dmblack@google.com>
Cr-Commit-Position: refs/heads/master@{#696881}
parent bcb4584a
......@@ -6,9 +6,40 @@
#include "ash/public/cpp/assistant/proactive_suggestions.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sync/profile_sync_service_factory.h"
#include "chrome/browser/ui/ash/assistant/proactive_suggestions_loader.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_list.h"
#include "components/sync/driver/sync_service.h"
#include "components/sync/driver/sync_user_settings.h"
namespace {
// Helpers ---------------------------------------------------------------------
syncer::SyncService* GetSyncService(Profile* profile) {
return profile->IsSyncAllowed()
? ProfileSyncServiceFactory::GetForProfile(profile)
: nullptr;
}
bool IsHistorySyncEnabledWithoutPassphrase(Profile* profile) {
auto* sync_service = GetSyncService(profile);
if (!sync_service)
return false;
const auto* user_settings = sync_service->GetUserSettings();
if (!user_settings)
return false;
return user_settings->GetSelectedTypes().Has(
syncer::UserSelectableType::kHistory) &&
!user_settings->IsUsingSecondaryPassphrase();
}
} // namespace
// ProactiveSuggestionsClientImpl ----------------------------------------------
ProactiveSuggestionsClientImpl::ProactiveSuggestionsClientImpl(
Profile* profile)
......@@ -20,6 +51,12 @@ ProactiveSuggestionsClientImpl::ProactiveSuggestionsClientImpl(
// We observe the singleton BrowserList to receive events pertaining to the
// currently active browser.
BrowserList::AddObserver(this);
// We observe the SyncService to detect changes in user sync settings which
// may affect whether or not we will observe the active browser.
auto* sync_service = GetSyncService(profile_);
if (sync_service)
sync_service->AddObserver(this);
}
ProactiveSuggestionsClientImpl::~ProactiveSuggestionsClientImpl() {
......@@ -31,6 +68,10 @@ ProactiveSuggestionsClientImpl::~ProactiveSuggestionsClientImpl() {
if (active_browser_)
active_browser_->tab_strip_model()->RemoveObserver(this);
auto* sync_service = GetSyncService(profile_);
if (sync_service)
sync_service->RemoveObserver(this);
BrowserList::RemoveObserver(this);
ash::AssistantState::Get()->RemoveObserver(this);
}
......@@ -90,6 +131,12 @@ void ProactiveSuggestionsClientImpl::OnAssistantContextEnabled(bool enabled) {
UpdateActiveState();
}
void ProactiveSuggestionsClientImpl::OnStateChanged(syncer::SyncService* sync) {
// When the state of the SyncService has changed, we may need to resume/pause
// observation of the browser. We accomplish this by updating active state.
UpdateActiveState();
}
void ProactiveSuggestionsClientImpl::SetActiveBrowser(Browser* browser) {
if (browser == active_browser_)
return;
......@@ -161,10 +208,13 @@ void ProactiveSuggestionsClientImpl::UpdateActiveState() {
auto* tab_strip_model = active_browser_->tab_strip_model();
// We never observe browsers that are off the record and we never observe
// browsers when the user has disabled either Assistant or screen context.
// browsers when the user has disabled either Assistant or screen context. We
// also don't observe the browser when the user has disabled history sync or
// is using a sync passphrase.
if (active_browser_->profile()->IsOffTheRecord() ||
!ash::AssistantState::Get()->settings_enabled().value_or(false) ||
!ash::AssistantState::Get()->context_enabled().value_or(false)) {
!ash::AssistantState::Get()->context_enabled().value_or(false) ||
!IsHistorySyncEnabledWithoutPassphrase(profile_)) {
tab_strip_model->RemoveObserver(this);
SetActiveContents(nullptr);
return;
......
......@@ -13,6 +13,7 @@
#include "base/memory/scoped_refptr.h"
#include "chrome/browser/ui/browser_list_observer.h"
#include "chrome/browser/ui/tabs/tab_strip_model_observer.h"
#include "components/sync/driver/sync_service_observer.h"
#include "content/public/browser/web_contents_observer.h"
class ProactiveSuggestionsLoader;
......@@ -25,7 +26,8 @@ class ProactiveSuggestionsClientImpl : public ash::ProactiveSuggestionsClient,
public BrowserListObserver,
public TabStripModelObserver,
public content::WebContentsObserver,
public ash::AssistantStateObserver {
public ash::AssistantStateObserver,
public syncer::SyncServiceObserver {
public:
explicit ProactiveSuggestionsClientImpl(Profile* profile);
~ProactiveSuggestionsClientImpl() override;
......@@ -52,6 +54,9 @@ class ProactiveSuggestionsClientImpl : public ash::ProactiveSuggestionsClient,
void OnAssistantSettingsEnabled(bool enabled) override;
void OnAssistantContextEnabled(bool enabled) override;
// syncer::SyncServiceObserver:
void OnStateChanged(syncer::SyncService* sync) override;
private:
void SetActiveBrowser(Browser* browser);
void SetActiveContents(content::WebContents* contents);
......
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