summaryrefslogtreecommitdiff
path: root/chromium/extensions/common/features/feature_channel.cc
blob: 9fa6ae1d9812e5a5c97b058d1307753789842894 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "extensions/common/features/feature_channel.h"

#include "base/logging.h"
#include "components/version_info/version_info.h"

namespace {

// The current channel to be reported, unless overridden by
// |ScopedCurrentChannel|.
version_info::Channel g_current_channel = version_info::Channel::STABLE;

// The current channel overridden by |ScopedCurrentChannel|. The value is valid
// only whenever |g_override_count| is non-zero.
version_info::Channel g_overridden_channel = version_info::Channel::STABLE;

// The number of currently existing instances of |ScopedCurrentChannel|.
int g_override_count = 0;

}  // namespace

namespace extensions {

version_info::Channel GetCurrentChannel() {
  return g_override_count ? g_overridden_channel : g_current_channel;
}

void SetCurrentChannel(version_info::Channel channel) {
  // In certain unit tests, SetCurrentChannel can be called within the same
  // process (where e.g. utility processes run as a separate thread). Don't
  // write if the value is the same to avoid TSAN failures.
  if (channel != g_current_channel)
    g_current_channel = channel;
}

ScopedCurrentChannel::ScopedCurrentChannel(version_info::Channel channel)
    : channel_(channel),
      original_overridden_channel_(g_overridden_channel),
      original_override_count_(g_override_count) {
  g_overridden_channel = channel;
  ++g_override_count;
}

ScopedCurrentChannel::~ScopedCurrentChannel() {
  DCHECK_EQ(original_override_count_ + 1, g_override_count)
      << "Scoped channel setters are not nested properly";
  DCHECK_EQ(g_overridden_channel, channel_)
      << "Scoped channel setters are not nested properly";
  g_overridden_channel = original_overridden_channel_;
  --g_override_count;
}

}  // namespace extensions