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"
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"
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();
28 return base::SHA1HashString(base::WideToUTF8(path
.value()));
30 return base::SHA1HashString(path
.value());
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(
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();
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();
64 stream
<< persistent_notification_id
;
69 std::string
NotificationIdGenerator::GenerateForNonPersistentNotification(
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();
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();
87 stream
<< render_process_id_
;
90 stream
<< non_persistent_notification_id
;
98 } // namespace content