Fix the duplicatedly commited composition text when switching IME.
[chromium-blink-merge.git] / chrome / browser / chromeos / policy / consumer_management_notifier.cc
blob830b8ab8636fb3e20db8f10930ba9231e238c5d3
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 "ui/base/l10n/l10n_util.h"
24 #include "ui/base/page_transition_types.h"
25 #include "ui/base/resource/resource_bundle.h"
26 #include "ui/base/window_open_disposition.h"
27 #include "ui/message_center/notification_types.h"
28 #include "ui/message_center/notifier_settings.h"
29 #include "url/gurl.h"
31 namespace {
33 // Desktop notification constants.
34 const char kEnrollmentNotificationId[] = "consumer_management.enroll";
35 const char kEnrollmentNotificationUrl[] = "chrome://consumer-management/enroll";
36 const char kUnenrollmentNotificationId[] = "consumer_management.unenroll";
37 const char kUnenrollmentNotificationUrl[] =
38 "chrome://consumer-management/unenroll";
40 // The path to the consumer management enrollment/unenrollment confirmation
41 // overlay, relative to the settings page URL.
42 const char kConsumerManagementOverlay[] = "consumer-management-overlay";
44 class DesktopNotificationDelegate : public NotificationDelegate {
45 public:
46 // |button_click_callback| is called when the button in the notification is
47 // clicked.
48 DesktopNotificationDelegate(const std::string& id,
49 const base::Closure& button_click_callback);
51 // NotificationDelegate:
52 std::string id() const override;
53 void ButtonClick(int button_index) override;
55 private:
56 ~DesktopNotificationDelegate() override;
58 const std::string id_;
59 const base::Closure button_click_callback_;
61 DISALLOW_COPY_AND_ASSIGN(DesktopNotificationDelegate);
64 DesktopNotificationDelegate::DesktopNotificationDelegate(
65 const std::string& id,
66 const base::Closure& button_click_callback)
67 : id_(id), button_click_callback_(button_click_callback) {
70 DesktopNotificationDelegate::~DesktopNotificationDelegate() {
73 std::string DesktopNotificationDelegate::id() const {
74 return id_;
77 void DesktopNotificationDelegate::ButtonClick(int button_index) {
78 button_click_callback_.Run();
81 } // namespace
83 namespace policy {
85 ConsumerManagementNotifier::ConsumerManagementNotifier(
86 Profile* profile,
87 ConsumerManagementService* consumer_management_service)
88 : profile_(profile),
89 consumer_management_service_(consumer_management_service),
90 weak_ptr_factory_(this) {
91 consumer_management_service_->AddObserver(this);
92 OnConsumerManagementStatusChanged();
95 ConsumerManagementNotifier::~ConsumerManagementNotifier() {
98 void ConsumerManagementNotifier::Shutdown() {
99 consumer_management_service_->RemoveObserver(this);
102 void ConsumerManagementNotifier::OnConsumerManagementStatusChanged() {
103 const ConsumerManagementStage stage =
104 consumer_management_service_->GetStage();
105 if (stage.HasPendingNotification()) {
106 // Reset the stage so that we won't show the same notification, again.
107 consumer_management_service_->SetStage(ConsumerManagementStage::None());
108 ShowNotification(stage);
112 void ConsumerManagementNotifier::ShowNotification(
113 const ConsumerManagementStage& stage) {
114 if (stage.HasEnrollmentSucceeded()) {
115 ShowDesktopNotification(
116 kEnrollmentNotificationId,
117 kEnrollmentNotificationUrl,
118 IDS_CONSUMER_MANAGEMENT_ENROLLMENT_NOTIFICATION_TITLE,
119 IDS_CONSUMER_MANAGEMENT_ENROLLMENT_NOTIFICATION_BODY,
120 IDS_CONSUMER_MANAGEMENT_NOTIFICATION_MODIFY_SETTINGS_BUTTON,
121 base::Bind(&ConsumerManagementNotifier::OpenSettingsPage,
122 weak_ptr_factory_.GetWeakPtr()));
123 } else if (stage.HasEnrollmentFailed()) {
124 ShowDesktopNotification(
125 kEnrollmentNotificationId,
126 kEnrollmentNotificationUrl,
127 IDS_CONSUMER_MANAGEMENT_ENROLLMENT_FAILURE_NOTIFICATION_TITLE,
128 IDS_CONSUMER_MANAGEMENT_ENROLLMENT_FAILURE_NOTIFICATION_BODY,
129 IDS_CONSUMER_MANAGEMENT_NOTIFICATION_TRY_AGAIN_BUTTON,
130 base::Bind(&ConsumerManagementNotifier::TryAgain,
131 weak_ptr_factory_.GetWeakPtr()));
132 } else if (stage.HasUnenrollmentSucceeded()) {
133 ShowDesktopNotification(
134 kUnenrollmentNotificationId,
135 kUnenrollmentNotificationUrl,
136 IDS_CONSUMER_MANAGEMENT_UNENROLLMENT_NOTIFICATION_TITLE,
137 IDS_CONSUMER_MANAGEMENT_UNENROLLMENT_NOTIFICATION_BODY,
138 IDS_CONSUMER_MANAGEMENT_NOTIFICATION_MODIFY_SETTINGS_BUTTON,
139 base::Bind(&ConsumerManagementNotifier::OpenSettingsPage,
140 weak_ptr_factory_.GetWeakPtr()));
141 } else if (stage.HasUnenrollmentFailed()) {
142 ShowDesktopNotification(
143 kUnenrollmentNotificationId,
144 kUnenrollmentNotificationUrl,
145 IDS_CONSUMER_MANAGEMENT_UNENROLLMENT_FAILURE_NOTIFICATION_TITLE,
146 IDS_CONSUMER_MANAGEMENT_UNENROLLMENT_FAILURE_NOTIFICATION_BODY,
147 IDS_CONSUMER_MANAGEMENT_NOTIFICATION_TRY_AGAIN_BUTTON,
148 base::Bind(&ConsumerManagementNotifier::TryAgain,
149 weak_ptr_factory_.GetWeakPtr()));
150 } else {
151 NOTREACHED();
155 void ConsumerManagementNotifier::ShowDesktopNotification(
156 const std::string& notification_id,
157 const std::string& notification_url,
158 int title_message_id,
159 int body_message_id,
160 int button_label_message_id,
161 const base::Closure& button_click_callback) {
162 message_center::RichNotificationData optional_field;
163 optional_field.buttons.push_back(message_center::ButtonInfo(
164 l10n_util::GetStringUTF16(button_label_message_id)));
166 Notification notification(
167 message_center::NOTIFICATION_TYPE_SIMPLE,
168 l10n_util::GetStringUTF16(title_message_id),
169 l10n_util::GetStringUTF16(body_message_id),
170 ui::ResourceBundle::GetSharedInstance().GetImageNamed(
171 IDR_CONSUMER_MANAGEMENT_NOTIFICATION_ICON),
172 message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT,
173 notification_id),
174 base::string16(), // display_source
175 GURL(notification_url), notification_id, optional_field,
176 new DesktopNotificationDelegate(notification_id, button_click_callback));
178 notification.SetSystemPriority();
179 g_browser_process->notification_ui_manager()->Add(notification, profile_);
182 void ConsumerManagementNotifier::OpenSettingsPage() const {
183 const GURL url(chrome::kChromeUISettingsURL);
184 chrome::NavigateParams params(profile_, url, ui::PAGE_TRANSITION_LINK);
185 params.disposition = NEW_FOREGROUND_TAB;
186 chrome::Navigate(&params);
189 void ConsumerManagementNotifier::TryAgain() const {
190 const GURL base_url(chrome::kChromeUISettingsURL);
191 const GURL url = base_url.Resolve(kConsumerManagementOverlay);
193 chrome::NavigateParams params(profile_, url, ui::PAGE_TRANSITION_LINK);
194 params.disposition = NEW_FOREGROUND_TAB;
195 chrome::Navigate(&params);
198 } // namespace policy