| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 // Given an extension id and another id, returns an id that is unique | 84 // Given an extension id and another id, returns an id that is unique |
| 85 // relative to other extensions. | 85 // relative to other extensions. |
| 86 std::string CreateScopedIdentifier(const std::string& extension_id, | 86 std::string CreateScopedIdentifier(const std::string& extension_id, |
| 87 const std::string& id) { | 87 const std::string& id) { |
| 88 return extension_id + "-" + id; | 88 return extension_id + "-" + id; |
| 89 } | 89 } |
| 90 | 90 |
| 91 // Removes the unique internal identifier to send the ID as the | 91 // Removes the unique internal identifier to send the ID as the |
| 92 // extension expects it. | 92 // extension expects it. |
| 93 std::string StripScopeFromIdentifier(const std::string& extension_id, | 93 std::string StripScopeFromIdentifier(const std::string& extension_id, |
| 94 const std::string& id) { | 94 const std::string& scoped_id) { |
| 95 size_t index_of_separator = extension_id.length() + 1; | 95 size_t index_of_separator = extension_id.length() + 1; |
| 96 DCHECK_LT(index_of_separator, id.length()); | 96 DCHECK_LT(index_of_separator, scoped_id.length()); |
| 97 | 97 |
| 98 return id.substr(index_of_separator); | 98 return scoped_id.substr(index_of_separator); |
| 99 } | 99 } |
| 100 | 100 |
| 101 const gfx::ImageSkia CreateSolidColorImage(int width, | 101 const gfx::ImageSkia CreateSolidColorImage(int width, |
| 102 int height, | 102 int height, |
| 103 SkColor color) { | 103 SkColor color) { |
| 104 SkBitmap bitmap; | 104 SkBitmap bitmap; |
| 105 bitmap.allocN32Pixels(width, height); | 105 bitmap.allocN32Pixels(width, height); |
| 106 bitmap.eraseColor(color); | 106 bitmap.eraseColor(color); |
| 107 return gfx::ImageSkia::CreateFrom1xBitmap(bitmap); | 107 return gfx::ImageSkia::CreateFrom1xBitmap(bitmap); |
| 108 } | 108 } |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 public: | 208 public: |
| 209 NotificationsApiDelegate(ChromeAsyncExtensionFunction* api_function, | 209 NotificationsApiDelegate(ChromeAsyncExtensionFunction* 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 : api_function_(api_function), | 213 : api_function_(api_function), |
| 214 event_router_(EventRouter::Get(profile)), | 214 event_router_(EventRouter::Get(profile)), |
| 215 display_helper_( | 215 display_helper_( |
| 216 ExtensionNotificationDisplayHelperFactory::GetForProfile(profile)), | 216 ExtensionNotificationDisplayHelperFactory::GetForProfile(profile)), |
| 217 extension_id_(extension_id), | 217 extension_id_(extension_id), |
| 218 id_(id), | |
| 219 scoped_id_(CreateScopedIdentifier(extension_id, id)) { | 218 scoped_id_(CreateScopedIdentifier(extension_id, id)) { |
| 220 DCHECK(api_function_); | 219 DCHECK(api_function_); |
| 221 DCHECK(display_helper_); | 220 DCHECK(display_helper_); |
| 222 | 221 |
| 223 shutdown_notifier_subscription_ = | 222 shutdown_notifier_subscription_ = |
| 224 ShutdownNotifierFactory::GetInstance()->Get(profile)->Subscribe( | 223 ShutdownNotifierFactory::GetInstance()->Get(profile)->Subscribe( |
| 225 base::Bind(&NotificationsApiDelegate::Shutdown, | 224 base::Bind(&NotificationsApiDelegate::Shutdown, |
| 226 base::Unretained(this))); | 225 base::Unretained(this))); |
| 227 } | 226 } |
| 228 | 227 |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 309 } | 308 } |
| 310 | 309 |
| 311 void Shutdown() { | 310 void Shutdown() { |
| 312 shutdown_notifier_subscription_.reset(); | 311 shutdown_notifier_subscription_.reset(); |
| 313 event_router_ = nullptr; | 312 event_router_ = nullptr; |
| 314 display_helper_ = nullptr; | 313 display_helper_ = nullptr; |
| 315 } | 314 } |
| 316 | 315 |
| 317 std::unique_ptr<base::ListValue> CreateBaseEventArgs() { | 316 std::unique_ptr<base::ListValue> CreateBaseEventArgs() { |
| 318 std::unique_ptr<base::ListValue> args(new base::ListValue()); | 317 std::unique_ptr<base::ListValue> args(new base::ListValue()); |
| 319 args->AppendString(id_); | 318 args->AppendString(StripScopeFromIdentifier(extension_id_, scoped_id_)); |
| 320 return args; | 319 return args; |
| 321 } | 320 } |
| 322 | 321 |
| 323 scoped_refptr<ChromeAsyncExtensionFunction> api_function_; | 322 scoped_refptr<ChromeAsyncExtensionFunction> api_function_; |
| 324 | 323 |
| 325 // Since this class is refcounted it may outlive the profile. We listen for | 324 // Since this class is refcounted it may outlive the profile. We listen for |
| 326 // profile-keyed service shutdown events and reset to nullptr at that time, | 325 // profile-keyed service shutdown events and reset to nullptr at that time, |
| 327 // so make sure to check for a valid pointer before use. | 326 // so make sure to check for a valid pointer before use. |
| 328 EventRouter* event_router_; | 327 EventRouter* event_router_; |
| 329 ExtensionNotificationDisplayHelper* display_helper_; | 328 ExtensionNotificationDisplayHelper* display_helper_; |
| 330 | 329 |
| 331 const std::string extension_id_; | 330 const std::string extension_id_; |
| 332 const std::string id_; | |
| 333 const std::string scoped_id_; | 331 const std::string scoped_id_; |
| 334 | 332 |
| 335 std::unique_ptr<KeyedServiceShutdownNotifier::Subscription> | 333 std::unique_ptr<KeyedServiceShutdownNotifier::Subscription> |
| 336 shutdown_notifier_subscription_; | 334 shutdown_notifier_subscription_; |
| 337 | 335 |
| 338 DISALLOW_COPY_AND_ASSIGN(NotificationsApiDelegate); | 336 DISALLOW_COPY_AND_ASSIGN(NotificationsApiDelegate); |
| 339 }; | 337 }; |
| 340 | 338 |
| 341 } // namespace | 339 } // namespace |
| 342 | 340 |
| (...skipping 487 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 830 : api::notifications::PERMISSION_LEVEL_DENIED; | 828 : api::notifications::PERMISSION_LEVEL_DENIED; |
| 831 | 829 |
| 832 SetResult( | 830 SetResult( |
| 833 base::MakeUnique<base::Value>(api::notifications::ToString(result))); | 831 base::MakeUnique<base::Value>(api::notifications::ToString(result))); |
| 834 SendResponse(true); | 832 SendResponse(true); |
| 835 | 833 |
| 836 return true; | 834 return true; |
| 837 } | 835 } |
| 838 | 836 |
| 839 } // namespace extensions | 837 } // namespace extensions |
| OLD | NEW |