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 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
123 | 133 |
124 DISALLOW_COPY_AND_ASSIGN(NotificationsApiDelegate); | 134 DISALLOW_COPY_AND_ASSIGN(NotificationsApiDelegate); |
125 }; | 135 }; |
126 | 136 |
127 } // namespace | 137 } // namespace |
128 | 138 |
129 bool NotificationsApiFunction::IsNotificationsApiAvailable() { | 139 bool NotificationsApiFunction::IsNotificationsApiAvailable() { |
130 // We need to check this explicitly rather than letting | 140 // We need to check this explicitly rather than letting |
131 // _permission_features.json enforce it, because we're sharing the | 141 // _permission_features.json enforce it, because we're sharing the |
132 // chrome.notifications permissions namespace with WebKit notifications. | 142 // chrome.notifications permissions namespace with WebKit notifications. |
133 if (!(GetExtension()->is_platform_app() || GetExtension()->is_extension())) | 143 return GetExtension()->is_platform_app() || GetExtension()->is_extension(); |
134 return false; | |
135 | |
136 return true; | |
137 } | 144 } |
138 | 145 |
139 NotificationsApiFunction::NotificationsApiFunction() { | 146 NotificationsApiFunction::NotificationsApiFunction() { |
140 } | 147 } |
141 | 148 |
142 NotificationsApiFunction::~NotificationsApiFunction() { | 149 NotificationsApiFunction::~NotificationsApiFunction() { |
143 } | 150 } |
144 | 151 |
145 void NotificationsApiFunction::CreateNotification( | 152 void NotificationsApiFunction::CreateNotification( |
146 const std::string& id, | 153 const std::string& id, |
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
323 NotificationsUpdateFunction::NotificationsUpdateFunction() { | 330 NotificationsUpdateFunction::NotificationsUpdateFunction() { |
324 } | 331 } |
325 | 332 |
326 NotificationsUpdateFunction::~NotificationsUpdateFunction() { | 333 NotificationsUpdateFunction::~NotificationsUpdateFunction() { |
327 } | 334 } |
328 | 335 |
329 bool NotificationsUpdateFunction::RunNotificationsApi() { | 336 bool NotificationsUpdateFunction::RunNotificationsApi() { |
330 params_ = api::notifications::Update::Params::Create(*args_); | 337 params_ = api::notifications::Update::Params::Create(*args_); |
331 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 338 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
332 | 339 |
333 if (g_browser_process->notification_ui_manager()-> | 340 if (g_browser_process->notification_ui_manager()->DoesIdExist( |
334 DoesIdExist(NotificationsApiDelegate::CreateScopedIdentifier( | 341 CreateScopedIdentifier(extension_->id(), params_->notification_id))) { |
335 extension_->id(), params_->notification_id))) { | |
336 CreateNotification(params_->notification_id, ¶ms_->options); | 342 CreateNotification(params_->notification_id, ¶ms_->options); |
337 SetResult(Value::CreateBooleanValue(true)); | 343 SetResult(Value::CreateBooleanValue(true)); |
338 } else { | 344 } else { |
339 SetResult(Value::CreateBooleanValue(false)); | 345 SetResult(Value::CreateBooleanValue(false)); |
340 } | 346 } |
341 | 347 |
342 SendResponse(true); | 348 SendResponse(true); |
343 | 349 |
344 return true; | 350 return true; |
345 } | 351 } |
346 | 352 |
347 NotificationsClearFunction::NotificationsClearFunction() { | 353 NotificationsClearFunction::NotificationsClearFunction() { |
348 } | 354 } |
349 | 355 |
350 NotificationsClearFunction::~NotificationsClearFunction() { | 356 NotificationsClearFunction::~NotificationsClearFunction() { |
351 } | 357 } |
352 | 358 |
353 bool NotificationsClearFunction::RunNotificationsApi() { | 359 bool NotificationsClearFunction::RunNotificationsApi() { |
354 params_ = api::notifications::Clear::Params::Create(*args_); | 360 params_ = api::notifications::Clear::Params::Create(*args_); |
355 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 361 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
356 | 362 |
357 bool cancel_result = g_browser_process->notification_ui_manager()-> | 363 bool cancel_result = g_browser_process->notification_ui_manager()->CancelById( |
358 CancelById(NotificationsApiDelegate::CreateScopedIdentifier( | 364 CreateScopedIdentifier(extension_->id(), params_->notification_id)); |
359 extension_->id(), params_->notification_id)); | |
360 | 365 |
361 SetResult(Value::CreateBooleanValue(cancel_result)); | 366 SetResult(Value::CreateBooleanValue(cancel_result)); |
362 SendResponse(true); | 367 SendResponse(true); |
363 | 368 |
364 return true; | 369 return true; |
365 } | 370 } |
366 | 371 |
372 NotificationsGetAllFunction::NotificationsGetAllFunction() {} | |
373 | |
374 NotificationsGetAllFunction::~NotificationsGetAllFunction() {} | |
375 | |
376 bool NotificationsGetAllFunction::RunNotificationsApi() { | |
377 std::set<std::string> notification_ids; | |
378 NotificationUIManager* notification_ui_manager = | |
379 g_browser_process->notification_ui_manager(); | |
380 notification_ui_manager->GetAllIdsByProfileAndSourceOrigin( | |
381 profile_, extension_->url(), ¬ification_ids); | |
382 | |
383 scoped_ptr<DictionaryValue> result(new DictionaryValue()); | |
384 | |
385 for (std::set<std::string>::iterator iter = notification_ids.begin(); | |
386 iter != notification_ids.end(); | |
387 iter++) { | |
Dmitry Titov
2013/05/16 20:39:15
nit: Most often, this loop is split in 2 lines, no
dewittj
2013/05/16 23:40:34
ALL HAIL CLANG-FORMAT-DIFF
| |
388 result->SetBooleanWithoutPathExpansion( | |
389 StripScopeFromIdentifier(extension_->id(), *iter), true); | |
Dmitry Titov
2013/05/16 20:39:15
Just a note: this makes me think we might want to
dewittj
2013/05/16 23:40:34
I agree, but it is indeed better done in a separat
| |
390 } | |
391 | |
392 SetResult(result.release()); | |
393 SendResponse(true); | |
394 | |
395 return true; | |
396 } | |
397 | |
367 } // namespace extensions | 398 } // namespace extensions |
OLD | NEW |