Commit dab6c6fa authored by bauerb's avatar bauerb Committed by Commit bot

Enable invalidations for supervised user settings on Android.

BUG=none

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

Cr-Commit-Position: refs/heads/master@{#292353}
parent ad468e8b
...@@ -8,6 +8,7 @@ import android.content.Context; ...@@ -8,6 +8,7 @@ import android.content.Context;
import android.util.Log; import android.util.Log;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Joiner;
import org.chromium.base.CalledByNative; import org.chromium.base.CalledByNative;
import org.chromium.base.ThreadUtils; import org.chromium.base.ThreadUtils;
...@@ -18,6 +19,8 @@ import org.chromium.sync.internal_api.pub.base.ModelType; ...@@ -18,6 +19,8 @@ import org.chromium.sync.internal_api.pub.base.ModelType;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
/** /**
...@@ -345,6 +348,11 @@ public class ProfileSyncService { ...@@ -345,6 +348,11 @@ public class ProfileSyncService {
public Set<ModelType> getPreferredDataTypes() { public Set<ModelType> getPreferredDataTypes() {
long modelTypeSelection = long modelTypeSelection =
nativeGetEnabledDataTypes(mNativeProfileSyncServiceAndroid); nativeGetEnabledDataTypes(mNativeProfileSyncServiceAndroid);
return modelTypeSelectionToSet(modelTypeSelection);
}
@VisibleForTesting
public static Set<ModelType> modelTypeSelectionToSet(long modelTypeSelection) {
Set<ModelType> syncTypes = new HashSet<ModelType>(); Set<ModelType> syncTypes = new HashSet<ModelType>();
if ((modelTypeSelection & ModelTypeSelection.AUTOFILL) != 0) { if ((modelTypeSelection & ModelTypeSelection.AUTOFILL) != 0) {
syncTypes.add(ModelType.AUTOFILL); syncTypes.add(ModelType.AUTOFILL);
...@@ -385,6 +393,9 @@ public class ProfileSyncService { ...@@ -385,6 +393,9 @@ public class ProfileSyncService {
if ((modelTypeSelection & ModelTypeSelection.FAVICON_TRACKING) != 0) { if ((modelTypeSelection & ModelTypeSelection.FAVICON_TRACKING) != 0) {
syncTypes.add(ModelType.FAVICON_TRACKING); syncTypes.add(ModelType.FAVICON_TRACKING);
} }
if ((modelTypeSelection & ModelTypeSelection.SUPERVISED_USER_SETTING) != 0) {
syncTypes.add(ModelType.MANAGED_USER_SETTING);
}
return syncTypes; return syncTypes;
} }
...@@ -513,6 +524,17 @@ public class ProfileSyncService { ...@@ -513,6 +524,17 @@ public class ProfileSyncService {
nativeOverrideNetworkResourcesForTest(mNativeProfileSyncServiceAndroid, networkResources); nativeOverrideNetworkResourcesForTest(mNativeProfileSyncServiceAndroid, networkResources);
} }
@CalledByNative
private static String modelTypeSelectionToStringForTest(long modelTypeSelection) {
SortedSet<String> set = new TreeSet<String>();
Set<ModelType> filteredTypes = ModelType.filterOutNonInvalidationTypes(
modelTypeSelectionToSet(modelTypeSelection));
for (ModelType type : filteredTypes) {
set.add(type.toString());
}
return Joiner.on(", ").join(set);
}
// Native methods // Native methods
private native void nativeNudgeSyncer( private native void nativeNudgeSyncer(
long nativeProfileSyncServiceAndroid, int objectSource, String objectId, long version, long nativeProfileSyncServiceAndroid, int objectSource, String objectId, long version,
......
...@@ -279,16 +279,6 @@ void ProfileSyncComponentsFactoryImpl::RegisterCommonDataTypes( ...@@ -279,16 +279,6 @@ void ProfileSyncComponentsFactoryImpl::RegisterCommonDataTypes(
syncer::SUPERVISED_USER_SETTINGS, syncer::SUPERVISED_USER_SETTINGS,
this, this,
profile_)); profile_));
pss->RegisterDataTypeController(
new SupervisedUserSyncDataTypeController(
syncer::SUPERVISED_USERS,
this,
profile_));
pss->RegisterDataTypeController(
new SupervisedUserSyncDataTypeController(
syncer::SUPERVISED_USER_SHARED_SETTINGS,
this,
profile_));
#endif #endif
} }
...@@ -400,6 +390,19 @@ void ProfileSyncComponentsFactoryImpl::RegisterDesktopDataTypes( ...@@ -400,6 +390,19 @@ void ProfileSyncComponentsFactoryImpl::RegisterDesktopDataTypes(
this)); this));
} }
#endif #endif
#if defined(ENABLE_MANAGED_USERS)
pss->RegisterDataTypeController(
new SupervisedUserSyncDataTypeController(
syncer::SUPERVISED_USERS,
this,
profile_));
pss->RegisterDataTypeController(
new SupervisedUserSyncDataTypeController(
syncer::SUPERVISED_USER_SHARED_SETTINGS,
this,
profile_));
#endif
} }
DataTypeManager* ProfileSyncComponentsFactoryImpl::CreateDataTypeManager( DataTypeManager* ProfileSyncComponentsFactoryImpl::CreateDataTypeManager(
......
...@@ -347,49 +347,9 @@ jboolean ProfileSyncServiceAndroid::IsSyncKeystoreMigrationDone( ...@@ -347,49 +347,9 @@ jboolean ProfileSyncServiceAndroid::IsSyncKeystoreMigrationDone(
jlong ProfileSyncServiceAndroid::GetEnabledDataTypes(JNIEnv* env, jlong ProfileSyncServiceAndroid::GetEnabledDataTypes(JNIEnv* env,
jobject obj) { jobject obj) {
jlong model_type_selection = 0;
syncer::ModelTypeSet types = sync_service_->GetActiveDataTypes(); syncer::ModelTypeSet types = sync_service_->GetActiveDataTypes();
types.PutAll(syncer::ControlTypes()); types.PutAll(syncer::ControlTypes());
if (types.Has(syncer::BOOKMARKS)) { return ModelTypeSetToSelection(types);
model_type_selection |= BOOKMARK;
}
if (types.Has(syncer::AUTOFILL)) {
model_type_selection |= AUTOFILL;
}
if (types.Has(syncer::AUTOFILL_PROFILE)) {
model_type_selection |= AUTOFILL_PROFILE;
}
if (types.Has(syncer::PASSWORDS)) {
model_type_selection |= PASSWORD;
}
if (types.Has(syncer::TYPED_URLS)) {
model_type_selection |= TYPED_URL;
}
if (types.Has(syncer::SESSIONS)) {
model_type_selection |= SESSION;
}
if (types.Has(syncer::HISTORY_DELETE_DIRECTIVES)) {
model_type_selection |= HISTORY_DELETE_DIRECTIVE;
}
if (types.Has(syncer::PROXY_TABS)) {
model_type_selection |= PROXY_TABS;
}
if (types.Has(syncer::FAVICON_IMAGES)) {
model_type_selection |= FAVICON_IMAGE;
}
if (types.Has(syncer::FAVICON_TRACKING)) {
model_type_selection |= FAVICON_TRACKING;
}
if (types.Has(syncer::DEVICE_INFO)) {
model_type_selection |= DEVICE_INFO;
}
if (types.Has(syncer::NIGORI)) {
model_type_selection |= NIGORI;
}
if (types.Has(syncer::EXPERIMENTS)) {
model_type_selection |= EXPERIMENTS;
}
return model_type_selection;
} }
void ProfileSyncServiceAndroid::SetPreferredDataTypes( void ProfileSyncServiceAndroid::SetPreferredDataTypes(
...@@ -486,6 +446,64 @@ void ProfileSyncServiceAndroid::OverrideNetworkResourcesForTest( ...@@ -486,6 +446,64 @@ void ProfileSyncServiceAndroid::OverrideNetworkResourcesForTest(
make_scoped_ptr<syncer::NetworkResources>(resources)); make_scoped_ptr<syncer::NetworkResources>(resources));
} }
// static
jlong ProfileSyncServiceAndroid::ModelTypeSetToSelection(
syncer::ModelTypeSet types) {
jlong model_type_selection = 0;
if (types.Has(syncer::BOOKMARKS)) {
model_type_selection |= BOOKMARK;
}
if (types.Has(syncer::AUTOFILL)) {
model_type_selection |= AUTOFILL;
}
if (types.Has(syncer::AUTOFILL_PROFILE)) {
model_type_selection |= AUTOFILL_PROFILE;
}
if (types.Has(syncer::PASSWORDS)) {
model_type_selection |= PASSWORD;
}
if (types.Has(syncer::TYPED_URLS)) {
model_type_selection |= TYPED_URL;
}
if (types.Has(syncer::SESSIONS)) {
model_type_selection |= SESSION;
}
if (types.Has(syncer::HISTORY_DELETE_DIRECTIVES)) {
model_type_selection |= HISTORY_DELETE_DIRECTIVE;
}
if (types.Has(syncer::PROXY_TABS)) {
model_type_selection |= PROXY_TABS;
}
if (types.Has(syncer::FAVICON_IMAGES)) {
model_type_selection |= FAVICON_IMAGE;
}
if (types.Has(syncer::FAVICON_TRACKING)) {
model_type_selection |= FAVICON_TRACKING;
}
if (types.Has(syncer::DEVICE_INFO)) {
model_type_selection |= DEVICE_INFO;
}
if (types.Has(syncer::NIGORI)) {
model_type_selection |= NIGORI;
}
if (types.Has(syncer::EXPERIMENTS)) {
model_type_selection |= EXPERIMENTS;
}
if (types.Has(syncer::SUPERVISED_USER_SETTINGS)) {
model_type_selection |= SUPERVISED_USER_SETTING;
}
return model_type_selection;
}
// static
std::string ProfileSyncServiceAndroid::ModelTypeSelectionToStringForTest(
jlong model_type_selection) {
ScopedJavaLocalRef<jstring> string =
Java_ProfileSyncService_modelTypeSelectionToStringForTest(
AttachCurrentThread(), model_type_selection);
return ConvertJavaStringToUTF8(string);
}
void ProfileSyncServiceAndroid::NudgeSyncer(JNIEnv* env, void ProfileSyncServiceAndroid::NudgeSyncer(JNIEnv* env,
jobject obj, jobject obj,
jint objectSource, jint objectSource,
......
...@@ -204,6 +204,14 @@ class ProfileSyncServiceAndroid : public ProfileSyncServiceObserver { ...@@ -204,6 +204,14 @@ class ProfileSyncServiceAndroid : public ProfileSyncServiceObserver {
jobject obj, jobject obj,
jlong network_resources); jlong network_resources);
// Public for tests.
static jlong ModelTypeSetToSelection(syncer::ModelTypeSet model_types);
// Converts a bitmap of model types to a set of Java ModelTypes, and returns
// their string descriptions separated by commas.
static std::string ModelTypeSelectionToStringForTest(
jlong model_type_selection);
static ProfileSyncServiceAndroid* GetProfileSyncServiceAndroid(); static ProfileSyncServiceAndroid* GetProfileSyncServiceAndroid();
// Registers the ProfileSyncServiceAndroid's native methods through JNI. // Registers the ProfileSyncServiceAndroid's native methods through JNI.
......
// 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.
#include <algorithm>
#include <vector>
#include "base/command_line.h"
#include "base/memory/scoped_ptr.h"
#include "base/strings/string_util.h"
#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
#include "chrome/browser/sync/profile_sync_components_factory_impl.h"
#include "chrome/browser/sync/profile_sync_service.h"
#include "chrome/browser/sync/profile_sync_service_android.h"
#include "chrome/browser/sync/supervised_user_signin_manager_wrapper.h"
#include "chrome/test/base/testing_profile.h"
#include "components/signin/core/browser/profile_oauth2_token_service.h"
#include "components/sync_driver/data_type_controller.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "sync/internal_api/public/base/model_type.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
int NumberOfSetBits(jlong bitmask) {
int num = 0;
while (bitmask > 0) {
num += (bitmask & 1);
bitmask >>= 1;
}
return num;
}
} // namespace
class ProfileSyncServiceAndroidTest : public testing::Test {
public:
ProfileSyncServiceAndroidTest()
: command_line_(base::CommandLine::NO_PROGRAM) {}
virtual ~ProfileSyncServiceAndroidTest() {}
virtual void SetUp() OVERRIDE {
ProfileOAuth2TokenService* token_service =
ProfileOAuth2TokenServiceFactory::GetForProfile(&profile_);
ProfileSyncComponentsFactory* factory =
new ProfileSyncComponentsFactoryImpl(
&profile_,
&command_line_,
GURL(),
token_service,
profile_.GetRequestContext());
sync_service_.reset(new ProfileSyncService(
make_scoped_ptr(factory),
&profile_,
scoped_ptr<SupervisedUserSigninManagerWrapper>(),
token_service,
browser_sync::MANUAL_START));
factory->RegisterDataTypes(sync_service_.get());
}
protected:
syncer::ModelTypeSet GetRegisteredDataTypes() {
sync_driver::DataTypeController::StateMap controller_states;
sync_service_->GetDataTypeControllerStates(&controller_states);
syncer::ModelTypeSet model_types;
for (sync_driver::DataTypeController::StateMap::const_iterator it =
controller_states.begin();
it != controller_states.end(); ++it) {
model_types.Put(it->first);
}
return model_types;
}
private:
base::CommandLine command_line_;
content::TestBrowserThreadBundle thread_bundle_;
TestingProfile profile_;
scoped_ptr<ProfileSyncService> sync_service_;
};
TEST_F(ProfileSyncServiceAndroidTest, ModelTypesToInvalidationNames) {
syncer::ModelTypeSet model_types = GetRegisteredDataTypes();
jlong model_type_selection =
ProfileSyncServiceAndroid::ModelTypeSetToSelection(model_types);
// The number of set bits in the model type bitmask should be equal to the
// number of model types.
EXPECT_EQ(static_cast<int>(model_types.Size()),
NumberOfSetBits(model_type_selection));
std::vector<std::string> invalidation_names;
for (syncer::ModelTypeSet::Iterator it = model_types.First(); it.Good();
it.Inc()) {
std::string notification_type;
if (syncer::RealModelTypeToNotificationType(it.Get(), &notification_type))
invalidation_names.push_back(notification_type);
}
std::sort(invalidation_names.begin(), invalidation_names.end());
EXPECT_EQ(JoinString(invalidation_names, ", "),
ProfileSyncServiceAndroid::ModelTypeSelectionToStringForTest(
model_type_selection));
}
...@@ -33,3 +33,5 @@ DEFINE_MODEL_TYPE_SELECTION(NIGORI, 1<<10) ...@@ -33,3 +33,5 @@ DEFINE_MODEL_TYPE_SELECTION(NIGORI, 1<<10)
DEFINE_MODEL_TYPE_SELECTION(DEVICE_INFO, 1<<11) DEFINE_MODEL_TYPE_SELECTION(DEVICE_INFO, 1<<11)
DEFINE_MODEL_TYPE_SELECTION(EXPERIMENTS, 1<<12) DEFINE_MODEL_TYPE_SELECTION(EXPERIMENTS, 1<<12)
DEFINE_MODEL_TYPE_SELECTION(SUPERVISED_USER_SETTING, 1<<13)
...@@ -1331,6 +1331,7 @@ ...@@ -1331,6 +1331,7 @@
'browser/sync/glue/ui_model_worker_unittest.cc', 'browser/sync/glue/ui_model_worker_unittest.cc',
'browser/sync/profile_sync_auth_provider_unittest.cc', 'browser/sync/profile_sync_auth_provider_unittest.cc',
'browser/sync/profile_sync_components_factory_impl_unittest.cc', 'browser/sync/profile_sync_components_factory_impl_unittest.cc',
'browser/sync/profile_sync_service_android_unittest.cc',
'browser/sync/profile_sync_service_autofill_unittest.cc', 'browser/sync/profile_sync_service_autofill_unittest.cc',
'browser/sync/profile_sync_service_bookmark_unittest.cc', 'browser/sync/profile_sync_service_bookmark_unittest.cc',
'browser/sync/profile_sync_service_startup_unittest.cc', 'browser/sync/profile_sync_service_startup_unittest.cc',
......
...@@ -73,7 +73,12 @@ public enum ModelType { ...@@ -73,7 +73,12 @@ public enum ModelType {
/** /**
* A favicon tracking object. * A favicon tracking object.
*/ */
FAVICON_TRACKING("FAVICON_TRACKING"); FAVICON_TRACKING("FAVICON_TRACKING"),
/**
* A supervised user setting object. The old name "managed user" is used for backwards
* compatibility.
*/
MANAGED_USER_SETTING("MANAGED_USER_SETTING");
/** Special type representing all possible types. */ /** Special type representing all possible types. */
public static final String ALL_TYPES_TYPE = "ALL_TYPES"; public static final String ALL_TYPES_TYPE = "ALL_TYPES";
...@@ -85,6 +90,7 @@ public enum ModelType { ...@@ -85,6 +90,7 @@ public enum ModelType {
private final boolean mNonInvalidationType; private final boolean mNonInvalidationType;
ModelType(String modelType, boolean nonInvalidationType) { ModelType(String modelType, boolean nonInvalidationType) {
assert nonInvalidationType || modelType == toString();
mModelType = modelType; mModelType = modelType;
mNonInvalidationType = nonInvalidationType; mNonInvalidationType = nonInvalidationType;
} }
...@@ -165,21 +171,33 @@ public enum ModelType { ...@@ -165,21 +171,33 @@ public enum ModelType {
* This strips out any {@link ModelType} that is not an invalidation type. * This strips out any {@link ModelType} that is not an invalidation type.
*/ */
public static Set<ObjectId> modelTypesToObjectIds(Set<ModelType> modelTypes) { public static Set<ObjectId> modelTypesToObjectIds(Set<ModelType> modelTypes) {
Set<ObjectId> objectIds = new HashSet<ObjectId>(modelTypes.size()); Set<ModelType> filteredModelTypes = filterOutNonInvalidationTypes(modelTypes);
for (ModelType modelType : modelTypes) { Set<ObjectId> objectIds = new HashSet<ObjectId>(filteredModelTypes.size());
if (!modelType.isNonInvalidationType()) { for (ModelType modelType : filteredModelTypes) {
objectIds.add(modelType.toObjectId()); objectIds.add(modelType.toObjectId());
} }
}
return objectIds; return objectIds;
} }
/** Converts a set of {@link ModelType} to a set of string names. */ /** Converts a set of {@link ModelType} to a set of string names. */
public static Set<String> modelTypesToSyncTypes(Set<ModelType> modelTypes) { public static Set<String> modelTypesToSyncTypesForTest(Set<ModelType> modelTypes) {
Set<String> objectIds = new HashSet<String>(modelTypes.size()); Set<String> objectIds = new HashSet<String>(modelTypes.size());
for (ModelType modelType : modelTypes) { for (ModelType modelType : modelTypes) {
objectIds.add(modelType.toString()); objectIds.add(modelType.toString());
} }
return objectIds; return objectIds;
} }
/** Filters out non-invalidation types from a set of {@link ModelType}. */
@VisibleForTesting
public static Set<ModelType> filterOutNonInvalidationTypes(Set<ModelType> modelTypes) {
Set<ModelType> filteredTypes = new HashSet<ModelType>(modelTypes.size());
for (ModelType modelType : modelTypes) {
if (!modelType.isNonInvalidationType()) {
filteredTypes.add(modelType);
}
}
return filteredTypes;
}
} }
...@@ -529,7 +529,7 @@ public class InvalidationServiceTest extends ServiceTestCase<TestableInvalidatio ...@@ -529,7 +529,7 @@ public class InvalidationServiceTest extends ServiceTestCase<TestableInvalidatio
assertTrue(InvalidationService.getIsClientStartedForTest()); assertTrue(InvalidationService.getIsClientStartedForTest());
InvalidationPreferences invPrefs = new InvalidationPreferences(getContext()); InvalidationPreferences invPrefs = new InvalidationPreferences(getContext());
assertEquals(account, invPrefs.getSavedSyncedAccount()); assertEquals(account, invPrefs.getSavedSyncedAccount());
assertEquals(ModelType.modelTypesToSyncTypes(desiredRegistrations), assertEquals(ModelType.modelTypesToSyncTypesForTest(desiredRegistrations),
invPrefs.getSavedSyncedTypes()); invPrefs.getSavedSyncedTypes());
assertNull(invPrefs.getSavedObjectIds()); assertNull(invPrefs.getSavedObjectIds());
assertEquals(1, mStartServiceIntents.size()); assertEquals(1, mStartServiceIntents.size());
...@@ -566,7 +566,7 @@ public class InvalidationServiceTest extends ServiceTestCase<TestableInvalidatio ...@@ -566,7 +566,7 @@ public class InvalidationServiceTest extends ServiceTestCase<TestableInvalidatio
private boolean expectedObjectIdsRegistered(Set<ModelType> expectedTypes, private boolean expectedObjectIdsRegistered(Set<ModelType> expectedTypes,
Set<ObjectId> expectedObjectIds, boolean isReady) { Set<ObjectId> expectedObjectIds, boolean isReady) {
// Get synced types saved to preferences. // Get synced types saved to preferences.
Set<String> expectedSyncTypes = ModelType.modelTypesToSyncTypes(expectedTypes); Set<String> expectedSyncTypes = ModelType.modelTypesToSyncTypesForTest(expectedTypes);
InvalidationPreferences invPrefs = new InvalidationPreferences(getContext()); InvalidationPreferences invPrefs = new InvalidationPreferences(getContext());
Set<String> actualSyncTypes = invPrefs.getSavedSyncedTypes(); Set<String> actualSyncTypes = invPrefs.getSavedSyncedTypes();
if (actualSyncTypes == null) { if (actualSyncTypes == null) {
...@@ -749,7 +749,7 @@ public class InvalidationServiceTest extends ServiceTestCase<TestableInvalidatio ...@@ -749,7 +749,7 @@ public class InvalidationServiceTest extends ServiceTestCase<TestableInvalidatio
assertFalse(InvalidationService.getIsClientStartedForTest()); assertFalse(InvalidationService.getIsClientStartedForTest());
InvalidationPreferences invPrefs = new InvalidationPreferences(getContext()); InvalidationPreferences invPrefs = new InvalidationPreferences(getContext());
assertEquals(account, invPrefs.getSavedSyncedAccount()); assertEquals(account, invPrefs.getSavedSyncedAccount());
assertEquals(ModelType.modelTypesToSyncTypes(desiredRegistrations), assertEquals(ModelType.modelTypesToSyncTypesForTest(desiredRegistrations),
invPrefs.getSavedSyncedTypes()); invPrefs.getSavedSyncedTypes());
assertEquals(0, mStartServiceIntents.size()); assertEquals(0, mStartServiceIntents.size());
} }
...@@ -777,7 +777,7 @@ public class InvalidationServiceTest extends ServiceTestCase<TestableInvalidatio ...@@ -777,7 +777,7 @@ public class InvalidationServiceTest extends ServiceTestCase<TestableInvalidatio
assertEquals(1, mStartServiceIntents.size()); assertEquals(1, mStartServiceIntents.size());
assertTrue(isAndroidListenerStartIntent(mStartServiceIntents.get(0))); assertTrue(isAndroidListenerStartIntent(mStartServiceIntents.get(0)));
InvalidationPreferences invPrefs = new InvalidationPreferences(getContext()); InvalidationPreferences invPrefs = new InvalidationPreferences(getContext());
assertEquals(ModelType.modelTypesToSyncTypes(desiredRegistrations), assertEquals(ModelType.modelTypesToSyncTypesForTest(desiredRegistrations),
invPrefs.getSavedSyncedTypes()); invPrefs.getSavedSyncedTypes());
assertEquals(desiredObjectIds, getService().readRegistrationsFromPrefs()); assertEquals(desiredObjectIds, getService().readRegistrationsFromPrefs());
......
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