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