Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / chrome / browser / notifications / message_center_notification_manager.cc
blobb57ad7e6bcb19785f8c6b4e18bd3196e9b68b806
1 // Copyright (c) 2012 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/notifications/message_center_notification_manager.h"
7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/prefs/pref_registry_simple.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/stl_util.h"
12 #include "base/strings/stringprintf.h"
13 #include "chrome/browser/extensions/api/notification_provider/notification_provider_api.h"
14 #include "chrome/browser/notifications/extension_welcome_notification.h"
15 #include "chrome/browser/notifications/extension_welcome_notification_factory.h"
16 #include "chrome/browser/notifications/fullscreen_notification_blocker.h"
17 #include "chrome/browser/notifications/message_center_settings_controller.h"
18 #include "chrome/browser/notifications/notification.h"
19 #include "chrome/browser/notifications/notification_conversion_helper.h"
20 #include "chrome/browser/notifications/profile_notification.h"
21 #include "chrome/browser/notifications/screen_lock_notification_blocker.h"
22 #include "chrome/browser/profiles/profile.h"
23 #include "chrome/common/extensions/api/notification_provider.h"
24 #include "chrome/common/pref_names.h"
25 #include "content/public/browser/web_contents.h"
26 #include "content/public/common/url_constants.h"
27 #include "extensions/browser/extension_registry.h"
28 #include "extensions/common/extension_set.h"
29 #include "extensions/common/permissions/permissions_data.h"
30 #include "ui/gfx/image/image_skia.h"
31 #include "ui/message_center/message_center_style.h"
32 #include "ui/message_center/message_center_tray.h"
33 #include "ui/message_center/message_center_types.h"
34 #include "ui/message_center/notifier_settings.h"
36 #if defined(OS_CHROMEOS)
37 #include "chrome/browser/notifications/login_state_notification_blocker_chromeos.h"
38 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h"
39 #endif
41 #if defined(USE_ASH)
42 #include "ash/shell.h"
43 #include "ash/system/web_notification/web_notification_tray.h"
44 #endif
46 #if defined(OS_WIN)
47 // The first-run balloon will be shown |kFirstRunIdleDelaySeconds| after all
48 // popups go away and the user has notifications in the message center.
49 const int kFirstRunIdleDelaySeconds = 1;
50 #endif
52 MessageCenterNotificationManager::MessageCenterNotificationManager(
53 message_center::MessageCenter* message_center,
54 PrefService* local_state,
55 scoped_ptr<message_center::NotifierSettingsProvider> settings_provider)
56 : message_center_(message_center),
57 #if defined(OS_WIN)
58 first_run_idle_timeout_(
59 base::TimeDelta::FromSeconds(kFirstRunIdleDelaySeconds)),
60 #endif
61 settings_provider_(settings_provider.Pass()),
62 system_observer_(this),
63 stats_collector_(message_center),
64 google_now_stats_collector_(message_center)
65 #if defined(OS_WIN)
67 weak_factory_(this)
68 #endif
70 #if defined(OS_WIN)
71 first_run_pref_.Init(prefs::kMessageCenterShowedFirstRunBalloon, local_state);
72 #endif
74 message_center_->AddObserver(this);
75 message_center_->SetNotifierSettingsProvider(settings_provider_.get());
77 #if defined(OS_CHROMEOS)
78 blockers_.push_back(
79 new LoginStateNotificationBlockerChromeOS(message_center));
80 #else
81 blockers_.push_back(new ScreenLockNotificationBlocker(message_center));
82 #endif
83 blockers_.push_back(new FullscreenNotificationBlocker(message_center));
85 #if defined(OS_WIN) || defined(OS_MACOSX) \
86 || (defined(OS_LINUX) && !defined(OS_CHROMEOS))
87 // On Windows, Linux and Mac, the notification manager owns the tray icon and
88 // views.Other platforms have global ownership and Create will return NULL.
89 tray_.reset(message_center::CreateMessageCenterTray());
90 #endif
93 MessageCenterNotificationManager::~MessageCenterNotificationManager() {
94 message_center_->SetNotifierSettingsProvider(NULL);
95 message_center_->RemoveObserver(this);
97 STLDeleteContainerPairSecondPointers(profile_notifications_.begin(),
98 profile_notifications_.end());
99 profile_notifications_.clear();
102 void MessageCenterNotificationManager::RegisterPrefs(
103 PrefRegistrySimple* registry) {
104 registry->RegisterBooleanPref(prefs::kMessageCenterShowedFirstRunBalloon,
105 false);
106 registry->RegisterBooleanPref(prefs::kMessageCenterShowIcon, true);
107 registry->RegisterBooleanPref(prefs::kMessageCenterForcedOnTaskbar, false);
110 ////////////////////////////////////////////////////////////////////////////////
111 // NotificationUIManager
113 void MessageCenterNotificationManager::Add(const Notification& notification,
114 Profile* profile) {
115 if (Update(notification, profile))
116 return;
118 ProfileNotification* profile_notification =
119 new ProfileNotification(profile, notification);
121 ExtensionWelcomeNotificationFactory::GetForBrowserContext(profile)->
122 ShowWelcomeNotificationIfNecessary(profile_notification->notification());
124 // WARNING: You MUST use AddProfileNotification or update the message center
125 // via the notification within a ProfileNotification object or the profile ID
126 // will not be correctly set for ChromeOS.
127 // Takes ownership of profile_notification.
128 AddProfileNotification(profile_notification);
130 // TODO(liyanhou): Change the logic to only send notifications to one party.
131 // Currently, if there is an app with notificationProvider permission,
132 // notifications will go to both message center and the app.
133 // Change it to send notifications to message center only when the user chose
134 // default message center (extension_id.empty()).
136 // If there exist apps/extensions that have notificationProvider permission,
137 // route notifications to one of the apps/extensions.
138 std::string extension_id = GetExtensionTakingOverNotifications(profile);
139 if (!extension_id.empty())
140 AddNotificationToAlternateProvider(profile_notification->notification(),
141 profile, extension_id);
143 message_center_->AddNotification(make_scoped_ptr(
144 new message_center::Notification(profile_notification->notification())));
147 bool MessageCenterNotificationManager::Update(const Notification& notification,
148 Profile* profile) {
149 const std::string& tag = notification.tag();
150 if (tag.empty())
151 return false;
153 const GURL origin_url = notification.origin_url();
154 DCHECK(origin_url.is_valid());
156 // Since tag is provided by arbitrary JS, we need to use origin_url
157 // (which is an app url in case of app/extension) to scope the tags
158 // in the given profile.
159 for (NotificationMap::iterator iter = profile_notifications_.begin();
160 iter != profile_notifications_.end(); ++iter) {
161 ProfileNotification* old_notification = (*iter).second;
162 if (old_notification->notification().tag() == tag &&
163 old_notification->notification().origin_url() == origin_url &&
164 old_notification->profile_id() ==
165 NotificationUIManager::GetProfileID(profile)) {
166 // Changing the type from non-progress to progress does not count towards
167 // the immediate update allowed in the message center.
168 std::string old_id = old_notification->notification().id();
170 // Add/remove notification in the local list but just update the same
171 // one in MessageCenter.
172 delete old_notification;
173 profile_notifications_.erase(old_id);
174 ProfileNotification* new_notification =
175 new ProfileNotification(profile, notification);
176 profile_notifications_[new_notification->notification().id()] =
177 new_notification;
179 // TODO(liyanhou): Add routing updated notifications to alternative
180 // providers.
182 // WARNING: You MUST use AddProfileNotification or update the message
183 // center via the notification within a ProfileNotification object or the
184 // profile ID will not be correctly set for ChromeOS.
185 message_center_->UpdateNotification(
186 old_id,
187 make_scoped_ptr(new message_center::Notification(
188 new_notification->notification())));
190 return true;
193 return false;
196 const Notification* MessageCenterNotificationManager::FindById(
197 const std::string& delegate_id,
198 ProfileID profile_id) const {
199 // The profile pointer can be weak, the instance may have been destroyed, so
200 // no profile method should be called inside this function.
202 std::string profile_notification_id =
203 ProfileNotification::GetProfileNotificationId(delegate_id, profile_id);
204 NotificationMap::const_iterator iter =
205 profile_notifications_.find(profile_notification_id);
206 if (iter == profile_notifications_.end())
207 return NULL;
208 return &(iter->second->notification());
211 bool MessageCenterNotificationManager::CancelById(
212 const std::string& delegate_id,
213 ProfileID profile_id) {
214 // The profile pointer can be weak, the instance may have been destroyed, so
215 // no profile method should be called inside this function.
217 std::string profile_notification_id =
218 ProfileNotification::GetProfileNotificationId(delegate_id, profile_id);
219 // See if this ID hasn't been shown yet.
220 // If it has been shown, remove it.
221 NotificationMap::iterator iter =
222 profile_notifications_.find(profile_notification_id);
223 if (iter == profile_notifications_.end())
224 return false;
226 RemoveProfileNotification(iter->second);
227 message_center_->RemoveNotification(profile_notification_id,
228 /* by_user */ false);
229 return true;
232 std::set<std::string>
233 MessageCenterNotificationManager::GetAllIdsByProfileAndSourceOrigin(
234 ProfileID profile_id,
235 const GURL& source) {
236 std::set<std::string> delegate_ids;
237 for (const auto& pair : profile_notifications_) {
238 const Notification& notification = pair.second->notification();
239 if (pair.second->profile_id() == profile_id &&
240 notification.origin_url() == source) {
241 delegate_ids.insert(notification.delegate_id());
245 return delegate_ids;
248 std::set<std::string> MessageCenterNotificationManager::GetAllIdsByProfile(
249 ProfileID profile_id) {
250 std::set<std::string> delegate_ids;
251 for (const auto& pair : profile_notifications_) {
252 if (pair.second->profile_id() == profile_id)
253 delegate_ids.insert(pair.second->notification().delegate_id());
256 return delegate_ids;
259 bool MessageCenterNotificationManager::CancelAllBySourceOrigin(
260 const GURL& source) {
261 // Same pattern as CancelById, but more complicated than the above
262 // because there may be multiple notifications from the same source.
263 bool removed = false;
265 for (NotificationMap::iterator loopiter = profile_notifications_.begin();
266 loopiter != profile_notifications_.end(); ) {
267 NotificationMap::iterator curiter = loopiter++;
268 if ((*curiter).second->notification().origin_url() == source) {
269 const std::string id = curiter->first;
270 RemoveProfileNotification(curiter->second);
271 message_center_->RemoveNotification(id, /* by_user */ false);
272 removed = true;
275 return removed;
278 bool MessageCenterNotificationManager::CancelAllByProfile(
279 ProfileID profile_id) {
280 // Same pattern as CancelAllBySourceOrigin.
281 bool removed = false;
283 for (NotificationMap::iterator loopiter = profile_notifications_.begin();
284 loopiter != profile_notifications_.end(); ) {
285 NotificationMap::iterator curiter = loopiter++;
286 if (profile_id == (*curiter).second->profile_id()) {
287 const std::string id = curiter->first;
288 RemoveProfileNotification(curiter->second);
289 message_center_->RemoveNotification(id, /* by_user */ false);
290 removed = true;
293 return removed;
296 void MessageCenterNotificationManager::CancelAll() {
297 message_center_->RemoveAllNotifications(/* by_user */ false);
300 ////////////////////////////////////////////////////////////////////////////////
301 // MessageCenter::Observer
302 void MessageCenterNotificationManager::OnNotificationRemoved(
303 const std::string& id,
304 bool by_user) {
305 NotificationMap::const_iterator iter = profile_notifications_.find(id);
306 if (iter != profile_notifications_.end())
307 RemoveProfileNotification(iter->second);
309 #if defined(OS_WIN)
310 CheckFirstRunTimer();
311 #endif
314 void MessageCenterNotificationManager::OnCenterVisibilityChanged(
315 message_center::Visibility visibility) {
316 #if defined(OS_WIN)
317 if (visibility == message_center::VISIBILITY_TRANSIENT)
318 CheckFirstRunTimer();
319 #endif
322 void MessageCenterNotificationManager::OnNotificationUpdated(
323 const std::string& id) {
324 #if defined(OS_WIN)
325 CheckFirstRunTimer();
326 #endif
329 void MessageCenterNotificationManager::EnsureMessageCenterClosed() {
330 if (tray_.get())
331 tray_->GetMessageCenterTray()->HideMessageCenterBubble();
333 #if defined(USE_ASH)
334 if (ash::Shell::HasInstance()) {
335 ash::WebNotificationTray* tray =
336 ash::Shell::GetInstance()->GetWebNotificationTray();
337 if (tray)
338 tray->GetMessageCenterTray()->HideMessageCenterBubble();
340 #endif
343 void MessageCenterNotificationManager::SetMessageCenterTrayDelegateForTest(
344 message_center::MessageCenterTrayDelegate* delegate) {
345 tray_.reset(delegate);
348 std::string
349 MessageCenterNotificationManager::GetMessageCenterNotificationIdForTest(
350 const std::string& delegate_id,
351 Profile* profile) {
352 return ProfileNotification::GetProfileNotificationId(delegate_id,
353 GetProfileID(profile));
356 ////////////////////////////////////////////////////////////////////////////////
357 // private
359 void MessageCenterNotificationManager::AddNotificationToAlternateProvider(
360 const Notification& notification,
361 Profile* profile,
362 const std::string& extension_id) const {
363 // Convert data from Notification type to NotificationOptions type.
364 extensions::api::notifications::NotificationOptions options;
365 NotificationConversionHelper::NotificationToNotificationOptions(notification,
366 &options);
368 // Send the notification to the alternate provider extension/app.
369 extensions::NotificationProviderEventRouter event_router(profile);
370 event_router.CreateNotification(extension_id,
371 notification.notifier_id().id,
372 notification.delegate_id(),
373 options);
376 void MessageCenterNotificationManager::AddProfileNotification(
377 ProfileNotification* profile_notification) {
378 const Notification& notification = profile_notification->notification();
379 std::string id = notification.id();
380 // Notification ids should be unique.
381 DCHECK(profile_notifications_.find(id) == profile_notifications_.end());
382 profile_notifications_[id] = profile_notification;
385 void MessageCenterNotificationManager::RemoveProfileNotification(
386 ProfileNotification* profile_notification) {
387 std::string id = profile_notification->notification().id();
388 profile_notifications_.erase(id);
389 delete profile_notification;
392 ProfileNotification* MessageCenterNotificationManager::FindProfileNotification(
393 const std::string& id) const {
394 NotificationMap::const_iterator iter = profile_notifications_.find(id);
395 if (iter == profile_notifications_.end())
396 return NULL;
398 return (*iter).second;
401 std::string
402 MessageCenterNotificationManager::GetExtensionTakingOverNotifications(
403 Profile* profile) {
404 // TODO(liyanhou): When additional settings in Chrome Settings is implemented,
405 // change choosing the last app with permission to a user selected app.
406 extensions::ExtensionRegistry* registry =
407 extensions::ExtensionRegistry::Get(profile);
408 DCHECK(registry);
409 std::string extension_id;
410 for (extensions::ExtensionSet::const_iterator it =
411 registry->enabled_extensions().begin();
412 it != registry->enabled_extensions().end();
413 ++it) {
414 if ((*it->get()).permissions_data()->HasAPIPermission(
415 extensions::APIPermission::ID::kNotificationProvider)) {
416 extension_id = (*it->get()).id();
417 return extension_id;
420 return extension_id;