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

Side by Side Diff: content/browser/renderer_host/routing_id_issuer.cc

Issue 643733002: Add RoutingIDIssuer to ensure the correctness of ID to be routed. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Updated Created 6 years, 2 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 (c) 2012 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/renderer_host/routing_id_issuer.h"
6
7 namespace content {
8
9 namespace {
10
11 const int kManglerBits = 3;
12 const int kIDBits = 31 - kManglerBits;
13 const int kManglerWrap = 1 << kManglerBits;
14 const int kIDWrap = 1 << kIDBits;
15 const int kIDMask = kIDWrap - 1;
16
17 static base::StaticAtomicSequenceNumber g_manglerSequence;
18 static int g_disablingIDManglingCount = 0;
19
20 int GetNextMangler() {
21 // Does +1 to always gives some mangler.
22 return (g_manglerSequence.GetNext() % (kManglerWrap - 1)) + 1;
23 }
24
25 } // namespace
26
27 scoped_ptr<RoutingIDIssuer> RoutingIDIssuer::Create() {
28 if (0 < g_disablingIDManglingCount)
29 return make_scoped_ptr(new RoutingIDIssuer(0));
30
31 DCHECK(0 == g_disablingIDManglingCount);
32 return make_scoped_ptr(new RoutingIDIssuer(GetNextMangler()));
33 }
34
35 scoped_ptr<RoutingIDIssuer> RoutingIDIssuer::CreateWithMangler(int mangler) {
36 return make_scoped_ptr(new RoutingIDIssuer(mangler));
37 }
38
39 RoutingIDIssuer::RoutingIDIssuer(int mangler) : mangler_(mangler) {
40 DCHECK(0 <= mangler_ && mangler_ < kManglerWrap) << mangler_
41 << ">=" << kManglerWrap;
42 }
43
44 int RoutingIDIssuer::IssueNext() {
45 return (mangler_ << kIDBits) | ((next_.GetNext() + 1) & kIDMask);
46 }
47
48 bool RoutingIDIssuer::IsProbablyValid(int issued_id) const {
49 return (mangler_ << kIDBits) == (issued_id & ~kIDMask);
50 }
51
52 void RoutingIDIssuer::DisableIDMangling() {
53 g_disablingIDManglingCount++;
54 }
55
56 void RoutingIDIssuer::EnableIDMangling() {
57 g_disablingIDManglingCount--;
58 DCHECK(0 <= g_disablingIDManglingCount);
59 }
60
61 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/renderer_host/routing_id_issuer.h ('k') | content/browser/renderer_host/routing_id_issuer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698