Chromium Code Reviews| 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 "base/bind.h" | |
| 8 #include "base/callback.h" | |
| 9 #include "base/strings/utf_string_conversions.h" | |
| 10 #include "chrome/browser/browser_process.h" | |
| 11 #include "chrome/browser/notifications/notification.h" | |
| 12 #include "chrome/browser/notifications/notification_delegate.h" | |
| 13 #include "chrome/browser/notifications/notification_ui_manager.h" | |
| 14 #include "chrome/browser/ui/browser_navigator.h" | |
| 15 #include "chrome/common/url_constants.h" | |
| 16 #include "grit/generated_resources.h" | |
| 17 #include "grit/theme_resources.h" | |
| 18 #include "third_party/WebKit/public/web/WebTextDirection.h" | |
| 19 #include "ui/base/l10n/l10n_util.h" | |
| 20 #include "ui/base/page_transition_types.h" | |
| 21 #include "ui/base/resource/resource_bundle.h" | |
| 22 #include "ui/base/window_open_disposition.h" | |
| 23 #include "ui/message_center/notifier_settings.h" | |
| 24 #include "url/gurl.h" | |
| 25 | |
| 26 namespace { | |
| 27 | |
| 28 // Desktop notification constants. | |
| 29 const char kEnrollmentNotificationId[] = "consumer_management.enroll"; | |
| 30 const char kEnrollmentNotificationUrl[] = "chrome://consumer-management/enroll"; | |
| 31 | |
| 32 // The path to the consumer management enrollment/unenrollment confirmation | |
| 33 // overlay, relative to the settings page URL. | |
| 34 const char kConsumerManagementOverlay[] = "consumer-management-overlay"; | |
| 35 | |
| 36 class DesktopNotificationDelegate : public NotificationDelegate { | |
| 37 public: | |
| 38 // |button_click_callback| is called when the button in the notification is | |
| 39 // clicked. | |
| 40 DesktopNotificationDelegate(const std::string& id, | |
|
bartfab (slow)
2014/11/27 14:09:09
Nit: #include <string>
davidyu
2014/11/28 02:45:25
Done.
| |
| 41 const base::Closure& button_click_callback); | |
| 42 | |
| 43 // NotificationDelegate: | |
| 44 virtual std::string id() const override; | |
|
bartfab (slow)
2014/11/27 14:09:09
Nit: No |virtual| with |override|.
davidyu
2014/11/28 02:45:25
Done.
| |
| 45 virtual void ButtonClick(int button_index) override; | |
|
bartfab (slow)
2014/11/27 14:09:09
Nit: No |virtual| with |override|.
davidyu
2014/11/28 02:45:26
Done.
| |
| 46 | |
| 47 private: | |
| 48 virtual ~DesktopNotificationDelegate(); | |
|
bartfab (slow)
2014/11/27 14:09:09
Nit: Use |override| instead of |virtual|.
davidyu
2014/11/28 02:45:26
Done.
| |
| 49 | |
| 50 std::string id_; | |
|
bartfab (slow)
2014/11/27 14:09:09
Nit: const.
davidyu
2014/11/28 02:45:25
Done.
| |
| 51 base::Closure button_click_callback_; | |
|
bartfab (slow)
2014/11/27 14:09:09
Nit: const.
davidyu
2014/11/28 02:45:26
Done.
| |
| 52 | |
| 53 DISALLOW_COPY_AND_ASSIGN(DesktopNotificationDelegate); | |
| 54 }; | |
| 55 | |
| 56 DesktopNotificationDelegate::DesktopNotificationDelegate( | |
| 57 const std::string& id, | |
| 58 const base::Closure& button_click_callback) | |
| 59 : id_(id), button_click_callback_(button_click_callback) { | |
| 60 } | |
| 61 | |
| 62 DesktopNotificationDelegate::~DesktopNotificationDelegate() { | |
| 63 } | |
| 64 | |
| 65 std::string DesktopNotificationDelegate::id() const { | |
| 66 return id_; | |
| 67 } | |
| 68 | |
| 69 void DesktopNotificationDelegate::ButtonClick(int button_index) { | |
| 70 button_click_callback_.Run(); | |
| 71 } | |
| 72 | |
| 73 } // namespace | |
| 74 | |
| 75 namespace policy { | |
| 76 | |
| 77 ConsumerManagementNotifier::ConsumerManagementNotifier( | |
| 78 Profile* profile, | |
| 79 ConsumerManagementService* consumer_management_service) | |
| 80 : profile_(profile), | |
| 81 consumer_management_service_(consumer_management_service), | |
| 82 weak_ptr_factory_(this) { | |
| 83 consumer_management_service_->AddObserver(this); | |
| 84 OnConsumerManagementStatusChanged(); | |
| 85 } | |
| 86 | |
| 87 ConsumerManagementNotifier::~ConsumerManagementNotifier() { | |
| 88 } | |
| 89 | |
| 90 void ConsumerManagementNotifier::Shutdown() { | |
| 91 consumer_management_service_->RemoveObserver(this); | |
| 92 } | |
| 93 | |
| 94 void ConsumerManagementNotifier::OnConsumerManagementStatusChanged() { | |
| 95 if (consumer_management_service_->HasPendingEnrollmentNotification()) { | |
| 96 ConsumerManagementService::EnrollmentStage stage = | |
|
bartfab (slow)
2014/11/27 14:09:09
Nit: const.
davidyu
2014/11/28 02:45:25
Done.
| |
| 97 consumer_management_service_->GetEnrollmentStage(); | |
| 98 // Reset the enrollment stage so that we won't show the same notification, | |
| 99 // again. | |
| 100 consumer_management_service_->SetEnrollmentStage( | |
| 101 ConsumerManagementService::ENROLLMENT_STAGE_NONE); | |
| 102 ShowDesktopNotification(stage); | |
| 103 } | |
| 104 } | |
| 105 | |
| 106 void ConsumerManagementNotifier::ShowDesktopNotification( | |
| 107 ConsumerManagementService::EnrollmentStage stage) { | |
| 108 base::string16 title; | |
|
bartfab (slow)
2014/11/27 14:09:09
Nit: #include "base/strings/string16.h"
davidyu
2014/11/28 02:45:25
Done.
| |
| 109 base::string16 body; | |
| 110 base::string16 button_label; | |
| 111 base::Closure button_click_callback; | |
| 112 | |
| 113 if (stage == ConsumerManagementService::ENROLLMENT_STAGE_SUCCESS) { | |
| 114 title = l10n_util::GetStringUTF16( | |
| 115 IDS_CONSUMER_MANAGEMENT_ENROLLMENT_NOTIFICATION_TITLE); | |
| 116 body = l10n_util::GetStringUTF16( | |
| 117 IDS_CONSUMER_MANAGEMENT_ENROLLMENT_NOTIFICATION_BODY); | |
| 118 button_label = l10n_util::GetStringUTF16( | |
| 119 IDS_CONSUMER_MANAGEMENT_NOTIFICATION_MODIFY_SETTINGS_BUTTON); | |
| 120 button_click_callback = base::Bind( | |
| 121 &ConsumerManagementNotifier::OpenSettingsPage, | |
| 122 weak_ptr_factory_.GetWeakPtr()); | |
| 123 } else { | |
| 124 title = l10n_util::GetStringUTF16( | |
| 125 IDS_CONSUMER_MANAGEMENT_ENROLLMENT_FAILURE_NOTIFICATION_TITLE); | |
| 126 body = l10n_util::GetStringUTF16( | |
| 127 IDS_CONSUMER_MANAGEMENT_ENROLLMENT_FAILURE_NOTIFICATION_BODY); | |
| 128 button_label = l10n_util::GetStringUTF16( | |
| 129 IDS_CONSUMER_MANAGEMENT_NOTIFICATION_TRY_AGAIN_BUTTON); | |
| 130 button_click_callback = base::Bind( | |
| 131 &ConsumerManagementNotifier::TryEnrollmentAgain, | |
| 132 weak_ptr_factory_.GetWeakPtr()); | |
| 133 } | |
| 134 | |
| 135 message_center::RichNotificationData optional_field; | |
| 136 optional_field.buttons.push_back(message_center::ButtonInfo(button_label)); | |
| 137 Notification notification( | |
| 138 message_center::NOTIFICATION_TYPE_SIMPLE, | |
|
bartfab (slow)
2014/11/27 14:09:09
Nit: #include "ui/message_center/notification_type
davidyu
2014/11/28 02:45:25
Done.
| |
| 139 GURL(kEnrollmentNotificationUrl), | |
| 140 title, | |
| 141 body, | |
| 142 ui::ResourceBundle::GetSharedInstance().GetImageNamed( | |
| 143 IDR_CONSUMER_MANAGEMENT_NOTIFICATION_ICON), | |
| 144 blink::WebTextDirectionDefault, | |
| 145 message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT, | |
| 146 kEnrollmentNotificationId), | |
| 147 base::string16(), // display_source | |
| 148 base::UTF8ToUTF16(kEnrollmentNotificationId), | |
| 149 optional_field, | |
| 150 new DesktopNotificationDelegate(kEnrollmentNotificationId, | |
| 151 button_click_callback)); | |
| 152 notification.SetSystemPriority(); | |
| 153 g_browser_process->notification_ui_manager()->Add(notification, profile_); | |
| 154 } | |
| 155 | |
| 156 void ConsumerManagementNotifier::OpenSettingsPage() const { | |
| 157 const GURL url(chrome::kChromeUISettingsURL); | |
| 158 chrome::NavigateParams params(profile_, url, ui::PAGE_TRANSITION_LINK); | |
| 159 params.disposition = NEW_FOREGROUND_TAB; | |
| 160 chrome::Navigate(¶ms); | |
| 161 } | |
| 162 | |
| 163 void ConsumerManagementNotifier::TryEnrollmentAgain() const { | |
| 164 const GURL base_url(chrome::kChromeUISettingsURL); | |
| 165 const GURL url = base_url.Resolve(kConsumerManagementOverlay); | |
| 166 | |
| 167 chrome::NavigateParams params(profile_, url, ui::PAGE_TRANSITION_LINK); | |
| 168 params.disposition = NEW_FOREGROUND_TAB; | |
| 169 chrome::Navigate(¶ms); | |
| 170 } | |
| 171 | |
| 172 } // namespace policy | |
| OLD | NEW |