Make USB permissions work in the new permission message system
[chromium-blink-merge.git] / content / browser / notifications / notification_id_generator.cc
blob252aae478d0476cd323e00007846925c44f909de
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
3 // found in the LICENSE file.
5 #include "content/browser/notifications/notification_id_generator.h"
7 #include <sstream>
9 #include "base/files/file_path.h"
10 #include "base/logging.h"
11 #include "base/sha1.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "content/public/browser/browser_context.h"
14 #include "url/gurl.h"
16 namespace content {
17 namespace {
19 const char kSeparator = '#';
21 // Computes a hash based on the path in which the |browser_context| is stored.
22 // Since we only store the hash, SHA-1 is used to make the probability of
23 // collisions negligible.
24 std::string ComputeBrowserContextHash(BrowserContext* browser_context) {
25 const base::FilePath path = browser_context->GetPath();
27 #if defined(OS_WIN)
28 return base::SHA1HashString(base::WideToUTF8(path.value()));
29 #else
30 return base::SHA1HashString(path.value());
31 #endif
34 } // namespace
36 NotificationIdGenerator::NotificationIdGenerator(
37 BrowserContext* browser_context,
38 int render_process_id)
39 : browser_context_(browser_context),
40 render_process_id_(render_process_id) {}
42 NotificationIdGenerator::~NotificationIdGenerator() {}
44 std::string NotificationIdGenerator::GenerateForPersistentNotification(
45 const GURL& origin,
46 const std::string& tag,
47 int64_t persistent_notification_id) const {
48 DCHECK(origin.is_valid());
49 DCHECK_EQ(origin, origin.GetOrigin());
51 std::stringstream stream;
53 stream << ComputeBrowserContextHash(browser_context_);
54 stream << browser_context_->IsOffTheRecord();
55 stream << origin;
57 // Persistent notification ids are unique for the lifetime of the notification
58 // database, orthogonal to the renderer that created the notification.
60 stream << !!tag.size();
61 if (tag.size())
62 stream << tag;
63 else
64 stream << persistent_notification_id;
66 return stream.str();
69 std::string NotificationIdGenerator::GenerateForNonPersistentNotification(
70 const GURL& origin,
71 const std::string& tag,
72 int non_persistent_notification_id) const {
73 DCHECK(origin.is_valid());
74 DCHECK_EQ(origin, origin.GetOrigin());
76 std::stringstream stream;
78 stream << ComputeBrowserContextHash(browser_context_);
79 stream << browser_context_->IsOffTheRecord();
80 stream << origin;
82 // Non-persistent notification ids are unique per renderer process when no
83 // tag is being used. Tags still identify uniqueness for the given origin.
85 stream << !!tag.size();
86 if (!tag.size()) {
87 stream << render_process_id_;
88 stream << kSeparator;
90 stream << non_persistent_notification_id;
91 } else {
92 stream << tag;
95 return stream.str();
98 } // namespace content