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

Side by Side Diff: chrome/browser/extensions/api/notification/notification_api.cc

Issue 12277024: Notificaitons refactor step 2 (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: more feedback from Steven Created 7 years, 10 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
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/notification/notification_api.h" 5 #include "chrome/browser/extensions/api/notification/notification_api.h"
6 6
7 #include "base/callback.h" 7 #include "base/callback.h"
8 #include "base/strings/string_number_conversions.h" 8 #include "base/strings/string_number_conversions.h"
9 #include "base/utf_string_conversions.h" 9 #include "base/utf_string_conversions.h"
10 #include "chrome/browser/browser_process.h" 10 #include "chrome/browser/browser_process.h"
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
109 }; 109 };
110 110
111 } // namespace 111 } // namespace
112 112
113 NotificationApiFunction::NotificationApiFunction() { 113 NotificationApiFunction::NotificationApiFunction() {
114 } 114 }
115 115
116 NotificationApiFunction::~NotificationApiFunction() { 116 NotificationApiFunction::~NotificationApiFunction() {
117 } 117 }
118 118
119 ui::notifications::NotificationType 119 message_center::NotificationType
120 NotificationApiFunction::MapApiTemplateTypeToType( 120 NotificationApiFunction::MapApiTemplateTypeToType(
121 api::experimental_notification::TemplateType type) { 121 api::experimental_notification::TemplateType type) {
122 switch (type) { 122 switch (type) {
123 case api::experimental_notification::TEMPLATE_TYPE_NONE: 123 case api::experimental_notification::TEMPLATE_TYPE_NONE:
124 case api::experimental_notification::TEMPLATE_TYPE_SIMPLE: 124 case api::experimental_notification::TEMPLATE_TYPE_SIMPLE:
125 return ui::notifications::NOTIFICATION_TYPE_SIMPLE; 125 return message_center::NOTIFICATION_TYPE_SIMPLE;
126 case api::experimental_notification::TEMPLATE_TYPE_BASIC: 126 case api::experimental_notification::TEMPLATE_TYPE_BASIC:
127 return ui::notifications::NOTIFICATION_TYPE_BASE_FORMAT; 127 return message_center::NOTIFICATION_TYPE_BASE_FORMAT;
128 case api::experimental_notification::TEMPLATE_TYPE_IMAGE: 128 case api::experimental_notification::TEMPLATE_TYPE_IMAGE:
129 return ui::notifications::NOTIFICATION_TYPE_IMAGE; 129 return message_center::NOTIFICATION_TYPE_IMAGE;
130 case api::experimental_notification::TEMPLATE_TYPE_LIST: 130 case api::experimental_notification::TEMPLATE_TYPE_LIST:
131 return ui::notifications::NOTIFICATION_TYPE_MULTIPLE; 131 return message_center::NOTIFICATION_TYPE_MULTIPLE;
132 default: 132 default:
133 // Gracefully handle newer application code that is running on an older 133 // Gracefully handle newer application code that is running on an older
134 // runtime that doesn't recognize the requested template. 134 // runtime that doesn't recognize the requested template.
135 return ui::notifications::NOTIFICATION_TYPE_BASE_FORMAT; 135 return message_center::NOTIFICATION_TYPE_BASE_FORMAT;
136 } 136 }
137 } 137 }
138 138
139 // If older notification runtime is used, MessageCenter is not built.
140 // Use simpler bridge then, ignoring all options.
141 #if !defined (ENABLE_MESSAGE_CENTER)
139 void NotificationApiFunction::CreateNotification( 142 void NotificationApiFunction::CreateNotification(
140 const std::string& id, 143 const std::string& id,
141 api::experimental_notification::NotificationOptions* options) { 144 api::experimental_notification::NotificationOptions* options) {
142 ui::notifications::NotificationType type = MapApiTemplateTypeToType( 145 message_center::NotificationType type =
143 options->template_type); 146 MapApiTemplateTypeToType(options->template_type);
144 GURL icon_url(UTF8ToUTF16(options->icon_url)); 147 GURL icon_url(UTF8ToUTF16(options->icon_url));
145 string16 title(UTF8ToUTF16(options->title)); 148 string16 title(UTF8ToUTF16(options->title));
146 string16 message(UTF8ToUTF16(options->message)); 149 string16 message(UTF8ToUTF16(options->message));
150
151 // Ignore options if running on the old notification runtime.
152 scoped_ptr<DictionaryValue> optional_fields(new DictionaryValue());
153
154 NotificationApiDelegate* api_delegate(new NotificationApiDelegate(
155 this,
156 profile(),
157 extension_->id(),
158 id)); // ownership is passed to Notification
159 Notification notification(type, extension_->url(), icon_url, title, message,
160 WebKit::WebTextDirectionDefault,
161 string16(), UTF8ToUTF16(api_delegate->id()),
162 optional_fields.get(), api_delegate);
163
164 g_browser_process->notification_ui_manager()->Add(notification, profile());
165 }
166 #else // defined(ENABLE_MESSAGE_CENTER)
167 void NotificationApiFunction::CreateNotification(
168 const std::string& id,
169 api::experimental_notification::NotificationOptions* options) {
170 message_center::NotificationType type =
171 MapApiTemplateTypeToType(options->template_type);
172 GURL icon_url(UTF8ToUTF16(options->icon_url));
173 string16 title(UTF8ToUTF16(options->title));
174 string16 message(UTF8ToUTF16(options->message));
147 175
148 scoped_ptr<DictionaryValue> optional_fields(new DictionaryValue()); 176 scoped_ptr<DictionaryValue> optional_fields(new DictionaryValue());
149 177
150 // For all notification types. 178 // For all notification types.
151 if (options->priority.get()) 179 if (options->priority.get())
152 optional_fields->SetInteger(ui::notifications::kPriorityKey, 180 optional_fields->SetInteger(message_center::kPriorityKey,
153 *options->priority); 181 *options->priority);
154 if (options->event_time.get()) 182 if (options->event_time.get())
155 optional_fields->SetDouble(ui::notifications::kTimestampKey, 183 optional_fields->SetDouble(message_center::kTimestampKey,
156 *options->event_time); 184 *options->event_time);
157 if (options->buttons.get()) { 185 if (options->buttons.get()) {
158 if (options->buttons->size() > 0) { 186 if (options->buttons->size() > 0) {
159 linked_ptr<api::experimental_notification::NotificationButton> button = 187 linked_ptr<api::experimental_notification::NotificationButton> button =
160 (*options->buttons)[0]; 188 (*options->buttons)[0];
161 optional_fields->SetString(ui::notifications::kButtonOneTitleKey, 189 optional_fields->SetString(message_center::kButtonOneTitleKey,
162 UTF8ToUTF16(button->title)); 190 UTF8ToUTF16(button->title));
163 if (button->icon_url.get()) 191 if (button->icon_url.get())
164 optional_fields->SetString(ui::notifications::kButtonOneIconUrlKey, 192 optional_fields->SetString(message_center::kButtonOneIconUrlKey,
165 UTF8ToUTF16(*button->icon_url)); 193 UTF8ToUTF16(*button->icon_url));
166 } 194 }
167 if (options->buttons->size() > 1) { 195 if (options->buttons->size() > 1) {
168 linked_ptr<api::experimental_notification::NotificationButton> button = 196 linked_ptr<api::experimental_notification::NotificationButton> button =
169 (*options->buttons)[1]; 197 (*options->buttons)[1];
170 optional_fields->SetString(ui::notifications::kButtonTwoTitleKey, 198 optional_fields->SetString(message_center::kButtonTwoTitleKey,
171 UTF8ToUTF16(button->title)); 199 UTF8ToUTF16(button->title));
172 if (button->icon_url.get()) 200 if (button->icon_url.get())
173 optional_fields->SetString(ui::notifications::kButtonTwoIconUrlKey, 201 optional_fields->SetString(message_center::kButtonTwoIconUrlKey,
174 UTF8ToUTF16(*button->icon_url)); 202 UTF8ToUTF16(*button->icon_url));
175 } 203 }
176 } 204 }
177 if (options->expanded_message.get()) 205 if (options->expanded_message.get())
178 optional_fields->SetString(ui::notifications::kExpandedMessageKey, 206 optional_fields->SetString(message_center::kExpandedMessageKey,
179 UTF8ToUTF16(*options->expanded_message)); 207 UTF8ToUTF16(*options->expanded_message));
180 208
181 // For image notifications (type == 'image'). 209 // For image notifications (type == 'image').
182 // TODO(dharcourt): Fail if (type == 'image' && !options->image_url.get()) 210 // TODO(dharcourt): Fail if (type == 'image' && !options->image_url.get())
183 // TODO(dharcourt): Fail if (type != 'image' && options->image_url.get()) 211 // TODO(dharcourt): Fail if (type != 'image' && options->image_url.get())
184 if (options->image_url.get()) 212 if (options->image_url.get())
185 optional_fields->SetString(ui::notifications::kImageUrlKey, 213 optional_fields->SetString(message_center::kImageUrlKey,
186 UTF8ToUTF16(*options->image_url)); 214 UTF8ToUTF16(*options->image_url));
187 215
188 // For list notifications (type == 'multiple'). 216 // For list notifications (type == 'multiple').
189 // TODO(dharcourt): Fail if (type == 'multiple' && !options->items.get()) 217 // TODO(dharcourt): Fail if (type == 'multiple' && !options->items.get())
190 // TODO(dharcourt): Fail if (type != 'multiple' && options->items.get()) 218 // TODO(dharcourt): Fail if (type != 'multiple' && options->items.get())
191 if (options->items.get()) { 219 if (options->items.get()) {
192 base::ListValue* items = new base::ListValue(); 220 base::ListValue* items = new base::ListValue();
193 std::vector< 221 std::vector<
194 linked_ptr< 222 linked_ptr<
195 api::experimental_notification::NotificationItem> >::iterator i; 223 api::experimental_notification::NotificationItem> >::iterator i;
196 for (i = options->items->begin(); i != options->items->end(); ++i) { 224 for (i = options->items->begin(); i != options->items->end(); ++i) {
197 base::DictionaryValue* item = new base::DictionaryValue(); 225 base::DictionaryValue* item = new base::DictionaryValue();
198 item->SetString(ui::notifications::kItemTitleKey, 226 item->SetString(message_center::kItemTitleKey,
199 UTF8ToUTF16(i->get()->title)); 227 UTF8ToUTF16(i->get()->title));
200 item->SetString(ui::notifications::kItemMessageKey, 228 item->SetString(message_center::kItemMessageKey,
201 UTF8ToUTF16(i->get()->message)); 229 UTF8ToUTF16(i->get()->message));
202 items->Append(item); 230 items->Append(item);
203 } 231 }
204 optional_fields->Set(ui::notifications::kItemsKey, items); 232 optional_fields->Set(message_center::kItemsKey, items);
205 } 233 }
206 234
207 NotificationApiDelegate* api_delegate(new NotificationApiDelegate( 235 NotificationApiDelegate* api_delegate(new NotificationApiDelegate(
208 this, 236 this,
209 profile(), 237 profile(),
210 extension_->id(), 238 extension_->id(),
211 id)); // ownership is passed to Notification 239 id)); // ownership is passed to Notification
212 Notification notification(type, extension_->url(), icon_url, title, message, 240 Notification notification(type, extension_->url(), icon_url, title, message,
213 WebKit::WebTextDirectionDefault, 241 WebKit::WebTextDirectionDefault,
214 string16(), UTF8ToUTF16(api_delegate->id()), 242 string16(), UTF8ToUTF16(api_delegate->id()),
215 optional_fields.get(), api_delegate); 243 optional_fields.get(), api_delegate);
216 244
217 g_browser_process->notification_ui_manager()->Add(notification, profile()); 245 g_browser_process->notification_ui_manager()->Add(notification, profile());
218 } 246 }
247 #endif // !defined(ENABLE_MESSAGE_CENTER)
219 248
220 bool NotificationApiFunction::IsNotificationApiEnabled() { 249 bool NotificationApiFunction::IsNotificationApiEnabled() {
221 DesktopNotificationService* service = 250 DesktopNotificationService* service =
222 DesktopNotificationServiceFactory::GetForProfile(profile()); 251 DesktopNotificationServiceFactory::GetForProfile(profile());
223 return service->IsExtensionEnabled(extension_->id()); 252 return service->IsExtensionEnabled(extension_->id());
224 } 253 }
225 254
226 bool NotificationApiFunction::RunImpl() { 255 bool NotificationApiFunction::RunImpl() {
227 if (!IsNotificationApiEnabled()) { 256 if (!IsNotificationApiEnabled()) {
228 SendResponse(false); 257 SendResponse(false);
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
305 CancelById(NotificationApiDelegate::CreateScopedIdentifier( 334 CancelById(NotificationApiDelegate::CreateScopedIdentifier(
306 extension_->id(), params_->notification_id)); 335 extension_->id(), params_->notification_id));
307 336
308 SetResult(Value::CreateBooleanValue(cancel_result)); 337 SetResult(Value::CreateBooleanValue(cancel_result));
309 SendResponse(true); 338 SendResponse(true);
310 339
311 return true; 340 return true;
312 } 341 }
313 342
314 } // namespace extensions 343 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698