peter | 59d19e2c | 2015-05-29 13:30:50 | [diff] [blame] | 1 | // 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 Beverloo | d9c512d4 | 2017-11-30 16:48:51 | [diff] [blame] | 11 | #include "base/macros.h" |
peter | 00fed17 | 2016-05-05 16:01:07 | [diff] [blame] | 12 | #include "base/strings/string_piece.h" |
peter | 59d19e2c | 2015-05-29 13:30:50 | [diff] [blame] | 13 | #include "content/common/content_export.h" |
| 14 | |
| 15 | class GURL; |
| 16 | |
| 17 | namespace content { |
| 18 | |
peter | 59d19e2c | 2015-05-29 13:30:50 | [diff] [blame] | 19 | // Generates deterministic notification ids for Web Notifications. |
| 20 | // |
Peter Beverloo | d9c512d4 | 2017-11-30 16:48:51 | [diff] [blame] | 21 | // 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. |
peter | 59d19e2c | 2015-05-29 13:30:50 | [diff] [blame] | 26 | // |
| 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 Beverloo | d9c512d4 | 2017-11-30 16:48:51 | [diff] [blame] | 37 | // |
| 38 | // Note that the PlatformNotificationService is expected to handle |
| 39 | // distinguishing identical generated ids from different browser contexts. |
Mugdha Lakhani | 3e41705 | 2018-01-12 17:48:01 | [diff] [blame] | 40 | // |
| 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 |
peter | 59d19e2c | 2015-05-29 13:30:50 | [diff] [blame] | 45 | class CONTENT_EXPORT NotificationIdGenerator { |
| 46 | public: |
Peter Beverloo | d9c512d4 | 2017-11-30 16:48:51 | [diff] [blame] | 47 | NotificationIdGenerator() = default; |
peter | 59d19e2c | 2015-05-29 13:30:50 | [diff] [blame] | 48 | |
peter | 00fed17 | 2016-05-05 16:01:07 | [diff] [blame] | 49 | // 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 | |
peter | 59d19e2c | 2015-05-29 13:30:50 | [diff] [blame] | 57 | // 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 Woodruff | fd1fa7e | 2017-12-18 16:34:14 | [diff] [blame] | 66 | // 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; |
peter | 59d19e2c | 2015-05-29 13:30:50 | [diff] [blame] | 72 | |
| 73 | private: |
Peter Beverloo | d9c512d4 | 2017-11-30 16:48:51 | [diff] [blame] | 74 | DISALLOW_COPY_AND_ASSIGN(NotificationIdGenerator); |
peter | 59d19e2c | 2015-05-29 13:30:50 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | } // namespace context |
| 78 | |
| 79 | #endif // CONTENT_BROWSER_NOTIFICATIONS_NOTIFICATION_ID_GENERATOR_H_ |