| 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/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 10 #include "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 #include "content/public/browser/render_view_host.h" | 22 #include "content/public/browser/render_view_host.h" |
| 23 #include "googleurl/src/gurl.h" | 23 #include "googleurl/src/gurl.h" |
| 24 #include "ui/message_center/message_center_util.h" | 24 #include "ui/message_center/message_center_util.h" |
| 25 | 25 |
| 26 namespace extensions { | 26 namespace extensions { |
| 27 | 27 |
| 28 namespace { | 28 namespace { |
| 29 | 29 |
| 30 const char kResultKey[] = "result"; | 30 const char kResultKey[] = "result"; |
| 31 | 31 |
| 32 // Given an extension id and another id, returns an id that is unique | |
| 33 // relative to other extensions. | |
| 34 std::string CreateScopedIdentifier(const std::string& extension_id, | |
| 35 const std::string& id) { | |
| 36 return extension_id + "-" + id; | |
| 37 } | |
| 38 | |
| 39 // Removes the unique internal identifier to send the ID as the | |
| 40 // extension expects it. | |
| 41 std::string StripScopeFromIdentifier(const std::string& extension_id, | |
| 42 const std::string& id) { | |
| 43 size_t index_of_separator = extension_id.length() + 1; | |
| 44 DCHECK_LT(index_of_separator, id.length()); | |
| 45 | |
| 46 return id.substr(index_of_separator); | |
| 47 } | |
| 48 | |
| 49 class NotificationsApiDelegate : public NotificationDelegate { | 32 class NotificationsApiDelegate : public NotificationDelegate { |
| 50 public: | 33 public: |
| 51 NotificationsApiDelegate(ApiFunction* api_function, | 34 NotificationsApiDelegate(ApiFunction* api_function, |
| 52 Profile* profile, | 35 Profile* profile, |
| 53 const std::string& extension_id, | 36 const std::string& extension_id, |
| 54 const std::string& id) | 37 const std::string& id) |
| 55 : api_function_(api_function), | 38 : api_function_(api_function), |
| 56 profile_(profile), | 39 profile_(profile), |
| 57 extension_id_(extension_id), | 40 extension_id_(extension_id), |
| 58 id_(id), | 41 id_(id), |
| 59 scoped_id_(CreateScopedIdentifier(extension_id, id)), | 42 scoped_id_(CreateScopedIdentifier(extension_id, id)), |
| 60 process_id_(-1) { | 43 process_id_(-1) { |
| 61 DCHECK(api_function_); | 44 DCHECK(api_function_); |
| 62 if (api_function_->render_view_host()) | 45 if (api_function_->render_view_host()) |
| 63 process_id_ = api_function->render_view_host()->GetProcess()->GetID(); | 46 process_id_ = api_function->render_view_host()->GetProcess()->GetID(); |
| 64 } | 47 } |
| 65 | 48 |
| 49 // Given an extension id and another id, returns an id that is unique |
| 50 // relative to other extensions. |
| 51 static std::string CreateScopedIdentifier(const std::string& extension_id, |
| 52 const std::string& id) { |
| 53 return extension_id + "-" + id; |
| 54 } |
| 55 |
| 66 virtual void Display() OVERRIDE { } | 56 virtual void Display() OVERRIDE { } |
| 67 | 57 |
| 68 virtual void Error() OVERRIDE { | 58 virtual void Error() OVERRIDE { |
| 69 scoped_ptr<ListValue> args(CreateBaseEventArgs()); | 59 scoped_ptr<ListValue> args(CreateBaseEventArgs()); |
| 70 SendEvent(event_names::kOnNotificationError, args.Pass()); | 60 SendEvent(event_names::kOnNotificationError, args.Pass()); |
| 71 } | 61 } |
| 72 | 62 |
| 73 virtual void Close(bool by_user) OVERRIDE { | 63 virtual void Close(bool by_user) OVERRIDE { |
| 74 scoped_ptr<ListValue> args(CreateBaseEventArgs()); | 64 scoped_ptr<ListValue> args(CreateBaseEventArgs()); |
| 75 args->Append(Value::CreateBooleanValue(by_user)); | 65 args->Append(Value::CreateBooleanValue(by_user)); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 | 128 |
| 139 DISALLOW_COPY_AND_ASSIGN(NotificationsApiDelegate); | 129 DISALLOW_COPY_AND_ASSIGN(NotificationsApiDelegate); |
| 140 }; | 130 }; |
| 141 | 131 |
| 142 } // namespace | 132 } // namespace |
| 143 | 133 |
| 144 bool NotificationsApiFunction::IsNotificationsApiAvailable() { | 134 bool NotificationsApiFunction::IsNotificationsApiAvailable() { |
| 145 // We need to check this explicitly rather than letting | 135 // We need to check this explicitly rather than letting |
| 146 // _permission_features.json enforce it, because we're sharing the | 136 // _permission_features.json enforce it, because we're sharing the |
| 147 // chrome.notifications permissions namespace with WebKit notifications. | 137 // chrome.notifications permissions namespace with WebKit notifications. |
| 148 return GetExtension()->is_platform_app() || GetExtension()->is_extension(); | 138 if (!(GetExtension()->is_platform_app() || GetExtension()->is_extension())) |
| 139 return false; |
| 140 |
| 141 return true; |
| 149 } | 142 } |
| 150 | 143 |
| 151 NotificationsApiFunction::NotificationsApiFunction() { | 144 NotificationsApiFunction::NotificationsApiFunction() { |
| 152 } | 145 } |
| 153 | 146 |
| 154 NotificationsApiFunction::~NotificationsApiFunction() { | 147 NotificationsApiFunction::~NotificationsApiFunction() { |
| 155 } | 148 } |
| 156 | 149 |
| 157 void NotificationsApiFunction::CreateNotification( | 150 void NotificationsApiFunction::CreateNotification( |
| 158 const std::string& id, | 151 const std::string& id, |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 333 NotificationsUpdateFunction::NotificationsUpdateFunction() { | 326 NotificationsUpdateFunction::NotificationsUpdateFunction() { |
| 334 } | 327 } |
| 335 | 328 |
| 336 NotificationsUpdateFunction::~NotificationsUpdateFunction() { | 329 NotificationsUpdateFunction::~NotificationsUpdateFunction() { |
| 337 } | 330 } |
| 338 | 331 |
| 339 bool NotificationsUpdateFunction::RunNotificationsApi() { | 332 bool NotificationsUpdateFunction::RunNotificationsApi() { |
| 340 params_ = api::notifications::Update::Params::Create(*args_); | 333 params_ = api::notifications::Update::Params::Create(*args_); |
| 341 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 334 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 342 | 335 |
| 343 if (g_browser_process->notification_ui_manager()->DoesIdExist( | 336 if (g_browser_process->notification_ui_manager()-> |
| 344 CreateScopedIdentifier(extension_->id(), params_->notification_id))) { | 337 DoesIdExist(NotificationsApiDelegate::CreateScopedIdentifier( |
| 338 extension_->id(), params_->notification_id))) { |
| 345 CreateNotification(params_->notification_id, ¶ms_->options); | 339 CreateNotification(params_->notification_id, ¶ms_->options); |
| 346 SetResult(Value::CreateBooleanValue(true)); | 340 SetResult(Value::CreateBooleanValue(true)); |
| 347 } else { | 341 } else { |
| 348 SetResult(Value::CreateBooleanValue(false)); | 342 SetResult(Value::CreateBooleanValue(false)); |
| 349 } | 343 } |
| 350 | 344 |
| 351 SendResponse(true); | 345 SendResponse(true); |
| 352 | 346 |
| 353 return true; | 347 return true; |
| 354 } | 348 } |
| 355 | 349 |
| 356 NotificationsClearFunction::NotificationsClearFunction() { | 350 NotificationsClearFunction::NotificationsClearFunction() { |
| 357 } | 351 } |
| 358 | 352 |
| 359 NotificationsClearFunction::~NotificationsClearFunction() { | 353 NotificationsClearFunction::~NotificationsClearFunction() { |
| 360 } | 354 } |
| 361 | 355 |
| 362 bool NotificationsClearFunction::RunNotificationsApi() { | 356 bool NotificationsClearFunction::RunNotificationsApi() { |
| 363 params_ = api::notifications::Clear::Params::Create(*args_); | 357 params_ = api::notifications::Clear::Params::Create(*args_); |
| 364 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 358 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 365 | 359 |
| 366 bool cancel_result = g_browser_process->notification_ui_manager()->CancelById( | 360 bool cancel_result = g_browser_process->notification_ui_manager()-> |
| 367 CreateScopedIdentifier(extension_->id(), params_->notification_id)); | 361 CancelById(NotificationsApiDelegate::CreateScopedIdentifier( |
| 362 extension_->id(), params_->notification_id)); |
| 368 | 363 |
| 369 SetResult(Value::CreateBooleanValue(cancel_result)); | 364 SetResult(Value::CreateBooleanValue(cancel_result)); |
| 370 SendResponse(true); | 365 SendResponse(true); |
| 371 | 366 |
| 372 return true; | 367 return true; |
| 373 } | 368 } |
| 374 | 369 |
| 375 NotificationsGetAllFunction::NotificationsGetAllFunction() {} | |
| 376 | |
| 377 NotificationsGetAllFunction::~NotificationsGetAllFunction() {} | |
| 378 | |
| 379 bool NotificationsGetAllFunction::RunNotificationsApi() { | |
| 380 NotificationUIManager* notification_ui_manager = | |
| 381 g_browser_process->notification_ui_manager(); | |
| 382 std::set<std::string> notification_ids = | |
| 383 notification_ui_manager->GetAllIdsByProfileAndSourceOrigin( | |
| 384 profile_, extension_->url()); | |
| 385 | |
| 386 scoped_ptr<DictionaryValue> result(new DictionaryValue()); | |
| 387 | |
| 388 for (std::set<std::string>::iterator iter = notification_ids.begin(); | |
| 389 iter != notification_ids.end(); iter++) { | |
| 390 result->SetBooleanWithoutPathExpansion( | |
| 391 StripScopeFromIdentifier(extension_->id(), *iter), true); | |
| 392 } | |
| 393 | |
| 394 SetResult(result.release()); | |
| 395 SendResponse(true); | |
| 396 | |
| 397 return true; | |
| 398 } | |
| 399 | |
| 400 } // namespace extensions | 370 } // namespace extensions |
| OLD | NEW |