Index: content/browser/notifications/notification_id_generator.cc |
diff --git a/content/browser/notifications/notification_id_generator.cc b/content/browser/notifications/notification_id_generator.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..9dc9725c6880e0a2ebd4895856be1b184407267c |
--- /dev/null |
+++ b/content/browser/notifications/notification_id_generator.cc |
@@ -0,0 +1,70 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "content/browser/notifications/notification_id_generator.h" |
+ |
+#include <sstream> |
+ |
+#include "base/files/file_path.h" |
+#include "base/hash.h" |
+#include "base/logging.h" |
+#include "base/strings/string_number_conversions.h" |
+#include "content/public/browser/browser_context.h" |
+#include "url/gurl.h" |
+ |
+namespace content { |
+ |
+NotificationIdGenerator::NotificationIdGenerator( |
+ BrowserContext* browser_context, |
+ int render_process_id) |
+ : browser_context_(browser_context), |
+ render_process_id_(render_process_id) {} |
+ |
+NotificationIdGenerator::~NotificationIdGenerator() {} |
+ |
+std::string NotificationIdGenerator::GenerateForPersistentNotification( |
+ const GURL& origin, |
+ const std::string& tag, |
+ int64_t persistent_notification_id) const { |
+ std::stringstream stream; |
+ |
+ stream << base::Hash(browser_context_->GetPath().value()); |
johnme
2015/05/08 15:35:25
base::Hash is just 32-bits, and not a secure hash.
Peter Beverloo
2015/05/20 18:43:33
What would you propose to do instead, without over
|
+ stream << browser_context_->IsOffTheRecord(); |
+ stream << origin.GetOrigin(); |
johnme
2015/05/08 15:35:25
This is ambiguous. The following two cases will gi
Peter Beverloo
2015/05/20 18:43:33
They wouldn't, because origins end with slashes. :
|
+ |
+ // Persistent notification ids are unique for the lifetime of the notification |
+ // database, orthogonal to the renderer that created the notification. |
+ |
+ stream << !!tag.size(); |
+ if (tag.size()) |
+ stream << tag; |
+ else |
+ stream << base::Int64ToString(persistent_notification_id); |
+ |
+ return stream.str(); |
+} |
+ |
+std::string NotificationIdGenerator::GenerateForNonPersistentNotification( |
+ const GURL& origin, |
+ const std::string& tag, |
+ int non_persistent_notification_id) const { |
+ std::stringstream stream; |
+ |
+ stream << base::Hash(browser_context_->GetPath().value()); |
+ stream << browser_context_->IsOffTheRecord(); |
+ stream << origin.GetOrigin(); |
+ |
+ // Non-persistent notification ids are unique per renderer process. |
+ stream << render_process_id_; |
johnme
2015/05/08 15:35:25
This is ambiguous. The following two cases will gi
Peter Beverloo
2015/05/20 18:43:33
Fixed, and added a test.
|
+ |
+ stream << !!tag.size(); |
+ if (tag.size()) |
+ stream << tag; |
+ else |
+ stream << base::IntToString(non_persistent_notification_id); |
+ |
+ return stream.str(); |
+} |
+ |
+} // namespace content |