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