Roll src/third_party/WebKit eac3800:0237a66 (svn 202606:202607)
[chromium-blink-merge.git] / chrome / browser / notifications / extension_welcome_notification.h
blob728a1b9bf84612315f8ed123ca5563e80a532a2a
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_NOTIFICATIONS_EXTENSION_WELCOME_NOTIFICATION_H_
6 #define CHROME_BROWSER_NOTIFICATIONS_EXTENSION_WELCOME_NOTIFICATION_H_
8 #include <string>
10 #include "base/memory/scoped_ptr.h"
11 #include "base/prefs/pref_member.h"
12 #include "base/timer/timer.h"
13 #include "components/keyed_service/core/keyed_service.h"
14 #include "components/syncable_prefs/pref_service_syncable_observer.h"
15 #include "ui/message_center/notifier_settings.h"
17 namespace base {
18 typedef Callback<void(void)> Closure;
21 namespace message_center {
22 class MessageCenter;
25 namespace tracked_objects {
26 class Location;
29 namespace user_prefs {
30 class PrefRegistrySyncable;
33 class Notification;
34 class Profile;
36 // ExtensionWelcomeNotification is a keyed service which manages showing and
37 // hiding a welcome notification for built-in components that show
38 // notifications. The Welcome Notification presumes network connectivity since
39 // it relies on synced preferences to work. This is generally fine since the
40 // current consumers on the welcome notification also presume network
41 // connectivity.
43 // This class expects to be created and called from the UI thread.
44 class ExtensionWelcomeNotification
45 : public KeyedService,
46 public syncable_prefs::PrefServiceSyncableObserver {
47 public:
48 // Allows for overriding global calls.
49 class Delegate {
50 public:
51 Delegate() {}
52 virtual ~Delegate() {}
53 virtual message_center::MessageCenter* GetMessageCenter() = 0;
54 virtual base::Time GetCurrentTime() = 0;
55 virtual void PostTask(
56 const tracked_objects::Location& from_here,
57 const base::Closure& task) = 0;
58 private:
59 DISALLOW_COPY_AND_ASSIGN(Delegate);
62 // Requested time from showing the welcome notification to expiration.
63 static const int kRequestedShowTimeDays;
65 // The extension Id associated with the Google Now extension.
66 static const char kChromeNowExtensionID[];
68 ~ExtensionWelcomeNotification() override;
70 // To workaround the lack of delegating constructors prior to C++11, we use
71 // static Create methods.
72 // Creates an ExtensionWelcomeNotification with the default delegate.
73 static ExtensionWelcomeNotification* Create(Profile* const profile);
75 // Creates an ExtensionWelcomeNotification with the specified delegate.
76 static ExtensionWelcomeNotification* Create(Profile* const profile,
77 Delegate* const delegate);
79 // syncable_prefs::PrefServiceSyncableObserver
80 void OnIsSyncingChanged() override;
82 // Adds in the welcome notification if required for components built
83 // into Chrome that show notifications like Chrome Now.
84 void ShowWelcomeNotificationIfNecessary(const Notification& notification);
86 // Handles Preference Registration for the Welcome Notification.
87 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* prefs);
89 private:
90 enum PopUpRequest { POP_UP_HIDDEN = 0, POP_UP_SHOWN = 1, };
92 ExtensionWelcomeNotification(
93 Profile* const profile,
94 ExtensionWelcomeNotification::Delegate* const delegate);
96 // Gets the message center from the delegate.
97 message_center::MessageCenter* GetMessageCenter() const;
99 // Unconditionally shows the welcome notification.
100 void ShowWelcomeNotification(const base::string16& display_source,
101 const PopUpRequest pop_up_request);
103 // Hides the welcome notification.
104 void HideWelcomeNotification();
106 // Whether the notification has been dismissed.
107 bool UserHasDismissedWelcomeNotification() const;
109 // Called when the Welcome Notification Dismissed pref has been changed.
110 void OnWelcomeNotificationDismissedChanged();
112 // Starts the welcome notification expiration timer.
113 void StartExpirationTimer();
115 // Stops the welcome notification expiration timer.
116 void StopExpirationTimer();
118 // Expires the welcome notification by hiding it and marking it dismissed.
119 void ExpireWelcomeNotification();
121 // Gets the expiration timestamp or a null time is there is none.
122 base::Time GetExpirationTimestamp() const;
124 // Sets the expiration timestamp from now.
125 void SetExpirationTimestampFromNow();
127 // True if the welcome notification has expired, false otherwise.
128 bool IsWelcomeNotificationExpired() const;
130 // Prefs listener for welcome_notification_dismissed.
131 BooleanPrefMember welcome_notification_dismissed_pref_;
133 // Prefs listener for welcome_notification_dismissed_local.
134 // Dismissal flag migrated from a synced pref to a local one.
135 BooleanPrefMember welcome_notification_dismissed_local_pref_;
137 // The notifier for the extension that we're listening for.
138 message_center::NotifierId notifier_id_;
140 // The profile which owns this object.
141 Profile* profile_;
143 // Notification ID of the Welcome Notification.
144 std::string welcome_notification_id_;
146 // If the preferences are still syncing, store the last notification here
147 // so we can replay ShowWelcomeNotificationIfNecessary once the sync finishes.
148 // Simplifying Assumption: The delayed notification has passed the
149 // extension ID check. This means we do not need to store all of the
150 // notifications that may also show a welcome notification.
151 scoped_ptr<Notification> delayed_notification_;
153 // If the welcome notification is shown, this timer tracks when to hide the
154 // welcome notification.
155 scoped_ptr<base::OneShotTimer<ExtensionWelcomeNotification> >
156 expiration_timer_;
158 // Delegate for Chrome global calls like base::Time::GetTime() for
159 // testability.
160 scoped_ptr<Delegate> delegate_;
162 DISALLOW_COPY_AND_ASSIGN(ExtensionWelcomeNotification);
165 #endif // CHROME_BROWSER_NOTIFICATIONS_EXTENSION_WELCOME_NOTIFICATION_H_