OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/extensions/extension_app_api.h" |
| 6 |
| 7 #include "base/stl_util-inl.h" |
| 8 #include "base/values.h" |
| 9 #include "chrome/browser/extensions/extension_service.h" |
| 10 #include "chrome/browser/profiles/profile.h" |
| 11 #include "chrome/common/extensions/extension.h" |
| 12 #include "chrome/common/render_messages.h" |
| 13 #include "content/common/notification_service.h" |
| 14 #include "content/common/notification_type.h" |
| 15 |
| 16 |
| 17 const char kBodyTextKey[] = "bodyText"; |
| 18 const char kIconDataKey[] = "iconData"; |
| 19 const char kLinkTextKey[] = "linkText"; |
| 20 const char kLinkUrlKey[] = "linkUrl"; |
| 21 const char kTitleKey[] = "title"; |
| 22 |
| 23 const char kMissingLinkTextError[] = |
| 24 "You must specify linkText if you use linkUrl"; |
| 25 |
| 26 AppNotificationManager::AppNotificationManager() { |
| 27 registrar_.Add(this, |
| 28 NotificationType::EXTENSION_UNINSTALLED, |
| 29 NotificationService::AllSources()); |
| 30 } |
| 31 |
| 32 AppNotificationManager::~AppNotificationManager() {} |
| 33 |
| 34 void AppNotificationManager::Add(AppNotification* item) { |
| 35 CHECK(!item->extension_id.empty()); |
| 36 NotificationMap::iterator found = notifications_.find(item->extension_id); |
| 37 if (found == notifications_.end()) { |
| 38 notifications_[item->extension_id] = AppNotificationList(); |
| 39 found = notifications_.find(item->extension_id); |
| 40 } |
| 41 CHECK(found != notifications_.end()); |
| 42 AppNotificationList& list = (*found).second; |
| 43 list.push_back(linked_ptr<AppNotification>(item)); |
| 44 } |
| 45 |
| 46 const AppNotificationList* AppNotificationManager::GetAll( |
| 47 const std::string& extension_id) { |
| 48 if (ContainsKey(notifications_, extension_id)) |
| 49 return ¬ifications_[extension_id]; |
| 50 return NULL; |
| 51 } |
| 52 |
| 53 void AppNotificationManager::ClearAll(const std::string& extension_id) { |
| 54 NotificationMap::iterator found = notifications_.find(extension_id); |
| 55 if (found != notifications_.end()) |
| 56 notifications_.erase(found); |
| 57 } |
| 58 |
| 59 void AppNotificationManager::Observe(NotificationType type, |
| 60 const NotificationSource& source, |
| 61 const NotificationDetails& details) { |
| 62 CHECK(type == NotificationType::EXTENSION_UNINSTALLED); |
| 63 const std::string& id = |
| 64 Details<UninstalledExtensionInfo>(details)->extension_id; |
| 65 ClearAll(id); |
| 66 } |
| 67 |
| 68 bool AppNotifyFunction::RunImpl() { |
| 69 DictionaryValue* details; |
| 70 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details)); |
| 71 EXTENSION_FUNCTION_VALIDATE(details != NULL); |
| 72 |
| 73 scoped_ptr<AppNotification> item(new AppNotification()); |
| 74 item->extension_id = extension_id(); |
| 75 |
| 76 if (details->HasKey(kTitleKey)) |
| 77 EXTENSION_FUNCTION_VALIDATE(details->GetString(kTitleKey, &item->title)); |
| 78 |
| 79 if (details->HasKey(kBodyTextKey)) |
| 80 EXTENSION_FUNCTION_VALIDATE(details->GetString(kBodyTextKey, &item->body)); |
| 81 |
| 82 if (details->HasKey(kLinkUrlKey)) { |
| 83 std::string link_url; |
| 84 EXTENSION_FUNCTION_VALIDATE(details->GetString(kLinkUrlKey, &link_url)); |
| 85 item->linkUrl = GURL(link_url); |
| 86 if (!item->linkUrl.is_valid()) { |
| 87 error_ = "Invalid url: " + link_url; |
| 88 return false; |
| 89 } |
| 90 if (!details->HasKey(kLinkTextKey)) { |
| 91 error_ = kMissingLinkTextError; |
| 92 return false; |
| 93 } |
| 94 EXTENSION_FUNCTION_VALIDATE(details->GetString(kLinkTextKey, |
| 95 &item->linkText)); |
| 96 } |
| 97 |
| 98 if (details->HasKey(kIconDataKey)) { |
| 99 BinaryValue* binary = NULL; |
| 100 EXTENSION_FUNCTION_VALIDATE(details->GetBinary(kIconDataKey, &binary)); |
| 101 IPC::Message bitmap_pickle(binary->GetBuffer(), binary->GetSize()); |
| 102 void* iter = NULL; |
| 103 SkBitmap bitmap; |
| 104 EXTENSION_FUNCTION_VALIDATE( |
| 105 IPC::ReadParam(&bitmap_pickle, &iter, &bitmap)); |
| 106 // TODO(asargent) - use the bitmap to set the NTP icon! |
| 107 } |
| 108 |
| 109 AppNotificationManager* manager = |
| 110 profile()->GetExtensionService()->app_notification_manager(); |
| 111 |
| 112 manager->Add(item.release()); |
| 113 |
| 114 NotificationService::current()->Notify( |
| 115 NotificationType::APP_NOTIFICATION_STATE_CHANGED, |
| 116 Source<Profile>(profile_), |
| 117 NotificationService::NoDetails()); |
| 118 |
| 119 return true; |
| 120 } |
| 121 |
| 122 bool AppClearAllNotificationsFunction::RunImpl() { |
| 123 AppNotificationManager* manager = |
| 124 profile()->GetExtensionService()->app_notification_manager(); |
| 125 manager->ClearAll(extension_id()); |
| 126 NotificationService::current()->Notify( |
| 127 NotificationType::APP_NOTIFICATION_STATE_CHANGED, |
| 128 Source<Profile>(profile_), |
| 129 NotificationService::NoDetails()); |
| 130 return true; |
| 131 } |
OLD | NEW |