Commit c794f9f4 authored by skym's avatar skym Committed by Commit Bot

[Sync] Split UserEventService into interface and impl, add fake impl, add unit tests.

BUG=727825

Review-Url: https://codereview.chromium.org/2909283003
Cr-Commit-Position: refs/heads/master@{#476434}
parent 02bf4db5
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include <utility> #include <utility>
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "chrome/browser/profiles/incognito_helpers.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sync/profile_sync_service_factory.h" #include "chrome/browser/sync/profile_sync_service_factory.h"
#include "chrome/common/channel_info.h" #include "chrome/common/channel_info.h"
...@@ -15,7 +16,8 @@ ...@@ -15,7 +16,8 @@
#include "components/keyed_service/content/browser_context_dependency_manager.h" #include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/sync/base/model_type.h" #include "components/sync/base/model_type.h"
#include "components/sync/base/report_unrecoverable_error.h" #include "components/sync/base/report_unrecoverable_error.h"
#include "components/sync/user_events/user_event_service.h" #include "components/sync/user_events/no_op_user_event_service.h"
#include "components/sync/user_events/user_event_service_impl.h"
#include "components/sync/user_events/user_event_sync_bridge.h" #include "components/sync/user_events/user_event_sync_bridge.h"
namespace browser_sync { namespace browser_sync {
...@@ -41,6 +43,10 @@ UserEventServiceFactory::~UserEventServiceFactory() {} ...@@ -41,6 +43,10 @@ UserEventServiceFactory::~UserEventServiceFactory() {}
KeyedService* UserEventServiceFactory::BuildServiceInstanceFor( KeyedService* UserEventServiceFactory::BuildServiceInstanceFor(
content::BrowserContext* context) const { content::BrowserContext* context) const {
if (context->IsOffTheRecord()) {
return new syncer::NoOpUserEventService();
}
Profile* profile = Profile::FromBrowserContext(context); Profile* profile = Profile::FromBrowserContext(context);
syncer::ModelTypeStoreFactory store_factory = syncer::ModelTypeStoreFactory store_factory =
browser_sync::ProfileSyncService::GetModelTypeStoreFactory( browser_sync::ProfileSyncService::GetModelTypeStoreFactory(
...@@ -51,8 +57,13 @@ KeyedService* UserEventServiceFactory::BuildServiceInstanceFor( ...@@ -51,8 +57,13 @@ KeyedService* UserEventServiceFactory::BuildServiceInstanceFor(
chrome::GetChannel())); chrome::GetChannel()));
auto bridge = base::MakeUnique<syncer::UserEventSyncBridge>( auto bridge = base::MakeUnique<syncer::UserEventSyncBridge>(
std::move(store_factory), std::move(processor_factory)); std::move(store_factory), std::move(processor_factory));
return new syncer::UserEventService( return new syncer::UserEventServiceImpl(
ProfileSyncServiceFactory::GetForProfile(profile), std::move(bridge)); ProfileSyncServiceFactory::GetForProfile(profile), std::move(bridge));
} }
content::BrowserContext* UserEventServiceFactory::GetBrowserContextToUse(
content::BrowserContext* context) const {
return chrome::GetBrowserContextOwnInstanceInIncognito(context);
}
} // namespace browser_sync } // namespace browser_sync
...@@ -31,6 +31,8 @@ class UserEventServiceFactory : public BrowserContextKeyedServiceFactory { ...@@ -31,6 +31,8 @@ class UserEventServiceFactory : public BrowserContextKeyedServiceFactory {
// BrowserContextKeyedServiceFactory: // BrowserContextKeyedServiceFactory:
KeyedService* BuildServiceInstanceFor( KeyedService* BuildServiceInstanceFor(
content::BrowserContext* context) const override; content::BrowserContext* context) const override;
content::BrowserContext* GetBrowserContextToUse(
content::BrowserContext* context) const override;
DISALLOW_COPY_AND_ASSIGN(UserEventServiceFactory); DISALLOW_COPY_AND_ASSIGN(UserEventServiceFactory);
}; };
......
...@@ -215,8 +215,7 @@ void SyncInternalsMessageHandler::HandleWriteUserEvent( ...@@ -215,8 +215,7 @@ void SyncInternalsMessageHandler::HandleWriteUserEvent(
Profile* profile = Profile::FromWebUI(web_ui()); Profile* profile = Profile::FromWebUI(web_ui());
syncer::UserEventService* user_event_service = syncer::UserEventService* user_event_service =
browser_sync::UserEventServiceFactory::GetForProfile( browser_sync::UserEventServiceFactory::GetForProfile(profile);
profile->GetOriginalProfile());
sync_pb::UserEventSpecifics event_specifics; sync_pb::UserEventSpecifics event_specifics;
event_specifics.set_event_time_usec(StringAtIndexToInt64(args, 0)); event_specifics.set_event_time_usec(StringAtIndexToInt64(args, 0));
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include "components/sync/driver/fake_sync_service.h" #include "components/sync/driver/fake_sync_service.h"
#include "components/sync/driver/sync_service.h" #include "components/sync/driver/sync_service.h"
#include "components/sync/js/js_test_util.h" #include "components/sync/js/js_test_util.h"
#include "components/sync/user_events/fake_user_event_service.h"
#include "content/public/browser/site_instance.h" #include "content/public/browser/site_instance.h"
#include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents.h"
#include "content/public/test/test_browser_thread_bundle.h" #include "content/public/test/test_browser_thread_bundle.h"
...@@ -26,6 +27,7 @@ ...@@ -26,6 +27,7 @@
using base::DictionaryValue; using base::DictionaryValue;
using base::ListValue; using base::ListValue;
using base::Value; using base::Value;
using syncer::FakeUserEventService;
using syncer::SyncService; using syncer::SyncService;
using syncer::SyncServiceObserver; using syncer::SyncServiceObserver;
using syncer::TypeDebugInfoObserver; using syncer::TypeDebugInfoObserver;
...@@ -97,6 +99,11 @@ static std::unique_ptr<KeyedService> BuildTestSyncService( ...@@ -97,6 +99,11 @@ static std::unique_ptr<KeyedService> BuildTestSyncService(
return base::MakeUnique<TestSyncService>(); return base::MakeUnique<TestSyncService>();
} }
static std::unique_ptr<KeyedService> BuildFakeUserEventService(
content::BrowserContext* context) {
return base::MakeUnique<FakeUserEventService>();
}
class SyncInternalsMessageHandlerTest : public ::testing::Test { class SyncInternalsMessageHandlerTest : public ::testing::Test {
protected: protected:
SyncInternalsMessageHandlerTest() { SyncInternalsMessageHandlerTest() {
...@@ -107,6 +114,9 @@ class SyncInternalsMessageHandlerTest : public ::testing::Test { ...@@ -107,6 +114,9 @@ class SyncInternalsMessageHandlerTest : public ::testing::Test {
test_sync_service_ = static_cast<TestSyncService*>( test_sync_service_ = static_cast<TestSyncService*>(
ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse( ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse(
&profile_, &BuildTestSyncService)); &profile_, &BuildTestSyncService));
fake_user_event_service_ = static_cast<FakeUserEventService*>(
browser_sync::UserEventServiceFactory::GetInstance()
->SetTestingFactoryAndUse(&profile_, &BuildFakeUserEventService));
handler_.reset(new TestableSyncInternalsMessageHandler( handler_.reset(new TestableSyncInternalsMessageHandler(
&web_ui_, &web_ui_,
base::BindRepeating( base::BindRepeating(
...@@ -158,6 +168,10 @@ class SyncInternalsMessageHandlerTest : public ::testing::Test { ...@@ -158,6 +168,10 @@ class SyncInternalsMessageHandlerTest : public ::testing::Test {
TestSyncService* test_sync_service() { return test_sync_service_; } TestSyncService* test_sync_service() { return test_sync_service_; }
FakeUserEventService* fake_user_event_service() {
return fake_user_event_service_;
}
TestableSyncInternalsMessageHandler* handler() { return handler_.get(); } TestableSyncInternalsMessageHandler* handler() { return handler_.get(); }
int CallCountWithName(const std::string& function_name) { int CallCountWithName(const std::string& function_name) {
...@@ -187,6 +201,7 @@ class SyncInternalsMessageHandlerTest : public ::testing::Test { ...@@ -187,6 +201,7 @@ class SyncInternalsMessageHandlerTest : public ::testing::Test {
std::unique_ptr<content::WebContents> web_contents_; std::unique_ptr<content::WebContents> web_contents_;
content::TestWebUI web_ui_; content::TestWebUI web_ui_;
TestSyncService* test_sync_service_; TestSyncService* test_sync_service_;
FakeUserEventService* fake_user_event_service_;
std::unique_ptr<TestableSyncInternalsMessageHandler> handler_; std::unique_ptr<TestableSyncInternalsMessageHandler> handler_;
int about_sync_data_delegate_call_count_ = 0; int about_sync_data_delegate_call_count_ = 0;
SyncService* last_delegate_sync_service_ = nullptr; SyncService* last_delegate_sync_service_ = nullptr;
...@@ -315,4 +330,30 @@ TEST_F(SyncInternalsMessageHandlerTest, SendAboutInfoSyncDisabled) { ...@@ -315,4 +330,30 @@ TEST_F(SyncInternalsMessageHandlerTest, SendAboutInfoSyncDisabled) {
ValidateAboutInfoCall(); ValidateAboutInfoCall();
} }
TEST_F(SyncInternalsMessageHandlerTest, WriteUserEvent) {
ListValue args;
args.AppendString("1000000000000000000");
args.AppendString("-1");
handler()->HandleWriteUserEvent(&args);
ASSERT_EQ(1u, fake_user_event_service()->GetRecordedUserEvents().size());
const sync_pb::UserEventSpecifics& event =
*fake_user_event_service()->GetRecordedUserEvents().begin();
EXPECT_EQ(1000000000000000000, event.event_time_usec());
EXPECT_EQ(-1, event.navigation_id());
}
TEST_F(SyncInternalsMessageHandlerTest, WriteUserEventBadParse) {
ListValue args;
args.AppendString("123abc");
args.AppendString("");
handler()->HandleWriteUserEvent(&args);
ASSERT_EQ(1u, fake_user_event_service()->GetRecordedUserEvents().size());
const sync_pb::UserEventSpecifics& event =
*fake_user_event_service()->GetRecordedUserEvents().begin();
EXPECT_EQ(0, event.event_time_usec());
EXPECT_EQ(0, event.navigation_id());
}
} // namespace } // namespace
...@@ -572,8 +572,14 @@ static_library("sync") { ...@@ -572,8 +572,14 @@ static_library("sync") {
"syncable/write_transaction.h", "syncable/write_transaction.h",
"syncable/write_transaction_info.cc", "syncable/write_transaction_info.cc",
"syncable/write_transaction_info.h", "syncable/write_transaction_info.h",
"user_events/fake_user_event_service.cc",
"user_events/fake_user_event_service.h",
"user_events/no_op_user_event_service.cc",
"user_events/no_op_user_event_service.h",
"user_events/user_event_service.cc", "user_events/user_event_service.cc",
"user_events/user_event_service.h", "user_events/user_event_service.h",
"user_events/user_event_service_impl.cc",
"user_events/user_event_service_impl.h",
"user_events/user_event_sync_bridge.cc", "user_events/user_event_sync_bridge.cc",
"user_events/user_event_sync_bridge.h", "user_events/user_event_sync_bridge.h",
] ]
...@@ -945,7 +951,7 @@ source_set("unit_tests") { ...@@ -945,7 +951,7 @@ source_set("unit_tests") {
"syncable/syncable_enum_conversions_unittest.cc", "syncable/syncable_enum_conversions_unittest.cc",
"syncable/syncable_id_unittest.cc", "syncable/syncable_id_unittest.cc",
"syncable/syncable_unittest.cc", "syncable/syncable_unittest.cc",
"user_events/user_event_service_unittest.cc", "user_events/user_event_service_impl_unittest.cc",
"user_events/user_event_sync_bridge_unittest.cc", "user_events/user_event_sync_bridge_unittest.cc",
] ]
......
// Copyright 2017 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 "components/sync/user_events/fake_user_event_service.h"
using sync_pb::UserEventSpecifics;
namespace syncer {
FakeUserEventService::FakeUserEventService() {}
FakeUserEventService::~FakeUserEventService() {}
void FakeUserEventService::RecordUserEvent(
std::unique_ptr<UserEventSpecifics> specifics) {
DCHECK(specifics);
RecordUserEvent(*specifics);
}
void FakeUserEventService::RecordUserEvent(
const UserEventSpecifics& specifics) {
recorded_user_events_.push_back(specifics);
}
base::WeakPtr<ModelTypeSyncBridge> FakeUserEventService::GetSyncBridge() {
return base::WeakPtr<ModelTypeSyncBridge>();
}
void FakeUserEventService::RegisterDependentFieldTrial(
const std::string& trial_name,
UserEventSpecifics::EventCase event_case) {
registered_dependent_field_trials_[trial_name].insert(event_case);
}
const std::vector<UserEventSpecifics>&
FakeUserEventService::GetRecordedUserEvents() const {
return recorded_user_events_;
}
const std::map<std::string, std::set<sync_pb::UserEventSpecifics::EventCase>>&
FakeUserEventService::GetRegisteredDependentFieldTrials() const {
return registered_dependent_field_trials_;
}
} // namespace syncer
// Copyright 2017 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 COMPONENTS_SYNC_USER_EVENTS_FAKE_USER_EVENT_SERVICE_H_
#define COMPONENTS_SYNC_USER_EVENTS_FAKE_USER_EVENT_SERVICE_H_
#include <map>
#include <memory>
#include <set>
#include <string>
#include <vector>
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "components/sync/protocol/user_event_specifics.pb.h"
#include "components/sync/user_events/user_event_service.h"
namespace syncer {
class ModelTypeSyncBridge;
// This implementation is intended to be used in unit tests, with public
// accessors that allow reading all data to verify expectations.
class FakeUserEventService : public UserEventService {
public:
FakeUserEventService();
~FakeUserEventService() override;
// UserEventService implementation.
void RecordUserEvent(
std::unique_ptr<sync_pb::UserEventSpecifics> specifics) override;
void RecordUserEvent(const sync_pb::UserEventSpecifics& specifics) override;
void RegisterDependentFieldTrial(
const std::string& trial_name,
sync_pb::UserEventSpecifics::EventCase event_case) override;
base::WeakPtr<ModelTypeSyncBridge> GetSyncBridge() override;
const std::vector<sync_pb::UserEventSpecifics>& GetRecordedUserEvents() const;
const std::map<std::string, std::set<sync_pb::UserEventSpecifics::EventCase>>&
GetRegisteredDependentFieldTrials() const;
private:
std::vector<sync_pb::UserEventSpecifics> recorded_user_events_;
std::map<std::string, std::set<sync_pb::UserEventSpecifics::EventCase>>
registered_dependent_field_trials_;
DISALLOW_COPY_AND_ASSIGN(FakeUserEventService);
};
} // namespace syncer
#endif // COMPONENTS_SYNC_USER_EVENTS_FAKE_USER_EVENT_SERVICE_H_
// Copyright 2017 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 "components/sync/user_events/no_op_user_event_service.h"
#include "base/memory/weak_ptr.h"
using sync_pb::UserEventSpecifics;
namespace syncer {
NoOpUserEventService::NoOpUserEventService() {}
NoOpUserEventService::~NoOpUserEventService() {}
void NoOpUserEventService::RecordUserEvent(
std::unique_ptr<UserEventSpecifics> specifics) {}
void NoOpUserEventService::RecordUserEvent(
const UserEventSpecifics& specifics) {}
base::WeakPtr<ModelTypeSyncBridge> NoOpUserEventService::GetSyncBridge() {
return base::WeakPtr<ModelTypeSyncBridge>();
}
void NoOpUserEventService::RegisterDependentFieldTrial(
const std::string& trial_name,
UserEventSpecifics::EventCase event_case) {}
} // namespace syncer
// Copyright 2017 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 COMPONENTS_SYNC_USER_EVENTS_NO_OP_USER_EVENT_SERVICE_H_
#define COMPONENTS_SYNC_USER_EVENTS_NO_OP_USER_EVENT_SERVICE_H_
#include <memory>
#include <string>
#include "base/macros.h"
#include "components/sync/user_events/user_event_service.h"
namespace syncer {
class ModelTypeSyncBridge;
// This implementation is used when we know event should never be recorded,
// such as in incognito mode.
class NoOpUserEventService : public UserEventService {
public:
NoOpUserEventService();
~NoOpUserEventService() override;
// UserEventService implementation.
void RecordUserEvent(
std::unique_ptr<sync_pb::UserEventSpecifics> specifics) override;
void RecordUserEvent(const sync_pb::UserEventSpecifics& specifics) override;
void RegisterDependentFieldTrial(
const std::string& trial_name,
sync_pb::UserEventSpecifics::EventCase event_case) override;
base::WeakPtr<ModelTypeSyncBridge> GetSyncBridge() override;
private:
DISALLOW_COPY_AND_ASSIGN(NoOpUserEventService);
};
} // namespace syncer
#endif // COMPONENTS_SYNC_USER_EVENTS_NO_OP_USER_EVENT_SERVICE_H_
...@@ -4,62 +4,10 @@ ...@@ -4,62 +4,10 @@
#include "components/sync/user_events/user_event_service.h" #include "components/sync/user_events/user_event_service.h"
#include <utility>
#include "base/bind.h"
#include "base/feature_list.h"
#include "base/memory/ptr_util.h"
#include "base/rand_util.h"
#include "components/sync/driver/sync_driver_switches.h"
#include "components/sync/driver/sync_service.h"
#include "components/sync/model/model_type_sync_bridge.h"
#include "components/sync/user_events/user_event_sync_bridge.h"
using sync_pb::UserEventSpecifics;
namespace syncer { namespace syncer {
UserEventService::UserEventService(SyncService* sync_service, UserEventService::UserEventService() {}
std::unique_ptr<UserEventSyncBridge> bridge)
: sync_service_(sync_service),
bridge_(std::move(bridge)),
session_id_(base::RandUint64()) {
// TODO(skym): Subscribe to events about field trial membership changing.
}
UserEventService::~UserEventService() {} UserEventService::~UserEventService() {}
void UserEventService::Shutdown() {}
void UserEventService::RecordUserEvent(
std::unique_ptr<UserEventSpecifics> specifics) {
if (CanRecordEvent(*specifics)) {
DCHECK(!specifics->has_session_id());
specifics->set_session_id(session_id_);
bridge_->RecordUserEvent(std::move(specifics));
}
}
void UserEventService::RecordUserEvent(const UserEventSpecifics& specifics) {
RecordUserEvent(base::MakeUnique<UserEventSpecifics>(specifics));
}
base::WeakPtr<ModelTypeSyncBridge> UserEventService::GetSyncBridge() {
return bridge_->AsWeakPtr();
}
bool UserEventService::CanRecordEvent(const UserEventSpecifics& specifics) {
// We only record events if the user is syncing history and has not enabled
// a custom passphrase. The type HISTORY_DELETE_DIRECTIVES is enabled in and
// only in this exact scenario.
return base::FeatureList::IsEnabled(switches::kSyncUserEvents) &&
sync_service_ != nullptr && sync_service_->IsEngineInitialized() &&
sync_service_->GetPreferredDataTypes().Has(HISTORY_DELETE_DIRECTIVES);
}
void RegisterDependentFieldTrial(const std::string& trial_name,
UserEventSpecifics::EventCase event_case) {
// TODO(skym): Implementation.
}
} // namespace syncer } // namespace syncer
...@@ -16,45 +16,30 @@ ...@@ -16,45 +16,30 @@
namespace syncer { namespace syncer {
class ModelTypeSyncBridge; class ModelTypeSyncBridge;
class SyncService;
class UserEventSyncBridge;
class UserEventService : public KeyedService { class UserEventService : public KeyedService {
public: public:
UserEventService(SyncService* sync_service, UserEventService();
std::unique_ptr<UserEventSyncBridge> bridge);
~UserEventService() override; ~UserEventService() override;
// KeyedService implementation
void Shutdown() override;
// Records a given event to be reported. Relevant settings will be checked to // Records a given event to be reported. Relevant settings will be checked to
// verify user events should be emitted and this will no-op if the the // verify user events should be emitted and this will no-op if the the
// requisite permissions are not present. // requisite permissions are not present.
void RecordUserEvent(std::unique_ptr<sync_pb::UserEventSpecifics> specifics); virtual void RecordUserEvent(
void RecordUserEvent(const sync_pb::UserEventSpecifics& specifics); std::unique_ptr<sync_pb::UserEventSpecifics> specifics) = 0;
virtual void RecordUserEvent(
const sync_pb::UserEventSpecifics& specifics) = 0;
// Register that knowledge about a given field trial is important when // Register that knowledge about a given field trial is important when
// interpreting specified user event type, and should be recorded if assigned. // interpreting specified user event type, and should be recorded if assigned.
void RegisterDependentFieldTrial( virtual void RegisterDependentFieldTrial(
const std::string& trial_name, const std::string& trial_name,
sync_pb::UserEventSpecifics::EventCase event_case); sync_pb::UserEventSpecifics::EventCase event_case) = 0;
base::WeakPtr<ModelTypeSyncBridge> GetSyncBridge(); // Returns the underlying Sync integration point.
virtual base::WeakPtr<ModelTypeSyncBridge> GetSyncBridge() = 0;
private: private:
bool CanRecordEvent(const sync_pb::UserEventSpecifics& specifics);
SyncService* sync_service_;
std::unique_ptr<UserEventSyncBridge> bridge_;
// Holds onto a random number for the duration of this execution of chrome. On
// restart it will be regenerated. This can be attached to events to know
// which events came from the same session.
uint64_t session_id_;
DISALLOW_COPY_AND_ASSIGN(UserEventService); DISALLOW_COPY_AND_ASSIGN(UserEventService);
}; };
......
// Copyright 2017 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 "components/sync/user_events/user_event_service_impl.h"
#include <utility>
#include "base/bind.h"
#include "base/feature_list.h"
#include "base/memory/ptr_util.h"
#include "base/rand_util.h"
#include "components/sync/driver/sync_driver_switches.h"
#include "components/sync/driver/sync_service.h"
#include "components/sync/model/model_type_sync_bridge.h"
#include "components/sync/user_events/user_event_sync_bridge.h"
using sync_pb::UserEventSpecifics;
namespace syncer {
UserEventServiceImpl::UserEventServiceImpl(
SyncService* sync_service,
std::unique_ptr<UserEventSyncBridge> bridge)
: sync_service_(sync_service),
bridge_(std::move(bridge)),
session_id_(base::RandUint64()) {
// TODO(skym): Subscribe to events about field trial membership changing.
}
UserEventServiceImpl::~UserEventServiceImpl() {}
void UserEventServiceImpl::Shutdown() {}
void UserEventServiceImpl::RecordUserEvent(
std::unique_ptr<UserEventSpecifics> specifics) {
if (ShouldRecordEvent(*specifics)) {
DCHECK(!specifics->has_session_id());
specifics->set_session_id(session_id_);
bridge_->RecordUserEvent(std::move(specifics));
}
}
void UserEventServiceImpl::RecordUserEvent(
const UserEventSpecifics& specifics) {
RecordUserEvent(base::MakeUnique<UserEventSpecifics>(specifics));
}
base::WeakPtr<ModelTypeSyncBridge> UserEventServiceImpl::GetSyncBridge() {
return bridge_->AsWeakPtr();
}
bool UserEventServiceImpl::ShouldRecordEvent(
const UserEventSpecifics& specifics) {
// We only record events if the user is syncing history and has not enabled
// a custom passphrase. The type HISTORY_DELETE_DIRECTIVES is enabled in and
// only in this exact scenario.
return base::FeatureList::IsEnabled(switches::kSyncUserEvents) &&
sync_service_ != nullptr && sync_service_->IsEngineInitialized() &&
sync_service_->GetPreferredDataTypes().Has(HISTORY_DELETE_DIRECTIVES);
}
void UserEventServiceImpl::RegisterDependentFieldTrial(
const std::string& trial_name,
UserEventSpecifics::EventCase event_case) {
// TODO(skym): Implementation.
}
} // namespace syncer
// Copyright 2017 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 COMPONENTS_SYNC_USER_EVENTS_USER_EVENT_SERVICE_IMPL_H_
#define COMPONENTS_SYNC_USER_EVENTS_USER_EVENT_SERVICE_IMPL_H_
#include <memory>
#include <string>
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/sync/protocol/user_event_specifics.pb.h"
#include "components/sync/user_events/user_event_service.h"
namespace syncer {
class ModelTypeSyncBridge;
class SyncService;
class UserEventSyncBridge;
class UserEventServiceImpl : public UserEventService {
public:
UserEventServiceImpl(SyncService* sync_service,
std::unique_ptr<UserEventSyncBridge> bridge);
~UserEventServiceImpl() override;
// KeyedService implementation.
void Shutdown() override;
// UserEventService implementation.
void RecordUserEvent(
std::unique_ptr<sync_pb::UserEventSpecifics> specifics) override;
void RecordUserEvent(const sync_pb::UserEventSpecifics& specifics) override;
void RegisterDependentFieldTrial(
const std::string& trial_name,
sync_pb::UserEventSpecifics::EventCase event_case) override;
base::WeakPtr<ModelTypeSyncBridge> GetSyncBridge() override;
private:
bool ShouldRecordEvent(const sync_pb::UserEventSpecifics& specifics);
SyncService* sync_service_;
std::unique_ptr<UserEventSyncBridge> bridge_;
// Holds onto a random number for the duration of this execution of chrome. On
// restart it will be regenerated. This can be attached to events to know
// which events came from the same session.
uint64_t session_id_;
DISALLOW_COPY_AND_ASSIGN(UserEventServiceImpl);
};
} // namespace syncer
#endif // COMPONENTS_SYNC_USER_EVENTS_USER_EVENT_SERVICE_IMPL_H_
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "components/sync/user_events/user_event_service.h" #include "components/sync/user_events/user_event_service_impl.h"
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "base/message_loop/message_loop.h" #include "base/message_loop/message_loop.h"
...@@ -39,9 +39,9 @@ class TestSyncService : public FakeSyncService { ...@@ -39,9 +39,9 @@ class TestSyncService : public FakeSyncService {
ModelTypeSet preferred_data_types_; ModelTypeSet preferred_data_types_;
}; };
class UserEventServiceTest : public testing::Test { class UserEventServiceImplTest : public testing::Test {
protected: protected:
UserEventServiceTest() { UserEventServiceImplTest() {
scoped_feature_list_ = base::MakeUnique<base::test::ScopedFeatureList>(); scoped_feature_list_ = base::MakeUnique<base::test::ScopedFeatureList>();
scoped_feature_list_->InitAndEnableFeature(switches::kSyncUserEvents); scoped_feature_list_->InitAndEnableFeature(switches::kSyncUserEvents);
} }
...@@ -65,51 +65,51 @@ class UserEventServiceTest : public testing::Test { ...@@ -65,51 +65,51 @@ class UserEventServiceTest : public testing::Test {
base::MessageLoop message_loop_; base::MessageLoop message_loop_;
}; };
TEST_F(UserEventServiceTest, ShouldNotRecordNoSync) { TEST_F(UserEventServiceImplTest, ShouldNotRecordNoSync) {
UserEventService service(nullptr, MakeBridge()); UserEventServiceImpl service(nullptr, MakeBridge());
service.RecordUserEvent(base::MakeUnique<UserEventSpecifics>()); service.RecordUserEvent(base::MakeUnique<UserEventSpecifics>());
EXPECT_EQ(0u, processor().put_multimap().size()); EXPECT_EQ(0u, processor().put_multimap().size());
} }
TEST_F(UserEventServiceTest, ShouldNotRecordFeatureIsDisabled) { TEST_F(UserEventServiceImplTest, ShouldNotRecordFeatureIsDisabled) {
DisableUserEvents(); DisableUserEvents();
TestSyncService sync_service(false, ModelTypeSet(HISTORY_DELETE_DIRECTIVES)); TestSyncService sync_service(false, ModelTypeSet(HISTORY_DELETE_DIRECTIVES));
UserEventService service(&sync_service, MakeBridge()); UserEventServiceImpl service(&sync_service, MakeBridge());
service.RecordUserEvent(base::MakeUnique<UserEventSpecifics>()); service.RecordUserEvent(base::MakeUnique<UserEventSpecifics>());
EXPECT_EQ(0u, processor().put_multimap().size()); EXPECT_EQ(0u, processor().put_multimap().size());
} }
TEST_F(UserEventServiceTest, ShouldNotRecordNoHistory) { TEST_F(UserEventServiceImplTest, ShouldNotRecordNoHistory) {
TestSyncService sync_service(true, ModelTypeSet()); TestSyncService sync_service(true, ModelTypeSet());
UserEventService service(&sync_service, MakeBridge()); UserEventServiceImpl service(&sync_service, MakeBridge());
service.RecordUserEvent(base::MakeUnique<UserEventSpecifics>()); service.RecordUserEvent(base::MakeUnique<UserEventSpecifics>());
EXPECT_EQ(0u, processor().put_multimap().size()); EXPECT_EQ(0u, processor().put_multimap().size());
} }
TEST_F(UserEventServiceTest, ShouldNotRecordEngineOff) { TEST_F(UserEventServiceImplTest, ShouldNotRecordEngineOff) {
TestSyncService sync_service(false, ModelTypeSet(HISTORY_DELETE_DIRECTIVES)); TestSyncService sync_service(false, ModelTypeSet(HISTORY_DELETE_DIRECTIVES));
UserEventService service(&sync_service, MakeBridge()); UserEventServiceImpl service(&sync_service, MakeBridge());
service.RecordUserEvent(base::MakeUnique<UserEventSpecifics>()); service.RecordUserEvent(base::MakeUnique<UserEventSpecifics>());
EXPECT_EQ(0u, processor().put_multimap().size()); EXPECT_EQ(0u, processor().put_multimap().size());
} }
TEST_F(UserEventServiceTest, ShouldRecord) { TEST_F(UserEventServiceImplTest, ShouldRecord) {
TestSyncService sync_service(true, ModelTypeSet(HISTORY_DELETE_DIRECTIVES)); TestSyncService sync_service(true, ModelTypeSet(HISTORY_DELETE_DIRECTIVES));
UserEventService service(&sync_service, MakeBridge()); UserEventServiceImpl service(&sync_service, MakeBridge());
service.RecordUserEvent(base::MakeUnique<UserEventSpecifics>()); service.RecordUserEvent(base::MakeUnique<UserEventSpecifics>());
EXPECT_EQ(1u, processor().put_multimap().size()); EXPECT_EQ(1u, processor().put_multimap().size());
} }
TEST_F(UserEventServiceTest, SessionIdIsDifferent) { TEST_F(UserEventServiceImplTest, SessionIdIsDifferent) {
TestSyncService sync_service(true, ModelTypeSet(HISTORY_DELETE_DIRECTIVES)); TestSyncService sync_service(true, ModelTypeSet(HISTORY_DELETE_DIRECTIVES));
UserEventService service1(&sync_service, MakeBridge()); UserEventServiceImpl service1(&sync_service, MakeBridge());
service1.RecordUserEvent(base::MakeUnique<UserEventSpecifics>()); service1.RecordUserEvent(base::MakeUnique<UserEventSpecifics>());
ASSERT_EQ(1u, processor().put_multimap().size()); ASSERT_EQ(1u, processor().put_multimap().size());
auto put1 = processor().put_multimap().begin(); auto put1 = processor().put_multimap().begin();
int64_t session_id1 = put1->second->specifics.user_event().session_id(); int64_t session_id1 = put1->second->specifics.user_event().session_id();
UserEventService service2(&sync_service, MakeBridge()); UserEventServiceImpl service2(&sync_service, MakeBridge());
service2.RecordUserEvent(base::MakeUnique<UserEventSpecifics>()); service2.RecordUserEvent(base::MakeUnique<UserEventSpecifics>());
// The object processor() points to has changed to be |service2|'s processor. // The object processor() points to has changed to be |service2|'s processor.
ASSERT_EQ(1u, processor().put_multimap().size()); ASSERT_EQ(1u, processor().put_multimap().size());
......
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