1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_BANNERS_APP_BANNER_SETTINGS_HELPER_H_
6 #define CHROME_BROWSER_BANNERS_APP_BANNER_SETTINGS_HELPER_H_
12 #include "base/macros.h"
13 #include "base/time/time.h"
14 #include "ui/base/page_transition_types.h"
18 } // namespace content
23 // Utility class to record banner events for the given package or start url.
25 // These events are used to decide when banners should be shown, using a
26 // heuristic based on how many different days in a recent period of time (for
27 // example the past two weeks) the banner could have been shown, when it was
28 // last shown, when it was last blocked, and when it was last installed (for
29 // ServiceWorker style apps - native apps can query whether the app was
30 // installed directly).
32 // The desired effect is to have banners appear once a user has demonstrated
33 // an ongoing relationship with the app, and not to pester the user too much.
35 // For most events only the last event is recorded. The exception are the
36 // could show events. For these a list of the events is maintained. At most
37 // one event is stored per day, and events outside the window the heuristic
38 // uses are discarded. Local times are used to enforce these rules, to ensure
39 // what we count as a day matches what the user perceives to be days.
40 class AppBannerSettingsHelper
{
43 APP_BANNER_EVENT_COULD_SHOW
,
44 APP_BANNER_EVENT_DID_SHOW
,
45 APP_BANNER_EVENT_DID_BLOCK
,
46 APP_BANNER_EVENT_DID_ADD_TO_HOMESCREEN
,
47 APP_BANNER_EVENT_NUM_EVENTS
,
50 enum AppBannerRapporMetric
{
55 // BannerEvents record the time that a site was accessed, along with an
56 // engagement weight representing the importance of the access.
62 // The content setting basically records a simplified subset of history.
63 // For privacy reasons this needs to be cleared. The ClearHistoryForURLs
64 // function removes any information from the banner content settings for the
66 static void ClearHistoryForURLs(Profile
* profile
,
67 const std::set
<GURL
>& origin_urls
);
69 // Record a banner installation event, for either a WEB or NATIVE app.
70 static void RecordBannerInstallEvent(
71 content::WebContents
* web_contents
,
72 const std::string
& package_name_or_start_url
,
73 AppBannerRapporMetric rappor_metric
);
75 // Record a banner dismissal event, for either a WEB or NATIVE app.
76 static void RecordBannerDismissEvent(
77 content::WebContents
* web_contents
,
78 const std::string
& package_name_or_start_url
,
79 AppBannerRapporMetric rappor_metric
);
81 // Record a banner event. Should not be used for could show events, as they
82 // require a transition type.
83 static void RecordBannerEvent(content::WebContents
* web_contents
,
84 const GURL
& origin_url
,
85 const std::string
& package_name_or_start_url
,
89 // Record a banner could show event, with a specified transition type.
90 static void RecordBannerCouldShowEvent(
91 content::WebContents
* web_contents
,
92 const GURL
& origin_url
,
93 const std::string
& package_name_or_start_url
,
95 ui::PageTransition transition_type
);
97 // Determine if the banner should be shown, given the recorded events for the
99 static bool ShouldShowBanner(content::WebContents
* web_contents
,
100 const GURL
& origin_url
,
101 const std::string
& package_name_or_start_url
,
104 // Gets the could have been shown events that are stored for the given package
105 // or start url. This is only exposed for testing.
106 static std::vector
<BannerEvent
> GetCouldShowBannerEvents(
107 content::WebContents
* web_contents
,
108 const GURL
& origin_url
,
109 const std::string
& package_name_or_start_url
);
111 // Get the recorded event for an event type that only records the last event.
112 // Should not be used with APP_BANNER_EVENT_COULD_SHOW. This is only exposed
114 static base::Time
GetSingleBannerEvent(
115 content::WebContents
* web_contents
,
116 const GURL
& origin_url
,
117 const std::string
& package_name_or_start_url
,
118 AppBannerEvent event
);
120 // Set the engagement weights assigned to direct and indirect navigations.
121 // This is only exposed for testing.
122 static void SetEngagementWeights(double direct_engagement
,
123 double indirect_engagement
);
125 // Set the minimum number of minutes between banner visits that will
126 // trigger a could show banner event. This must be less than the
127 // number of minutes in a day, and evenly divide the number of minutes
129 static void SetMinimumMinutesBetweenVisits(unsigned int minutes
);
131 // Bucket a given time to the given resolution in local time.
132 static base::Time
BucketTimeToResolution(base::Time time
,
133 unsigned int minutes
);
135 // Updates all values from field trial.
136 static void UpdateFromFieldTrial();
139 DISALLOW_IMPLICIT_CONSTRUCTORS(AppBannerSettingsHelper
);
142 #endif // CHROME_BROWSER_BANNERS_APP_BANNER_SETTINGS_HELPER_H_