OLD | NEW |
(Empty) | |
| 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 #include "content/browser/notifications/notification_id_generator.h" |
| 6 |
| 7 #include <sstream> |
| 8 |
| 9 #include "base/files/file_path.h" |
| 10 #include "base/logging.h" |
| 11 #include "base/sha1.h" |
| 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "content/public/browser/browser_context.h" |
| 14 #include "url/gurl.h" |
| 15 |
| 16 namespace content { |
| 17 namespace { |
| 18 |
| 19 const char kSeparator = '#'; |
| 20 |
| 21 // Computes a hash based on the path in which the |browser_context| is stored. |
| 22 // Since we only store the hash, SHA-1 is used to make the probability of |
| 23 // collisions negligible. |
| 24 std::string ComputeBrowserContextHash(BrowserContext* browser_context) { |
| 25 const base::FilePath path = browser_context->GetPath(); |
| 26 |
| 27 #if defined(OS_WIN) |
| 28 return base::SHA1HashString(base::WideToUTF8(path.value())); |
| 29 #else |
| 30 return base::SHA1HashString(path.value()); |
| 31 #endif |
| 32 } |
| 33 |
| 34 } // namespace |
| 35 |
| 36 NotificationIdGenerator::NotificationIdGenerator( |
| 37 BrowserContext* browser_context, |
| 38 int render_process_id) |
| 39 : browser_context_(browser_context), |
| 40 render_process_id_(render_process_id) {} |
| 41 |
| 42 NotificationIdGenerator::~NotificationIdGenerator() {} |
| 43 |
| 44 std::string NotificationIdGenerator::GenerateForPersistentNotification( |
| 45 const GURL& origin, |
| 46 const std::string& tag, |
| 47 int64_t persistent_notification_id) const { |
| 48 DCHECK(origin.is_valid()); |
| 49 DCHECK_EQ(origin, origin.GetOrigin()); |
| 50 |
| 51 std::stringstream stream; |
| 52 |
| 53 stream << ComputeBrowserContextHash(browser_context_); |
| 54 stream << browser_context_->IsOffTheRecord(); |
| 55 stream << origin; |
| 56 |
| 57 // Persistent notification ids are unique for the lifetime of the notification |
| 58 // database, orthogonal to the renderer that created the notification. |
| 59 |
| 60 stream << !!tag.size(); |
| 61 if (tag.size()) |
| 62 stream << tag; |
| 63 else |
| 64 stream << persistent_notification_id; |
| 65 |
| 66 return stream.str(); |
| 67 } |
| 68 |
| 69 std::string NotificationIdGenerator::GenerateForNonPersistentNotification( |
| 70 const GURL& origin, |
| 71 const std::string& tag, |
| 72 int non_persistent_notification_id) const { |
| 73 DCHECK(origin.is_valid()); |
| 74 DCHECK_EQ(origin, origin.GetOrigin()); |
| 75 |
| 76 std::stringstream stream; |
| 77 |
| 78 stream << ComputeBrowserContextHash(browser_context_); |
| 79 stream << browser_context_->IsOffTheRecord(); |
| 80 stream << origin; |
| 81 |
| 82 // Non-persistent notification ids are unique per renderer process when no |
| 83 // tag is being used. Tags still identify uniqueness for the given origin. |
| 84 |
| 85 stream << !!tag.size(); |
| 86 if (!tag.size()) { |
| 87 stream << render_process_id_; |
| 88 stream << kSeparator; |
| 89 |
| 90 stream << non_persistent_notification_id; |
| 91 } else { |
| 92 stream << tag; |
| 93 } |
| 94 |
| 95 return stream.str(); |
| 96 } |
| 97 |
| 98 } // namespace content |
OLD | NEW |