1 // Copyright 2013 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 "extensions/common/features/feature_channel.h"
6 
7 #include "base/logging.h"
8 #include "components/version_info/version_info.h"
9 
10 namespace {
11 
12 // The current channel to be reported, unless overridden by
13 // |ScopedCurrentChannel|.
14 version_info::Channel g_current_channel = version_info::Channel::STABLE;
15 
16 // The current channel overridden by |ScopedCurrentChannel|. The value is valid
17 // only whenever |g_override_count| is non-zero.
18 version_info::Channel g_overridden_channel = version_info::Channel::STABLE;
19 
20 // The number of currently existing instances of |ScopedCurrentChannel|.
21 int g_override_count = 0;
22 
23 }  // namespace
24 
25 namespace extensions {
26 
GetCurrentChannel()27 version_info::Channel GetCurrentChannel() {
28   return g_override_count ? g_overridden_channel : g_current_channel;
29 }
30 
SetCurrentChannel(version_info::Channel channel)31 void SetCurrentChannel(version_info::Channel channel) {
32   // In certain unit tests, SetCurrentChannel can be called within the same
33   // process (where e.g. utility processes run as a separate thread). Don't
34   // write if the value is the same to avoid TSAN failures.
35   if (channel != g_current_channel)
36     g_current_channel = channel;
37 }
38 
ScopedCurrentChannel(version_info::Channel channel)39 ScopedCurrentChannel::ScopedCurrentChannel(version_info::Channel channel)
40     : channel_(channel),
41       original_overridden_channel_(g_overridden_channel),
42       original_override_count_(g_override_count) {
43   g_overridden_channel = channel;
44   ++g_override_count;
45 }
46 
~ScopedCurrentChannel()47 ScopedCurrentChannel::~ScopedCurrentChannel() {
48   DCHECK_EQ(original_override_count_ + 1, g_override_count)
49       << "Scoped channel setters are not nested properly";
50   DCHECK_EQ(g_overridden_channel, channel_)
51       << "Scoped channel setters are not nested properly";
52   g_overridden_channel = original_overridden_channel_;
53   --g_override_count;
54 }
55 
56 }  // namespace extensions
57