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 AppNotificationManager::AppNotificationManager() { | |
24 registrar_.Add(this, | |
25 NotificationType::EXTENSION_UNINSTALLED, | |
26 NotificationService::AllSources()); | |
27 } | |
28 | |
29 AppNotificationManager::~AppNotificationManager() {} | |
30 | |
31 void AppNotificationManager::Add(AppNotification* item) { | |
32 CHECK(!item->extension_id.empty()); | |
33 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_".
| |
34 if (found == items_.end()) { | |
35 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
| |
36 found = items_.find(item->extension_id); | |
37 } | |
38 CHECK(found != items_.end()); | |
39 AppNotificationList& list = (*found).second; | |
40 list.push_back(linked_ptr<AppNotification>(item)); | |
41 } | |
42 | |
43 const AppNotificationList* AppNotificationManager::GetAll( | |
44 const std::string& extension_id) { | |
45 if (ContainsKey(items_, extension_id)) | |
46 return &items_[extension_id]; | |
47 return NULL; | |
48 } | |
49 | |
50 void AppNotificationManager::ClearAll(const std::string& extension_id) { | |
51 NotificationMap::iterator found = items_.find(extension_id); | |
52 if (found != items_.end()) | |
53 items_.erase(found); | |
54 } | |
55 | |
56 void AppNotificationManager::Observe(NotificationType type, | |
57 const NotificationSource& source, | |
58 const NotificationDetails& details) { | |
59 CHECK(type == NotificationType::EXTENSION_UNINSTALLED); | |
60 const std::string& id = | |
61 Details<UninstalledExtensionInfo>(details)->extension_id; | |
62 ClearAll(id); | |
63 } | |
64 | |
65 | |
Finnur
2011/06/20 12:21:46
nit: extra line break
asargent_no_longer_on_chrome
2011/06/21 18:10:11
Done.
| |
66 bool AppNotifyFunction::RunImpl() { | |
67 DictionaryValue* details; | |
68 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details)); | |
69 EXTENSION_FUNCTION_VALIDATE(details != NULL); | |
70 | |
71 scoped_ptr<AppNotification> item(new AppNotification()); | |
72 item->extension_id = extension_id(); | |
73 | |
74 if (details->HasKey(kTitleKey)) | |
75 EXTENSION_FUNCTION_VALIDATE(details->GetString(kTitleKey, &item->title)); | |
76 | |
77 if (details->HasKey(kBodyTextKey)) | |
78 EXTENSION_FUNCTION_VALIDATE(details->GetString(kBodyTextKey, &item->body)); | |
79 | |
80 if (details->HasKey(kLinkUrlKey)) { | |
81 std::string link_url; | |
82 EXTENSION_FUNCTION_VALIDATE(details->GetString(kLinkUrlKey, &link_url)); | |
83 item->linkUrl = GURL(link_url); | |
84 if (!item->linkUrl.is_valid()) { | |
85 error_ = "Invalid url: " + link_url; | |
86 return false; | |
87 } | |
88 } | |
89 | |
90 if (details->HasKey(kLinkTextKey)) | |
91 EXTENSION_FUNCTION_VALIDATE(details->GetString(kLinkTextKey, | |
92 &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
| |
93 | |
94 if (details->HasKey(kIconDataKey)) { | |
95 BinaryValue* binary = NULL; | |
96 EXTENSION_FUNCTION_VALIDATE(details->GetBinary(kIconDataKey, &binary)); | |
97 IPC::Message bitmap_pickle(binary->GetBuffer(), binary->GetSize()); | |
98 void* iter = NULL; | |
99 SkBitmap bitmap; | |
100 EXTENSION_FUNCTION_VALIDATE( | |
101 IPC::ReadParam(&bitmap_pickle, &iter, &bitmap)); | |
102 // TODO(asargent) - use the bitmap to set the NTP icon! | |
103 } | |
104 | |
105 AppNotificationManager* manager = | |
106 profile()->GetExtensionService()->app_notification_manager(); | |
107 | |
108 manager->Add(item.release()); | |
109 | |
110 NotificationService::current()->Notify( | |
111 NotificationType::APP_NOTIFICATION_STATE_CHANGED, | |
112 Source<Profile>(profile_), | |
113 NotificationService::NoDetails()); | |
114 | |
115 return true; | |
116 } | |
117 | |
118 bool AppClearAllNotificationsFunction::RunImpl() { | |
119 AppNotificationManager* manager = | |
120 profile()->GetExtensionService()->app_notification_manager(); | |
121 manager->ClearAll(extension_id()); | |
122 NotificationService::current()->Notify( | |
123 NotificationType::APP_NOTIFICATION_STATE_CHANGED, | |
124 Source<Profile>(profile_), | |
125 NotificationService::NoDetails()); | |
126 return true; | |
127 } | |
OLD | NEW |