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 |
32 class NotificationsApiDelegate : public NotificationDelegate { | 49 class NotificationsApiDelegate : public NotificationDelegate { |
33 public: | 50 public: |
34 NotificationsApiDelegate(ApiFunction* api_function, | 51 NotificationsApiDelegate(ApiFunction* api_function, |
35 Profile* profile, | 52 Profile* profile, |
36 const std::string& extension_id, | 53 const std::string& extension_id, |
37 const std::string& id) | 54 const std::string& id) |
38 : api_function_(api_function), | 55 : api_function_(api_function), |
39 profile_(profile), | 56 profile_(profile), |
40 extension_id_(extension_id), | 57 extension_id_(extension_id), |
41 id_(id), | 58 id_(id), |
42 scoped_id_(CreateScopedIdentifier(extension_id, id)), | 59 scoped_id_(CreateScopedIdentifier(extension_id, id)), |
43 process_id_(-1) { | 60 process_id_(-1) { |
44 DCHECK(api_function_); | 61 DCHECK(api_function_); |
45 if (api_function_->render_view_host()) | 62 if (api_function_->render_view_host()) |
46 process_id_ = api_function->render_view_host()->GetProcess()->GetID(); | 63 process_id_ = api_function->render_view_host()->GetProcess()->GetID(); |
47 } | 64 } |
48 | 65 |
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 | |
56 virtual void Display() OVERRIDE { } | 66 virtual void Display() OVERRIDE { } |
57 | 67 |
58 virtual void Error() OVERRIDE { | 68 virtual void Error() OVERRIDE { |
59 scoped_ptr<ListValue> args(CreateBaseEventArgs()); | 69 scoped_ptr<ListValue> args(CreateBaseEventArgs()); |
60 SendEvent(event_names::kOnNotificationError, args.Pass()); | 70 SendEvent(event_names::kOnNotificationError, args.Pass()); |
61 } | 71 } |
62 | 72 |
63 virtual void Close(bool by_user) OVERRIDE { | 73 virtual void Close(bool by_user) OVERRIDE { |
64 scoped_ptr<ListValue> args(CreateBaseEventArgs()); | 74 scoped_ptr<ListValue> args(CreateBaseEventArgs()); |
65 args->Append(Value::CreateBooleanValue(by_user)); | 75 args->Append(Value::CreateBooleanValue(by_user)); |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 | 138 |
129 DISALLOW_COPY_AND_ASSIGN(NotificationsApiDelegate); | 139 DISALLOW_COPY_AND_ASSIGN(NotificationsApiDelegate); |
130 }; | 140 }; |
131 | 141 |
132 } // namespace | 142 } // namespace |
133 | 143 |
134 bool NotificationsApiFunction::IsNotificationsApiAvailable() { | 144 bool NotificationsApiFunction::IsNotificationsApiAvailable() { |
135 // We need to check this explicitly rather than letting | 145 // We need to check this explicitly rather than letting |
136 // _permission_features.json enforce it, because we're sharing the | 146 // _permission_features.json enforce it, because we're sharing the |
137 // chrome.notifications permissions namespace with WebKit notifications. | 147 // chrome.notifications permissions namespace with WebKit notifications. |
138 if (!(GetExtension()->is_platform_app() || GetExtension()->is_extension())) | 148 return GetExtension()->is_platform_app() || GetExtension()->is_extension(); |
139 return false; | |
140 | |
141 return true; | |
142 } | 149 } |
143 | 150 |
144 NotificationsApiFunction::NotificationsApiFunction() { | 151 NotificationsApiFunction::NotificationsApiFunction() { |
145 } | 152 } |
146 | 153 |
147 NotificationsApiFunction::~NotificationsApiFunction() { | 154 NotificationsApiFunction::~NotificationsApiFunction() { |
148 } | 155 } |
149 | 156 |
150 void NotificationsApiFunction::CreateNotification( | 157 void NotificationsApiFunction::CreateNotification( |
151 const std::string& id, | 158 const std::string& id, |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
326 NotificationsUpdateFunction::NotificationsUpdateFunction() { | 333 NotificationsUpdateFunction::NotificationsUpdateFunction() { |
327 } | 334 } |
328 | 335 |
329 NotificationsUpdateFunction::~NotificationsUpdateFunction() { | 336 NotificationsUpdateFunction::~NotificationsUpdateFunction() { |
330 } | 337 } |
331 | 338 |
332 bool NotificationsUpdateFunction::RunNotificationsApi() { | 339 bool NotificationsUpdateFunction::RunNotificationsApi() { |
333 params_ = api::notifications::Update::Params::Create(*args_); | 340 params_ = api::notifications::Update::Params::Create(*args_); |
334 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 341 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
335 | 342 |
336 if (g_browser_process->notification_ui_manager()-> | 343 if (g_browser_process->notification_ui_manager()->DoesIdExist( |
337 DoesIdExist(NotificationsApiDelegate::CreateScopedIdentifier( | 344 CreateScopedIdentifier(extension_->id(), params_->notification_id))) { |
338 extension_->id(), params_->notification_id))) { | |
339 CreateNotification(params_->notification_id, ¶ms_->options); | 345 CreateNotification(params_->notification_id, ¶ms_->options); |
340 SetResult(Value::CreateBooleanValue(true)); | 346 SetResult(Value::CreateBooleanValue(true)); |
341 } else { | 347 } else { |
342 SetResult(Value::CreateBooleanValue(false)); | 348 SetResult(Value::CreateBooleanValue(false)); |
343 } | 349 } |
344 | 350 |
345 SendResponse(true); | 351 SendResponse(true); |
346 | 352 |
347 return true; | 353 return true; |
348 } | 354 } |
349 | 355 |
350 NotificationsClearFunction::NotificationsClearFunction() { | 356 NotificationsClearFunction::NotificationsClearFunction() { |
351 } | 357 } |
352 | 358 |
353 NotificationsClearFunction::~NotificationsClearFunction() { | 359 NotificationsClearFunction::~NotificationsClearFunction() { |
354 } | 360 } |
355 | 361 |
356 bool NotificationsClearFunction::RunNotificationsApi() { | 362 bool NotificationsClearFunction::RunNotificationsApi() { |
357 params_ = api::notifications::Clear::Params::Create(*args_); | 363 params_ = api::notifications::Clear::Params::Create(*args_); |
358 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 364 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
359 | 365 |
360 bool cancel_result = g_browser_process->notification_ui_manager()-> | 366 bool cancel_result = g_browser_process->notification_ui_manager()->CancelById( |
361 CancelById(NotificationsApiDelegate::CreateScopedIdentifier( | 367 CreateScopedIdentifier(extension_->id(), params_->notification_id)); |
362 extension_->id(), params_->notification_id)); | |
363 | 368 |
364 SetResult(Value::CreateBooleanValue(cancel_result)); | 369 SetResult(Value::CreateBooleanValue(cancel_result)); |
365 SendResponse(true); | 370 SendResponse(true); |
366 | 371 |
367 return true; | 372 return true; |
368 } | 373 } |
369 | 374 |
| 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 |
370 } // namespace extensions | 400 } // namespace extensions |
OLD | NEW |