| 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/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/sha1.h" | 11 #include "base/sha1.h" |
| 12 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 13 #include "build/build_config.h" | 14 #include "build/build_config.h" |
| 14 #include "content/public/browser/browser_context.h" | 15 #include "content/public/browser/browser_context.h" |
| 15 #include "url/gurl.h" | 16 #include "url/gurl.h" |
| 16 | 17 |
| 18 #include "base/base64url.h" |
| 19 |
| 17 namespace content { | 20 namespace content { |
| 18 namespace { | 21 namespace { |
| 19 | 22 |
| 20 const char kPersistentPrefix[] = "p:"; | 23 const char kPersistentPrefix[] = "p:"; |
| 21 const char kNonPersistentPrefix[] = "n:"; | 24 const char kNonPersistentPrefix[] = "n:"; |
| 22 | 25 |
| 23 const char kSeparator = '#'; | 26 const char kSeparator = '#'; |
| 24 | 27 |
| 25 // 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. |
| 26 // 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 |
| 27 // collisions negligible. | 30 // collisions negligible. |
| 28 std::string ComputeBrowserContextHash(BrowserContext* browser_context) { | 31 std::string ComputeBrowserContextHash(BrowserContext* browser_context) { |
| 29 const base::FilePath path = browser_context->GetPath(); | 32 const base::FilePath path = browser_context->GetPath(); |
| 30 | 33 |
| 31 #if defined(OS_WIN) | 34 #if defined(OS_WIN) |
| 32 return base::SHA1HashString(base::WideToUTF8(path.value())); | 35 std::string path_hash = base::SHA1HashString(base::WideToUTF8(path.value())); |
| 33 #else | 36 #else |
| 34 return base::SHA1HashString(path.value()); | 37 std::string path_hash = base::SHA1HashString(path.value()); |
| 35 #endif | 38 #endif |
| 39 |
| 40 return base::HexEncode(path_hash.c_str(), path_hash.length()); |
| 36 } | 41 } |
| 37 | 42 |
| 38 } // namespace | 43 } // namespace |
| 39 | 44 |
| 40 NotificationIdGenerator::NotificationIdGenerator( | 45 NotificationIdGenerator::NotificationIdGenerator( |
| 41 BrowserContext* browser_context, | 46 BrowserContext* browser_context, |
| 42 int render_process_id) | 47 int render_process_id) |
| 43 : browser_context_(browser_context), | 48 : browser_context_(browser_context), |
| 44 render_process_id_(render_process_id) {} | 49 render_process_id_(render_process_id) {} |
| 45 | 50 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 61 const GURL& origin, | 66 const GURL& origin, |
| 62 const std::string& tag, | 67 const std::string& tag, |
| 63 int64_t persistent_notification_id) const { | 68 int64_t persistent_notification_id) const { |
| 64 DCHECK(origin.is_valid()); | 69 DCHECK(origin.is_valid()); |
| 65 DCHECK_EQ(origin, origin.GetOrigin()); | 70 DCHECK_EQ(origin, origin.GetOrigin()); |
| 66 | 71 |
| 67 std::stringstream stream; | 72 std::stringstream stream; |
| 68 | 73 |
| 69 stream << kPersistentPrefix; | 74 stream << kPersistentPrefix; |
| 70 stream << ComputeBrowserContextHash(browser_context_); | 75 stream << ComputeBrowserContextHash(browser_context_); |
| 71 stream << browser_context_->IsOffTheRecord(); | 76 stream << base::IntToString(browser_context_->IsOffTheRecord() ? 1 : 0); |
| 72 stream << origin; | 77 stream << origin; |
| 73 | 78 |
| 74 // Persistent notification ids are unique for the lifetime of the notification | 79 // Persistent notification ids are unique for the lifetime of the notification |
| 75 // database, orthogonal to the renderer that created the notification. | 80 // database, orthogonal to the renderer that created the notification. |
| 76 | 81 |
| 77 stream << !!tag.size(); | 82 stream << base::IntToString(tag.size() ? 1 : 0); |
| 78 if (tag.size()) | 83 if (tag.size()) |
| 79 stream << tag; | 84 stream << tag; |
| 80 else | 85 else |
| 81 stream << persistent_notification_id; | 86 stream << base::Int64ToString(persistent_notification_id); |
| 82 | 87 |
| 83 return stream.str(); | 88 return stream.str(); |
| 84 } | 89 } |
| 85 | 90 |
| 86 std::string NotificationIdGenerator::GenerateForNonPersistentNotification( | 91 std::string NotificationIdGenerator::GenerateForNonPersistentNotification( |
| 87 const GURL& origin, | 92 const GURL& origin, |
| 88 const std::string& tag, | 93 const std::string& tag, |
| 89 int non_persistent_notification_id) const { | 94 int non_persistent_notification_id) const { |
| 90 DCHECK(origin.is_valid()); | 95 DCHECK(origin.is_valid()); |
| 91 DCHECK_EQ(origin, origin.GetOrigin()); | 96 DCHECK_EQ(origin, origin.GetOrigin()); |
| 92 | 97 |
| 93 std::stringstream stream; | 98 std::stringstream stream; |
| 94 | 99 |
| 95 stream << kNonPersistentPrefix; | 100 stream << kNonPersistentPrefix; |
| 96 stream << ComputeBrowserContextHash(browser_context_); | 101 stream << ComputeBrowserContextHash(browser_context_); |
| 97 stream << browser_context_->IsOffTheRecord(); | 102 stream << base::IntToString(browser_context_->IsOffTheRecord() ? 1 : 0); |
| 98 stream << origin; | 103 stream << origin; |
| 99 | 104 |
| 100 // Non-persistent notification ids are unique per renderer process when no | 105 // Non-persistent notification ids are unique per renderer process when no |
| 101 // tag is being used. Tags still identify uniqueness for the given origin. | 106 // tag is being used. Tags still identify uniqueness for the given origin. |
| 102 | 107 |
| 103 stream << !!tag.size(); | 108 stream << base::IntToString(tag.size() ? 1 : 0); |
| 104 if (!tag.size()) { | 109 if (!tag.size()) { |
| 105 stream << render_process_id_; | 110 stream << base::IntToString(render_process_id_); |
| 106 stream << kSeparator; | 111 stream << kSeparator; |
| 107 | 112 |
| 108 stream << non_persistent_notification_id; | 113 stream << base::IntToString(non_persistent_notification_id); |
| 109 } else { | 114 } else { |
| 110 stream << tag; | 115 stream << tag; |
| 111 } | 116 } |
| 112 | 117 |
| 113 return stream.str(); | 118 return stream.str(); |
| 114 } | 119 } |
| 115 | 120 |
| 116 } // namespace content | 121 } // namespace content |
| OLD | NEW |