 Chromium Code Reviews
 Chromium Code Reviews Issue 2300093002:
  Make //content responsible for generating notification Ids  (Closed)
    
  
    Issue 2300093002:
  Make //content responsible for generating notification Ids  (Closed) 
  | 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 "url/gurl.h" | 17 #include "url/gurl.h" | 
| 18 | 18 | 
| 19 namespace content { | 19 namespace content { | 
| 20 namespace { | 20 namespace { | 
| 21 | 21 | 
| 22 const char kPersistentPrefix[] = "p:"; | 22 const char kPersistentPrefix[] = "p:"; | 
| 
Miguel Garcia
2016/09/02 14:33:16
perhaps document these constants?
 
Peter Beverloo
2016/09/05 15:11:01
With what? I don't think "Prefix applied to notifi
 
Miguel Garcia
2016/09/07 17:32:33
Acknowledged.
 | |
| 23 const char kNonPersistentPrefix[] = "n:"; | 23 const char kNonPersistentPrefix[] = "n:"; | 
| 24 | 24 | 
| 25 const char kSeparator = '#'; | 25 const char kSeparator = '#'; | 
| 26 | 26 | 
| 27 // 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. | 
| 28 // 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 | 
| 29 // collisions negligible. | 29 // collisions negligible. | 
| 30 std::string ComputeBrowserContextHash(BrowserContext* browser_context) { | 30 std::string ComputeBrowserContextHash(BrowserContext* browser_context) { | 
| 31 const base::FilePath path = browser_context->GetPath(); | 31 const base::FilePath path = browser_context->GetPath(); | 
| 32 | 32 | 
| 33 #if defined(OS_WIN) | 33 #if defined(OS_WIN) | 
| 34 std::string path_hash = base::SHA1HashString(base::WideToUTF8(path.value())); | 34 std::string path_hash = base::SHA1HashString(base::WideToUTF8(path.value())); | 
| 35 #else | 35 #else | 
| 36 std::string path_hash = base::SHA1HashString(path.value()); | 36 std::string path_hash = base::SHA1HashString(path.value()); | 
| 37 #endif | 37 #endif | 
| 38 | 38 | 
| 39 return base::HexEncode(path_hash.c_str(), path_hash.length()); | 39 return base::HexEncode(path_hash.c_str(), path_hash.length()); | 
| 40 } | 40 } | 
| 41 | 41 | 
| 42 } // namespace | 42 } // namespace | 
| 43 | 43 | 
| 44 NotificationIdGenerator::NotificationIdGenerator( | 44 NotificationIdGenerator::NotificationIdGenerator( | 
| 45 BrowserContext* browser_context, | 45 BrowserContext* browser_context) | 
| 46 int render_process_id) | 46 : browser_context_(browser_context) {} | 
| 47 : browser_context_(browser_context), | |
| 48 render_process_id_(render_process_id) {} | |
| 49 | 47 | 
| 50 NotificationIdGenerator::~NotificationIdGenerator() {} | 48 NotificationIdGenerator::~NotificationIdGenerator() {} | 
| 51 | 49 | 
| 52 // static | 50 // static | 
| 53 bool NotificationIdGenerator::IsPersistentNotification( | 51 bool NotificationIdGenerator::IsPersistentNotification( | 
| 54 const base::StringPiece& notification_id) { | 52 const base::StringPiece& notification_id) { | 
| 55 return notification_id.starts_with(kPersistentPrefix); | 53 return notification_id.starts_with(kPersistentPrefix); | 
| 56 } | 54 } | 
| 57 | 55 | 
| 58 // static | 56 // static | 
| 59 bool NotificationIdGenerator::IsNonPersistentNotification( | 57 bool NotificationIdGenerator::IsNonPersistentNotification( | 
| 60 const base::StringPiece& notification_id) { | 58 const base::StringPiece& notification_id) { | 
| 61 return notification_id.starts_with(kNonPersistentPrefix); | 59 return notification_id.starts_with(kNonPersistentPrefix); | 
| 62 } | 60 } | 
| 63 | 61 | 
| 64 std::string NotificationIdGenerator::GenerateForPersistentNotification( | 62 std::string NotificationIdGenerator::GenerateForPersistentNotification( | 
| 65 const GURL& origin, | 63 const GURL& origin, | 
| 66 const std::string& tag, | 64 const std::string& tag, | 
| 67 int64_t persistent_notification_id) const { | 65 int64_t persistent_notification_id) const { | 
| 68 DCHECK(origin.is_valid()); | 66 DCHECK(origin.is_valid()); | 
| 69 DCHECK_EQ(origin, origin.GetOrigin()); | 67 DCHECK_EQ(origin, origin.GetOrigin()); | 
| 70 | 68 | 
| 71 std::stringstream stream; | 69 std::stringstream stream; | 
| 72 | 70 | 
| 73 stream << kPersistentPrefix; | 71 stream << kPersistentPrefix; | 
| 74 stream << ComputeBrowserContextHash(browser_context_); | 72 stream << ComputeBrowserContextHash(browser_context_); | 
| 75 stream << base::IntToString(browser_context_->IsOffTheRecord()); | 73 stream << base::IntToString(browser_context_->IsOffTheRecord()); | 
| 
Miguel Garcia
2016/09/02 14:33:16
why do you need to pass this to generate the notif
 
Peter Beverloo
2016/09/05 15:11:01
The ComputeBrowserContextHash() will yield the sam
 | |
| 76 stream << origin; | 74 stream << origin; | 
| 77 | 75 | 
| 78 // Persistent notification ids are unique for the lifetime of the notification | 76 // Persistent notification ids are unique for the lifetime of the notification | 
| 
Miguel Garcia
2016/09/02 14:33:16
why does this comment belong here? Seems an odd pl
 
Peter Beverloo
2016/09/05 15:11:01
It's redundant with the class-level command. I've
 | |
| 79 // database, orthogonal to the renderer that created the notification. | 77 // database, orthogonal to the renderer that created the notification. | 
| 80 | 78 | 
| 81 stream << base::IntToString(!tag.empty()); | 79 stream << base::IntToString(!tag.empty()); | 
| 82 if (tag.size()) | 80 if (tag.size()) | 
| 83 stream << tag; | 81 stream << tag; | 
| 84 else | 82 else | 
| 85 stream << base::Int64ToString(persistent_notification_id); | 83 stream << base::Int64ToString(persistent_notification_id); | 
| 86 | 84 | 
| 87 return stream.str(); | 85 return stream.str(); | 
| 88 } | 86 } | 
| 89 | 87 | 
| 90 std::string NotificationIdGenerator::GenerateForNonPersistentNotification( | 88 std::string NotificationIdGenerator::GenerateForNonPersistentNotification( | 
| 91 const GURL& origin, | 89 const GURL& origin, | 
| 92 const std::string& tag, | 90 const std::string& tag, | 
| 93 int non_persistent_notification_id) const { | 91 int non_persistent_notification_id, | 
| 92 int render_process_id) const { | |
| 94 DCHECK(origin.is_valid()); | 93 DCHECK(origin.is_valid()); | 
| 95 DCHECK_EQ(origin, origin.GetOrigin()); | 94 DCHECK_EQ(origin, origin.GetOrigin()); | 
| 96 | 95 | 
| 97 std::stringstream stream; | 96 std::stringstream stream; | 
| 98 | 97 | 
| 99 stream << kNonPersistentPrefix; | 98 stream << kNonPersistentPrefix; | 
| 100 stream << ComputeBrowserContextHash(browser_context_); | 99 stream << ComputeBrowserContextHash(browser_context_); | 
| 101 stream << base::IntToString(browser_context_->IsOffTheRecord()); | 100 stream << base::IntToString(browser_context_->IsOffTheRecord()); | 
| 102 stream << origin; | 101 stream << origin; | 
| 103 | 102 | 
| 104 // Non-persistent notification ids are unique per renderer process when no | 103 // Non-persistent notification ids are unique per renderer process when no | 
| 
Miguel Garcia
2016/09/02 14:33:16
Can you explain why you need the different rules f
 
Peter Beverloo
2016/09/05 15:11:01
We currently don't store any state for non-persist
 
Miguel Garcia
2016/09/07 17:32:33
Works for me
On 2016/09/05 15:11:01, Peter Beverl
 | |
| 105 // tag is being used. Tags still identify uniqueness for the given origin. | 104 // tag is being used. Tags still identify uniqueness for the given origin. | 
| 106 | 105 | 
| 107 stream << base::IntToString(!tag.empty()); | 106 stream << base::IntToString(!tag.empty()); | 
| 108 if (tag.empty()) { | 107 if (tag.empty()) { | 
| 109 stream << base::IntToString(render_process_id_); | 108 stream << base::IntToString(render_process_id); | 
| 110 stream << kSeparator; | 109 stream << kSeparator; | 
| 111 | 110 | 
| 112 stream << base::IntToString(non_persistent_notification_id); | 111 stream << base::IntToString(non_persistent_notification_id); | 
| 113 } else { | 112 } else { | 
| 114 stream << tag; | 113 stream << tag; | 
| 115 } | 114 } | 
| 116 | 115 | 
| 117 return stream.str(); | 116 return stream.str(); | 
| 118 } | 117 } | 
| 119 | 118 | 
| 120 } // namespace content | 119 } // namespace content | 
| OLD | NEW |