1 // Copyright 2014 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.
5 #include "chrome/browser/extensions/api/notification_provider/notification_provider_api.h"
7 #include "base/callback.h"
9 #include "base/rand_util.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/notifications/desktop_notification_service.h"
14 #include "chrome/browser/notifications/desktop_notification_service_factory.h"
15 #include "chrome/browser/notifications/notification.h"
16 #include "chrome/browser/notifications/notification_ui_manager.h"
17 #include "chrome/common/chrome_version_info.h"
18 #include "extensions/browser/event_router.h"
19 #include "extensions/common/extension.h"
20 #include "extensions/common/features/feature.h"
21 #include "ui/base/layout.h"
22 #include "ui/message_center/message_center.h"
23 #include "ui/message_center/notifier_settings.h"
26 namespace extensions
{
28 NotificationProviderEventRouter::NotificationProviderEventRouter(
33 NotificationProviderEventRouter::~NotificationProviderEventRouter() {
36 void NotificationProviderEventRouter::CreateNotification(
37 const std::string
& notification_provider_id
,
38 const std::string
& sender_id
,
39 const std::string
& notification_id
,
40 const api::notifications::NotificationOptions
& options
) {
41 Create(notification_provider_id
, sender_id
, notification_id
, options
);
44 void NotificationProviderEventRouter::UpdateNotification(
45 const std::string
& notification_provider_id
,
46 const std::string
& sender_id
,
47 const std::string
& notification_id
,
48 const api::notifications::NotificationOptions
& options
) {
49 Update(notification_provider_id
, sender_id
, notification_id
, options
);
51 void NotificationProviderEventRouter::ClearNotification(
52 const std::string
& notification_provider_id
,
53 const std::string
& sender_id
,
54 const std::string
& notification_id
) {
55 Clear(notification_provider_id
, sender_id
, notification_id
);
58 void NotificationProviderEventRouter::Create(
59 const std::string
& notification_provider_id
,
60 const std::string
& sender_id
,
61 const std::string
& notification_id
,
62 const api::notifications::NotificationOptions
& options
) {
63 scoped_ptr
<base::ListValue
> args
=
64 api::notification_provider::OnCreated::Create(
65 sender_id
, notification_id
, options
);
67 scoped_ptr
<Event
> event(new Event(
68 events::NOTIFICATION_PROVIDER_ON_CREATED
,
69 api::notification_provider::OnCreated::kEventName
, args
.Pass()));
71 EventRouter::Get(profile_
)
72 ->DispatchEventToExtension(notification_provider_id
, event
.Pass());
75 void NotificationProviderEventRouter::Update(
76 const std::string
& notification_provider_id
,
77 const std::string
& sender_id
,
78 const std::string
& notification_id
,
79 const api::notifications::NotificationOptions
& options
) {
80 scoped_ptr
<base::ListValue
> args
=
81 api::notification_provider::OnUpdated::Create(
82 sender_id
, notification_id
, options
);
84 scoped_ptr
<Event
> event(new Event(
85 events::NOTIFICATION_PROVIDER_ON_UPDATED
,
86 api::notification_provider::OnUpdated::kEventName
, args
.Pass()));
88 EventRouter::Get(profile_
)
89 ->DispatchEventToExtension(notification_provider_id
, event
.Pass());
92 void NotificationProviderEventRouter::Clear(
93 const std::string
& notification_provider_id
,
94 const std::string
& sender_id
,
95 const std::string
& notification_id
) {
96 scoped_ptr
<base::ListValue
> args
=
97 api::notification_provider::OnCleared::Create(sender_id
, notification_id
);
99 scoped_ptr
<Event
> event(new Event(
100 events::NOTIFICATION_PROVIDER_ON_CLEARED
,
101 api::notification_provider::OnCleared::kEventName
, args
.Pass()));
103 EventRouter::Get(profile_
)
104 ->DispatchEventToExtension(notification_provider_id
, event
.Pass());
107 NotificationProviderNotifyOnClearedFunction::
108 NotificationProviderNotifyOnClearedFunction() {
111 NotificationProviderNotifyOnClearedFunction::
112 ~NotificationProviderNotifyOnClearedFunction() {
115 ExtensionFunction::ResponseAction
116 NotificationProviderNotifyOnClearedFunction::Run() {
117 scoped_ptr
<api::notification_provider::NotifyOnCleared::Params
> params
=
118 api::notification_provider::NotifyOnCleared::Params::Create(*args_
);
119 EXTENSION_FUNCTION_VALIDATE(params
.get());
121 const Notification
* notification
=
122 g_browser_process
->notification_ui_manager()->FindById(
123 params
->notification_id
,
124 NotificationUIManager::GetProfileID(GetProfile()));
126 bool found_notification
= notification
!= NULL
;
127 if (found_notification
)
128 notification
->delegate()->Close(true);
131 ArgumentList(api::notification_provider::NotifyOnCleared::Results::Create(
132 found_notification
)));
135 NotificationProviderNotifyOnClickedFunction::
136 NotificationProviderNotifyOnClickedFunction() {
139 NotificationProviderNotifyOnClickedFunction::
140 ~NotificationProviderNotifyOnClickedFunction() {
143 ExtensionFunction::ResponseAction
144 NotificationProviderNotifyOnClickedFunction::Run() {
145 scoped_ptr
<api::notification_provider::NotifyOnClicked::Params
> params
=
146 api::notification_provider::NotifyOnClicked::Params::Create(*args_
);
147 EXTENSION_FUNCTION_VALIDATE(params
.get());
149 const Notification
* notification
=
150 g_browser_process
->notification_ui_manager()->FindById(
151 params
->notification_id
,
152 NotificationUIManager::GetProfileID(GetProfile()));
154 bool found_notification
= notification
!= NULL
;
155 if (found_notification
)
156 notification
->delegate()->Click();
159 ArgumentList(api::notification_provider::NotifyOnClicked::Results::Create(
160 found_notification
)));
163 NotificationProviderNotifyOnButtonClickedFunction::
164 NotificationProviderNotifyOnButtonClickedFunction() {
167 NotificationProviderNotifyOnButtonClickedFunction::
168 ~NotificationProviderNotifyOnButtonClickedFunction() {
171 ExtensionFunction::ResponseAction
172 NotificationProviderNotifyOnButtonClickedFunction::Run() {
173 scoped_ptr
<api::notification_provider::NotifyOnButtonClicked::Params
> params
=
174 api::notification_provider::NotifyOnButtonClicked::Params::Create(*args_
);
175 EXTENSION_FUNCTION_VALIDATE(params
.get());
177 const Notification
* notification
=
178 g_browser_process
->notification_ui_manager()->FindById(
179 params
->notification_id
,
180 NotificationUIManager::GetProfileID(GetProfile()));
182 bool found_notification
= notification
!= NULL
;
183 if (found_notification
)
184 notification
->delegate()->ButtonClick(params
->button_index
);
186 return RespondNow(ArgumentList(
187 api::notification_provider::NotifyOnButtonClicked::Results::Create(
188 found_notification
)));
191 NotificationProviderNotifyOnPermissionLevelChangedFunction::
192 NotificationProviderNotifyOnPermissionLevelChangedFunction() {
195 NotificationProviderNotifyOnPermissionLevelChangedFunction::
196 ~NotificationProviderNotifyOnPermissionLevelChangedFunction() {
199 ExtensionFunction::ResponseAction
200 NotificationProviderNotifyOnPermissionLevelChangedFunction::Run() {
201 scoped_ptr
<api::notification_provider::NotifyOnPermissionLevelChanged::Params
>
202 params
= api::notification_provider::NotifyOnPermissionLevelChanged::
203 Params::Create(*args_
);
204 EXTENSION_FUNCTION_VALIDATE(params
.get());
206 // Third party apps/extensions with notification provider API will not be able
207 // to change permission levels of web notifiers, because the list of allowed
208 // websites should only be set in Chrome Settings manually by users. But they
209 // are able to change permission levels of application type notifiers.
210 bool is_application_type
=
211 (params
->notifier_type
==
212 api::notification_provider::NotifierType::NOTIFIER_TYPE_APPLICATION
);
213 if (is_application_type
) {
215 (params
->level
== api::notification_provider::NotifierPermissionLevel::
216 NOTIFIER_PERMISSION_LEVEL_GRANTED
);
218 DesktopNotificationService
* desktop_notification_service
=
219 DesktopNotificationServiceFactory::GetForProfile(GetProfile());
220 message_center::NotifierId
notifier_id(
221 message_center::NotifierId::NotifierType::APPLICATION
,
222 params
->notifier_id
);
224 desktop_notification_service
->SetNotifierEnabled(notifier_id
, enabled
);
228 ArgumentList(api::notification_provider::NotifyOnPermissionLevelChanged::
229 Results::Create(is_application_type
)));
232 NotificationProviderNotifyOnShowSettingsFunction::
233 NotificationProviderNotifyOnShowSettingsFunction() {
236 NotificationProviderNotifyOnShowSettingsFunction::
237 ~NotificationProviderNotifyOnShowSettingsFunction() {
240 ExtensionFunction::ResponseAction
241 NotificationProviderNotifyOnShowSettingsFunction::Run() {
242 scoped_ptr
<api::notification_provider::NotifyOnShowSettings::Params
> params
=
243 api::notification_provider::NotifyOnShowSettings::Params::Create(*args_
);
244 EXTENSION_FUNCTION_VALIDATE(params
.get());
246 bool has_advanced_settings
;
247 // Only application type notifiers have advanced settings.
248 if (params
->notifier_type
==
249 api::notification_provider::NotifierType::NOTIFIER_TYPE_APPLICATION
) {
250 // TODO(dewittj): Refactor NotificationUIManage API to have a getter of
251 // NotifierSettingsProvider, since it holds the settings provider.
252 message_center::NotifierSettingsProvider
* settings_provider
=
253 message_center::MessageCenter::Get()->GetNotifierSettingsProvider();
255 message_center::NotifierId
notifier_id(
256 message_center::NotifierId::NotifierType::APPLICATION
,
257 params
->notifier_id
);
259 has_advanced_settings
=
260 settings_provider
->NotifierHasAdvancedSettings(notifier_id
);
261 if (has_advanced_settings
)
262 settings_provider
->OnNotifierAdvancedSettingsRequested(notifier_id
, NULL
);
264 has_advanced_settings
= false;
267 return RespondNow(ArgumentList(
268 api::notification_provider::NotifyOnShowSettings::Results::Create(
269 has_advanced_settings
)));
272 NotificationProviderGetNotifierFunction::
273 NotificationProviderGetNotifierFunction() {
276 NotificationProviderGetNotifierFunction::
277 ~NotificationProviderGetNotifierFunction() {
280 ExtensionFunction::ResponseAction
281 NotificationProviderGetNotifierFunction::Run() {
282 api::notification_provider::Notifier notifier
;
284 return RespondNow(ArgumentList(
285 api::notification_provider::GetNotifier::Results::Create(notifier
)));
288 NotificationProviderGetAllNotifiersFunction::
289 NotificationProviderGetAllNotifiersFunction() {
292 NotificationProviderGetAllNotifiersFunction::
293 ~NotificationProviderGetAllNotifiersFunction() {
296 ExtensionFunction::ResponseAction
297 NotificationProviderGetAllNotifiersFunction::Run() {
298 std::vector
<linked_ptr
<api::notification_provider::Notifier
> > notifiers
;
300 return RespondNow(ArgumentList(
301 api::notification_provider::GetAllNotifiers::Results::Create(notifiers
)));
304 } // namespace extensions