diff options
author | miket@chromium.org <miket@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-10-15 23:16:32 +0000 |
---|---|---|
committer | miket@chromium.org <miket@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-10-15 23:16:32 +0000 |
commit | d28a1a25b7dc2a7c0708005ce272900d50e85667 (patch) | |
tree | f2044b4b483523d5ef399794a3b58d55026ec520 /chrome/browser/extensions | |
parent | 3bf0ef4bf197bc05a8a509c37292c60e5a860b63 (diff) | |
download | chromium_src-d28a1a25b7dc2a7c0708005ce272900d50e85667.zip chromium_src-d28a1a25b7dc2a7c0708005ce272900d50e85667.tar.gz chromium_src-d28a1a25b7dc2a7c0708005ce272900d50e85667.tar.bz2 |
Another interim checkin. This CL completes the plumbing between our nascent API and the existing desktop notification UI.
Next step will be to create a new peer class to the existing desktop notification class, then create a new IPC message (or alter the existing one) to invoke it. Then we will have a subsystem that's entirely our own to start UI iteration.
Review URL: https://chromiumcodereview.appspot.com/11116016
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@161993 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/extensions')
5 files changed, 134 insertions, 44 deletions
diff --git a/chrome/browser/extensions/api/api_function.cc b/chrome/browser/extensions/api/api_function.cc index 7bcbdc7..398b139 100644 --- a/chrome/browser/extensions/api/api_function.cc +++ b/chrome/browser/extensions/api/api_function.cc @@ -12,6 +12,29 @@ using content::BrowserThread; namespace extensions { +ApiFunction::ApiFunction() { +} + +ApiFunction::~ApiFunction() { +} + +int ApiFunction::ExtractSrcId(const DictionaryValue* options) { + int src_id = -1; + if (options) { + if (options->HasKey(kSrcIdKey)) + EXTENSION_FUNCTION_VALIDATE(options->GetInteger(kSrcIdKey, &src_id)); + } + return src_id; +} + +ApiResourceEventNotifier* ApiFunction::CreateEventNotifier(int src_id) { + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); + return new ApiResourceEventNotifier( + profile()->GetExtensionEventRouter(), profile(), extension_id(), + src_id, source_url()); +} + +// AsyncApiFunction AsyncApiFunction::AsyncApiFunction() : work_thread_id_(BrowserThread::IO) { } @@ -69,20 +92,4 @@ void AsyncApiFunction::RespondOnUIThread() { SendResponse(Respond()); } -int AsyncApiFunction::ExtractSrcId(const DictionaryValue* options) { - int src_id = -1; - if (options) { - if (options->HasKey(kSrcIdKey)) - EXTENSION_FUNCTION_VALIDATE(options->GetInteger(kSrcIdKey, &src_id)); - } - return src_id; -} - -ApiResourceEventNotifier* AsyncApiFunction::CreateEventNotifier(int src_id) { - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); - return new ApiResourceEventNotifier( - profile()->GetExtensionEventRouter(), profile(), extension_id(), - src_id, source_url()); -} - } // namespace extensions diff --git a/chrome/browser/extensions/api/api_function.h b/chrome/browser/extensions/api/api_function.h index 270e085..2199253 100644 --- a/chrome/browser/extensions/api/api_function.h +++ b/chrome/browser/extensions/api/api_function.h @@ -12,9 +12,22 @@ namespace extensions { class ApiResourceEventNotifier; +class ApiFunction : public UIThreadExtensionFunction { + protected: + ApiFunction(); + virtual ~ApiFunction(); + + // Looks for a kSrcId key that might have been added to a create method's + // options object. + int ExtractSrcId(const DictionaryValue* options); + + // Utility. + ApiResourceEventNotifier* CreateEventNotifier(int src_id); +}; + // AsyncApiFunction provides convenient thread management for APIs that need to // do essentially all their work on a thread other than the UI thread. -class AsyncApiFunction : public AsyncExtensionFunction { +class AsyncApiFunction : public ApiFunction { protected: AsyncApiFunction(); virtual ~AsyncApiFunction(); @@ -40,13 +53,6 @@ class AsyncApiFunction : public AsyncExtensionFunction { // Respond. Guaranteed to happen on UI thread. virtual bool Respond() = 0; - // Looks for a kSrcId key that might have been added to a create method's - // options object. - int ExtractSrcId(const DictionaryValue* options); - - // Utility. - ApiResourceEventNotifier* CreateEventNotifier(int src_id); - // ExtensionFunction::RunImpl() virtual bool RunImpl() OVERRIDE; diff --git a/chrome/browser/extensions/api/notification/notification_api.cc b/chrome/browser/extensions/api/notification/notification_api.cc index 6c48ce1..b7eee80 100644 --- a/chrome/browser/extensions/api/notification/notification_api.cc +++ b/chrome/browser/extensions/api/notification/notification_api.cc @@ -4,9 +4,62 @@ #include "chrome/browser/extensions/api/notification/notification_api.h" -#include "base/bind.h" -#include "chrome/common/extensions/extension.h" +#include "base/callback.h" +#include "base/utf_string_conversions.h" +#include "chrome/browser/browser_process.h" +#include "chrome/browser/extensions/api/api_resource_event_notifier.h" #include "chrome/browser/extensions/extension_system.h" +#include "chrome/browser/notifications/notification.h" +#include "chrome/browser/notifications/notification_ui_manager.h" +#include "chrome/common/extensions/extension.h" +#include "googleurl/src/gurl.h" + +const char kResultKey[] = "result"; + +namespace { + +class NotificationApiDelegate : public NotificationDelegate { + public: + explicit NotificationApiDelegate( + extensions::ApiResourceEventNotifier* event_notifier) + : event_notifier_(event_notifier) { + } + + virtual void Display() OVERRIDE { + // TODO(miket): propagate to JS + } + + virtual void Error() OVERRIDE { + // TODO(miket): propagate to JS + } + + virtual void Close(bool by_user) OVERRIDE { + // TODO(miket): propagate to JS + } + + virtual void Click() OVERRIDE { + // TODO(miket): propagate to JS + } + + virtual std::string id() const OVERRIDE { + // TODO(miket): implement + return std::string(); + } + + virtual content::RenderViewHost* GetRenderViewHost() const OVERRIDE { + // TODO(miket): required to handle icon + return NULL; + } + + private: + virtual ~NotificationApiDelegate() {} + + extensions::ApiResourceEventNotifier* event_notifier_; + + DISALLOW_COPY_AND_ASSIGN(NotificationApiDelegate); +}; + +} // namespace namespace extensions { @@ -16,18 +69,33 @@ NotificationShowFunction::NotificationShowFunction() { NotificationShowFunction::~NotificationShowFunction() { } -bool NotificationShowFunction::Prepare() { +bool NotificationShowFunction::RunImpl() { params_ = api::experimental_notification::Show::Params::Create(*args_); EXTENSION_FUNCTION_VALIDATE(params_.get()); - return true; -} -void NotificationShowFunction::Work() { - SetResult(Value::CreateBooleanValue(true)); -} + api::experimental_notification::ShowOptions* options = ¶ms_->options; + scoped_ptr<DictionaryValue> options_dict(options->ToValue()); + src_id_ = ExtractSrcId(options_dict.get()); + event_notifier_ = CreateEventNotifier(src_id_); + + GURL icon_url(UTF8ToUTF16(options->icon_url)); + string16 title(UTF8ToUTF16(options->title)); + string16 message(UTF8ToUTF16(options->message)); + string16 replace_id(UTF8ToUTF16(options->replace_id)); + + Notification notification( + GURL(), icon_url, title, message, WebKit::WebTextDirectionDefault, + string16(), replace_id, + new NotificationApiDelegate(event_notifier_)); + g_browser_process->notification_ui_manager()->Add(notification, profile()); -bool NotificationShowFunction::Respond() { - return error_.empty(); + // TODO(miket): why return a result if it's always true? + DictionaryValue* result = new DictionaryValue(); + result->SetBoolean(kResultKey, true); + SetResult(result); + SendResponse(true); + + return true; } } // namespace extensions diff --git a/chrome/browser/extensions/api/notification/notification_api.h b/chrome/browser/extensions/api/notification/notification_api.h index 520eb0e..9f94a3c 100644 --- a/chrome/browser/extensions/api/notification/notification_api.h +++ b/chrome/browser/extensions/api/notification/notification_api.h @@ -15,22 +15,24 @@ namespace extensions { -class NotificationShowFunction : public AsyncApiFunction { - public: - DECLARE_EXTENSION_FUNCTION_NAME("notification.show") +class ApiResourceEventNotifier; +class NotificationShowFunction : public ApiFunction { + public: NotificationShowFunction(); - // AsyncApiFunction: - virtual bool Prepare() OVERRIDE; - virtual void Work() OVERRIDE; - virtual bool Respond() OVERRIDE; + // UIThreadExtensionFunction: + virtual bool RunImpl() OVERRIDE; protected: virtual ~NotificationShowFunction(); private: scoped_ptr<api::experimental_notification::Show::Params> params_; + int src_id_; + ApiResourceEventNotifier* event_notifier_; + + DECLARE_EXTENSION_FUNCTION_NAME("experimental.notification.show") }; } // namespace extensions diff --git a/chrome/browser/extensions/api/notification/notification_apitest.cc b/chrome/browser/extensions/api/notification/notification_apitest.cc index 5956e0f..f5b4c05 100644 --- a/chrome/browser/extensions/api/notification/notification_apitest.cc +++ b/chrome/browser/extensions/api/notification/notification_apitest.cc @@ -17,7 +17,7 @@ class NotificationApiTest : public PlatformAppApiTest { } // namespace -IN_PROC_BROWSER_TEST_F(NotificationApiTest, TestNothing) { +IN_PROC_BROWSER_TEST_F(NotificationApiTest, TestNormalNotification) { scoped_refptr<extensions::NotificationShowFunction> notification_show_function(new extensions::NotificationShowFunction()); scoped_refptr<Extension> empty_extension(utils::CreateEmptyExtension()); @@ -27,7 +27,14 @@ IN_PROC_BROWSER_TEST_F(NotificationApiTest, TestNothing) { scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult( notification_show_function, - "[{\"text\": \"Check out Cirque du Soleil\"}]", + "[{" + "\"iconUrl\": \"http://www.google.com/intl/en/chrome/assets/" + "common/images/chrome_logo_2x.png\"," + "\"title\": \"Attention!\"," + "\"message\": \"Check out Cirque du Soleil\"," + "\"replaceId\": \"12345678\"" + "}]", browser(), utils::NONE)); - ASSERT_EQ(base::Value::TYPE_BOOLEAN, result->GetType()); + + ASSERT_EQ(base::Value::TYPE_DICTIONARY, result->GetType()); } |