Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(530)

Side by Side Diff: chrome/browser/extensions/extension_app_api.cc

Issue 7187023: Adding an experimental app notification API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: avoid rebuilding NTP apps section when notifications arrive Created 9 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 &notifications_[extension_id];
50 return NULL;
51 }
52
53 const AppNotification* AppNotificationManager::GetLast(
54 const std::string& extension_id) {
55 NotificationMap::iterator found = notifications_.find(extension_id);
56 if (found == notifications_.end())
57 return NULL;
58 const AppNotificationList& list = found->second;
59 return list.rbegin()->get();
60 }
61
62 void AppNotificationManager::ClearAll(const std::string& extension_id) {
63 NotificationMap::iterator found = notifications_.find(extension_id);
64 if (found != notifications_.end())
65 notifications_.erase(found);
66 }
67
68 void AppNotificationManager::Observe(NotificationType type,
69 const NotificationSource& source,
70 const NotificationDetails& details) {
71 CHECK(type == NotificationType::EXTENSION_UNINSTALLED);
72 const std::string& id =
73 Details<UninstalledExtensionInfo>(details)->extension_id;
74 ClearAll(id);
75 }
76
77 bool AppNotifyFunction::RunImpl() {
78 DictionaryValue* details;
79 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details));
80 EXTENSION_FUNCTION_VALIDATE(details != NULL);
81
82 scoped_ptr<AppNotification> item(new AppNotification());
83 item->extension_id = extension_id();
84
85 if (details->HasKey(kTitleKey))
86 EXTENSION_FUNCTION_VALIDATE(details->GetString(kTitleKey, &item->title));
87
88 if (details->HasKey(kBodyTextKey))
89 EXTENSION_FUNCTION_VALIDATE(details->GetString(kBodyTextKey, &item->body));
90
91 if (details->HasKey(kLinkUrlKey)) {
92 std::string link_url;
93 EXTENSION_FUNCTION_VALIDATE(details->GetString(kLinkUrlKey, &link_url));
94 item->linkUrl = GURL(link_url);
95 if (!item->linkUrl.is_valid()) {
96 error_ = "Invalid url: " + link_url;
97 return false;
98 }
99 if (!details->HasKey(kLinkTextKey)) {
100 error_ = kMissingLinkTextError;
101 return false;
102 }
103 EXTENSION_FUNCTION_VALIDATE(details->GetString(kLinkTextKey,
104 &item->linkText));
105 }
106
107 if (details->HasKey(kIconDataKey)) {
108 BinaryValue* binary = NULL;
109 EXTENSION_FUNCTION_VALIDATE(details->GetBinary(kIconDataKey, &binary));
110 IPC::Message bitmap_pickle(binary->GetBuffer(), binary->GetSize());
111 void* iter = NULL;
112 SkBitmap bitmap;
113 EXTENSION_FUNCTION_VALIDATE(
114 IPC::ReadParam(&bitmap_pickle, &iter, &bitmap));
115 // TODO(asargent) - use the bitmap to set the NTP icon!
116 }
117
118 AppNotificationManager* manager =
119 profile()->GetExtensionService()->app_notification_manager();
120
121 manager->Add(item.release());
122
123 NotificationService::current()->Notify(
124 NotificationType::APP_NOTIFICATION_STATE_CHANGED,
125 Source<Profile>(profile_),
126 Details<const std::string>(&extension_id()));
127
128 return true;
129 }
130
131 bool AppClearAllNotificationsFunction::RunImpl() {
132 AppNotificationManager* manager =
133 profile()->GetExtensionService()->app_notification_manager();
134 manager->ClearAll(extension_id());
135 NotificationService::current()->Notify(
136 NotificationType::APP_NOTIFICATION_STATE_CHANGED,
137 Source<Profile>(profile_),
138 Details<const std::string>(&extension_id()));
139 return true;
140 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698