blob: f34784a1ef697e439ee2395dbb1a43342b830c11 [file] [log] [blame]
peter59d19e2c2015-05-29 13:30:501// Copyright 2015 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#ifndef CONTENT_BROWSER_NOTIFICATIONS_NOTIFICATION_ID_GENERATOR_H_
6#define CONTENT_BROWSER_NOTIFICATIONS_NOTIFICATION_ID_GENERATOR_H_
7
8#include <stdint.h>
9#include <string>
10
Peter Beverlood9c512d42017-11-30 16:48:5111#include "base/macros.h"
peter00fed172016-05-05 16:01:0712#include "base/strings/string_piece.h"
peter59d19e2c2015-05-29 13:30:5013#include "content/common/content_export.h"
14
15class GURL;
16
17namespace content {
18
peter59d19e2c2015-05-29 13:30:5019// Generates deterministic notification ids for Web Notifications.
20//
Peter Beverlood9c512d42017-11-30 16:48:5121// The notification id must be deterministic for a given origin and tag, when
22// the tag is non-empty, or unique for the given notification when the tag is
23// empty. For non-persistent notifications, the uniqueness will be based on the
24// render process id. For persistent notifications, the generated id will be
25// globally unique for the lifetime of the notification database.
peter59d19e2c2015-05-29 13:30:5026//
27// Notifications coming from the same origin and having the same tag will result
28// in the same notification id being generated. This id may then be used to
29// update the notification in the platform notification service.
30//
31// The notification id will be used by the notification service for determining
32// when to replace notifications, and as the unique identifier when a
33// notification has to be closed programmatically.
34//
35// It is important to note that, for persistent notifications, the generated
36// notification id can outlive the browser process responsible for creating it.
Peter Beverlood9c512d42017-11-30 16:48:5137//
38// Note that the PlatformNotificationService is expected to handle
39// distinguishing identical generated ids from different browser contexts.
peter59d19e2c2015-05-29 13:30:5040class CONTENT_EXPORT NotificationIdGenerator {
41 public:
Peter Beverlood9c512d42017-11-30 16:48:5142 NotificationIdGenerator() = default;
peter59d19e2c2015-05-29 13:30:5043
peter00fed172016-05-05 16:01:0744 // Returns whether |notification_id| belongs to a persistent notification.
45 static bool IsPersistentNotification(
46 const base::StringPiece& notification_id);
47
48 // Returns whether |notification_id| belongs to a non-persistent notification.
49 static bool IsNonPersistentNotification(
50 const base::StringPiece& notification_id);
51
peter59d19e2c2015-05-29 13:30:5052 // Generates an id for a persistent notification given the notification's
53 // origin, tag and persistent notification id. The persistent notification id
54 // will have been created by the persistent notification database.
55 std::string GenerateForPersistentNotification(
56 const GURL& origin,
57 const std::string& tag,
58 int64_t persistent_notification_id) const;
59
60 // Generates an id for a non-persistent notification given the notification's
Anita Woodrufffd1fa7e2017-12-18 16:34:1461 // origin, tag and request id. The request id must've been created by the
62 // |render_process_id|.
63 std::string GenerateForNonPersistentNotification(const GURL& origin,
64 const std::string& tag,
65 int request_id,
66 int render_process_id) const;
peter59d19e2c2015-05-29 13:30:5067
68 private:
Peter Beverlood9c512d42017-11-30 16:48:5169 DISALLOW_COPY_AND_ASSIGN(NotificationIdGenerator);
peter59d19e2c2015-05-29 13:30:5070};
71
72} // namespace context
73
74#endif // CONTENT_BROWSER_NOTIFICATIONS_NOTIFICATION_ID_GENERATOR_H_