Skip to content

Commit

Permalink
Sync: Add a SyncTypePreferenceProvider interface to specify sync type…
Browse files Browse the repository at this point in the history
…s to enable. The PSS evaluates these.

For now, this is used by the SupervisedUserService to specify data types that must always be enabled.

BUG=395105

Review URL: https://codereview.chromium.org/428143002

Cr-Commit-Position: refs/heads/master@{#288951}
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@288951 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
treib@chromium.org committed Aug 12, 2014
1 parent af66c12 commit 3a276ff
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 14 deletions.
46 changes: 37 additions & 9 deletions chrome/browser/supervised_user/supervised_user_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -141,21 +141,31 @@ SupervisedUserService::SupervisedUserService(Profile* profile)
waiting_for_sync_initialization_(false),
is_profile_active_(false),
elevated_for_testing_(false),
did_init_(false),
did_shutdown_(false),
waiting_for_permissions_(false),
weak_ptr_factory_(this) {
}

SupervisedUserService::~SupervisedUserService() {
DCHECK(did_shutdown_);
DCHECK(!did_init_ || did_shutdown_);
}

void SupervisedUserService::Shutdown() {
if (!did_init_)
return;
DCHECK(!did_shutdown_);
did_shutdown_ = true;
if (ProfileIsSupervised()) {
content::RecordAction(UserMetricsAction("ManagedUsers_QuitBrowser"));
}
SetActive(false);

ProfileSyncService* sync_service =
ProfileSyncServiceFactory::GetForProfile(profile_);
// Can be null in tests.
if (sync_service)
sync_service->RemovePreferenceProvider(this);
}

bool SupervisedUserService::ProfileIsSupervised() const {
Expand Down Expand Up @@ -335,6 +345,21 @@ void SupervisedUserService::OnExtensionUnloaded(
}
#endif // defined(ENABLE_EXTENSIONS)

syncer::ModelTypeSet SupervisedUserService::GetPreferredDataTypes() const {
if (!ProfileIsSupervised())
return syncer::ModelTypeSet();

syncer::ModelTypeSet result;
result.Put(syncer::SESSIONS);
result.Put(syncer::EXTENSIONS);
result.Put(syncer::EXTENSION_SETTINGS);
result.Put(syncer::APPS);
result.Put(syncer::APP_SETTINGS);
result.Put(syncer::APP_NOTIFICATIONS);
result.Put(syncer::APP_LIST);
return result;
}

void SupervisedUserService::OnStateChanged() {
ProfileSyncService* service =
ProfileSyncServiceFactory::GetForProfile(profile_);
Expand Down Expand Up @@ -382,11 +407,9 @@ void SupervisedUserService::FinishSetupSync() {
ProfileSyncServiceFactory::GetForProfile(profile_);
DCHECK(service->sync_initialized());

// Sync nothing (except types which are set via GetPreferredDataTypes).
bool sync_everything = false;
syncer::ModelTypeSet synced_datatypes;
synced_datatypes.Put(syncer::SESSIONS);
synced_datatypes.Put(syncer::APPS);
synced_datatypes.Put(syncer::EXTENSIONS);
service->OnUserChoseDatatypes(sync_everything, synced_datatypes);

// Notify ProfileSyncService that we are done with configuration.
Expand Down Expand Up @@ -567,6 +590,8 @@ void SupervisedUserService::InitSync(const std::string& refresh_token) {
}

void SupervisedUserService::Init() {
DCHECK(!did_init_);
did_init_ = true;
DCHECK(GetSettingsService()->IsReady());

pref_change_registrar_.Init(profile_->GetPrefs());
Expand All @@ -575,6 +600,12 @@ void SupervisedUserService::Init() {
base::Bind(&SupervisedUserService::OnSupervisedUserIdChanged,
base::Unretained(this)));

ProfileSyncService* sync_service =
ProfileSyncServiceFactory::GetForProfile(profile_);
// Can be null in tests.
if (sync_service)
sync_service->AddPreferenceProvider(this);

SetActive(ProfileIsSupervised());
}

Expand Down Expand Up @@ -666,11 +697,8 @@ void SupervisedUserService::SetActive(bool active) {
pref_change_registrar_.Remove(prefs::kSupervisedUserManualHosts);
pref_change_registrar_.Remove(prefs::kSupervisedUserManualURLs);

if (waiting_for_sync_initialization_) {
ProfileSyncService* sync_service =
ProfileSyncServiceFactory::GetForProfile(profile_);
sync_service->RemoveObserver(this);
}
if (waiting_for_sync_initialization_)
ProfileSyncServiceFactory::GetForProfile(profile_)->RemoveObserver(this);

#if !defined(OS_ANDROID)
// TODO(bauerb): Get rid of the platform-specific #ifdef here.
Expand Down
10 changes: 9 additions & 1 deletion chrome/browser/supervised_user/supervised_user_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "chrome/browser/supervised_user/supervised_user_url_filter.h"
#include "chrome/browser/supervised_user/supervised_users.h"
#include "chrome/browser/sync/profile_sync_service_observer.h"
#include "chrome/browser/sync/sync_type_preference_provider.h"
#include "chrome/browser/ui/browser_list_observer.h"
#include "components/keyed_service/core/keyed_service.h"
#include "content/public/browser/web_contents.h"
Expand Down Expand Up @@ -51,6 +52,7 @@ class SupervisedUserService : public KeyedService,
public extensions::ManagementPolicy::Provider,
public extensions::ExtensionRegistryObserver,
#endif
public SyncTypePreferenceProvider,
public ProfileSyncServiceObserver,
public chrome::BrowserListObserver {
public:
Expand Down Expand Up @@ -139,7 +141,7 @@ class SupervisedUserService : public KeyedService,
// Convenience method that registers this supervised user using
// |registration_utility| and initializes sync with the returned token.
// The |callback| will be called when registration is complete,
// whether it suceeded or not -- unless registration was cancelled manually,
// whether it succeeded or not -- unless registration was cancelled manually,
// in which case the callback will be ignored.
void RegisterAndInitSync(
SupervisedUserRegistrationUtility* registration_utility,
Expand Down Expand Up @@ -172,6 +174,9 @@ class SupervisedUserService : public KeyedService,
extensions::UnloadedExtensionInfo::Reason reason) OVERRIDE;
#endif

// SyncTypePreferenceProvider implementation:
virtual syncer::ModelTypeSet GetPreferredDataTypes() const OVERRIDE;

// ProfileSyncServiceObserver implementation:
virtual void OnStateChanged() OVERRIDE;

Expand Down Expand Up @@ -291,6 +296,9 @@ class SupervisedUserService : public KeyedService,
// Sets a profile in elevated state for testing if set to true.
bool elevated_for_testing_;

// True only when |Init()| method has been called.
bool did_init_;

// True only when |Shutdown()| method has been called.
bool did_shutdown_;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
#include "chrome/browser/supervised_user/supervised_user_service.h"
#include "chrome/browser/sync/profile_sync_service_factory.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"

#if defined(ENABLE_EXTENSIONS)
Expand Down Expand Up @@ -41,6 +42,7 @@ SupervisedUserServiceFactory::SupervisedUserServiceFactory()
extensions::ExtensionsBrowserClient::Get()->GetExtensionSystemFactory());
#endif
DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance());
DependsOn(ProfileSyncServiceFactory::GetInstance());
}

SupervisedUserServiceFactory::~SupervisedUserServiceFactory() {}
Expand Down
38 changes: 35 additions & 3 deletions chrome/browser/sync/profile_sync_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

#include <cstddef>
#include <map>
#include <set>
#include <utility>
#include <vector>

#include "base/basictypes.h"
#include "base/bind.h"
Expand Down Expand Up @@ -55,6 +54,7 @@
#include "chrome/browser/sync/sessions/sessions_sync_manager.h"
#include "chrome/browser/sync/supervised_user_signin_manager_wrapper.h"
#include "chrome/browser/sync/sync_error_controller.h"
#include "chrome/browser/sync/sync_type_preference_provider.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_window.h"
Expand Down Expand Up @@ -1824,7 +1824,8 @@ ProfileSyncService::GetPreferredDirectoryDataTypes() const {
GetRegisteredDirectoryDataTypes();
const syncer::ModelTypeSet preferred_types =
sync_prefs_.GetPreferredDataTypes(registered_directory_types);
return preferred_types;

return Union(preferred_types, GetDataTypesFromPreferenceProviders());
}

syncer::ModelTypeSet
Expand Down Expand Up @@ -2280,6 +2281,25 @@ void ProfileSyncService::RemoveTypeDebugInfoObserver(
}
}

void ProfileSyncService::AddPreferenceProvider(
SyncTypePreferenceProvider* provider) {
DCHECK(!HasPreferenceProvider(provider))
<< "Providers may only be added once!";
preference_providers_.insert(provider);
}

void ProfileSyncService::RemovePreferenceProvider(
SyncTypePreferenceProvider* provider) {
DCHECK(HasPreferenceProvider(provider))
<< "Only providers that have been added before can be removed!";
preference_providers_.erase(provider);
}

bool ProfileSyncService::HasPreferenceProvider(
SyncTypePreferenceProvider* provider) const {
return preference_providers_.count(provider) > 0;
}

namespace {

class GetAllNodesRequestHelper
Expand Down Expand Up @@ -2468,6 +2488,18 @@ void ProfileSyncService::ReconfigureDatatypeManager() {
}
}

syncer::ModelTypeSet ProfileSyncService::GetDataTypesFromPreferenceProviders()
const {
syncer::ModelTypeSet types;
for (std::set<SyncTypePreferenceProvider*>::const_iterator it =
preference_providers_.begin();
it != preference_providers_.end();
++it) {
types.PutAll((*it)->GetPreferredDataTypes());
}
return types;
}

const FailedDataTypesHandler& ProfileSyncService::failed_data_types_handler()
const {
return failed_data_types_handler_;
Expand Down
17 changes: 16 additions & 1 deletion chrome/browser/sync/profile_sync_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
#ifndef CHROME_BROWSER_SYNC_PROFILE_SYNC_SERVICE_H_
#define CHROME_BROWSER_SYNC_PROFILE_SYNC_SERVICE_H_

#include <set>
#include <string>
#include <utility>
#include <vector>

#include "base/basictypes.h"
#include "base/compiler_specific.h"
Expand Down Expand Up @@ -57,6 +57,7 @@ class ProfileOAuth2TokenService;
class ProfileSyncComponentsFactory;
class SupervisedUserSigninManagerWrapper;
class SyncErrorController;
class SyncTypePreferenceProvider;

namespace base {
class CommandLine;
Expand Down Expand Up @@ -301,6 +302,15 @@ class ProfileSyncService : public ProfileSyncServiceBase,
void AddTypeDebugInfoObserver(syncer::TypeDebugInfoObserver* observer);
void RemoveTypeDebugInfoObserver(syncer::TypeDebugInfoObserver* observer);

// Add a sync type preference provider. Each provider may only be added once.
void AddPreferenceProvider(SyncTypePreferenceProvider* provider);
// Remove a sync type preference provider. May only be called for providers
// that have been added. Providers must not remove themselves while being
// called back.
void RemovePreferenceProvider(SyncTypePreferenceProvider* provider);
// Check whether a given sync type preference provider has been added.
bool HasPreferenceProvider(SyncTypePreferenceProvider* provider) const;

// Asynchronously fetches base::Value representations of all sync nodes and
// returns them to the specified callback on this thread.
//
Expand Down Expand Up @@ -915,6 +925,9 @@ class ProfileSyncService : public ProfileSyncServiceBase,
// want to startup once more.
virtual void ReconfigureDatatypeManager();

// Collects preferred sync data types from |preference_providers_|.
syncer::ModelTypeSet GetDataTypesFromPreferenceProviders() const;

// Called when the user changes the sync configuration, to update the UMA
// stats.
void UpdateSelectedTypesHistogram(
Expand Down Expand Up @@ -1019,6 +1032,8 @@ class ProfileSyncService : public ProfileSyncServiceBase,
ObserverList<browser_sync::ProtocolEventObserver> protocol_event_observers_;
ObserverList<syncer::TypeDebugInfoObserver> type_debug_info_observers_;

std::set<SyncTypePreferenceProvider*> preference_providers_;

syncer::SyncJsController sync_js_controller_;

// This allows us to gracefully handle an ABORTED return code from the
Expand Down
18 changes: 18 additions & 0 deletions chrome/browser/sync/sync_type_preference_provider.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2014 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_SYNC_SYNC_TYPE_PREFERENCE_PROVIDER_H_
#define CHROME_BROWSER_SYNC_SYNC_TYPE_PREFERENCE_PROVIDER_H_

#include "sync/internal_api/public/base/model_type.h"

class SyncTypePreferenceProvider {
public:
virtual syncer::ModelTypeSet GetPreferredDataTypes() const = 0;

protected:
virtual ~SyncTypePreferenceProvider() {}
};

#endif // CHROME_BROWSER_SYNC_SYNC_TYPE_PREFERENCE_PROVIDER_H_
1 change: 1 addition & 0 deletions chrome/chrome_browser.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -1376,6 +1376,7 @@
'browser/sync/sync_error_controller.h',
'browser/sync/sync_startup_tracker.cc',
'browser/sync/sync_startup_tracker.h',
'browser/sync/sync_type_preference_provider.h',
'browser/tab_contents/navigation_metrics_recorder.cc',
'browser/tab_contents/navigation_metrics_recorder.h',
'browser/tab_contents/retargeting_details.h',
Expand Down

0 comments on commit 3a276ff

Please sign in to comment.