blob: ecba5f58c09e905eb8376012507fff57b092000c [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.
Mugdha Lakhani3e417052018-01-12 17:48:0140//
41// Also note that several functions in NotificationPlatformBridge class
42// rely on the format of the notification generated here.
43// Code: chrome/android/java/src/org/chromium/chrome/browser/notifications/
44// NotificationPlatformBridge.java
peter59d19e2c2015-05-29 13:30:5045class CONTENT_EXPORT NotificationIdGenerator {
46 public:
Peter Beverlood9c512d42017-11-30 16:48:5147 NotificationIdGenerator() = default;
peter59d19e2c2015-05-29 13:30:5048
peter00fed172016-05-05 16:01:0749 // Returns whether |notification_id| belongs to a persistent notification.
50 static bool IsPersistentNotification(
51 const base::StringPiece& notification_id);
52
53 // Returns whether |notification_id| belongs to a non-persistent notification.
54 static bool IsNonPersistentNotification(
55 const base::StringPiece& notification_id);
56
peter59d19e2c2015-05-29 13:30:5057 // Generates an id for a persistent notification given the notification's
58 // origin, tag and persistent notification id. The persistent notification id
59 // will have been created by the persistent notification database.
60 std::string GenerateForPersistentNotification(
61 const GURL& origin,
62 const std::string& tag,
63 int64_t persistent_notification_id) const;
64
65 // Generates an id for a non-persistent notification given the notification's
Anita Woodrufffd1fa7e2017-12-18 16:34:1466 // origin, tag and request id. The request id must've been created by the
67 // |render_process_id|.
68 std::string GenerateForNonPersistentNotification(const GURL& origin,
69 const std::string& tag,
70 int request_id,
71 int render_process_id) const;
peter59d19e2c2015-05-29 13:30:5072
73 private:
Peter Beverlood9c512d42017-11-30 16:48:5174 DISALLOW_COPY_AND_ASSIGN(NotificationIdGenerator);
peter59d19e2c2015-05-29 13:30:5075};
76
77} // namespace context
78
79#endif // CONTENT_BROWSER_NOTIFICATIONS_NOTIFICATION_ID_GENERATOR_H_