Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / ios / chrome / common / channel_info.mm
blob2d0eb870937456e4dab2e61c897318d03eebd2b3
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.
5 #include "ios/chrome/common/channel_info.h"
7 #include <dispatch/dispatch.h>
8 #import <Foundation/Foundation.h>
10 #import "base/mac/bundle_locations.h"
11 #include "base/profiler/scoped_tracker.h"
12 #import "base/strings/sys_string_conversions.h"
13 #include "components/version_info/version_info.h"
15 namespace {
17 #if defined(GOOGLE_CHROME_BUILD)
18 // Channel of the running application, initialized by the first call to
19 // GetChannel() and cached for the whole application lifetime.
20 version_info::Channel g_channel = version_info::Channel::UNKNOWN;
21 #endif
23 }  // namespace
25 std::string GetVersionString() {
26   // TODO(robliao): Remove ScopedTracker below once https://crbug.com/422460 is
27   // fixed.
28   tracked_objects::ScopedTracker tracking_profile(
29       FROM_HERE_WITH_EXPLICIT_FUNCTION(
30           "422460 VersionInfo::CreateVersionString"));
32   return version_info::GetVersionStringWithModifier(GetChannelString());
35 std::string GetChannelString() {
36 #if defined(GOOGLE_CHROME_BUILD)
37   // Only ever return one of "" (for STABLE channel), "unknown", "beta", "dev"
38   // or "canary" in branded build.
39   switch (GetChannel()) {
40     case version_info::Channel::STABLE:
41       return std::string();
43     case version_info::Channel::BETA:
44       return "beta";
46     case version_info::Channel::DEV:
47       return "dev";
49     case version_info::Channel::CANARY:
50       return "canary";
52     case version_info::Channel::UNKNOWN:
53       return "unknown";
54   }
55 #else
56   // Always return empty string for non-branded builds.
57   return std::string();
58 #endif
61 version_info::Channel GetChannel() {
62 #if defined(GOOGLE_CHROME_BUILD)
63   static dispatch_once_t channel_dispatch_token;
64   dispatch_once(&channel_dispatch_token, ^{
65     NSBundle* bundle = base::mac::OuterBundle();
67     // Only Keystone-enabled build can have a channel.
68     if (![bundle objectForInfoDictionaryKey:@"KSProductID"])
69       return;
71     NSString* channel = [bundle objectForInfoDictionaryKey:@"KSChannelID"];
72     if (!channel) {
73       // KSChannelID is unset for the stable channel.
74       g_channel = version_info::Channel::STABLE;
75     } else if ([channel isEqualToString:@"beta"]) {
76       g_channel = version_info::Channel::BETA;
77     } else if ([channel isEqualToString:@"dev"]) {
78       g_channel = version_info::Channel::DEV;
79     } else if ([channel isEqualToString:@"canary"]) {
80       g_channel = version_info::Channel::CANARY;
81     }
82   });
84   return g_channel;
85 #else
86   return version_info::Channel::UNKNOWN;
87 #endif