| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/api/notifications/notifications_api.h" | 5 #include "chrome/browser/extensions/api/notifications/notifications_api.h" |
| 6 | 6 |
| 7 #include "base/callback.h" | 7 #include "base/callback.h" |
| 8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 | 142 |
| 143 virtual void Display() OVERRIDE { } | 143 virtual void Display() OVERRIDE { } |
| 144 | 144 |
| 145 virtual void Error() OVERRIDE { | 145 virtual void Error() OVERRIDE { |
| 146 scoped_ptr<base::ListValue> args(CreateBaseEventArgs()); | 146 scoped_ptr<base::ListValue> args(CreateBaseEventArgs()); |
| 147 SendEvent(event_names::kOnNotificationError, args.Pass()); | 147 SendEvent(event_names::kOnNotificationError, args.Pass()); |
| 148 } | 148 } |
| 149 | 149 |
| 150 virtual void Close(bool by_user) OVERRIDE { | 150 virtual void Close(bool by_user) OVERRIDE { |
| 151 scoped_ptr<base::ListValue> args(CreateBaseEventArgs()); | 151 scoped_ptr<base::ListValue> args(CreateBaseEventArgs()); |
| 152 args->Append(Value::CreateBooleanValue(by_user)); | 152 args->Append(new base::FundamentalValue(by_user)); |
| 153 SendEvent(event_names::kOnNotificationClosed, args.Pass()); | 153 SendEvent(event_names::kOnNotificationClosed, args.Pass()); |
| 154 } | 154 } |
| 155 | 155 |
| 156 virtual void Click() OVERRIDE { | 156 virtual void Click() OVERRIDE { |
| 157 scoped_ptr<base::ListValue> args(CreateBaseEventArgs()); | 157 scoped_ptr<base::ListValue> args(CreateBaseEventArgs()); |
| 158 SendEvent(event_names::kOnNotificationClicked, args.Pass()); | 158 SendEvent(event_names::kOnNotificationClicked, args.Pass()); |
| 159 } | 159 } |
| 160 | 160 |
| 161 virtual bool HasClickedListener() OVERRIDE { | 161 virtual bool HasClickedListener() OVERRIDE { |
| 162 return ExtensionSystem::Get(profile_)->event_router()->HasEventListener( | 162 return ExtensionSystem::Get(profile_)->event_router()->HasEventListener( |
| 163 event_names::kOnNotificationClicked); | 163 event_names::kOnNotificationClicked); |
| 164 } | 164 } |
| 165 | 165 |
| 166 virtual void ButtonClick(int index) OVERRIDE { | 166 virtual void ButtonClick(int index) OVERRIDE { |
| 167 scoped_ptr<base::ListValue> args(CreateBaseEventArgs()); | 167 scoped_ptr<base::ListValue> args(CreateBaseEventArgs()); |
| 168 args->Append(Value::CreateIntegerValue(index)); | 168 args->Append(new base::FundamentalValue(index)); |
| 169 SendEvent(event_names::kOnNotificationButtonClicked, args.Pass()); | 169 SendEvent(event_names::kOnNotificationButtonClicked, args.Pass()); |
| 170 } | 170 } |
| 171 | 171 |
| 172 virtual std::string id() const OVERRIDE { | 172 virtual std::string id() const OVERRIDE { |
| 173 return scoped_id_; | 173 return scoped_id_; |
| 174 } | 174 } |
| 175 | 175 |
| 176 virtual int process_id() const OVERRIDE { | 176 virtual int process_id() const OVERRIDE { |
| 177 return process_id_; | 177 return process_id_; |
| 178 } | 178 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 195 virtual ~NotificationsApiDelegate() {} | 195 virtual ~NotificationsApiDelegate() {} |
| 196 | 196 |
| 197 void SendEvent(const std::string& name, scoped_ptr<base::ListValue> args) { | 197 void SendEvent(const std::string& name, scoped_ptr<base::ListValue> args) { |
| 198 scoped_ptr<Event> event(new Event(name, args.Pass())); | 198 scoped_ptr<Event> event(new Event(name, args.Pass())); |
| 199 ExtensionSystem::Get(profile_)->event_router()->DispatchEventToExtension( | 199 ExtensionSystem::Get(profile_)->event_router()->DispatchEventToExtension( |
| 200 extension_id_, event.Pass()); | 200 extension_id_, event.Pass()); |
| 201 } | 201 } |
| 202 | 202 |
| 203 scoped_ptr<base::ListValue> CreateBaseEventArgs() { | 203 scoped_ptr<base::ListValue> CreateBaseEventArgs() { |
| 204 scoped_ptr<base::ListValue> args(new base::ListValue()); | 204 scoped_ptr<base::ListValue> args(new base::ListValue()); |
| 205 args->Append(Value::CreateStringValue(id_)); | 205 args->Append(new base::StringValue(id_)); |
| 206 return args.Pass(); | 206 return args.Pass(); |
| 207 } | 207 } |
| 208 | 208 |
| 209 scoped_refptr<ApiFunction> api_function_; | 209 scoped_refptr<ApiFunction> api_function_; |
| 210 Profile* profile_; | 210 Profile* profile_; |
| 211 const std::string extension_id_; | 211 const std::string extension_id_; |
| 212 const std::string id_; | 212 const std::string id_; |
| 213 const std::string scoped_id_; | 213 const std::string scoped_id_; |
| 214 int process_id_; | 214 int process_id_; |
| 215 | 215 |
| (...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 484 // arbitrary "extension.api.999" notificationIds that will collide with | 484 // arbitrary "extension.api.999" notificationIds that will collide with |
| 485 // future generated IDs. It doesn't seem necessary to try to prevent this; if | 485 // future generated IDs. It doesn't seem necessary to try to prevent this; if |
| 486 // developers want to hurt themselves, we'll let them. | 486 // developers want to hurt themselves, we'll let them. |
| 487 const std::string extension_id(extension_->id()); | 487 const std::string extension_id(extension_->id()); |
| 488 std::string notification_id; | 488 std::string notification_id; |
| 489 if (!params_->notification_id.empty()) | 489 if (!params_->notification_id.empty()) |
| 490 notification_id = params_->notification_id; | 490 notification_id = params_->notification_id; |
| 491 else | 491 else |
| 492 notification_id = kNotificationPrefix + base::Uint64ToString(next_id_++); | 492 notification_id = kNotificationPrefix + base::Uint64ToString(next_id_++); |
| 493 | 493 |
| 494 SetResult(Value::CreateStringValue(notification_id)); | 494 SetResult(new base::StringValue(notification_id)); |
| 495 | 495 |
| 496 // TODO(dewittj): Add more human-readable error strings if this fails. | 496 // TODO(dewittj): Add more human-readable error strings if this fails. |
| 497 if (!CreateNotification(notification_id, ¶ms_->options)) | 497 if (!CreateNotification(notification_id, ¶ms_->options)) |
| 498 return false; | 498 return false; |
| 499 | 499 |
| 500 SendResponse(true); | 500 SendResponse(true); |
| 501 | 501 |
| 502 return true; | 502 return true; |
| 503 } | 503 } |
| 504 | 504 |
| 505 NotificationsUpdateFunction::NotificationsUpdateFunction() { | 505 NotificationsUpdateFunction::NotificationsUpdateFunction() { |
| 506 } | 506 } |
| 507 | 507 |
| 508 NotificationsUpdateFunction::~NotificationsUpdateFunction() { | 508 NotificationsUpdateFunction::~NotificationsUpdateFunction() { |
| 509 } | 509 } |
| 510 | 510 |
| 511 bool NotificationsUpdateFunction::RunNotificationsApi() { | 511 bool NotificationsUpdateFunction::RunNotificationsApi() { |
| 512 params_ = api::notifications::Update::Params::Create(*args_); | 512 params_ = api::notifications::Update::Params::Create(*args_); |
| 513 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 513 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 514 | 514 |
| 515 // We are in update. If the ID doesn't exist, succeed but call the callback | 515 // We are in update. If the ID doesn't exist, succeed but call the callback |
| 516 // with "false". | 516 // with "false". |
| 517 const Notification* matched_notification = | 517 const Notification* matched_notification = |
| 518 g_browser_process->notification_ui_manager()->FindById( | 518 g_browser_process->notification_ui_manager()->FindById( |
| 519 CreateScopedIdentifier(extension_->id(), params_->notification_id)); | 519 CreateScopedIdentifier(extension_->id(), params_->notification_id)); |
| 520 if (!matched_notification) { | 520 if (!matched_notification) { |
| 521 SetResult(Value::CreateBooleanValue(false)); | 521 SetResult(new base::FundamentalValue(false)); |
| 522 SendResponse(true); | 522 SendResponse(true); |
| 523 return true; | 523 return true; |
| 524 } | 524 } |
| 525 | 525 |
| 526 // If we have trouble updating the notification (could be improper use of API | 526 // If we have trouble updating the notification (could be improper use of API |
| 527 // or some other reason), mark the function as failed, calling the callback | 527 // or some other reason), mark the function as failed, calling the callback |
| 528 // with false. | 528 // with false. |
| 529 // TODO(dewittj): Add more human-readable error strings if this fails. | 529 // TODO(dewittj): Add more human-readable error strings if this fails. |
| 530 Notification notification = *matched_notification; | 530 Notification notification = *matched_notification; |
| 531 bool could_update_notification = UpdateNotification( | 531 bool could_update_notification = UpdateNotification( |
| 532 params_->notification_id, ¶ms_->options, ¬ification); | 532 params_->notification_id, ¶ms_->options, ¬ification); |
| 533 SetResult(Value::CreateBooleanValue(could_update_notification)); | 533 SetResult(new base::FundamentalValue(could_update_notification)); |
| 534 if (!could_update_notification) | 534 if (!could_update_notification) |
| 535 return false; | 535 return false; |
| 536 | 536 |
| 537 // No trouble, created the notification, send true to the callback and | 537 // No trouble, created the notification, send true to the callback and |
| 538 // succeed. | 538 // succeed. |
| 539 SendResponse(true); | 539 SendResponse(true); |
| 540 return true; | 540 return true; |
| 541 } | 541 } |
| 542 | 542 |
| 543 NotificationsClearFunction::NotificationsClearFunction() { | 543 NotificationsClearFunction::NotificationsClearFunction() { |
| 544 } | 544 } |
| 545 | 545 |
| 546 NotificationsClearFunction::~NotificationsClearFunction() { | 546 NotificationsClearFunction::~NotificationsClearFunction() { |
| 547 } | 547 } |
| 548 | 548 |
| 549 bool NotificationsClearFunction::RunNotificationsApi() { | 549 bool NotificationsClearFunction::RunNotificationsApi() { |
| 550 params_ = api::notifications::Clear::Params::Create(*args_); | 550 params_ = api::notifications::Clear::Params::Create(*args_); |
| 551 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 551 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 552 | 552 |
| 553 bool cancel_result = g_browser_process->notification_ui_manager()->CancelById( | 553 bool cancel_result = g_browser_process->notification_ui_manager()->CancelById( |
| 554 CreateScopedIdentifier(extension_->id(), params_->notification_id)); | 554 CreateScopedIdentifier(extension_->id(), params_->notification_id)); |
| 555 | 555 |
| 556 SetResult(Value::CreateBooleanValue(cancel_result)); | 556 SetResult(new base::FundamentalValue(cancel_result)); |
| 557 SendResponse(true); | 557 SendResponse(true); |
| 558 | 558 |
| 559 return true; | 559 return true; |
| 560 } | 560 } |
| 561 | 561 |
| 562 NotificationsGetAllFunction::NotificationsGetAllFunction() {} | 562 NotificationsGetAllFunction::NotificationsGetAllFunction() {} |
| 563 | 563 |
| 564 NotificationsGetAllFunction::~NotificationsGetAllFunction() {} | 564 NotificationsGetAllFunction::~NotificationsGetAllFunction() {} |
| 565 | 565 |
| 566 bool NotificationsGetAllFunction::RunNotificationsApi() { | 566 bool NotificationsGetAllFunction::RunNotificationsApi() { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 578 StripScopeFromIdentifier(extension_->id(), *iter), true); | 578 StripScopeFromIdentifier(extension_->id(), *iter), true); |
| 579 } | 579 } |
| 580 | 580 |
| 581 SetResult(result.release()); | 581 SetResult(result.release()); |
| 582 SendResponse(true); | 582 SendResponse(true); |
| 583 | 583 |
| 584 return true; | 584 return true; |
| 585 } | 585 } |
| 586 | 586 |
| 587 } // namespace extensions | 587 } // namespace extensions |
| OLD | NEW |