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. 4 5 #ifndef CHROME_BROWSER_NOTIFICATIONS_EXTENSION_WELCOME_NOTIFICATION_H_ 6 #define CHROME_BROWSER_NOTIFICATIONS_EXTENSION_WELCOME_NOTIFICATION_H_ 7 8 #include <string> 9 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" 15 16 namespace base { 17 typedef Callback<void(void)> Closure; 18 } 19 20 namespace message_center { 21 class MessageCenter; 22 } 23 24 namespace tracked_objects { 25 class Location; 26 } 27 28 namespace user_prefs { 29 class PrefRegistrySyncable; 30 } 31 32 class Notification; 33 class Profile; 34 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 { 43 public: 44 // Allows for overriding global calls. 45 class Delegate { 46 public: 47 Delegate() {} 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; 54 private: 55 DISALLOW_COPY_AND_ASSIGN(Delegate); 56 }; 57 58 // Requested time from showing the welcome notification to expiration. 59 static const int kRequestedShowTimeDays; 60 61 virtual ~ExtensionWelcomeNotification(); 62 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); 70 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); 77 78 // PrefServiceSyncableObserver 79 virtual void OnIsSyncingChanged() OVERRIDE; 80 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); 84 85 // Handles Preference Registration for the Welcome Notification. 86 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* prefs); 87 88 private: 89 enum PopUpRequest { POP_UP_HIDDEN = 0, POP_UP_SHOWN = 1, }; 90 91 ExtensionWelcomeNotification( 92 const std::string& extension_id, 93 Profile* const profile, 94 ExtensionWelcomeNotification::Delegate* const delegate); 95 96 // Gets the message center from the delegate. 97 message_center::MessageCenter* GetMessageCenter() const; 98 99 // Unconditionally shows the welcome notification. 100 void ShowWelcomeNotification(const base::string16& display_source, 101 const PopUpRequest pop_up_request); 102 103 // Hides the welcome notification. 104 void HideWelcomeNotification(); 105 106 // Whether the notification has been dismissed. 107 bool UserHasDismissedWelcomeNotification() const; 108 109 // Called when the Welcome Notification Dismissed pref has been changed. 110 void OnWelcomeNotificationDismissedChanged(); 111 112 // Starts the welcome notification expiration timer. 113 void StartExpirationTimer(); 114 115 // Stops the welcome notification expiration timer. 116 void StopExpirationTimer(); 117 118 // Expires the welcome notification by hiding it and marking it dismissed. 119 void ExpireWelcomeNotification(); 120 121 // Gets the expiration timestamp or a null time is there is none. 122 base::Time GetExpirationTimestamp() const; 123 124 // Sets the expiration timestamp from now. 125 void SetExpirationTimestampFromNow(); 126 127 // True if the welcome notification has expired, false otherwise. 128 bool IsWelcomeNotificationExpired() const; 129 130 // Prefs listener for welcome_notification_dismissed. 131 BooleanPrefMember welcome_notification_dismissed_pref_; 132 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_; 136 137 // The notifier for the extension that we're listening for. 138 message_center::NotifierId notifier_id_; 139 140 // The profile which owns this object. 141 Profile* profile_; 142 143 // Notification ID of the Welcome Notification. 144 std::string welcome_notification_id_; 145 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_; 152 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_; 157 158 // Delegate for Chrome global calls like base::Time::GetTime() for 159 // testability. 160 scoped_ptr<Delegate> delegate_; 161 162 DISALLOW_COPY_AND_ASSIGN(ExtensionWelcomeNotification); 163 }; 164 165 #endif // CHROME_BROWSER_NOTIFICATIONS_EXTENSION_WELCOME_NOTIFICATION_H_ 166