Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / ios / chrome / common / channel_info.mm
blob5daeab267c4b170f2ee017b452148492e1727009
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 #import <Foundation/Foundation.h>
9 #import "base/mac/bundle_locations.h"
10 #include "base/profiler/scoped_tracker.h"
11 #import "base/strings/sys_string_conversions.h"
12 #include "components/version_info/version_info.h"
14 namespace {
16 // Returns the channel and if |channel_string| is not null, it is set
17 // to the string representation of the channel (only ever return one
18 // of "", "unknown", "beta", "dev" or "canary" in branded build).
19 version_info::Channel GetChannelImpl(std::string* channel_string) {
20 #if defined(GOOGLE_CHROME_BUILD)
21   NSBundle* bundle = base::mac::OuterBundle();
22   NSString* channel = [bundle objectForInfoDictionaryKey:@"KSChannelID"];
24   // Only Keystone-enabled build can have a channel.
25   if ([bundle objectForInfoDictionaryKey:@"KSProductID"]) {
26     // KSChannelID is unset for the stable channel.
27     if (!channel) {
28       if (channel_string)
29         channel_string->clear();
30       return version_info::Channel::STABLE;
31     }
33     if ([channel isEqualToString:@"beta"]) {
34       if (channel_string)
35         channel_string->assign(base::SysNSStringToUTF8(channel));
36       return version_info::Channel::BETA;
37     }
39     if ([channel isEqualToString:@"dev"]) {
40       if (channel_string)
41         channel_string->assign(base::SysNSStringToUTF8(channel));
42       return version_info::Channel::DEV;
43     }
45     if ([channel isEqualToString:@"canary"]) {
46       if (channel_string)
47         channel_string->assign(base::SysNSStringToUTF8(channel));
48       return version_info::Channel::CANARY;
49     }
50   }
52   if (channel_string)
53     channel_string->assign("unknown");
54   return version_info::Channel::UNKNOWN;
55 #else
56   // Always return empty string for non-branded builds.
57   return version_info::Channel::UNKNOWN;
58 #endif
61 }  // namespace
63 std::string GetVersionString() {
64   // TODO(robliao): Remove ScopedTracker below once https://crbug.com/422460 is
65   // fixed.
66   tracked_objects::ScopedTracker tracking_profile(
67       FROM_HERE_WITH_EXPLICIT_FUNCTION(
68           "422460 VersionInfo::CreateVersionString"));
70   return version_info::GetVersionStringWithModifier(GetChannelString());
73 std::string GetChannelString() {
74   std::string channel;
75   GetChannelImpl(&channel);
76   return channel;
79 version_info::Channel GetChannel() {
80   return GetChannelImpl(nullptr);