Commit 4fff43ef authored by Jeffrey Cohen's avatar Jeffrey Cohen Committed by Commit Bot

initialize hats_base upon profile creation

Bug: 807714
Change-Id: Ib6a6b2fce7ddb494741364976c73569ce87756f8
Reviewed-on: https://chromium-review.googlesource.com/1228881
Commit-Queue: Jeffrey Cohen <jeffreycohen@chromium.org>
Reviewed-by: default avatarScott Violet <sky@chromium.org>
Cr-Commit-Position: refs/heads/master@{#595504}
parent ea4a445b
......@@ -963,6 +963,10 @@ jumbo_split_static_library("ui") {
"global_error/global_error_service.h",
"global_error/global_error_service_factory.cc",
"global_error/global_error_service_factory.h",
"hats/hats_service.cc",
"hats/hats_service.h",
"hats/hats_service_factory.cc",
"hats/hats_service_factory.h",
"hung_renderer/hung_renderer_core.cc",
"hung_renderer/hung_renderer_core.h",
"javascript_dialogs/javascript_dialog_views.cc",
......
chrome/browser/ui/hats
=====================
This directory contains HaTS (Happiness Tracking Survey) code that is used to service the display of surveys launched from any trigger point within Chrome.
This code will coordinate with user's profiles to ensure that Chrome is not serving too many surveys to a single profile, only targeting profiles have UMA enabled, and not targeting enterprise users.
// Copyright 2018 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 "chrome/browser/ui/hats/hats_service.h"
#include <stddef.h>
#include <iostream>
#include "base/metrics/field_trial_params.h"
#include "base/rand_util.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/common/chrome_features.h"
#include "content/public/browser/browser_thread.h"
namespace {
// which survey we're triggering
const char kHatsSurveyTrigger[] = "survey";
const char kHatsSurveyProbability[] = "probability";
const char kHatsSurveyEnSiteID[] = "en_site_id";
const char kHatsSurveyTriggerDefault[] = "test";
const double kHatsSurveyProbabilityDefault = 1;
const char kHatsSurveyEnSiteIDDefault[] = "z4cctguzopq5x2ftal6vdgjrui";
HatsFinchConfig CreateHatsFinchConfig() {
HatsFinchConfig config;
config.trigger = base::FeatureParam<std::string>(
&features::kHappinessTrackingSurveysForDesktop,
kHatsSurveyTrigger, kHatsSurveyTriggerDefault)
.Get();
config.probability =
base::FeatureParam<double>(&features::kHappinessTrackingSurveysForDesktop,
kHatsSurveyProbability,
kHatsSurveyProbabilityDefault)
.Get();
config.site_ids.insert(
std::make_pair("en", base::FeatureParam<std::string>(
&features::kHappinessTrackingSurveysForDesktop,
kHatsSurveyEnSiteID, kHatsSurveyEnSiteIDDefault)
.Get()));
return config;
}
} // namespace
HatsFinchConfig::HatsFinchConfig() = default;
HatsFinchConfig::~HatsFinchConfig() = default;
HatsFinchConfig::HatsFinchConfig(const HatsFinchConfig& other) = default;
HatsService::HatsService(Profile* profile)
: profile_(profile), hats_finch_config_(CreateHatsFinchConfig()) {}
bool HatsService::ShouldShowSurvey() {
return (base::RandDouble() < hats_finch_config_.probability);
// TODO add pref checks to avoid too many surveys for a single profile
}
// Copyright 2018 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_UI_HATS_HATS_SERVICE_H_
#define CHROME_BROWSER_UI_HATS_HATS_SERVICE_H_
#include <stddef.h>
#include <map>
#include <string>
#include "base/gtest_prod_util.h"
#include "base/macros.h"
#include "components/keyed_service/core/keyed_service.h"
class Profile;
struct HatsFinchConfig {
HatsFinchConfig();
~HatsFinchConfig();
HatsFinchConfig(const HatsFinchConfig& other);
double probability; // This is the percent of users [0,1] that will see the
// survey
std::string trigger; // This is the name of the survey in question.
// This is a map between the locale being presented and the site ID used to
// fetch the survey.
std::map<std::string, std::string> site_ids;
};
// This class provides the client side logic for determining if a
// survey should be shown for any trigger based on input from a finch
// configuration. It is created on a per profile basis.
class HatsService : public KeyedService {
public:
explicit HatsService(Profile* profile);
bool ShouldShowSurvey();
private:
Profile* profile_;
const HatsFinchConfig hats_finch_config_;
FRIEND_TEST_ALL_PREFIXES(HatsForceEnabledTest, ParamsWithAForcedFlagTest);
DISALLOW_COPY_AND_ASSIGN(HatsService);
};
#endif // CHROME_BROWSER_UI_HATS_HATS_SERVICE_H_
// Copyright 2018 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 "chrome/browser/ui/hats/hats_service_factory.h"
#include "base/memory/singleton.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/ui/hats/hats_service.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
// static
HatsService* HatsServiceFactory::GetForProfile(Profile* profile,
bool create_if_necessary) {
return static_cast<HatsService*>(
GetInstance()->GetServiceForBrowserContext(profile, create_if_necessary));
}
// static
HatsServiceFactory* HatsServiceFactory::GetInstance() {
return base::Singleton<HatsServiceFactory>::get();
}
HatsServiceFactory::HatsServiceFactory()
: BrowserContextKeyedServiceFactory(
"HatsService",
BrowserContextDependencyManager::GetInstance()) {
DependsOn(IdentityManagerFactory::GetInstance());
}
KeyedService* HatsServiceFactory::BuildServiceInstanceFor(
content::BrowserContext* context) const {
Profile* profile = Profile::FromBrowserContext(context);
return new HatsService(profile);
}
HatsServiceFactory::~HatsServiceFactory() {}
// Copyright 2018 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_UI_HATS_HATS_SERVICE_FACTORY_H_
#define CHROME_BROWSER_UI_HATS_HATS_SERVICE_FACTORY_H_
#include "base/macros.h"
#include "base/memory/singleton.h"
#include "components/keyed_service/content/browser_context_keyed_service_factory.h"
class HatsService;
class Profile;
class HatsServiceFactory : public BrowserContextKeyedServiceFactory {
public:
static HatsService* GetForProfile(Profile* profile, bool create_if_necessary);
static HatsServiceFactory* GetInstance();
private:
friend struct base::DefaultSingletonTraits<HatsServiceFactory>;
HatsServiceFactory();
~HatsServiceFactory() override;
// Overrides from BrowserContextKeyedServiceFactory:
KeyedService* BuildServiceInstanceFor(
content::BrowserContext* context) const override;
DISALLOW_COPY_AND_ASSIGN(HatsServiceFactory);
};
#endif // CHROME_BROWSER_UI_HATS_HATS_SERVICE_FACTORY_H_
// Copyright (c) 2018 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 "chrome/browser/ui/hats/hats_service.h"
#include <memory>
#include "base/test/scoped_feature_list.h"
#include "chrome/common/chrome_features.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
class HatsForceEnabledTest : public testing::Test {
public:
HatsForceEnabledTest() {}
void SetUp() override {
scoped_feature_list_.InitWithFeatureState(
features::kHappinessTrackingSurveysForDesktop, true);
// I'm passing in a null pointer for profile for now.
hats_service_ = std::make_unique<HatsService>(nullptr);
}
protected:
std::unique_ptr<HatsService> hats_service_;
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
TEST_F(HatsForceEnabledTest, ParamsWithAForcedFlagTest) {
ASSERT_EQ(1, hats_service_->hats_finch_config_.probability);
}
......@@ -19,11 +19,14 @@
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/chrome_select_file_policy.h"
#include "chrome/browser/ui/hats/hats_service.h"
#include "chrome/browser/ui/hats/hats_service_factory.h"
#include "chrome/browser/ui/location_bar/location_bar.h"
#include "chrome/browser/ui/omnibox/clipboard_utils.h"
#include "chrome/browser/ui/search/ntp_user_data_logger.h"
#include "chrome/browser/ui/search/search_ipc_router_policy_impl.h"
#include "chrome/browser/ui/tab_contents/core_tab_helper.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/url_constants.h"
#include "chrome/grit/generated_resources.h"
#include "components/browser_sync/profile_sync_service.h"
......@@ -220,6 +223,17 @@ void SearchTabHelper::DidFinishLoad(content::RenderFrameHost* render_frame_host,
if (search::IsInstantNTP(web_contents_))
RecordNewTabLoadTime(web_contents_);
}
#if !defined(OS_ANDROID)
if (base::FeatureList::IsEnabled(
features::kHappinessTrackingSurveysForDesktop)) {
HatsService* hats_service =
HatsServiceFactory::GetForProfile(profile(), true);
if (hats_service->ShouldShowSurvey()) {
// TODO launch bubble;
}
}
#endif // !defined(OS_ANDROID)
}
void SearchTabHelper::NavigationEntryCommitted(
......
......@@ -2805,6 +2805,7 @@ test("unit_tests") {
# platforms.
sources += [
"../browser/component_updater/crl_set_component_installer_unittest.cc",
"../browser/ui/hats/hats_unittest.cc",
]
}
......
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