summaryrefslogtreecommitdiffstats
path: root/ios/chrome/common/channel_info.mm
blob: 2d0eb870937456e4dab2e61c897318d03eebd2b3 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright 2015 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 "ios/chrome/common/channel_info.h"

#include <dispatch/dispatch.h>
#import <Foundation/Foundation.h>

#import "base/mac/bundle_locations.h"
#include "base/profiler/scoped_tracker.h"
#import "base/strings/sys_string_conversions.h"
#include "components/version_info/version_info.h"

namespace {

#if defined(GOOGLE_CHROME_BUILD)
// Channel of the running application, initialized by the first call to
// GetChannel() and cached for the whole application lifetime.
version_info::Channel g_channel = version_info::Channel::UNKNOWN;
#endif

}  // namespace

std::string GetVersionString() {
  // TODO(robliao): Remove ScopedTracker below once https://crbug.com/422460 is
  // fixed.
  tracked_objects::ScopedTracker tracking_profile(
      FROM_HERE_WITH_EXPLICIT_FUNCTION(
          "422460 VersionInfo::CreateVersionString"));

  return version_info::GetVersionStringWithModifier(GetChannelString());
}

std::string GetChannelString() {
#if defined(GOOGLE_CHROME_BUILD)
  // Only ever return one of "" (for STABLE channel), "unknown", "beta", "dev"
  // or "canary" in branded build.
  switch (GetChannel()) {
    case version_info::Channel::STABLE:
      return std::string();

    case version_info::Channel::BETA:
      return "beta";

    case version_info::Channel::DEV:
      return "dev";

    case version_info::Channel::CANARY:
      return "canary";

    case version_info::Channel::UNKNOWN:
      return "unknown";
  }
#else
  // Always return empty string for non-branded builds.
  return std::string();
#endif
}

version_info::Channel GetChannel() {
#if defined(GOOGLE_CHROME_BUILD)
  static dispatch_once_t channel_dispatch_token;
  dispatch_once(&channel_dispatch_token, ^{
    NSBundle* bundle = base::mac::OuterBundle();

    // Only Keystone-enabled build can have a channel.
    if (![bundle objectForInfoDictionaryKey:@"KSProductID"])
      return;

    NSString* channel = [bundle objectForInfoDictionaryKey:@"KSChannelID"];
    if (!channel) {
      // KSChannelID is unset for the stable channel.
      g_channel = version_info::Channel::STABLE;
    } else if ([channel isEqualToString:@"beta"]) {
      g_channel = version_info::Channel::BETA;
    } else if ([channel isEqualToString:@"dev"]) {
      g_channel = version_info::Channel::DEV;
    } else if ([channel isEqualToString:@"canary"]) {
      g_channel = version_info::Channel::CANARY;
    }
  });

  return g_channel;
#else
  return version_info::Channel::UNKNOWN;
#endif
}