Commit 7b3cdc42 authored by Yasmin's avatar Yasmin Committed by Commit Bot

Check enterprise policy on the receiver side for the Shared Clipboard feature.

Bug: 1005813
Change-Id: I7c135ef3a969bd774c735f89fa82413c73aa9ecb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1825601
Commit-Queue: Yasmin Molazadeh <yasmo@chromium.org>
Reviewed-by: default avatarMichael van Ouwerkerk <mvanouwerkerk@chromium.org>
Cr-Commit-Position: refs/heads/master@{#701059}
parent ca9c5257
......@@ -50,7 +50,8 @@ constexpr int kSeparatorCommandId = -1;
class MockSharingService : public SharingService {
public:
explicit MockSharingService(std::unique_ptr<SharingFCMHandler> fcm_handler)
: SharingService(/* sync_prefs= */ nullptr,
: SharingService(/* pref_service= */ nullptr,
/* sync_prefs= */ nullptr,
/* vapid_key_manager= */ nullptr,
/* sharing_device_registration= */ nullptr,
/* fcm_sender= */ nullptr,
......
......@@ -43,7 +43,8 @@ const char kReceiverName[] = "test_receiver_name";
class MockSharingService : public SharingService {
public:
explicit MockSharingService(std::unique_ptr<SharingFCMHandler> fcm_handler)
: SharingService(/* sync_prefs= */ nullptr,
: SharingService(/* pref_service= */ nullptr,
/* sync_prefs= */ nullptr,
/* vapid_key_manager= */ nullptr,
/* sharing_device_registration= */ nullptr,
/* fcm_sender= */ nullptr,
......
......@@ -41,7 +41,8 @@ const char kSelectionTextWithNumber[] = "9876543210";
class MockSharingService : public SharingService {
public:
explicit MockSharingService(std::unique_ptr<SharingFCMHandler> fcm_handler)
: SharingService(/* sync_prefs= */ nullptr,
: SharingService(/* pref_service= */ nullptr,
/* sync_prefs= */ nullptr,
/* vapid_key_manager= */ nullptr,
/* sharing_device_registration= */ nullptr,
/* fcm_sender= */ nullptr,
......
......@@ -46,7 +46,8 @@ constexpr int kSeparatorCommandId = -1;
class MockSharingService : public SharingService {
public:
explicit MockSharingService(std::unique_ptr<SharingFCMHandler> fcm_handler)
: SharingService(/* sync_prefs= */ nullptr,
: SharingService(/* pref_service= */ nullptr,
/* sync_prefs= */ nullptr,
/* vapid_key_manager= */ nullptr,
/* sharing_device_registration= */ nullptr,
/* fcm_sender= */ nullptr,
......
......@@ -31,6 +31,7 @@ class MockSharingService : public SharingService {
explicit MockSharingService(
NotificationDisplayService* notification_display_service)
: SharingService(
/* pref_service= */ nullptr,
/* sync_prefs= */ nullptr,
/* vapid_key_manager= */ nullptr,
/* sharing_device_registration= */ nullptr,
......
......@@ -43,7 +43,8 @@ const char kReceiverName[] = "test_receiver_name";
class MockSharingService : public SharingService {
public:
explicit MockSharingService(std::unique_ptr<SharingFCMHandler> fcm_handler)
: SharingService(/* sync_prefs= */ nullptr,
: SharingService(/* pref_service= */ nullptr,
/* sync_prefs= */ nullptr,
/* vapid_key_manager= */ nullptr,
/* sharing_device_registration= */ nullptr,
/* fcm_sender= */ nullptr,
......
......@@ -37,7 +37,8 @@ const char kText[] = "Some text to copy to phone device.";
class MockSharingService : public SharingService {
public:
explicit MockSharingService(std::unique_ptr<SharingFCMHandler> fcm_handler)
: SharingService(/* sync_prefs= */ nullptr,
: SharingService(/* pref_service= */ nullptr,
/* sync_prefs= */ nullptr,
/* vapid_key_manager= */ nullptr,
/* sharing_device_registration= */ nullptr,
/* fcm_sender= */ nullptr,
......
......@@ -17,8 +17,10 @@
#include "chrome/browser/sharing/sharing_device_registration_result.h"
#include "chrome/browser/sharing/sharing_sync_preference.h"
#include "chrome/browser/sharing/vapid_key_manager.h"
#include "chrome/common/pref_names.h"
#include "components/gcm_driver/crypto/p256_key_util.h"
#include "components/gcm_driver/instance_id/instance_id_driver.h"
#include "components/prefs/pref_service.h"
#include "components/sync_device_info/device_info.h"
#include "components/sync_device_info/local_device_info_provider.h"
#include "crypto/ec_private_key.h"
......@@ -31,11 +33,13 @@ using instance_id::InstanceID;
using sync_pb::SharingSpecificFields;
SharingDeviceRegistration::SharingDeviceRegistration(
PrefService* pref_service,
SharingSyncPreference* sharing_sync_preference,
instance_id::InstanceIDDriver* instance_id_driver,
VapidKeyManager* vapid_key_manager,
syncer::LocalDeviceInfoProvider* local_device_info_provider)
: sharing_sync_preference_(sharing_sync_preference),
: pref_service_(pref_service),
sharing_sync_preference_(sharing_sync_preference),
instance_id_driver_(instance_id_driver),
vapid_key_manager_(vapid_key_manager),
local_device_info_provider_(local_device_info_provider) {}
......@@ -221,6 +225,11 @@ bool SharingDeviceRegistration::IsClickToCallSupported() const {
}
bool SharingDeviceRegistration::IsSharedClipboardSupported() const {
// Check the enterprise policy for Shared Clipboard.
if (pref_service_ &&
!pref_service_->GetBoolean(prefs::kSharedClipboardEnabled)) {
return false;
}
return base::FeatureList::IsEnabled(kSharedClipboardReceiver);
}
......
......@@ -15,6 +15,8 @@
#include "components/gcm_driver/instance_id/instance_id.h"
#include "components/sync/protocol/device_info_specifics.pb.h"
class PrefService;
namespace instance_id {
class InstanceIDDriver;
}
......@@ -35,6 +37,7 @@ class SharingDeviceRegistration {
base::OnceCallback<void(SharingDeviceRegistrationResult)>;
SharingDeviceRegistration(
PrefService* pref_service,
SharingSyncPreference* prefs,
instance_id::InstanceIDDriver* instance_id_driver,
VapidKeyManager* vapid_key_manager,
......@@ -53,6 +56,9 @@ class SharingDeviceRegistration {
std::set<sync_pb::SharingSpecificFields_EnabledFeatures>
enabled_feautres);
// Returns if device can handle receiving of shared clipboard contents.
bool IsSharedClipboardSupported() const;
private:
FRIEND_TEST_ALL_PREFIXES(SharingDeviceRegistrationTest,
RegisterDeviceTest_Success);
......@@ -95,9 +101,7 @@ class SharingDeviceRegistration {
// Returns if device can handle receiving phone numbers for calling.
bool IsClickToCallSupported() const;
// Returns if device can handle receiving of shared clipboard contents.
bool IsSharedClipboardSupported() const;
PrefService* pref_service_;
SharingSyncPreference* sharing_sync_preference_;
instance_id::InstanceIDDriver* instance_id_driver_;
VapidKeyManager* vapid_key_manager_;
......
......@@ -10,14 +10,20 @@
#include "base/run_loop.h"
#include "base/test/bind_test_util.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "chrome/browser/sharing/fake_local_device_info_provider.h"
#include "chrome/browser/sharing/shared_clipboard/feature_flags.h"
#include "chrome/browser/sharing/sharing_constants.h"
#include "chrome/browser/sharing/sharing_device_registration_result.h"
#include "chrome/browser/sharing/sharing_sync_preference.h"
#include "chrome/browser/sharing/vapid_key_manager.h"
#include "chrome/common/pref_names.h"
#include "components/gcm_driver/instance_id/instance_id_driver.h"
#include "components/prefs/pref_registry.h"
#include "components/prefs/pref_service_factory.h"
#include "components/sync_device_info/device_info.h"
#include "components/sync_preferences/pref_service_mock_factory.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "content/public/test/browser_task_environment.h"
#include "google_apis/gcm/engine/account_mapping.h"
......@@ -115,18 +121,36 @@ class SharingDeviceRegistrationTest : public testing::Test {
SharingDeviceRegistrationTest()
: sync_prefs_(&prefs_),
vapid_key_manager_(&sync_prefs_),
sharing_device_registration_(&sync_prefs_,
sharing_device_registration_(pref_service_.get(),
&sync_prefs_,
&mock_instance_id_driver_,
&vapid_key_manager_,
&fake_local_device_info_provider_) {
SharingSyncPreference::RegisterProfilePrefs(prefs_.registry());
}
static std::unique_ptr<PrefService> CreatePrefServiceAndRegisterPrefs() {
scoped_refptr<user_prefs::PrefRegistrySyncable> registry(
new user_prefs::PrefRegistrySyncable());
registry->RegisterBooleanPref(prefs::kSharedClipboardEnabled, true);
PrefServiceFactory factory;
factory.set_user_prefs(base::MakeRefCounted<TestingPrefStore>());
return factory.Create(registry);
}
void SetUp() {
ON_CALL(mock_instance_id_driver_, GetInstanceID(_))
.WillByDefault(testing::Return(&fake_instance_id_));
}
void SetSharedClipboardPolicy(bool val) {
pref_service_->SetBoolean(prefs::kSharedClipboardEnabled, val);
}
void EnableSharedClipboardReceiverFlag() {
scoped_feature_list_.InitAndEnableFeature(kSharedClipboardReceiver);
}
void RegisterDeviceSync() {
base::RunLoop run_loop;
sharing_device_registration_.RegisterDevice(
......@@ -168,6 +192,9 @@ class SharingDeviceRegistrationTest : public testing::Test {
FakeLocalDeviceInfoProvider fake_local_device_info_provider_;
FakeInstanceID fake_instance_id_;
base::test::ScopedFeatureList scoped_feature_list_;
std::unique_ptr<PrefService> pref_service_ =
CreatePrefServiceAndRegisterPrefs();
SharingSyncPreference sync_prefs_;
VapidKeyManager vapid_key_manager_;
SharingDeviceRegistration sharing_device_registration_;
......@@ -180,6 +207,20 @@ class SharingDeviceRegistrationTest : public testing::Test {
} // namespace
TEST_F(SharingDeviceRegistrationTest, IsSharedClipboardSupported_True) {
SetSharedClipboardPolicy(true);
EnableSharedClipboardReceiverFlag();
EXPECT_TRUE(sharing_device_registration_.IsSharedClipboardSupported());
}
TEST_F(SharingDeviceRegistrationTest, IsSharedClipboardSupported_False) {
SetSharedClipboardPolicy(false);
EnableSharedClipboardReceiverFlag();
EXPECT_FALSE(sharing_device_registration_.IsSharedClipboardSupported());
}
TEST_F(SharingDeviceRegistrationTest, RegisterDeviceTest_Success) {
SetInstanceIDFCMResult(InstanceID::Result::SUCCESS);
SetInstanceIDFCMToken(kFCMToken);
......
......@@ -27,14 +27,17 @@
#include "chrome/browser/sharing/sharing_metrics.h"
#include "chrome/browser/sharing/sharing_sync_preference.h"
#include "chrome/browser/sharing/vapid_key_manager.h"
#include "chrome/common/pref_names.h"
#include "components/gcm_driver/crypto/gcm_encryption_provider.h"
#include "components/gcm_driver/gcm_driver.h"
#include "components/prefs/pref_service.h"
#include "components/sync/driver/sync_service.h"
#include "components/sync_device_info/device_info.h"
#include "components/sync_device_info/local_device_info_provider.h"
#include "content/public/browser/browser_task_traits.h"
SharingService::SharingService(
PrefService* pref_service,
std::unique_ptr<SharingSyncPreference> sync_prefs,
std::unique_ptr<VapidKeyManager> vapid_key_manager,
std::unique_ptr<SharingDeviceRegistration> sharing_device_registration,
......@@ -45,7 +48,8 @@ SharingService::SharingService(
syncer::LocalDeviceInfoProvider* local_device_info_provider,
syncer::SyncService* sync_service,
NotificationDisplayService* notification_display_service)
: sync_prefs_(std::move(sync_prefs)),
: pref_service_(pref_service),
sync_prefs_(std::move(sync_prefs)),
vapid_key_manager_(std::move(vapid_key_manager)),
sharing_device_registration_(std::move(sharing_device_registration)),
fcm_sender_(std::move(fcm_sender)),
......@@ -91,7 +95,9 @@ SharingService::SharingService(
this, notification_display_service);
#endif // defined(OS_ANDROID)
if (base::FeatureList::IsEnabled(kSharedClipboardReceiver)) {
if (pref_service_ &&
!pref_service_->GetBoolean(prefs::kSharedClipboardEnabled) &&
base::FeatureList::IsEnabled(kSharedClipboardReceiver)) {
fcm_handler_->AddSharingHandler(
chrome_browser_sharing::SharingMessage::kSharedClipboardMessage,
shared_clipboard_message_handler_.get());
......
......@@ -44,6 +44,7 @@ class SyncService;
} // namespace syncer
class NotificationDisplayService;
class PrefService;
class SharingFCMHandler;
class SharingMessageHandler;
class SharingSyncPreference;
......@@ -72,6 +73,7 @@ class SharingService : public KeyedService,
};
SharingService(
PrefService* pref_service,
std::unique_ptr<SharingSyncPreference> sync_prefs,
std::unique_ptr<VapidKeyManager> vapid_key_manager,
std::unique_ptr<SharingDeviceRegistration> sharing_device_registration,
......@@ -154,6 +156,7 @@ class SharingService : public KeyedService,
// in transitioning state.
bool IsSyncDisabled() const;
PrefService* pref_service_;
std::unique_ptr<SharingSyncPreference> sync_prefs_;
std::unique_ptr<VapidKeyManager> vapid_key_manager_;
std::unique_ptr<SharingDeviceRegistration> sharing_device_registration_;
......
......@@ -86,7 +86,7 @@ KeyedService* SharingServiceFactory::BuildServiceInstanceFor(
std::make_unique<VapidKeyManager>(sync_prefs.get());
std::unique_ptr<SharingDeviceRegistration> sharing_device_registration =
std::make_unique<SharingDeviceRegistration>(
sync_prefs.get(), instance_id_service->driver(),
profile->GetPrefs(), sync_prefs.get(), instance_id_service->driver(),
vapid_key_manager.get(), local_device_info_provider);
std::unique_ptr<SharingFCMSender> fcm_sender =
std::make_unique<SharingFCMSender>(gcm_driver, local_device_info_provider,
......@@ -97,7 +97,7 @@ KeyedService* SharingServiceFactory::BuildServiceInstanceFor(
sync_prefs.get());
return new SharingService(
std::move(sync_prefs), std::move(vapid_key_manager),
profile->GetPrefs(), std::move(sync_prefs), std::move(vapid_key_manager),
std::move(sharing_device_registration), std::move(fcm_sender),
std::move(fcm_handler), gcm_driver, device_info_tracker,
local_device_info_provider, sync_service, notification_display_service);
......
......@@ -132,11 +132,13 @@ class MockSharingFCMHandler : public SharingFCMHandler {
class FakeSharingDeviceRegistration : public SharingDeviceRegistration {
public:
FakeSharingDeviceRegistration(
PrefService* pref_service,
SharingSyncPreference* prefs,
instance_id::InstanceIDDriver* instance_id_driver,
VapidKeyManager* vapid_key_manager,
syncer::LocalDeviceInfoProvider* device_info_tracker)
: SharingDeviceRegistration(prefs,
: SharingDeviceRegistration(pref_service,
prefs,
instance_id_driver,
vapid_key_manager,
device_info_tracker) {}
......@@ -211,8 +213,8 @@ class SharingServiceTest : public testing::Test {
SharingServiceTest() {
sync_prefs_ = new SharingSyncPreference(&prefs_);
sharing_device_registration_ = new FakeSharingDeviceRegistration(
sync_prefs_, &mock_instance_id_driver_, vapid_key_manager_,
&fake_local_device_info_provider_);
/* pref_service= */ nullptr, sync_prefs_, &mock_instance_id_driver_,
vapid_key_manager_, &fake_local_device_info_provider_);
vapid_key_manager_ = new VapidKeyManager(sync_prefs_);
fcm_sender_ = new SharingFCMSender(&fake_gcm_driver_,
&fake_local_device_info_provider_,
......@@ -253,7 +255,8 @@ class SharingServiceTest : public testing::Test {
SharingService* GetSharingService() {
if (!sharing_service_) {
sharing_service_ = std::make_unique<SharingService>(
base::WrapUnique(sync_prefs_), base::WrapUnique(vapid_key_manager_),
/* pref_service= */ nullptr, base::WrapUnique(sync_prefs_),
base::WrapUnique(vapid_key_manager_),
base::WrapUnique(sharing_device_registration_),
base::WrapUnique(fcm_sender_), base::WrapUnique(fcm_handler_),
&fake_gcm_driver_, &device_info_tracker_,
......
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