Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(22)

Side by Side Diff: content/browser/notifications/notification_id_generator.cc

Issue 2300093002: Make //content responsible for generating notification Ids (Closed)
Patch Set: comments Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/notifications/notification_id_generator.h" 5 #include "content/browser/notifications/notification_id_generator.h"
6 6
7 #include <sstream> 7 #include <sstream>
8 8
9 #include "base/base64.h" 9 #include "base/base64.h"
10 #include "base/files/file_path.h" 10 #include "base/files/file_path.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/sha1.h" 12 #include "base/sha1.h"
13 #include "base/strings/string_number_conversions.h" 13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/utf_string_conversions.h" 14 #include "base/strings/utf_string_conversions.h"
15 #include "build/build_config.h" 15 #include "build/build_config.h"
16 #include "content/public/browser/browser_context.h" 16 #include "content/public/browser/browser_context.h"
17 #include "url/gurl.h" 17 #include "url/gurl.h"
18 18
19 namespace content { 19 namespace content {
20 namespace { 20 namespace {
21 21
22 const char kPersistentPrefix[] = "p:"; 22 const char kPersistentNotificationPrefix[] = "p:";
23 const char kNonPersistentPrefix[] = "n:"; 23 const char kNonPersistentNotificationPrefix[] = "n:";
24 24
25 const char kSeparator = '#'; 25 const char kSeparator = '#';
26 26
27 // Computes a hash based on the path in which the |browser_context| is stored. 27 // Computes a hash based on the path in which the |browser_context| is stored.
28 // Since we only store the hash, SHA-1 is used to make the probability of 28 // Since we only store the hash, SHA-1 is used to make the probability of
29 // collisions negligible. 29 // collisions negligible.
30 std::string ComputeBrowserContextHash(BrowserContext* browser_context) { 30 std::string ComputeBrowserContextHash(BrowserContext* browser_context) {
31 const base::FilePath path = browser_context->GetPath(); 31 const base::FilePath path = browser_context->GetPath();
32 32
33 #if defined(OS_WIN) 33 #if defined(OS_WIN)
34 std::string path_hash = base::SHA1HashString(base::WideToUTF8(path.value())); 34 std::string path_hash = base::SHA1HashString(base::WideToUTF8(path.value()));
35 #else 35 #else
36 std::string path_hash = base::SHA1HashString(path.value()); 36 std::string path_hash = base::SHA1HashString(path.value());
37 #endif 37 #endif
38 38
39 return base::HexEncode(path_hash.c_str(), path_hash.length()); 39 return base::HexEncode(path_hash.c_str(), path_hash.length());
40 } 40 }
41 41
42 } // namespace 42 } // namespace
43 43
44 NotificationIdGenerator::NotificationIdGenerator( 44 NotificationIdGenerator::NotificationIdGenerator(
45 BrowserContext* browser_context, 45 BrowserContext* browser_context)
46 int render_process_id) 46 : browser_context_(browser_context) {}
47 : browser_context_(browser_context),
48 render_process_id_(render_process_id) {}
49 47
50 NotificationIdGenerator::~NotificationIdGenerator() {} 48 NotificationIdGenerator::~NotificationIdGenerator() {}
51 49
52 // static 50 // static
53 bool NotificationIdGenerator::IsPersistentNotification( 51 bool NotificationIdGenerator::IsPersistentNotification(
54 const base::StringPiece& notification_id) { 52 const base::StringPiece& notification_id) {
55 return notification_id.starts_with(kPersistentPrefix); 53 return notification_id.starts_with(kPersistentNotificationPrefix);
56 } 54 }
57 55
58 // static 56 // static
59 bool NotificationIdGenerator::IsNonPersistentNotification( 57 bool NotificationIdGenerator::IsNonPersistentNotification(
60 const base::StringPiece& notification_id) { 58 const base::StringPiece& notification_id) {
61 return notification_id.starts_with(kNonPersistentPrefix); 59 return notification_id.starts_with(kNonPersistentNotificationPrefix);
62 } 60 }
63 61
64 std::string NotificationIdGenerator::GenerateForPersistentNotification( 62 std::string NotificationIdGenerator::GenerateForPersistentNotification(
65 const GURL& origin, 63 const GURL& origin,
66 const std::string& tag, 64 const std::string& tag,
67 int64_t persistent_notification_id) const { 65 int64_t persistent_notification_id) const {
68 DCHECK(origin.is_valid()); 66 DCHECK(origin.is_valid());
69 DCHECK_EQ(origin, origin.GetOrigin()); 67 DCHECK_EQ(origin, origin.GetOrigin());
70 68
71 std::stringstream stream; 69 std::stringstream stream;
72 70
73 stream << kPersistentPrefix; 71 stream << kPersistentNotificationPrefix;
74 stream << ComputeBrowserContextHash(browser_context_); 72 stream << ComputeBrowserContextHash(browser_context_);
75 stream << base::IntToString(browser_context_->IsOffTheRecord()); 73 stream << base::IntToString(browser_context_->IsOffTheRecord());
76 stream << origin; 74 stream << origin;
77 75
78 // Persistent notification ids are unique for the lifetime of the notification
79 // database, orthogonal to the renderer that created the notification.
80
81 stream << base::IntToString(!tag.empty()); 76 stream << base::IntToString(!tag.empty());
82 if (tag.size()) 77 if (tag.size())
83 stream << tag; 78 stream << tag;
84 else 79 else
85 stream << base::Int64ToString(persistent_notification_id); 80 stream << base::Int64ToString(persistent_notification_id);
86 81
87 return stream.str(); 82 return stream.str();
88 } 83 }
89 84
90 std::string NotificationIdGenerator::GenerateForNonPersistentNotification( 85 std::string NotificationIdGenerator::GenerateForNonPersistentNotification(
91 const GURL& origin, 86 const GURL& origin,
92 const std::string& tag, 87 const std::string& tag,
93 int non_persistent_notification_id) const { 88 int non_persistent_notification_id,
89 int render_process_id) const {
94 DCHECK(origin.is_valid()); 90 DCHECK(origin.is_valid());
95 DCHECK_EQ(origin, origin.GetOrigin()); 91 DCHECK_EQ(origin, origin.GetOrigin());
96 92
97 std::stringstream stream; 93 std::stringstream stream;
98 94
99 stream << kNonPersistentPrefix; 95 stream << kNonPersistentNotificationPrefix;
100 stream << ComputeBrowserContextHash(browser_context_); 96 stream << ComputeBrowserContextHash(browser_context_);
101 stream << base::IntToString(browser_context_->IsOffTheRecord()); 97 stream << base::IntToString(browser_context_->IsOffTheRecord());
102 stream << origin; 98 stream << origin;
103 99
104 // Non-persistent notification ids are unique per renderer process when no
105 // tag is being used. Tags still identify uniqueness for the given origin.
106
107 stream << base::IntToString(!tag.empty()); 100 stream << base::IntToString(!tag.empty());
108 if (tag.empty()) { 101 if (tag.empty()) {
109 stream << base::IntToString(render_process_id_); 102 stream << base::IntToString(render_process_id);
110 stream << kSeparator; 103 stream << kSeparator;
111 104
112 stream << base::IntToString(non_persistent_notification_id); 105 stream << base::IntToString(non_persistent_notification_id);
113 } else { 106 } else {
114 stream << tag; 107 stream << tag;
115 } 108 }
116 109
117 return stream.str(); 110 return stream.str();
118 } 111 }
119 112
120 } // namespace content 113 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698