summaryrefslogtreecommitdiff
path: root/chromium/content/browser/notifications/notification_message_filter.cc
blob: 518fdf079911d9942dad20052fc7450d2b08673b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/notifications/notification_message_filter.h"

#include <utility>

#include "base/callback.h"
#include "base/command_line.h"
#include "base/feature_list.h"
#include "content/browser/bad_message.h"
#include "content/browser/notifications/notification_event_dispatcher_impl.h"
#include "content/browser/notifications/notification_id_generator.h"
#include "content/browser/notifications/platform_notification_context_impl.h"
#include "content/browser/service_worker/service_worker_context_wrapper.h"
#include "content/common/platform_notification_messages.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/browser/notification_database_data.h"
#include "content/public/browser/platform_notification_service.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/common/content_client.h"
#include "content/public/common/content_features.h"
#include "content/public/common/content_switches.h"
#include "third_party/blink/public/platform/modules/notifications/web_notification_constants.h"

namespace content {

namespace {

const int kMinimumVibrationDurationMs = 1;      // 1 millisecond
const int kMaximumVibrationDurationMs = 10000;  // 10 seconds

PlatformNotificationData SanitizeNotificationData(
    const PlatformNotificationData& notification_data) {
  PlatformNotificationData sanitized_data = notification_data;

  // Make sure that the vibration values are within reasonable bounds.
  for (int& pattern : sanitized_data.vibration_pattern) {
    pattern = std::min(kMaximumVibrationDurationMs,
                       std::max(kMinimumVibrationDurationMs, pattern));
  }

  // Ensure there aren't more actions than supported.
  if (sanitized_data.actions.size() > blink::kWebNotificationMaxActions)
    sanitized_data.actions.resize(blink::kWebNotificationMaxActions);

  return sanitized_data;
}

// Returns true when |resources| looks ok, false otherwise.
bool ValidateNotificationResources(const NotificationResources& resources) {
  if (!resources.image.drawsNothing() &&
      !base::FeatureList::IsEnabled(features::kNotificationContentImage)) {
    return false;
  }
  if (resources.image.width() > blink::kWebNotificationMaxImageWidthPx ||
      resources.image.height() > blink::kWebNotificationMaxImageHeightPx) {
    return false;
  }
  if (resources.notification_icon.width() >
          blink::kWebNotificationMaxIconSizePx ||
      resources.notification_icon.height() >
          blink::kWebNotificationMaxIconSizePx) {
    return false;
  }
  if (resources.badge.width() > blink::kWebNotificationMaxBadgeSizePx ||
      resources.badge.height() > blink::kWebNotificationMaxBadgeSizePx) {
    return false;
  }
  for (const auto& action_icon : resources.action_icons) {
    if (action_icon.width() > blink::kWebNotificationMaxActionIconSizePx ||
        action_icon.height() > blink::kWebNotificationMaxActionIconSizePx) {
      return false;
    }
  }
  return true;
}

}  // namespace

NotificationMessageFilter::NotificationMessageFilter(
    int process_id,
    PlatformNotificationContextImpl* notification_context,
    ResourceContext* resource_context,
    const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context,
    BrowserContext* browser_context)
    : BrowserMessageFilter(PlatformNotificationMsgStart),
      process_id_(process_id),
      notification_context_(notification_context),
      resource_context_(resource_context),
      service_worker_context_(service_worker_context),
      browser_context_(browser_context),
      weak_factory_io_(this) {}

NotificationMessageFilter::~NotificationMessageFilter() = default;

void NotificationMessageFilter::OnDestruct() const {
  BrowserThread::DeleteOnIOThread::Destruct(this);
}

bool NotificationMessageFilter::OnMessageReceived(const IPC::Message& message) {
  bool handled = true;
  IPC_BEGIN_MESSAGE_MAP(NotificationMessageFilter, message)
    IPC_MESSAGE_HANDLER(PlatformNotificationHostMsg_ShowPersistent,
                        OnShowPersistentNotification)
    IPC_MESSAGE_HANDLER(PlatformNotificationHostMsg_GetNotifications,
                        OnGetNotifications)
    IPC_MESSAGE_HANDLER(PlatformNotificationHostMsg_ClosePersistent,
                        OnClosePersistentNotification)
    IPC_MESSAGE_UNHANDLED(handled = false)
  IPC_END_MESSAGE_MAP()

  return handled;
}

void NotificationMessageFilter::OnShowPersistentNotification(
    int request_id,
    int64_t service_worker_registration_id,
    const GURL& origin,
    const PlatformNotificationData& notification_data,
    const NotificationResources& notification_resources) {
  DCHECK_CURRENTLY_ON(BrowserThread::IO);
  if (GetPermissionForOriginOnIO(origin) !=
      blink::mojom::PermissionStatus::GRANTED) {
    // We can't assume that the renderer is compromised at this point because
    // it's possible for the user to revoke an origin's permission between the
    // time where a website requests the notification to be shown and the call
    // arriving in the message filter.
    return;
  }

  if (!ValidateNotificationResources(notification_resources)) {
    bad_message::ReceivedBadMessage(this, bad_message::NMF_INVALID_ARGUMENT);
    return;
  }

  NotificationDatabaseData database_data;
  database_data.origin = origin;
  database_data.service_worker_registration_id = service_worker_registration_id;

  PlatformNotificationData sanitized_notification_data =
      SanitizeNotificationData(notification_data);
  database_data.notification_data = sanitized_notification_data;

  notification_context_->WriteNotificationData(
      origin, database_data,
      base::Bind(&NotificationMessageFilter::DidWritePersistentNotificationData,
                 weak_factory_io_.GetWeakPtr(), request_id,
                 service_worker_registration_id, origin,
                 sanitized_notification_data, notification_resources));
}

void NotificationMessageFilter::DidWritePersistentNotificationData(
    int request_id,
    int64_t service_worker_registration_id,
    const GURL& origin,
    const PlatformNotificationData& notification_data,
    const NotificationResources& notification_resources,
    bool success,
    const std::string& notification_id) {
  DCHECK_CURRENTLY_ON(BrowserThread::IO);

  if (!success) {
    Send(new PlatformNotificationMsg_DidShowPersistent(request_id, false));
    return;
  }

  // Get the service worker scope.
  service_worker_context_->FindReadyRegistrationForId(
      service_worker_registration_id, origin,
      base::BindOnce(
          &NotificationMessageFilter::DidFindServiceWorkerRegistration,
          weak_factory_io_.GetWeakPtr(), request_id, origin, notification_data,
          notification_resources, notification_id));
}

void NotificationMessageFilter::DidFindServiceWorkerRegistration(
    int request_id,
    const GURL& origin,
    const PlatformNotificationData& notification_data,
    const NotificationResources& notification_resources,
    const std::string& notification_id,
    content::ServiceWorkerStatusCode service_worker_status,
    scoped_refptr<content::ServiceWorkerRegistration> registration) {
  DCHECK_CURRENTLY_ON(BrowserThread::IO);

  if (service_worker_status != SERVICE_WORKER_OK) {
    Send(new PlatformNotificationMsg_DidShowPersistent(request_id, false));
    LOG(ERROR) << "Registration not found for " << origin.spec();
    // TODO(peter): Add UMA to track how often this occurs.
    return;
  }

  PlatformNotificationService* service =
      GetContentClient()->browser()->GetPlatformNotificationService();
  DCHECK(service);

  BrowserThread::PostTask(
      BrowserThread::UI, FROM_HERE,
      base::BindOnce(
          &PlatformNotificationService::DisplayPersistentNotification,
          base::Unretained(service),  // The service is a singleton.
          browser_context_, notification_id, registration->pattern(), origin,
          notification_data, notification_resources));

  Send(new PlatformNotificationMsg_DidShowPersistent(request_id, true));
}

void NotificationMessageFilter::OnGetNotifications(
    int request_id,
    int64_t service_worker_registration_id,
    const GURL& origin,
    const std::string& filter_tag) {
  DCHECK_CURRENTLY_ON(BrowserThread::IO);
  if (GetPermissionForOriginOnIO(origin) !=
      blink::mojom::PermissionStatus::GRANTED) {
    // No permission has been granted for the given origin. It is harmless to
    // try to get notifications without permission, so return an empty vector
    // indicating that no (accessible) notifications exist at this time.
    Send(new PlatformNotificationMsg_DidGetNotifications(
        request_id, std::vector<PersistentNotificationInfo>()));
    return;
  }

  notification_context_->ReadAllNotificationDataForServiceWorkerRegistration(
      origin, service_worker_registration_id,
      base::Bind(&NotificationMessageFilter::DidGetNotifications,
                 weak_factory_io_.GetWeakPtr(), request_id, filter_tag));
}

void NotificationMessageFilter::DidGetNotifications(
    int request_id,
    const std::string& filter_tag,
    bool success,
    const std::vector<NotificationDatabaseData>& notifications) {
  std::vector<PersistentNotificationInfo> persistent_notifications;
  for (const NotificationDatabaseData& database_data : notifications) {
    if (!filter_tag.empty()) {
      const std::string& tag = database_data.notification_data.tag;
      if (tag != filter_tag)
        continue;
    }

    persistent_notifications.push_back(std::make_pair(
        database_data.notification_id, database_data.notification_data));
  }

  Send(new PlatformNotificationMsg_DidGetNotifications(
      request_id, persistent_notifications));
}

void NotificationMessageFilter::OnClosePersistentNotification(
    const GURL& origin,
    const std::string& tag,
    const std::string& notification_id) {
  DCHECK_CURRENTLY_ON(BrowserThread::IO);
  if (GetPermissionForOriginOnIO(origin) !=
      blink::mojom::PermissionStatus::GRANTED) {
    return;
  }

  PlatformNotificationService* service =
      GetContentClient()->browser()->GetPlatformNotificationService();
  DCHECK(service);

  // There's no point in waiting until the database data has been removed before
  // closing the notification presented to the user. Post that task immediately.
  BrowserThread::PostTask(
      BrowserThread::UI, FROM_HERE,
      base::BindOnce(&PlatformNotificationService::ClosePersistentNotification,
                     base::Unretained(service),  // The service is a singleton.
                     browser_context_, notification_id));

  notification_context_->DeleteNotificationData(
      notification_id, origin,
      base::Bind(
          &NotificationMessageFilter::DidDeletePersistentNotificationData,
          weak_factory_io_.GetWeakPtr()));
}

void NotificationMessageFilter::DidDeletePersistentNotificationData(
    bool success) {
  DCHECK_CURRENTLY_ON(BrowserThread::IO);
  // TODO(peter): Consider feeding back to the renderer that the notification
  // has been closed.
}

blink::mojom::PermissionStatus
NotificationMessageFilter::GetPermissionForOriginOnIO(
    const GURL& origin) const {
  DCHECK_CURRENTLY_ON(BrowserThread::IO);

  PlatformNotificationService* service =
      GetContentClient()->browser()->GetPlatformNotificationService();
  if (!service)
    return blink::mojom::PermissionStatus::DENIED;

  return service->CheckPermissionOnIOThread(resource_context_, origin,
                                            process_id_);
}

bool NotificationMessageFilter::VerifyNotificationPermissionGranted(
    PlatformNotificationService* service,
    const GURL& origin) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);

  blink::mojom::PermissionStatus permission_status =
      service->CheckPermissionOnUIThread(browser_context_, origin, process_id_);

  // We can't assume that the renderer is compromised at this point because
  // it's possible for the user to revoke an origin's permission between the
  // time where a website requests the notification to be shown and the call
  // arriving in the message filter.

  return permission_status == blink::mojom::PermissionStatus::GRANTED;
}

NotificationIdGenerator* NotificationMessageFilter::GetNotificationIdGenerator()
    const {
  return notification_context_->notification_id_generator();
}

}  // namespace content