OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_BANNERS_APP_BANNER_SETTINGS_HELPER_H_ | 5 #ifndef CHROME_BROWSER_BANNERS_APP_BANNER_SETTINGS_HELPER_H_ |
6 #define CHROME_BROWSER_BANNERS_APP_BANNER_SETTINGS_HELPER_H_ | 6 #define CHROME_BROWSER_BANNERS_APP_BANNER_SETTINGS_HELPER_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
13 | 13 |
14 namespace content { | 14 namespace content { |
15 class WebContents; | 15 class WebContents; |
16 } // namespace content | 16 } // namespace content |
17 | 17 |
18 class GURL; | 18 class GURL; |
19 | 19 |
20 // Utility class for reading and updating ContentSettings for app banners. | 20 // Utility class to record banner events for the given package or start url. |
| 21 // |
| 22 // These events are used to decide when banners should be shown, using a |
| 23 // heuristic based on how many different days in a recent period of time (for |
| 24 // example the past two weeks) the banner could have been shown, when it was |
| 25 // last shown, when it was last blocked, and when it was last installed (for |
| 26 // ServiceWorker style apps - native apps can query whether the app was |
| 27 // installed directly). |
| 28 // |
| 29 // The desired effect is to have banners appear once a user has demonstrated |
| 30 // an ongoing relationship with the app, and not to pester the user too much. |
| 31 // |
| 32 // For most events only the last event is recorded. The exception are the |
| 33 // could show events. For these a list of the events is maintained. At most |
| 34 // one event is stored per day, and events outside the window the heuristic |
| 35 // uses are discarded. Local times are used to enforce these rules, to ensure |
| 36 // what we count as a day matches what the user perceives to be days. |
21 class AppBannerSettingsHelper { | 37 class AppBannerSettingsHelper { |
22 public: | 38 public: |
23 // TODO(benwells): Use this method to implement smarter triggering logic. | 39 enum AppBannerEvent { |
24 // See http://crbug.com/452825. | 40 APP_BANNER_EVENT_COULD_SHOW, |
25 // Records that a banner could have been shown for the given package or start | 41 APP_BANNER_EVENT_DID_SHOW, |
26 // url. | 42 APP_BANNER_EVENT_DID_BLOCK, |
27 // | 43 APP_BANNER_EVENT_DID_ADD_TO_HOMESCREEN, |
28 // These events are used to decide when banners should be shown, using a | 44 APP_BANNER_EVENT_NUM_EVENTS, |
29 // heuristic based on how many different days in a recent period of time (for | 45 }; |
30 // example the past two weeks) the banner could have been shown. The desired | 46 |
31 // effect is to have banners appear once a user has demonstrated an ongoing | 47 static void RecordBannerEvent(content::WebContents* web_contents, |
32 // relationship with the app. | 48 const GURL& origin_url, |
33 // | 49 const std::string& package_name_or_start_url, |
34 // At most one event is stored per day, and events outside the window the | 50 AppBannerEvent event, |
35 // heuristic uses are discarded. Local times are used to enforce these rules, | 51 base::Time time); |
36 // to ensure what we count as a day matches what the user perceives to be | 52 |
37 // days. | 53 // Determine if the banner should be shown, given the recorded events for the |
38 static void RecordCouldShowBannerEvent( | 54 // supplied app. |
39 content::WebContents* web_contents, | 55 static bool ShouldShowBanner(content::WebContents* web_contents, |
40 const GURL& origin_url, | 56 const GURL& origin_url, |
41 const std::string& package_name_or_start_url, | 57 const std::string& package_name_or_start_url, |
42 base::Time time); | 58 base::Time time); |
43 | 59 |
44 // Gets the could have been shown events that are stored for the given package | 60 // Gets the could have been shown events that are stored for the given package |
45 // or start url. This is only used for testing. | 61 // or start url. This is only exposed for testing. |
46 static std::vector<base::Time> GetCouldShowBannerEvents( | 62 static std::vector<base::Time> GetCouldShowBannerEvents( |
47 content::WebContents* web_contents, | 63 content::WebContents* web_contents, |
48 const GURL& origin_url, | 64 const GURL& origin_url, |
49 const std::string& package_name_or_start_url); | 65 const std::string& package_name_or_start_url); |
50 | 66 |
| 67 // Get the recorded event for an event type that only records the last event. |
| 68 // Should not be used with APP_BANNER_EVENT_COULD_SHOW. This is only exposed |
| 69 // for testing. |
| 70 static base::Time GetSingleBannerEvent( |
| 71 content::WebContents* web_contents, |
| 72 const GURL& origin_url, |
| 73 const std::string& package_name_or_start_url, |
| 74 AppBannerEvent event); |
| 75 |
51 // Checks if a URL is allowed to show a banner for the given package or start | 76 // Checks if a URL is allowed to show a banner for the given package or start |
52 // url. | 77 // url. |
53 static bool IsAllowed(content::WebContents* web_contents, | 78 static bool IsAllowed(content::WebContents* web_contents, |
54 const GURL& origin_url, | 79 const GURL& origin_url, |
55 const std::string& package_name_or_start_url); | 80 const std::string& package_name_or_start_url); |
56 | 81 |
57 // Blocks a URL from showing a banner for the given package or start url. | 82 // Blocks a URL from showing a banner for the given package or start url. |
58 static void Block(content::WebContents* web_contents, | 83 static void Block(content::WebContents* web_contents, |
59 const GURL& origin_url, | 84 const GURL& origin_url, |
60 const std::string& package_name_or_start_url); | 85 const std::string& package_name_or_start_url); |
61 | 86 |
62 private: | 87 private: |
63 DISALLOW_IMPLICIT_CONSTRUCTORS(AppBannerSettingsHelper); | 88 DISALLOW_IMPLICIT_CONSTRUCTORS(AppBannerSettingsHelper); |
64 }; | 89 }; |
65 | 90 |
66 #endif // CHROME_BROWSER_BANNERS_APP_BANNER_SETTINGS_HELPER_H_ | 91 #endif // CHROME_BROWSER_BANNERS_APP_BANNER_SETTINGS_HELPER_H_ |
OLD | NEW |