Chromium Code Reviews| Index: chrome/browser/extensions/extension_app_api.cc |
| diff --git a/chrome/browser/extensions/extension_app_api.cc b/chrome/browser/extensions/extension_app_api.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..41466a14decaeac5023859853fd2753c2db245dd |
| --- /dev/null |
| +++ b/chrome/browser/extensions/extension_app_api.cc |
| @@ -0,0 +1,127 @@ |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/extensions/extension_app_api.h" |
| + |
| +#include "base/stl_util-inl.h" |
| +#include "base/values.h" |
| +#include "chrome/browser/extensions/extension_service.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/common/extensions/extension.h" |
| +#include "chrome/common/render_messages.h" |
| +#include "content/common/notification_service.h" |
| +#include "content/common/notification_type.h" |
| + |
| + |
| +const char kBodyTextKey[] = "bodyText"; |
| +const char kIconDataKey[] = "iconData"; |
| +const char kLinkTextKey[] = "linkText"; |
| +const char kLinkUrlKey[] = "linkUrl"; |
| +const char kTitleKey[] = "title"; |
| + |
| +AppNotificationManager::AppNotificationManager() { |
| + registrar_.Add(this, |
| + NotificationType::EXTENSION_UNINSTALLED, |
| + NotificationService::AllSources()); |
| +} |
| + |
| +AppNotificationManager::~AppNotificationManager() {} |
| + |
| +void AppNotificationManager::Add(AppNotification* item) { |
| + CHECK(!item->extension_id.empty()); |
| + NotificationMap::iterator found = items_.find(item->extension_id); |
|
Finnur
2011/06/20 12:21:46
It would probably help readability a bit to rename
asargent_no_longer_on_chrome
2011/06/21 18:10:11
Good point. Changed to "notifications_".
|
| + if (found == items_.end()) { |
| + items_[item->extension_id] = AppNotificationList(); |
|
Finnur
2011/06/20 12:21:46
AppNotificationList()? Where does that come from?
asargent_no_longer_on_chrome
2011/06/21 18:10:11
It's in extension_app_api.h, just before the start
|
| + found = items_.find(item->extension_id); |
| + } |
| + CHECK(found != items_.end()); |
| + AppNotificationList& list = (*found).second; |
| + list.push_back(linked_ptr<AppNotification>(item)); |
| +} |
| + |
| +const AppNotificationList* AppNotificationManager::GetAll( |
| + const std::string& extension_id) { |
| + if (ContainsKey(items_, extension_id)) |
| + return &items_[extension_id]; |
| + return NULL; |
| +} |
| + |
| +void AppNotificationManager::ClearAll(const std::string& extension_id) { |
| + NotificationMap::iterator found = items_.find(extension_id); |
| + if (found != items_.end()) |
| + items_.erase(found); |
| +} |
| + |
| +void AppNotificationManager::Observe(NotificationType type, |
| + const NotificationSource& source, |
| + const NotificationDetails& details) { |
| + CHECK(type == NotificationType::EXTENSION_UNINSTALLED); |
| + const std::string& id = |
| + Details<UninstalledExtensionInfo>(details)->extension_id; |
| + ClearAll(id); |
| +} |
| + |
| + |
|
Finnur
2011/06/20 12:21:46
nit: extra line break
asargent_no_longer_on_chrome
2011/06/21 18:10:11
Done.
|
| +bool AppNotifyFunction::RunImpl() { |
| + DictionaryValue* details; |
| + EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details)); |
| + EXTENSION_FUNCTION_VALIDATE(details != NULL); |
| + |
| + scoped_ptr<AppNotification> item(new AppNotification()); |
| + item->extension_id = extension_id(); |
| + |
| + if (details->HasKey(kTitleKey)) |
| + EXTENSION_FUNCTION_VALIDATE(details->GetString(kTitleKey, &item->title)); |
| + |
| + if (details->HasKey(kBodyTextKey)) |
| + EXTENSION_FUNCTION_VALIDATE(details->GetString(kBodyTextKey, &item->body)); |
| + |
| + if (details->HasKey(kLinkUrlKey)) { |
| + std::string link_url; |
| + EXTENSION_FUNCTION_VALIDATE(details->GetString(kLinkUrlKey, &link_url)); |
| + item->linkUrl = GURL(link_url); |
| + if (!item->linkUrl.is_valid()) { |
| + error_ = "Invalid url: " + link_url; |
| + return false; |
| + } |
| + } |
| + |
| + if (details->HasKey(kLinkTextKey)) |
| + EXTENSION_FUNCTION_VALIDATE(details->GetString(kLinkTextKey, |
| + &item->linkText)); |
|
Finnur
2011/06/20 12:21:46
I would move this block into line 88 (within the b
asargent_no_longer_on_chrome
2011/06/21 18:10:11
Good point on moving it into the block above - I'v
Finnur
2011/06/22 10:40:23
I think we should address it at this funnel rather
asargent_no_longer_on_chrome
2011/06/22 17:36:36
Ok, that seems reasonable. We can certainly change
|
| + |
| + if (details->HasKey(kIconDataKey)) { |
| + BinaryValue* binary = NULL; |
| + EXTENSION_FUNCTION_VALIDATE(details->GetBinary(kIconDataKey, &binary)); |
| + IPC::Message bitmap_pickle(binary->GetBuffer(), binary->GetSize()); |
| + void* iter = NULL; |
| + SkBitmap bitmap; |
| + EXTENSION_FUNCTION_VALIDATE( |
| + IPC::ReadParam(&bitmap_pickle, &iter, &bitmap)); |
| + // TODO(asargent) - use the bitmap to set the NTP icon! |
| + } |
| + |
| + AppNotificationManager* manager = |
| + profile()->GetExtensionService()->app_notification_manager(); |
| + |
| + manager->Add(item.release()); |
| + |
| + NotificationService::current()->Notify( |
| + NotificationType::APP_NOTIFICATION_STATE_CHANGED, |
| + Source<Profile>(profile_), |
| + NotificationService::NoDetails()); |
| + |
| + return true; |
| +} |
| + |
| +bool AppClearAllNotificationsFunction::RunImpl() { |
| + AppNotificationManager* manager = |
| + profile()->GetExtensionService()->app_notification_manager(); |
| + manager->ClearAll(extension_id()); |
| + NotificationService::current()->Notify( |
| + NotificationType::APP_NOTIFICATION_STATE_CHANGED, |
| + Source<Profile>(profile_), |
| + NotificationService::NoDetails()); |
| + return true; |
| +} |