Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / chrome / browser / sync / sync_error_notifier_ash.cc
blob03a67133ccfee959e34825c7459f8c3565dfa249
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/sync/sync_error_notifier_ash.h"
7 #include "ash/shell.h"
8 #include "ash/shell_delegate.h"
9 #include "ash/system/system_notifier.h"
10 #include "base/strings/string16.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/notifications/notification.h"
14 #include "chrome/browser/notifications/notification_ui_manager.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h"
17 #include "chrome/browser/ui/chrome_pages.h"
18 #include "chrome/browser/ui/webui/signin/login_ui_service.h"
19 #include "chrome/browser/ui/webui/signin/login_ui_service_factory.h"
20 #include "chrome/common/url_constants.h"
21 #include "chrome/grit/chromium_strings.h"
22 #include "chrome/grit/generated_resources.h"
23 #include "grit/theme_resources.h"
24 #include "third_party/WebKit/public/web/WebTextDirection.h"
25 #include "ui/base/l10n/l10n_util.h"
26 #include "ui/base/resource/resource_bundle.h"
27 #include "ui/message_center/notification.h"
28 #include "ui/message_center/notification_delegate.h"
30 #if defined(OS_CHROMEOS)
31 #include "chrome/browser/chromeos/login/user_flow.h"
32 #include "chrome/browser/chromeos/login/users/chrome_user_manager.h"
33 #include "components/user_manager/user_manager.h"
34 #endif
37 namespace {
39 const char kProfileSyncNotificationId[] = "chrome://settings/sync/";
41 // A simple notification delegate for the sync setup button.
42 class SyncNotificationDelegate : public NotificationDelegate {
43 public:
44 SyncNotificationDelegate(const std::string& id,
45 Profile* profile);
47 // NotificationDelegate:
48 void Click() override;
49 void ButtonClick(int button_index) override;
50 std::string id() const override;
52 protected:
53 ~SyncNotificationDelegate() override;
55 private:
56 void ShowSyncSetup();
58 // Unique id of the notification.
59 const std::string id_;
61 Profile* profile_;
63 DISALLOW_COPY_AND_ASSIGN(SyncNotificationDelegate);
66 SyncNotificationDelegate::SyncNotificationDelegate(
67 const std::string& id,
68 Profile* profile)
69 : id_(id),
70 profile_(profile) {
73 SyncNotificationDelegate::~SyncNotificationDelegate() {
75 void SyncNotificationDelegate::Click() {
76 ShowSyncSetup();
79 void SyncNotificationDelegate::ButtonClick(int button_index) {
80 ShowSyncSetup();
83 std::string SyncNotificationDelegate::id() const {
84 return id_;
87 void SyncNotificationDelegate::ShowSyncSetup() {
88 LoginUIService* login_ui = LoginUIServiceFactory::GetForProfile(profile_);
89 if (login_ui->current_login_ui()) {
90 // TODO(michaelpg): The LoginUI might be on an inactive desktop.
91 // See crbug.com/354280.
92 login_ui->current_login_ui()->FocusUI();
93 return;
96 chrome::ShowSettingsSubPageForProfile(profile_, chrome::kSyncSetupSubPage);
99 } // namespace
101 SyncErrorNotifier::SyncErrorNotifier(SyncErrorController* controller,
102 Profile* profile)
103 : error_controller_(controller),
104 profile_(profile) {
105 // Create a unique notification ID for this profile.
106 notification_id_ =
107 kProfileSyncNotificationId + profile_->GetProfileUserName();
109 error_controller_->AddObserver(this);
110 OnErrorChanged();
113 SyncErrorNotifier::~SyncErrorNotifier() {
114 DCHECK(!error_controller_)
115 << "SyncErrorNotifier::Shutdown() was not called";
118 void SyncErrorNotifier::Shutdown() {
119 error_controller_->RemoveObserver(this);
120 error_controller_ = NULL;
123 void SyncErrorNotifier::OnErrorChanged() {
124 NotificationUIManager* notification_ui_manager =
125 g_browser_process->notification_ui_manager();
127 // notification_ui_manager() may return NULL when shutting down.
128 if (!notification_ui_manager)
129 return;
131 if (!error_controller_->HasError()) {
132 g_browser_process->notification_ui_manager()->CancelById(
133 notification_id_, NotificationUIManager::GetProfileID(profile_));
134 return;
137 #if defined(OS_CHROMEOS)
138 if (user_manager::UserManager::IsInitialized()) {
139 chromeos::UserFlow* user_flow =
140 chromeos::ChromeUserManager::Get()->GetCurrentUserFlow();
142 // Check whether Chrome OS user flow allows launching browser.
143 // Example: Supervised user creation flow which handles token invalidation
144 // itself and notifications should be suppressed. http://crbug.com/359045
145 if (!user_flow->ShouldLaunchBrowser())
146 return;
148 #endif
150 // Keep the existing notification if there is one.
151 if (notification_ui_manager->FindById(
152 notification_id_, NotificationUIManager::GetProfileID(profile_)))
153 return;
155 // Add an accept button to launch the sync setup settings subpage.
156 message_center::RichNotificationData data;
157 data.buttons.push_back(message_center::ButtonInfo(
158 l10n_util::GetStringUTF16(IDS_SYNC_NOTIFICATION_ACCEPT)));
160 // Set the delegate for the notification's sync setup button.
161 SyncNotificationDelegate* delegate =
162 new SyncNotificationDelegate(notification_id_, profile_);
164 message_center::NotifierId notifier_id(
165 message_center::NotifierId::SYSTEM_COMPONENT,
166 kProfileSyncNotificationId);
168 // Set |profile_id| for multi-user notification blocker.
169 notifier_id.profile_id = multi_user_util::GetUserIDFromProfile(profile_);
171 // Add a new notification.
172 Notification notification(
173 message_center::NOTIFICATION_TYPE_SIMPLE,
174 l10n_util::GetStringUTF16(IDS_SYNC_ERROR_BUBBLE_VIEW_TITLE),
175 l10n_util::GetStringUTF16(IDS_SYNC_PASSPHRASE_ERROR_BUBBLE_VIEW_MESSAGE),
176 ui::ResourceBundle::GetSharedInstance().GetImageNamed(
177 IDR_NOTIFICATION_ALERT),
178 notifier_id,
179 base::string16(), // display_source
180 GURL(notification_id_), notification_id_, data, delegate);
181 notification_ui_manager->Add(notification, profile_);