Chromium Code Reviews| 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..e02ea22e1c8f53a0b72090972b72b6253087cee2 |
| --- /dev/null |
| +++ b/content/browser/notifications/notification_id_generator.cc |
| @@ -0,0 +1,85 @@ |
| +// 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 { |
| + DCHECK(origin.is_valid()); |
| + DCHECK_EQ(origin, origin.GetOrigin()); |
| + |
| + std::stringstream stream; |
| + |
| + stream << |
| + BASE_HASH_NAMESPACE::hash<base::FilePath>()(browser_context_->GetPath()); |
|
johnme
2015/05/27 18:18:35
I'm still uncomfortable with using a hash function
Peter Beverloo
2015/05/28 12:51:44
Switched to SHA-1 after talking to bauerb@. Genera
|
| + stream << browser_context_->IsOffTheRecord(); |
| + stream << origin; |
| + |
| + // 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 { |
| + DCHECK(origin.is_valid()); |
| + DCHECK_EQ(origin, origin.GetOrigin()); |
| + |
| + std::stringstream stream; |
| + |
| + stream << |
| + BASE_HASH_NAMESPACE::hash<base::FilePath>()(browser_context_->GetPath()); |
| + stream << browser_context_->IsOffTheRecord(); |
| + stream << origin; |
| + |
| + // Non-persistent notification ids are unique per renderer process when no |
| + // tag is being used. Tags still identify uniqueness for the given origin. |
| + |
| + stream << !!tag.size(); |
| + if (!tag.size()) { |
| + const std::string render_process_id_string = |
| + base::IntToString(render_process_id_); |
| + |
| + stream << render_process_id_string.size(); |
|
johnme
2015/05/27 18:18:35
This is also ambiguous. Whilst it appears to be tr
Peter Beverloo
2015/05/28 12:51:44
Done.
|
| + stream << render_process_id_string; |
| + |
| + stream << base::IntToString(non_persistent_notification_id); |
| + } else { |
| + stream << tag; |
| + } |
| + |
| + return stream.str(); |
| +} |
| + |
| +} // namespace content |