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

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

Issue 1133813002: Introduce the NotificationIdGenerator in //content. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Windows build fix Created 5 years, 6 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
(Empty)
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.
4
5 #include "content/browser/notifications/notification_id_generator.h"
6
7 #include <sstream>
8
9 #include "base/files/file_path.h"
10 #include "base/logging.h"
11 #include "base/sha1.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "content/public/browser/browser_context.h"
15 #include "url/gurl.h"
16
17 namespace content {
18 namespace {
19
20 const char kSeparator = '#';
21
22 // Computes a hash based on the path in which the |browser_context| is stored.
23 // This hash might be used outside of Chrome (e.g. the platform's notification
24 // 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.
25 std::string ComputeBrowserContextHash(BrowserContext* browser_context) {
26 const base::FilePath path = browser_context->GetPath();
27
28 #if defined(OS_WIN)
29 return base::SHA1HashString(base::WideToUTF8(path.value()));
30 #else
31 return base::SHA1HashString(path.value());
32 #endif
33 }
34
35 } // namespace
36
37 NotificationIdGenerator::NotificationIdGenerator(
38 BrowserContext* browser_context,
39 int render_process_id)
40 : browser_context_(browser_context),
41 render_process_id_(render_process_id) {}
42
43 NotificationIdGenerator::~NotificationIdGenerator() {}
44
45 std::string NotificationIdGenerator::GenerateForPersistentNotification(
46 const GURL& origin,
47 const std::string& tag,
48 int64_t persistent_notification_id) const {
49 DCHECK(origin.is_valid());
50 DCHECK_EQ(origin, origin.GetOrigin());
51
52 std::stringstream stream;
53
54 stream << ComputeBrowserContextHash(browser_context_);
55 stream << browser_context_->IsOffTheRecord();
56 stream << origin;
57
58 // Persistent notification ids are unique for the lifetime of the notification
59 // database, orthogonal to the renderer that created the notification.
60
61 stream << !!tag.size();
62 if (tag.size())
63 stream << tag;
64 else
65 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.
66
67 return stream.str();
68 }
69
70 std::string NotificationIdGenerator::GenerateForNonPersistentNotification(
71 const GURL& origin,
72 const std::string& tag,
73 int non_persistent_notification_id) const {
74 DCHECK(origin.is_valid());
75 DCHECK_EQ(origin, origin.GetOrigin());
76
77 std::stringstream stream;
78
79 stream << ComputeBrowserContextHash(browser_context_);
80 stream << browser_context_->IsOffTheRecord();
81 stream << origin;
82
83 // Non-persistent notification ids are unique per renderer process when no
84 // tag is being used. Tags still identify uniqueness for the given origin.
85
86 stream << !!tag.size();
87 if (!tag.size()) {
88 stream << render_process_id_;
89 stream << kSeparator;
90
91 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.
92 } else {
93 stream << tag;
94 }
95
96 return stream.str();
97 }
98
99 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698