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..f3b2fb5b3b6dbfc8bc560b90209e432121ebaa87 |
| --- /dev/null |
| +++ b/content/browser/notifications/notification_id_generator.cc |
| @@ -0,0 +1,99 @@ |
| +// 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/logging.h" |
| +#include "base/sha1.h" |
| +#include "base/strings/string_number_conversions.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "content/public/browser/browser_context.h" |
| +#include "url/gurl.h" |
| + |
| +namespace content { |
| +namespace { |
| + |
| +const char kSeparator = '#'; |
| + |
| +// Computes a hash based on the path in which the |browser_context| is stored. |
| +// This hash might be used outside of Chrome (e.g. the platform's notification |
| +// manager), thus SHA-1 is used to maximise preimage resistance. |
|
johnme
2015/05/29 09:34:57
Nit: "preimage resistance" is somewhat obscure, an
Peter Beverloo
2015/05/29 12:34:08
Done.
|
| +std::string ComputeBrowserContextHash(BrowserContext* browser_context) { |
| + const base::FilePath path = browser_context->GetPath(); |
| + |
| +#if defined(OS_WIN) |
| + return base::SHA1HashString(base::WideToUTF8(path.value())); |
| +#else |
| + return base::SHA1HashString(path.value()); |
| +#endif |
| +} |
| + |
| +} // namespace |
| + |
| +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 << ComputeBrowserContextHash(browser_context_); |
| + 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); |
|
johnme
2015/05/29 09:34:57
Nit: the base::Int64ToString is presumably unneces
Peter Beverloo
2015/05/29 12:34:08
Done.
|
| + |
| + 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 << ComputeBrowserContextHash(browser_context_); |
| + 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()) { |
| + stream << render_process_id_; |
| + stream << kSeparator; |
| + |
| + stream << base::IntToString(non_persistent_notification_id); |
|
johnme
2015/05/29 09:34:57
Nit: the base::IntToString seems unnecessary, sinc
Peter Beverloo
2015/05/29 12:34:08
Done.
|
| + } else { |
| + stream << tag; |
| + } |
| + |
| + return stream.str(); |
| +} |
| + |
| +} // namespace content |