Commit 33286c9c authored by dewittj@chromium.org's avatar dewittj@chromium.org

Reland 201932: Add API function chrome.notifications.getAll

This function returns an object whose keys are the notification
IDs of all notifications created by that extension.

BUG=240924

TBR=miket@chromium.org

Review URL: https://chromiumcodereview.appspot.com/15715008

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@203001 0039d316-1c4b-4281-b951-d872f2087c98
parent 4fc00715
...@@ -29,6 +29,23 @@ namespace { ...@@ -29,6 +29,23 @@ namespace {
const char kResultKey[] = "result"; const char kResultKey[] = "result";
// Given an extension id and another id, returns an id that is unique
// relative to other extensions.
std::string CreateScopedIdentifier(const std::string& extension_id,
const std::string& id) {
return extension_id + "-" + id;
}
// Removes the unique internal identifier to send the ID as the
// extension expects it.
std::string StripScopeFromIdentifier(const std::string& extension_id,
const std::string& id) {
size_t index_of_separator = extension_id.length() + 1;
DCHECK_LT(index_of_separator, id.length());
return id.substr(index_of_separator);
}
class NotificationsApiDelegate : public NotificationDelegate { class NotificationsApiDelegate : public NotificationDelegate {
public: public:
NotificationsApiDelegate(ApiFunction* api_function, NotificationsApiDelegate(ApiFunction* api_function,
...@@ -46,13 +63,6 @@ class NotificationsApiDelegate : public NotificationDelegate { ...@@ -46,13 +63,6 @@ class NotificationsApiDelegate : public NotificationDelegate {
process_id_ = api_function->render_view_host()->GetProcess()->GetID(); process_id_ = api_function->render_view_host()->GetProcess()->GetID();
} }
// Given an extension id and another id, returns an id that is unique
// relative to other extensions.
static std::string CreateScopedIdentifier(const std::string& extension_id,
const std::string& id) {
return extension_id + "-" + id;
}
virtual void Display() OVERRIDE { } virtual void Display() OVERRIDE { }
virtual void Error() OVERRIDE { virtual void Error() OVERRIDE {
...@@ -135,10 +145,7 @@ bool NotificationsApiFunction::IsNotificationsApiAvailable() { ...@@ -135,10 +145,7 @@ bool NotificationsApiFunction::IsNotificationsApiAvailable() {
// We need to check this explicitly rather than letting // We need to check this explicitly rather than letting
// _permission_features.json enforce it, because we're sharing the // _permission_features.json enforce it, because we're sharing the
// chrome.notifications permissions namespace with WebKit notifications. // chrome.notifications permissions namespace with WebKit notifications.
if (!(GetExtension()->is_platform_app() || GetExtension()->is_extension())) return GetExtension()->is_platform_app() || GetExtension()->is_extension();
return false;
return true;
} }
NotificationsApiFunction::NotificationsApiFunction() { NotificationsApiFunction::NotificationsApiFunction() {
...@@ -333,9 +340,8 @@ bool NotificationsUpdateFunction::RunNotificationsApi() { ...@@ -333,9 +340,8 @@ bool NotificationsUpdateFunction::RunNotificationsApi() {
params_ = api::notifications::Update::Params::Create(*args_); params_ = api::notifications::Update::Params::Create(*args_);
EXTENSION_FUNCTION_VALIDATE(params_.get()); EXTENSION_FUNCTION_VALIDATE(params_.get());
if (g_browser_process->notification_ui_manager()-> if (g_browser_process->notification_ui_manager()->DoesIdExist(
DoesIdExist(NotificationsApiDelegate::CreateScopedIdentifier( CreateScopedIdentifier(extension_->id(), params_->notification_id))) {
extension_->id(), params_->notification_id))) {
CreateNotification(params_->notification_id, &params_->options); CreateNotification(params_->notification_id, &params_->options);
SetResult(Value::CreateBooleanValue(true)); SetResult(Value::CreateBooleanValue(true));
} else { } else {
...@@ -357,9 +363,8 @@ bool NotificationsClearFunction::RunNotificationsApi() { ...@@ -357,9 +363,8 @@ bool NotificationsClearFunction::RunNotificationsApi() {
params_ = api::notifications::Clear::Params::Create(*args_); params_ = api::notifications::Clear::Params::Create(*args_);
EXTENSION_FUNCTION_VALIDATE(params_.get()); EXTENSION_FUNCTION_VALIDATE(params_.get());
bool cancel_result = g_browser_process->notification_ui_manager()-> bool cancel_result = g_browser_process->notification_ui_manager()->CancelById(
CancelById(NotificationsApiDelegate::CreateScopedIdentifier( CreateScopedIdentifier(extension_->id(), params_->notification_id));
extension_->id(), params_->notification_id));
SetResult(Value::CreateBooleanValue(cancel_result)); SetResult(Value::CreateBooleanValue(cancel_result));
SendResponse(true); SendResponse(true);
...@@ -367,4 +372,29 @@ bool NotificationsClearFunction::RunNotificationsApi() { ...@@ -367,4 +372,29 @@ bool NotificationsClearFunction::RunNotificationsApi() {
return true; return true;
} }
NotificationsGetAllFunction::NotificationsGetAllFunction() {}
NotificationsGetAllFunction::~NotificationsGetAllFunction() {}
bool NotificationsGetAllFunction::RunNotificationsApi() {
NotificationUIManager* notification_ui_manager =
g_browser_process->notification_ui_manager();
std::set<std::string> notification_ids =
notification_ui_manager->GetAllIdsByProfileAndSourceOrigin(
profile_, extension_->url());
scoped_ptr<DictionaryValue> result(new DictionaryValue());
for (std::set<std::string>::iterator iter = notification_ids.begin();
iter != notification_ids.end(); iter++) {
result->SetBooleanWithoutPathExpansion(
StripScopeFromIdentifier(extension_->id(), *iter), true);
}
SetResult(result.release());
SendResponse(true);
return true;
}
} // namespace extensions } // namespace extensions
...@@ -89,6 +89,20 @@ class NotificationsClearFunction : public NotificationsApiFunction { ...@@ -89,6 +89,20 @@ class NotificationsClearFunction : public NotificationsApiFunction {
DECLARE_EXTENSION_FUNCTION("notifications.clear", NOTIFICATIONS_CLEAR) DECLARE_EXTENSION_FUNCTION("notifications.clear", NOTIFICATIONS_CLEAR)
}; };
class NotificationsGetAllFunction : public NotificationsApiFunction {
public:
NotificationsGetAllFunction();
// UIThreadExtensionFunction:
virtual bool RunNotificationsApi() OVERRIDE;
protected:
virtual ~NotificationsGetAllFunction();
private:
DECLARE_EXTENSION_FUNCTION("notifications.getAll", NOTIFICATIONS_GET_ALL)
};
} // namespace extensions } // namespace extensions
#endif // CHROME_BROWSER_EXTENSIONS_API_NOTIFICATIONS_NOTIFICATIONS_API_H_ #endif // CHROME_BROWSER_EXTENSIONS_API_NOTIFICATIONS_NOTIFICATIONS_API_H_
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "base/stringprintf.h"
#include "chrome/browser/browser_process.h" #include "chrome/browser/browser_process.h"
#include "chrome/browser/extensions/api/notifications/notifications_api.h" #include "chrome/browser/extensions/api/notifications/notifications_api.h"
#include "chrome/browser/extensions/extension_apitest.h" #include "chrome/browser/extensions/extension_apitest.h"
...@@ -12,6 +13,7 @@ ...@@ -12,6 +13,7 @@
#include "content/public/browser/notification_service.h" #include "content/public/browser/notification_service.h"
#include "content/public/test/test_utils.h" #include "content/public/test/test_utils.h"
#include "ui/message_center/message_center.h" #include "ui/message_center/message_center.h"
#include "ui/message_center/message_center_switches.h"
#include "ui/message_center/message_center_util.h" #include "ui/message_center/message_center_util.h"
// TODO(kbr): remove: http://crbug.com/222296 // TODO(kbr): remove: http://crbug.com/222296
...@@ -260,6 +262,78 @@ IN_PROC_BROWSER_TEST_F(NotificationsApiTest, TestMultipleItemNotification) { ...@@ -260,6 +262,78 @@ IN_PROC_BROWSER_TEST_F(NotificationsApiTest, TestMultipleItemNotification) {
ASSERT_TRUE(notification_id.length() > 0); ASSERT_TRUE(notification_id.length() > 0);
} }
#if defined(OS_LINUX)
#define MAYBE_TestGetAll DISABLED_TestGetAll
#else
#define MAYBE_TestGetAll TestGetAll
#endif
IN_PROC_BROWSER_TEST_F(NotificationsApiTest, MAYBE_TestGetAll) {
scoped_refptr<Extension> empty_extension(utils::CreateEmptyExtension());
{
scoped_refptr<extensions::NotificationsGetAllFunction>
notification_get_all_function(
new extensions::NotificationsGetAllFunction());
notification_get_all_function->set_extension(empty_extension.get());
notification_get_all_function->set_has_callback(true);
scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult(
notification_get_all_function, "[]", browser(), utils::NONE));
base::DictionaryValue* return_value;
ASSERT_EQ(base::Value::TYPE_DICTIONARY, result->GetType());
ASSERT_TRUE(result->GetAsDictionary(&return_value));
ASSERT_TRUE(return_value->size() == 0);
}
const unsigned int kNotificationsToCreate = 4;
for (unsigned int i = 0; i < kNotificationsToCreate; i++) {
scoped_refptr<extensions::NotificationsCreateFunction>
notification_create_function(
new extensions::NotificationsCreateFunction());
notification_create_function->set_extension(empty_extension.get());
notification_create_function->set_has_callback(true);
scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult(
notification_create_function,
base::StringPrintf("[\"identifier-%u\", "
"{"
"\"type\": \"basic\","
"\"iconUrl\": \"an/image/that/does/not/exist.png\","
"\"title\": \"Title\","
"\"message\": \"Message.\","
"\"priority\": 1,"
"\"eventTime\": 1361488019.9999999"
"}]",
i),
browser(),
utils::NONE));
}
{
scoped_refptr<extensions::NotificationsGetAllFunction>
notification_get_all_function(
new extensions::NotificationsGetAllFunction());
notification_get_all_function->set_extension(empty_extension.get());
notification_get_all_function->set_has_callback(true);
scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult(
notification_get_all_function, "[]", browser(), utils::NONE));
base::DictionaryValue* return_value;
ASSERT_EQ(base::Value::TYPE_DICTIONARY, result->GetType());
ASSERT_TRUE(result->GetAsDictionary(&return_value));
ASSERT_EQ(return_value->size(), kNotificationsToCreate);
bool dictionary_bool = false;
for (unsigned int i = 0; i < kNotificationsToCreate; i++) {
std::string id = base::StringPrintf("identifier-%u", i);
ASSERT_TRUE(return_value->GetBoolean(id, &dictionary_bool));
ASSERT_TRUE(dictionary_bool);
}
}
}
IN_PROC_BROWSER_TEST_F(NotificationsApiTest, TestEvents) { IN_PROC_BROWSER_TEST_F(NotificationsApiTest, TestEvents) {
#if defined(OS_MACOSX) #if defined(OS_MACOSX)
// TODO(kbr): re-enable: http://crbug.com/222296 // TODO(kbr): re-enable: http://crbug.com/222296
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include "chrome/browser/idle.h" #include "chrome/browser/idle.h"
#include "chrome/browser/notifications/balloon_collection.h" #include "chrome/browser/notifications/balloon_collection.h"
#include "chrome/browser/notifications/notification.h" #include "chrome/browser/notifications/notification.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_notification_types.h" #include "chrome/common/chrome_notification_types.h"
#include "chrome/common/pref_names.h" #include "chrome/common/pref_names.h"
#include "content/public/browser/notification_service.h" #include "content/public/browser/notification_service.h"
...@@ -59,6 +60,26 @@ bool BalloonNotificationUIManager::CancelById(const std::string& id) { ...@@ -59,6 +60,26 @@ bool BalloonNotificationUIManager::CancelById(const std::string& id) {
return balloon_collection_->RemoveById(id); return balloon_collection_->RemoveById(id);
} }
std::set<std::string>
BalloonNotificationUIManager::GetAllIdsByProfileAndSourceOrigin(
Profile* profile,
const GURL& source) {
std::set<std::string> notification_ids =
NotificationUIManagerImpl::GetAllIdsByProfileAndSourceOrigin(profile,
source);
const BalloonCollection::Balloons& balloons =
balloon_collection_->GetActiveBalloons();
for (BalloonCollection::Balloons::const_iterator iter = balloons.begin();
iter != balloons.end(); ++iter) {
if (profile->IsSameProfile((*iter)->profile()) &&
source == (*iter)->notification().origin_url()) {
notification_ids.insert((*iter)->notification().notification_id());
}
}
return notification_ids;
}
bool BalloonNotificationUIManager::CancelAllBySourceOrigin(const GURL& source) { bool BalloonNotificationUIManager::CancelAllBySourceOrigin(const GURL& source) {
// Same pattern as CancelById, but more complicated than the above // Same pattern as CancelById, but more complicated than the above
// because there may be multiple notifications from the same source. // because there may be multiple notifications from the same source.
......
...@@ -36,6 +36,9 @@ class BalloonNotificationUIManager ...@@ -36,6 +36,9 @@ class BalloonNotificationUIManager
// NotificationUIManager: // NotificationUIManager:
virtual bool DoesIdExist(const std::string& notification_id) OVERRIDE; virtual bool DoesIdExist(const std::string& notification_id) OVERRIDE;
virtual bool CancelById(const std::string& notification_id) OVERRIDE; virtual bool CancelById(const std::string& notification_id) OVERRIDE;
virtual std::set<std::string> GetAllIdsByProfileAndSourceOrigin(
Profile* profile,
const GURL& source) OVERRIDE;
virtual bool CancelAllBySourceOrigin(const GURL& source_origin) OVERRIDE; virtual bool CancelAllBySourceOrigin(const GURL& source_origin) OVERRIDE;
virtual bool CancelAllByProfile(Profile* profile) OVERRIDE; virtual bool CancelAllByProfile(Profile* profile) OVERRIDE;
virtual void CancelAll() OVERRIDE; virtual void CancelAll() OVERRIDE;
......
...@@ -70,6 +70,25 @@ bool MessageCenterNotificationManager::CancelById(const std::string& id) { ...@@ -70,6 +70,25 @@ bool MessageCenterNotificationManager::CancelById(const std::string& id) {
return true; return true;
} }
std::set<std::string>
MessageCenterNotificationManager::GetAllIdsByProfileAndSourceOrigin(
Profile* profile,
const GURL& source) {
std::set<std::string> notification_ids =
NotificationUIManagerImpl::GetAllIdsByProfileAndSourceOrigin(profile,
source);
for (NotificationMap::iterator iter = profile_notifications_.begin();
iter != profile_notifications_.end(); iter++) {
if ((*iter).second->notification().origin_url() == source &&
profile->IsSameProfile((*iter).second->profile())) {
notification_ids.insert(iter->first);
}
}
return notification_ids;
}
bool MessageCenterNotificationManager::CancelAllBySourceOrigin( bool MessageCenterNotificationManager::CancelAllBySourceOrigin(
const GURL& source) { const GURL& source) {
// Same pattern as CancelById, but more complicated than the above // Same pattern as CancelById, but more complicated than the above
...@@ -94,7 +113,7 @@ bool MessageCenterNotificationManager::CancelAllByProfile(Profile* profile) { ...@@ -94,7 +113,7 @@ bool MessageCenterNotificationManager::CancelAllByProfile(Profile* profile) {
for (NotificationMap::iterator loopiter = profile_notifications_.begin(); for (NotificationMap::iterator loopiter = profile_notifications_.begin();
loopiter != profile_notifications_.end(); ) { loopiter != profile_notifications_.end(); ) {
NotificationMap::iterator curiter = loopiter++; NotificationMap::iterator curiter = loopiter++;
if ((*curiter).second->profile() == profile) { if (profile->IsSameProfile((*curiter).second->profile())) {
message_center_->RemoveNotification(curiter->first, /* by_user */ false); message_center_->RemoveNotification(curiter->first, /* by_user */ false);
removed = true; removed = true;
} }
......
...@@ -34,6 +34,9 @@ class MessageCenterNotificationManager ...@@ -34,6 +34,9 @@ class MessageCenterNotificationManager
// NotificationUIManager // NotificationUIManager
virtual bool DoesIdExist(const std::string& notification_id) OVERRIDE; virtual bool DoesIdExist(const std::string& notification_id) OVERRIDE;
virtual bool CancelById(const std::string& notification_id) OVERRIDE; virtual bool CancelById(const std::string& notification_id) OVERRIDE;
virtual std::set<std::string> GetAllIdsByProfileAndSourceOrigin(
Profile* profile,
const GURL& source) OVERRIDE;
virtual bool CancelAllBySourceOrigin(const GURL& source_origin) OVERRIDE; virtual bool CancelAllBySourceOrigin(const GURL& source_origin) OVERRIDE;
virtual bool CancelAllByProfile(Profile* profile) OVERRIDE; virtual bool CancelAllByProfile(Profile* profile) OVERRIDE;
virtual void CancelAll() OVERRIDE; virtual void CancelAll() OVERRIDE;
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "chrome/browser/browser_process.h" #include "chrome/browser/browser_process.h"
#include "chrome/browser/notifications/balloon_notification_ui_manager.h" #include "chrome/browser/notifications/balloon_notification_ui_manager.h"
#include "chrome/browser/notifications/message_center_notification_manager.h" #include "chrome/browser/notifications/message_center_notification_manager.h"
#include "chrome/browser/profiles/profile.h"
#include "ui/message_center/message_center_util.h" #include "ui/message_center/message_center_util.h"
// static // static
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#ifndef CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_UI_MANAGER_H_ #ifndef CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_UI_MANAGER_H_
#define CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_UI_MANAGER_H_ #define CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_UI_MANAGER_H_
#include <set>
#include <string> #include <string>
#include <vector> #include <vector>
...@@ -36,6 +37,12 @@ class NotificationUIManager { ...@@ -36,6 +37,12 @@ class NotificationUIManager {
// displayed or in the queue. Returns true if anything was removed. // displayed or in the queue. Returns true if anything was removed.
virtual bool CancelById(const std::string& notification_id) = 0; virtual bool CancelById(const std::string& notification_id) = 0;
// Adds the notification_id for each outstanding notification to the set
// |notification_ids| (must not be NULL).
virtual std::set<std::string> GetAllIdsByProfileAndSourceOrigin(
Profile* profile,
const GURL& source) = 0;
// Removes notifications matching the |source_origin| (which could be an // Removes notifications matching the |source_origin| (which could be an
// extension ID). Returns true if anything was removed. // extension ID). Returns true if anything was removed.
virtual bool CancelAllBySourceOrigin(const GURL& source_origin) = 0; virtual bool CancelAllBySourceOrigin(const GURL& source_origin) = 0;
......
...@@ -95,6 +95,21 @@ bool NotificationUIManagerImpl::CancelById(const std::string& id) { ...@@ -95,6 +95,21 @@ bool NotificationUIManagerImpl::CancelById(const std::string& id) {
return false; return false;
} }
std::set<std::string>
NotificationUIManagerImpl::GetAllIdsByProfileAndSourceOrigin(
Profile* profile,
const GURL& source) {
std::set<std::string> notification_ids;
for (NotificationDeque::iterator iter = show_queue_.begin();
iter != show_queue_.end(); iter++) {
if ((*iter)->notification().origin_url() == source &&
profile->IsSameProfile((*iter)->profile())) {
notification_ids.insert((*iter)->notification().notification_id());
}
}
return notification_ids;
}
bool NotificationUIManagerImpl::CancelAllBySourceOrigin(const GURL& source) { bool NotificationUIManagerImpl::CancelAllBySourceOrigin(const GURL& source) {
// Same pattern as CancelById, but more complicated than the above // Same pattern as CancelById, but more complicated than the above
// because there may be multiple notifications from the same source. // because there may be multiple notifications from the same source.
......
...@@ -36,6 +36,9 @@ class NotificationUIManagerImpl ...@@ -36,6 +36,9 @@ class NotificationUIManagerImpl
Profile* profile) OVERRIDE; Profile* profile) OVERRIDE;
virtual bool DoesIdExist(const std::string& notification_id) OVERRIDE; virtual bool DoesIdExist(const std::string& notification_id) OVERRIDE;
virtual bool CancelById(const std::string& notification_id) OVERRIDE; virtual bool CancelById(const std::string& notification_id) OVERRIDE;
virtual std::set<std::string> GetAllIdsByProfileAndSourceOrigin(
Profile* profile,
const GURL& source) OVERRIDE;
virtual bool CancelAllBySourceOrigin(const GURL& source_origin) OVERRIDE; virtual bool CancelAllBySourceOrigin(const GURL& source_origin) OVERRIDE;
virtual bool CancelAllByProfile(Profile* profile) OVERRIDE; virtual bool CancelAllByProfile(Profile* profile) OVERRIDE;
virtual void CancelAll() OVERRIDE; virtual void CancelAll() OVERRIDE;
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#import <AppKit/AppKit.h> #import <AppKit/AppKit.h>
#include <map> #include <map>
#include <set>
#include "base/basictypes.h" #include "base/basictypes.h"
#include "base/memory/scoped_nsobject.h" #include "base/memory/scoped_nsobject.h"
...@@ -33,6 +34,8 @@ class NotificationUIManagerMac : public BalloonNotificationUIManager { ...@@ -33,6 +34,8 @@ class NotificationUIManagerMac : public BalloonNotificationUIManager {
// NotificationUIManager: // NotificationUIManager:
virtual void Add(const Notification& notification, virtual void Add(const Notification& notification,
Profile* profile) OVERRIDE; Profile* profile) OVERRIDE;
virtual std::set<std::string> GetAllIdsByProfileAndSourceOrigin(
Profile* profile, const GURL& source_origin) OVERRIDE;
virtual bool CancelById(const std::string& notification_id) OVERRIDE; virtual bool CancelById(const std::string& notification_id) OVERRIDE;
virtual bool CancelAllBySourceOrigin(const GURL& source_origin) OVERRIDE; virtual bool CancelAllBySourceOrigin(const GURL& source_origin) OVERRIDE;
virtual bool CancelAllByProfile(Profile* profile) OVERRIDE; virtual bool CancelAllByProfile(Profile* profile) OVERRIDE;
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include "chrome/browser/notifications/notification.h" #include "chrome/browser/notifications/notification.h"
#include "chrome/browser/notifications/balloon_notification_ui_manager.h" #include "chrome/browser/notifications/balloon_notification_ui_manager.h"
#include "chrome/browser/notifications/message_center_notification_manager.h" #include "chrome/browser/notifications/message_center_notification_manager.h"
#include "chrome/browser/profiles/profile.h"
#include "ui/message_center/message_center_util.h" #include "ui/message_center/message_center_util.h"
@class NSUserNotificationCenter; @class NSUserNotificationCenter;
...@@ -161,6 +162,25 @@ void NotificationUIManagerMac::Add(const Notification& notification, ...@@ -161,6 +162,25 @@ void NotificationUIManagerMac::Add(const Notification& notification,
} }
} }
std::set<std::string>
NotificationUIManagerMac::GetAllIdsByProfileAndSourceOrigin(
Profile* profile, const GURL& source_origin) {
std::set<std::string> notification_ids =
BalloonNotificationUIManager::GetAllIdsByProfileAndSourceOrigin(
profile, source_origin);
for (NotificationMap::iterator it = notification_map_.begin();
it != notification_map_.end(); ++it) {
ControllerNotification* controller_notification = it->second;
Notification* model = controller_notification->model;
if (model->origin_url() == source_origin &&
profile->IsSameProfile(controller_notification->profile)) {
notification_ids.insert(model->notification_id());
}
}
return notification_ids;
}
bool NotificationUIManagerMac::CancelById(const std::string& notification_id) { bool NotificationUIManagerMac::CancelById(const std::string& notification_id) {
NotificationMap::iterator it = notification_map_.find(notification_id); NotificationMap::iterator it = notification_map_.find(notification_id);
if (it == notification_map_.end()) if (it == notification_map_.end())
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include "chrome/browser/notifications/notification_ui_manager.h" #include "chrome/browser/notifications/notification_ui_manager.h"
#include "chrome/browser/notifications/sync_notifier/chrome_notifier_service.h" #include "chrome/browser/notifications/sync_notifier/chrome_notifier_service.h"
#include "chrome/browser/notifications/sync_notifier/synced_notification.h" #include "chrome/browser/notifications/sync_notifier/synced_notification.h"
#include "chrome/browser/profiles/profile.h"
#include "sync/api/sync_change.h" #include "sync/api/sync_change.h"
#include "sync/api/sync_change_processor.h" #include "sync/api/sync_change_processor.h"
#include "sync/api/sync_error_factory.h" #include "sync/api/sync_error_factory.h"
...@@ -122,6 +123,7 @@ class StubNotificationUIManager : public NotificationUIManager { ...@@ -122,6 +123,7 @@ class StubNotificationUIManager : public NotificationUIManager {
OVERRIDE { OVERRIDE {
// Make a deep copy of the notification that we can inspect. // Make a deep copy of the notification that we can inspect.
notification_ = notification; notification_ = notification;
profile_ = profile;
} }
// Returns true if any notifications match the supplied ID, either currently // Returns true if any notifications match the supplied ID, either currently
...@@ -136,6 +138,18 @@ class StubNotificationUIManager : public NotificationUIManager { ...@@ -136,6 +138,18 @@ class StubNotificationUIManager : public NotificationUIManager {
return false; return false;
} }
// Adds the notification_id for each outstanding notification to the set
// |notification_ids| (must not be NULL).
virtual std::set<std::string> GetAllIdsByProfileAndSourceOrigin(
Profile* profile,
const GURL& source) OVERRIDE {
std::set<std::string> notification_ids;
if (source == notification_.origin_url() &&
profile->IsSameProfile(profile_))
notification_ids.insert(notification_.notification_id());
return notification_ids;
}
// Removes notifications matching the |source_origin| (which could be an // Removes notifications matching the |source_origin| (which could be an
// extension ID). Returns true if anything was removed. // extension ID). Returns true if anything was removed.
virtual bool CancelAllBySourceOrigin(const GURL& source_origin) OVERRIDE { virtual bool CancelAllBySourceOrigin(const GURL& source_origin) OVERRIDE {
...@@ -157,6 +171,7 @@ class StubNotificationUIManager : public NotificationUIManager { ...@@ -157,6 +171,7 @@ class StubNotificationUIManager : public NotificationUIManager {
private: private:
DISALLOW_COPY_AND_ASSIGN(StubNotificationUIManager); DISALLOW_COPY_AND_ASSIGN(StubNotificationUIManager);
Notification notification_; Notification notification_;
Profile* profile_;
}; };
// Dummy SyncChangeProcessor used to help review what SyncChanges are pushed // Dummy SyncChangeProcessor used to help review what SyncChanges are pushed
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "chrome/browser/notifications/notification.h" #include "chrome/browser/notifications/notification.h"
#include "chrome/browser/notifications/notification_ui_manager.h" #include "chrome/browser/notifications/notification_ui_manager.h"
#include "chrome/browser/notifications/sync_notifier/synced_notification.h" #include "chrome/browser/notifications/sync_notifier/synced_notification.h"
#include "chrome/browser/profiles/profile.h"
#include "sync/api/sync_data.h" #include "sync/api/sync_data.h"
#include "sync/protocol/sync.pb.h" #include "sync/protocol/sync.pb.h"
#include "sync/protocol/synced_notification_specifics.pb.h" #include "sync/protocol/synced_notification_specifics.pb.h"
...@@ -87,6 +88,7 @@ class StubNotificationUIManager : public NotificationUIManager { ...@@ -87,6 +88,7 @@ class StubNotificationUIManager : public NotificationUIManager {
OVERRIDE { OVERRIDE {
// Make a deep copy of the notification that we can inspect. // Make a deep copy of the notification that we can inspect.
notification_ = notification; notification_ = notification;
profile_ = profile;
} }
// Returns true if any notifications match the supplied ID, either currently // Returns true if any notifications match the supplied ID, either currently
...@@ -101,6 +103,16 @@ class StubNotificationUIManager : public NotificationUIManager { ...@@ -101,6 +103,16 @@ class StubNotificationUIManager : public NotificationUIManager {
return false; return false;
} }
virtual std::set<std::string> GetAllIdsByProfileAndSourceOrigin(
Profile* profile,
const GURL& source) OVERRIDE {
std::set<std::string> notification_ids;
if (source == notification_.origin_url() &&
profile->IsSameProfile(profile_))
notification_ids.insert(notification_.notification_id());
return notification_ids;
}
// Removes notifications matching the |source_origin| (which could be an // Removes notifications matching the |source_origin| (which could be an
// extension ID). Returns true if anything was removed. // extension ID). Returns true if anything was removed.
virtual bool CancelAllBySourceOrigin(const GURL& source_origin) OVERRIDE { virtual bool CancelAllBySourceOrigin(const GURL& source_origin) OVERRIDE {
...@@ -122,6 +134,7 @@ class StubNotificationUIManager : public NotificationUIManager { ...@@ -122,6 +134,7 @@ class StubNotificationUIManager : public NotificationUIManager {
private: private:
DISALLOW_COPY_AND_ASSIGN(StubNotificationUIManager); DISALLOW_COPY_AND_ASSIGN(StubNotificationUIManager);
Notification notification_; Notification notification_;
Profile* profile_;
}; };
class SyncedNotificationTest : public testing::Test { class SyncedNotificationTest : public testing::Test {
......
...@@ -67,6 +67,8 @@ namespace notifications { ...@@ -67,6 +67,8 @@ namespace notifications {
callback ClearCallback = void (boolean wasCleared); callback ClearCallback = void (boolean wasCleared);
callback GetAllCallback = void (object notifications);
interface Functions { interface Functions {
// Creates and displays a notification having the contents in |options|, // Creates and displays a notification having the contents in |options|,
// identified by the id |notificationId|. If |notificationId| is empty, // identified by the id |notificationId|. If |notificationId| is empty,
...@@ -89,6 +91,10 @@ namespace notifications { ...@@ -89,6 +91,10 @@ namespace notifications {
// corresponding notification. |callback| indicates whether a matching // corresponding notification. |callback| indicates whether a matching
// notification existed. // notification existed.
static void clear(DOMString notificationId, ClearCallback callback); static void clear(DOMString notificationId, ClearCallback callback);
// |callback| is executed with the set of notification_ids currently in
// the system.
static void getAll(GetAllCallback callback);
}; };
interface Events { interface Events {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment