diff options
author | dewittj@chromium.org <dewittj@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-05-24 00:33:55 +0000 |
---|---|---|
committer | dewittj@chromium.org <dewittj@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-05-24 00:33:55 +0000 |
commit | e8341814fe1c35cdc8466e9f8e6dcf27b60890b0 (patch) | |
tree | bd414ddf543d6d9201abc36215d290b2571b9c88 /chrome/browser/extensions/api/notifications | |
parent | ad3203b607db49cc87f0d7a4d63417adc45dd756 (diff) | |
download | chromium_src-e8341814fe1c35cdc8466e9f8e6dcf27b60890b0.zip chromium_src-e8341814fe1c35cdc8466e9f8e6dcf27b60890b0.tar.gz chromium_src-e8341814fe1c35cdc8466e9f8e6dcf27b60890b0.tar.bz2 |
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
Review URL: https://chromiumcodereview.appspot.com/14767029
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@201932 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/extensions/api/notifications')
3 files changed, 139 insertions, 17 deletions
diff --git a/chrome/browser/extensions/api/notifications/notifications_api.cc b/chrome/browser/extensions/api/notifications/notifications_api.cc index 9865aff..597cc8a 100644 --- a/chrome/browser/extensions/api/notifications/notifications_api.cc +++ b/chrome/browser/extensions/api/notifications/notifications_api.cc @@ -29,6 +29,23 @@ namespace { 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 { public: NotificationsApiDelegate(ApiFunction* api_function, @@ -46,13 +63,6 @@ class NotificationsApiDelegate : public NotificationDelegate { 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 Error() OVERRIDE { @@ -135,10 +145,7 @@ bool NotificationsApiFunction::IsNotificationsApiAvailable() { // We need to check this explicitly rather than letting // _permission_features.json enforce it, because we're sharing the // chrome.notifications permissions namespace with WebKit notifications. - if (!(GetExtension()->is_platform_app() || GetExtension()->is_extension())) - return false; - - return true; + return GetExtension()->is_platform_app() || GetExtension()->is_extension(); } NotificationsApiFunction::NotificationsApiFunction() { @@ -333,9 +340,8 @@ bool NotificationsUpdateFunction::RunNotificationsApi() { params_ = api::notifications::Update::Params::Create(*args_); EXTENSION_FUNCTION_VALIDATE(params_.get()); - if (g_browser_process->notification_ui_manager()-> - DoesIdExist(NotificationsApiDelegate::CreateScopedIdentifier( - extension_->id(), params_->notification_id))) { + if (g_browser_process->notification_ui_manager()->DoesIdExist( + CreateScopedIdentifier(extension_->id(), params_->notification_id))) { CreateNotification(params_->notification_id, ¶ms_->options); SetResult(Value::CreateBooleanValue(true)); } else { @@ -357,9 +363,8 @@ bool NotificationsClearFunction::RunNotificationsApi() { params_ = api::notifications::Clear::Params::Create(*args_); EXTENSION_FUNCTION_VALIDATE(params_.get()); - bool cancel_result = g_browser_process->notification_ui_manager()-> - CancelById(NotificationsApiDelegate::CreateScopedIdentifier( - extension_->id(), params_->notification_id)); + bool cancel_result = g_browser_process->notification_ui_manager()->CancelById( + CreateScopedIdentifier(extension_->id(), params_->notification_id)); SetResult(Value::CreateBooleanValue(cancel_result)); SendResponse(true); @@ -367,4 +372,29 @@ bool NotificationsClearFunction::RunNotificationsApi() { 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 diff --git a/chrome/browser/extensions/api/notifications/notifications_api.h b/chrome/browser/extensions/api/notifications/notifications_api.h index 8635a39..447d0bf 100644 --- a/chrome/browser/extensions/api/notifications/notifications_api.h +++ b/chrome/browser/extensions/api/notifications/notifications_api.h @@ -89,6 +89,20 @@ class NotificationsClearFunction : public NotificationsApiFunction { 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 #endif // CHROME_BROWSER_EXTENSIONS_API_NOTIFICATIONS_NOTIFICATIONS_API_H_ diff --git a/chrome/browser/extensions/api/notifications/notifications_apitest.cc b/chrome/browser/extensions/api/notifications/notifications_apitest.cc index 59a48ac..08aa1bd 100644 --- a/chrome/browser/extensions/api/notifications/notifications_apitest.cc +++ b/chrome/browser/extensions/api/notifications/notifications_apitest.cc @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include "base/stringprintf.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/extensions/api/notifications/notifications_api.h" #include "chrome/browser/extensions/extension_apitest.h" @@ -12,6 +13,7 @@ #include "content/public/browser/notification_service.h" #include "content/public/test/test_utils.h" #include "ui/message_center/message_center.h" +#include "ui/message_center/message_center_switches.h" #include "ui/message_center/message_center_util.h" // TODO(kbr): remove: http://crbug.com/222296 @@ -260,6 +262,82 @@ IN_PROC_BROWSER_TEST_F(NotificationsApiTest, TestMultipleItemNotification) { ASSERT_TRUE(notification_id.length() > 0); } +#if defined(OS_LINUX) && defined(USE_AURA) +#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\": \"list\"," + "\"iconUrl\": \"an/image/that/does/not/exist.png\"," + "\"title\": \"Title\"," + "\"message\": \"Message.\"," + "\"items\": [" + " {\"title\": \"Grace Goe\"," + " \"message\": \"I saw Frank steal a sandwich :-)\"}" + "]," + "\"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) { #if defined(OS_MACOSX) // TODO(kbr): re-enable: http://crbug.com/222296 |