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