OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/notifications/notification_id_generator.h" | 5 #include "content/browser/notifications/notification_id_generator.h" |
6 | 6 |
7 #include <sstream> | 7 #include <sstream> |
8 | 8 |
9 #include "base/base64.h" | 9 #include "base/base64.h" |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/sha1.h" | 12 #include "base/sha1.h" |
13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
15 #include "build/build_config.h" | 15 #include "build/build_config.h" |
16 #include "content/public/browser/browser_context.h" | 16 #include "content/public/browser/browser_context.h" |
| 17 #include "content/public/browser/notification_id_verifier.h" |
17 #include "url/gurl.h" | 18 #include "url/gurl.h" |
18 | 19 |
19 namespace content { | 20 namespace content { |
20 namespace { | |
21 | 21 |
22 const char kPersistentNotificationPrefix[] = "p:"; | 22 const char kPersistentNotificationPrefix[] = "p:"; |
23 const char kNonPersistentNotificationPrefix[] = "n:"; | 23 const char kNonPersistentNotificationPrefix[] = "n:"; |
24 | 24 |
| 25 namespace { |
25 const char kSeparator = '#'; | 26 const char kSeparator = '#'; |
26 | 27 |
27 // Computes a hash based on the path in which the |browser_context| is stored. | 28 // Computes a hash based on the path in which the |browser_context| is stored. |
28 // Since we only store the hash, SHA-1 is used to make the probability of | 29 // Since we only store the hash, SHA-1 is used to make the probability of |
29 // collisions negligible. | 30 // collisions negligible. |
30 std::string ComputeBrowserContextHash(BrowserContext* browser_context) { | 31 std::string ComputeBrowserContextHash(BrowserContext* browser_context) { |
31 const base::FilePath path = browser_context->GetPath(); | 32 const base::FilePath path = browser_context->GetPath(); |
32 | 33 |
33 #if defined(OS_WIN) | 34 #if defined(OS_WIN) |
34 std::string path_hash = base::SHA1HashString(base::WideToUTF8(path.value())); | 35 std::string path_hash = base::SHA1HashString(base::WideToUTF8(path.value())); |
35 #else | 36 #else |
36 std::string path_hash = base::SHA1HashString(path.value()); | 37 std::string path_hash = base::SHA1HashString(path.value()); |
37 #endif | 38 #endif |
38 | 39 |
39 return base::HexEncode(path_hash.c_str(), path_hash.length()); | 40 return base::HexEncode(path_hash.c_str(), path_hash.length()); |
40 } | 41 } |
41 | 42 |
42 } // namespace | 43 } // namespace |
43 | 44 |
| 45 // NotificationIdVerifier implementation |
| 46 |
| 47 // static |
| 48 bool NotificationIdVerifier::IsPersistentNotification( |
| 49 const base::StringPiece& notification_id) { |
| 50 return notification_id.starts_with(kPersistentNotificationPrefix); |
| 51 } |
| 52 |
| 53 // static |
| 54 bool NotificationIdVerifier::IsNonPersistentNotification( |
| 55 const base::StringPiece& notification_id) { |
| 56 return notification_id.starts_with(kNonPersistentNotificationPrefix); |
| 57 } |
| 58 |
44 NotificationIdGenerator::NotificationIdGenerator( | 59 NotificationIdGenerator::NotificationIdGenerator( |
45 BrowserContext* browser_context) | 60 BrowserContext* browser_context) |
46 : browser_context_(browser_context) {} | 61 : browser_context_(browser_context) {} |
47 | 62 |
48 NotificationIdGenerator::~NotificationIdGenerator() {} | 63 NotificationIdGenerator::~NotificationIdGenerator() {} |
49 | 64 |
50 // static | |
51 bool NotificationIdGenerator::IsPersistentNotification( | |
52 const base::StringPiece& notification_id) { | |
53 return notification_id.starts_with(kPersistentNotificationPrefix); | |
54 } | |
55 | |
56 // static | |
57 bool NotificationIdGenerator::IsNonPersistentNotification( | |
58 const base::StringPiece& notification_id) { | |
59 return notification_id.starts_with(kNonPersistentNotificationPrefix); | |
60 } | |
61 | |
62 std::string NotificationIdGenerator::GenerateForPersistentNotification( | 65 std::string NotificationIdGenerator::GenerateForPersistentNotification( |
63 const GURL& origin, | 66 const GURL& origin, |
64 const std::string& tag, | 67 const std::string& tag, |
65 int64_t persistent_notification_id) const { | 68 int64_t persistent_notification_id) const { |
66 DCHECK(origin.is_valid()); | 69 DCHECK(origin.is_valid()); |
67 DCHECK_EQ(origin, origin.GetOrigin()); | 70 DCHECK_EQ(origin, origin.GetOrigin()); |
68 | 71 |
69 std::stringstream stream; | 72 std::stringstream stream; |
70 | 73 |
71 stream << kPersistentNotificationPrefix; | 74 stream << kPersistentNotificationPrefix; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 | 107 |
105 stream << base::IntToString(non_persistent_notification_id); | 108 stream << base::IntToString(non_persistent_notification_id); |
106 } else { | 109 } else { |
107 stream << tag; | 110 stream << tag; |
108 } | 111 } |
109 | 112 |
110 return stream.str(); | 113 return stream.str(); |
111 } | 114 } |
112 | 115 |
113 } // namespace content | 116 } // namespace content |
OLD | NEW |