Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / extensions / api / notification_provider / notification_provider_api.cc
blob717d71329003f1cd77f6e60438e9048dcf2c4d67
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"
8 #include "base/guid.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/notification.h"
14 #include "chrome/browser/notifications/notification_ui_manager.h"
15 #include "chrome/browser/notifications/notifier_state_tracker.h"
16 #include "chrome/browser/notifications/notifier_state_tracker_factory.h"
17 #include "extensions/browser/event_router.h"
18 #include "extensions/common/extension.h"
19 #include "extensions/common/features/feature.h"
20 #include "ui/base/layout.h"
21 #include "ui/message_center/message_center.h"
22 #include "ui/message_center/notifier_settings.h"
23 #include "url/gurl.h"
25 namespace extensions {
27 NotificationProviderEventRouter::NotificationProviderEventRouter(
28 Profile* profile)
29 : profile_(profile) {
32 NotificationProviderEventRouter::~NotificationProviderEventRouter() {
35 void NotificationProviderEventRouter::CreateNotification(
36 const std::string& notification_provider_id,
37 const std::string& sender_id,
38 const std::string& notification_id,
39 const api::notifications::NotificationOptions& options) {
40 Create(notification_provider_id, sender_id, notification_id, options);
43 void NotificationProviderEventRouter::UpdateNotification(
44 const std::string& notification_provider_id,
45 const std::string& sender_id,
46 const std::string& notification_id,
47 const api::notifications::NotificationOptions& options) {
48 Update(notification_provider_id, sender_id, notification_id, options);
50 void NotificationProviderEventRouter::ClearNotification(
51 const std::string& notification_provider_id,
52 const std::string& sender_id,
53 const std::string& notification_id) {
54 Clear(notification_provider_id, sender_id, notification_id);
57 void NotificationProviderEventRouter::Create(
58 const std::string& notification_provider_id,
59 const std::string& sender_id,
60 const std::string& notification_id,
61 const api::notifications::NotificationOptions& options) {
62 scoped_ptr<base::ListValue> args =
63 api::notification_provider::OnCreated::Create(
64 sender_id, notification_id, options);
66 scoped_ptr<Event> event(new Event(
67 events::NOTIFICATION_PROVIDER_ON_CREATED,
68 api::notification_provider::OnCreated::kEventName, args.Pass()));
70 EventRouter::Get(profile_)
71 ->DispatchEventToExtension(notification_provider_id, event.Pass());
74 void NotificationProviderEventRouter::Update(
75 const std::string& notification_provider_id,
76 const std::string& sender_id,
77 const std::string& notification_id,
78 const api::notifications::NotificationOptions& options) {
79 scoped_ptr<base::ListValue> args =
80 api::notification_provider::OnUpdated::Create(
81 sender_id, notification_id, options);
83 scoped_ptr<Event> event(new Event(
84 events::NOTIFICATION_PROVIDER_ON_UPDATED,
85 api::notification_provider::OnUpdated::kEventName, args.Pass()));
87 EventRouter::Get(profile_)
88 ->DispatchEventToExtension(notification_provider_id, event.Pass());
91 void NotificationProviderEventRouter::Clear(
92 const std::string& notification_provider_id,
93 const std::string& sender_id,
94 const std::string& notification_id) {
95 scoped_ptr<base::ListValue> args =
96 api::notification_provider::OnCleared::Create(sender_id, notification_id);
98 scoped_ptr<Event> event(new Event(
99 events::NOTIFICATION_PROVIDER_ON_CLEARED,
100 api::notification_provider::OnCleared::kEventName, args.Pass()));
102 EventRouter::Get(profile_)
103 ->DispatchEventToExtension(notification_provider_id, event.Pass());
106 NotificationProviderNotifyOnClearedFunction::
107 NotificationProviderNotifyOnClearedFunction() {
110 NotificationProviderNotifyOnClearedFunction::
111 ~NotificationProviderNotifyOnClearedFunction() {
114 ExtensionFunction::ResponseAction
115 NotificationProviderNotifyOnClearedFunction::Run() {
116 scoped_ptr<api::notification_provider::NotifyOnCleared::Params> params =
117 api::notification_provider::NotifyOnCleared::Params::Create(*args_);
118 EXTENSION_FUNCTION_VALIDATE(params.get());
120 const Notification* notification =
121 g_browser_process->notification_ui_manager()->FindById(
122 params->notification_id,
123 NotificationUIManager::GetProfileID(GetProfile()));
125 bool found_notification = notification != NULL;
126 if (found_notification)
127 notification->delegate()->Close(true);
129 return RespondNow(
130 ArgumentList(api::notification_provider::NotifyOnCleared::Results::Create(
131 found_notification)));
134 NotificationProviderNotifyOnClickedFunction::
135 NotificationProviderNotifyOnClickedFunction() {
138 NotificationProviderNotifyOnClickedFunction::
139 ~NotificationProviderNotifyOnClickedFunction() {
142 ExtensionFunction::ResponseAction
143 NotificationProviderNotifyOnClickedFunction::Run() {
144 scoped_ptr<api::notification_provider::NotifyOnClicked::Params> params =
145 api::notification_provider::NotifyOnClicked::Params::Create(*args_);
146 EXTENSION_FUNCTION_VALIDATE(params.get());
148 const Notification* notification =
149 g_browser_process->notification_ui_manager()->FindById(
150 params->notification_id,
151 NotificationUIManager::GetProfileID(GetProfile()));
153 bool found_notification = notification != NULL;
154 if (found_notification)
155 notification->delegate()->Click();
157 return RespondNow(
158 ArgumentList(api::notification_provider::NotifyOnClicked::Results::Create(
159 found_notification)));
162 NotificationProviderNotifyOnButtonClickedFunction::
163 NotificationProviderNotifyOnButtonClickedFunction() {
166 NotificationProviderNotifyOnButtonClickedFunction::
167 ~NotificationProviderNotifyOnButtonClickedFunction() {
170 ExtensionFunction::ResponseAction
171 NotificationProviderNotifyOnButtonClickedFunction::Run() {
172 scoped_ptr<api::notification_provider::NotifyOnButtonClicked::Params> params =
173 api::notification_provider::NotifyOnButtonClicked::Params::Create(*args_);
174 EXTENSION_FUNCTION_VALIDATE(params.get());
176 const Notification* notification =
177 g_browser_process->notification_ui_manager()->FindById(
178 params->notification_id,
179 NotificationUIManager::GetProfileID(GetProfile()));
181 bool found_notification = notification != NULL;
182 if (found_notification)
183 notification->delegate()->ButtonClick(params->button_index);
185 return RespondNow(ArgumentList(
186 api::notification_provider::NotifyOnButtonClicked::Results::Create(
187 found_notification)));
190 NotificationProviderNotifyOnPermissionLevelChangedFunction::
191 NotificationProviderNotifyOnPermissionLevelChangedFunction() {
194 NotificationProviderNotifyOnPermissionLevelChangedFunction::
195 ~NotificationProviderNotifyOnPermissionLevelChangedFunction() {
198 ExtensionFunction::ResponseAction
199 NotificationProviderNotifyOnPermissionLevelChangedFunction::Run() {
200 scoped_ptr<api::notification_provider::NotifyOnPermissionLevelChanged::Params>
201 params = api::notification_provider::NotifyOnPermissionLevelChanged::
202 Params::Create(*args_);
203 EXTENSION_FUNCTION_VALIDATE(params.get());
205 // Third party apps/extensions with notification provider API will not be able
206 // to change permission levels of web notifiers, because the list of allowed
207 // websites should only be set in Chrome Settings manually by users. But they
208 // are able to change permission levels of application type notifiers.
209 bool is_application_type =
210 (params->notifier_type ==
211 api::notification_provider::NotifierType::NOTIFIER_TYPE_APPLICATION);
212 if (is_application_type) {
213 bool enabled =
214 (params->level == api::notification_provider::NotifierPermissionLevel::
215 NOTIFIER_PERMISSION_LEVEL_GRANTED);
217 NotifierStateTracker* notifier_state_tracker =
218 NotifierStateTrackerFactory::GetForProfile(GetProfile());
220 message_center::NotifierId notifier_id(
221 message_center::NotifierId::NotifierType::APPLICATION,
222 params->notifier_id);
224 notifier_state_tracker->SetNotifierEnabled(notifier_id, enabled);
227 return RespondNow(
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);
263 } else {
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