| OLD | NEW |
| (Empty) |
| 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 #include "chrome/browser/chromeos/policy/consumer_management_notifier.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/callback.h" | |
| 11 #include "base/logging.h" | |
| 12 #include "base/macros.h" | |
| 13 #include "base/strings/string16.h" | |
| 14 #include "base/strings/utf_string_conversions.h" | |
| 15 #include "chrome/browser/browser_process.h" | |
| 16 #include "chrome/browser/chromeos/policy/consumer_management_stage.h" | |
| 17 #include "chrome/browser/notifications/notification.h" | |
| 18 #include "chrome/browser/notifications/notification_delegate.h" | |
| 19 #include "chrome/browser/notifications/notification_ui_manager.h" | |
| 20 #include "chrome/browser/ui/browser_navigator.h" | |
| 21 #include "chrome/browser/ui/browser_navigator_params.h" | |
| 22 #include "chrome/common/url_constants.h" | |
| 23 #include "grit/generated_resources.h" | |
| 24 #include "grit/theme_resources.h" | |
| 25 #include "ui/base/l10n/l10n_util.h" | |
| 26 #include "ui/base/page_transition_types.h" | |
| 27 #include "ui/base/resource/resource_bundle.h" | |
| 28 #include "ui/base/window_open_disposition.h" | |
| 29 #include "ui/message_center/notification_types.h" | |
| 30 #include "ui/message_center/notifier_settings.h" | |
| 31 #include "url/gurl.h" | |
| 32 | |
| 33 namespace { | |
| 34 | |
| 35 // Desktop notification constants. | |
| 36 const char kEnrollmentNotificationId[] = "consumer_management.enroll"; | |
| 37 const char kEnrollmentNotificationUrl[] = "chrome://consumer-management/enroll"; | |
| 38 const char kUnenrollmentNotificationId[] = "consumer_management.unenroll"; | |
| 39 const char kUnenrollmentNotificationUrl[] = | |
| 40 "chrome://consumer-management/unenroll"; | |
| 41 | |
| 42 // The path to the consumer management enrollment/unenrollment confirmation | |
| 43 // overlay, relative to the settings page URL. | |
| 44 const char kConsumerManagementOverlay[] = "consumer-management-overlay"; | |
| 45 | |
| 46 class DesktopNotificationDelegate : public NotificationDelegate { | |
| 47 public: | |
| 48 // |button_click_callback| is called when the button in the notification is | |
| 49 // clicked. | |
| 50 DesktopNotificationDelegate(const std::string& id, | |
| 51 const base::Closure& button_click_callback); | |
| 52 | |
| 53 // NotificationDelegate: | |
| 54 std::string id() const override; | |
| 55 void ButtonClick(int button_index) override; | |
| 56 | |
| 57 private: | |
| 58 ~DesktopNotificationDelegate() override; | |
| 59 | |
| 60 const std::string id_; | |
| 61 const base::Closure button_click_callback_; | |
| 62 | |
| 63 DISALLOW_COPY_AND_ASSIGN(DesktopNotificationDelegate); | |
| 64 }; | |
| 65 | |
| 66 DesktopNotificationDelegate::DesktopNotificationDelegate( | |
| 67 const std::string& id, | |
| 68 const base::Closure& button_click_callback) | |
| 69 : id_(id), button_click_callback_(button_click_callback) { | |
| 70 } | |
| 71 | |
| 72 DesktopNotificationDelegate::~DesktopNotificationDelegate() { | |
| 73 } | |
| 74 | |
| 75 std::string DesktopNotificationDelegate::id() const { | |
| 76 return id_; | |
| 77 } | |
| 78 | |
| 79 void DesktopNotificationDelegate::ButtonClick(int button_index) { | |
| 80 button_click_callback_.Run(); | |
| 81 } | |
| 82 | |
| 83 } // namespace | |
| 84 | |
| 85 namespace policy { | |
| 86 | |
| 87 ConsumerManagementNotifier::ConsumerManagementNotifier( | |
| 88 Profile* profile, | |
| 89 ConsumerManagementService* consumer_management_service) | |
| 90 : profile_(profile), | |
| 91 consumer_management_service_(consumer_management_service), | |
| 92 weak_ptr_factory_(this) { | |
| 93 consumer_management_service_->AddObserver(this); | |
| 94 OnConsumerManagementStatusChanged(); | |
| 95 } | |
| 96 | |
| 97 ConsumerManagementNotifier::~ConsumerManagementNotifier() { | |
| 98 } | |
| 99 | |
| 100 void ConsumerManagementNotifier::Shutdown() { | |
| 101 consumer_management_service_->RemoveObserver(this); | |
| 102 } | |
| 103 | |
| 104 void ConsumerManagementNotifier::OnConsumerManagementStatusChanged() { | |
| 105 const ConsumerManagementStage stage = | |
| 106 consumer_management_service_->GetStage(); | |
| 107 if (stage.HasPendingNotification()) { | |
| 108 // Reset the stage so that we won't show the same notification, again. | |
| 109 consumer_management_service_->SetStage(ConsumerManagementStage::None()); | |
| 110 ShowNotification(stage); | |
| 111 } | |
| 112 } | |
| 113 | |
| 114 void ConsumerManagementNotifier::ShowNotification( | |
| 115 const ConsumerManagementStage& stage) { | |
| 116 if (stage.HasEnrollmentSucceeded()) { | |
| 117 ShowDesktopNotification( | |
| 118 kEnrollmentNotificationId, | |
| 119 kEnrollmentNotificationUrl, | |
| 120 IDS_CONSUMER_MANAGEMENT_ENROLLMENT_NOTIFICATION_TITLE, | |
| 121 IDS_CONSUMER_MANAGEMENT_ENROLLMENT_NOTIFICATION_BODY, | |
| 122 IDS_CONSUMER_MANAGEMENT_NOTIFICATION_MODIFY_SETTINGS_BUTTON, | |
| 123 base::Bind(&ConsumerManagementNotifier::OpenSettingsPage, | |
| 124 weak_ptr_factory_.GetWeakPtr())); | |
| 125 } else if (stage.HasEnrollmentFailed()) { | |
| 126 ShowDesktopNotification( | |
| 127 kEnrollmentNotificationId, | |
| 128 kEnrollmentNotificationUrl, | |
| 129 IDS_CONSUMER_MANAGEMENT_ENROLLMENT_FAILURE_NOTIFICATION_TITLE, | |
| 130 IDS_CONSUMER_MANAGEMENT_ENROLLMENT_FAILURE_NOTIFICATION_BODY, | |
| 131 IDS_CONSUMER_MANAGEMENT_NOTIFICATION_TRY_AGAIN_BUTTON, | |
| 132 base::Bind(&ConsumerManagementNotifier::TryAgain, | |
| 133 weak_ptr_factory_.GetWeakPtr())); | |
| 134 } else if (stage.HasUnenrollmentSucceeded()) { | |
| 135 ShowDesktopNotification( | |
| 136 kUnenrollmentNotificationId, | |
| 137 kUnenrollmentNotificationUrl, | |
| 138 IDS_CONSUMER_MANAGEMENT_UNENROLLMENT_NOTIFICATION_TITLE, | |
| 139 IDS_CONSUMER_MANAGEMENT_UNENROLLMENT_NOTIFICATION_BODY, | |
| 140 IDS_CONSUMER_MANAGEMENT_NOTIFICATION_MODIFY_SETTINGS_BUTTON, | |
| 141 base::Bind(&ConsumerManagementNotifier::OpenSettingsPage, | |
| 142 weak_ptr_factory_.GetWeakPtr())); | |
| 143 } else if (stage.HasUnenrollmentFailed()) { | |
| 144 ShowDesktopNotification( | |
| 145 kUnenrollmentNotificationId, | |
| 146 kUnenrollmentNotificationUrl, | |
| 147 IDS_CONSUMER_MANAGEMENT_UNENROLLMENT_FAILURE_NOTIFICATION_TITLE, | |
| 148 IDS_CONSUMER_MANAGEMENT_UNENROLLMENT_FAILURE_NOTIFICATION_BODY, | |
| 149 IDS_CONSUMER_MANAGEMENT_NOTIFICATION_TRY_AGAIN_BUTTON, | |
| 150 base::Bind(&ConsumerManagementNotifier::TryAgain, | |
| 151 weak_ptr_factory_.GetWeakPtr())); | |
| 152 } else { | |
| 153 NOTREACHED(); | |
| 154 } | |
| 155 } | |
| 156 | |
| 157 void ConsumerManagementNotifier::ShowDesktopNotification( | |
| 158 const std::string& notification_id, | |
| 159 const std::string& notification_url, | |
| 160 int title_message_id, | |
| 161 int body_message_id, | |
| 162 int button_label_message_id, | |
| 163 const base::Closure& button_click_callback) { | |
| 164 message_center::RichNotificationData optional_field; | |
| 165 optional_field.buttons.push_back(message_center::ButtonInfo( | |
| 166 l10n_util::GetStringUTF16(button_label_message_id))); | |
| 167 | |
| 168 Notification notification( | |
| 169 message_center::NOTIFICATION_TYPE_SIMPLE, | |
| 170 l10n_util::GetStringUTF16(title_message_id), | |
| 171 l10n_util::GetStringUTF16(body_message_id), | |
| 172 ui::ResourceBundle::GetSharedInstance().GetImageNamed( | |
| 173 IDR_CONSUMER_MANAGEMENT_NOTIFICATION_ICON), | |
| 174 message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT, | |
| 175 notification_id), | |
| 176 base::string16(), // display_source | |
| 177 GURL(notification_url), notification_id, optional_field, | |
| 178 new DesktopNotificationDelegate(notification_id, button_click_callback)); | |
| 179 | |
| 180 notification.SetSystemPriority(); | |
| 181 g_browser_process->notification_ui_manager()->Add(notification, profile_); | |
| 182 } | |
| 183 | |
| 184 void ConsumerManagementNotifier::OpenSettingsPage() const { | |
| 185 const GURL url(chrome::kChromeUISettingsURL); | |
| 186 chrome::NavigateParams params(profile_, url, ui::PAGE_TRANSITION_LINK); | |
| 187 params.disposition = NEW_FOREGROUND_TAB; | |
| 188 chrome::Navigate(¶ms); | |
| 189 } | |
| 190 | |
| 191 void ConsumerManagementNotifier::TryAgain() const { | |
| 192 const GURL base_url(chrome::kChromeUISettingsURL); | |
| 193 const GURL url = base_url.Resolve(kConsumerManagementOverlay); | |
| 194 | |
| 195 chrome::NavigateParams params(profile_, url, ui::PAGE_TRANSITION_LINK); | |
| 196 params.disposition = NEW_FOREGROUND_TAB; | |
| 197 chrome::Navigate(¶ms); | |
| 198 } | |
| 199 | |
| 200 } // namespace policy | |
| OLD | NEW |