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_
10 #include "base/memory/scoped_ptr.h"
11 #include "base/prefs/pref_member.h"
12 #include "base/timer/timer.h"
13 #include "chrome/browser/prefs/pref_service_syncable_observer.h"
14 #include "ui/message_center/notifier_settings.h"
17 typedef Callback
<void(void)> Closure
;
20 namespace message_center
{
24 namespace tracked_objects
{
28 namespace user_prefs
{
29 class PrefRegistrySyncable
;
35 // ExtensionWelcomeNotification is a part of DesktopNotificationService and
36 // manages showing and hiding a welcome notification for built-in components
37 // that show notifications. The Welcome Notification presumes network
38 // connectivity since it relies on synced preferences to work. This is generally
39 // fine since the current consumers on the welcome notification also presume
40 // network connectivity.
41 // This class expects to be created and called from the UI thread.
42 class ExtensionWelcomeNotification
: public PrefServiceSyncableObserver
{
44 // Allows for overriding global calls.
48 virtual ~Delegate() {}
49 virtual message_center::MessageCenter
* GetMessageCenter() = 0;
50 virtual base::Time
GetCurrentTime() = 0;
51 virtual void PostTask(
52 const tracked_objects::Location
& from_here
,
53 const base::Closure
& task
) = 0;
55 DISALLOW_COPY_AND_ASSIGN(Delegate
);
58 // Requested time from showing the welcome notification to expiration.
59 static const int kRequestedShowTimeDays
;
61 virtual ~ExtensionWelcomeNotification();
63 // To workaround the lack of delegating constructors prior to C++11, we use
64 // static Create methods.
65 // Creates an ExtensionWelcomeNotification owned by the specified
66 // extension id with the default delegate.
67 static scoped_ptr
<ExtensionWelcomeNotification
> Create(
68 const std::string
& extension_id
,
69 Profile
* const profile
);
71 // Creates an ExtensionWelcomeNotification owned by the specified
72 // extension id with the specified delegate.
73 static scoped_ptr
<ExtensionWelcomeNotification
> Create(
74 const std::string
& extension_id
,
75 Profile
* const profile
,
76 Delegate
* const delegate
);
78 // PrefServiceSyncableObserver
79 virtual void OnIsSyncingChanged() OVERRIDE
;
81 // Adds in the welcome notification if required for components built
82 // into Chrome that show notifications like Chrome Now.
83 void ShowWelcomeNotificationIfNecessary(const Notification
& notification
);
85 // Handles Preference Registration for the Welcome Notification.
86 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable
* prefs
);
89 enum PopUpRequest
{ POP_UP_HIDDEN
= 0, POP_UP_SHOWN
= 1, };
91 ExtensionWelcomeNotification(
92 const std::string
& extension_id
,
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.
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
> >
158 // Delegate for Chrome global calls like base::Time::GetTime() for
160 scoped_ptr
<Delegate
> delegate_
;
162 DISALLOW_COPY_AND_ASSIGN(ExtensionWelcomeNotification
);
165 #endif // CHROME_BROWSER_NOTIFICATIONS_EXTENSION_WELCOME_NOTIFICATION_H_