Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / common / extensions / features / chrome_channel_feature_filter.cc
1 // Copyright 2014 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 "chrome/common/extensions/features/chrome_channel_feature_filter.h"
6
7 #include <map>
8
9 #include "base/lazy_instance.h"
10 #include "base/strings/stringprintf.h"
11 #include "chrome/common/extensions/features/feature_channel.h"
12 #include "extensions/common/features/simple_feature.h"
13
14 namespace extensions {
15
16 namespace {
17
18 static const char kFeatureChannelKey[] = "channel";
19
20 struct Mappings {
21   Mappings() {
22     channels["trunk"] = chrome::VersionInfo::CHANNEL_UNKNOWN;
23     channels["canary"] = chrome::VersionInfo::CHANNEL_CANARY;
24     channels["dev"] = chrome::VersionInfo::CHANNEL_DEV;
25     channels["beta"] = chrome::VersionInfo::CHANNEL_BETA;
26     channels["stable"] = chrome::VersionInfo::CHANNEL_STABLE;
27   }
28
29   std::map<std::string, chrome::VersionInfo::Channel> channels;
30 };
31
32 base::LazyInstance<Mappings> g_mappings = LAZY_INSTANCE_INITIALIZER;
33
34 std::string GetChannelName(chrome::VersionInfo::Channel channel) {
35   typedef std::map<std::string, chrome::VersionInfo::Channel> ChannelsMap;
36   ChannelsMap channels = g_mappings.Get().channels;
37   for (ChannelsMap::iterator i = channels.begin(); i != channels.end(); ++i) {
38     if (i->second == channel)
39       return i->first;
40   }
41   NOTREACHED();
42   return "unknown";
43 }
44
45 chrome::VersionInfo::Channel GetChannelValue(const std::string& name) {
46   typedef std::map<std::string, chrome::VersionInfo::Channel> ChannelsMap;
47   ChannelsMap channels = g_mappings.Get().channels;
48   ChannelsMap::const_iterator iter = channels.find(name);
49   CHECK(iter != channels.end());
50   return iter->second;
51 }
52
53 }  // namespace
54
55 ChromeChannelFeatureFilter::ChromeChannelFeatureFilter(SimpleFeature* feature)
56     : SimpleFeatureFilter(feature), channel_has_been_set_(false) {}
57
58 ChromeChannelFeatureFilter::~ChromeChannelFeatureFilter() {}
59
60 std::string ChromeChannelFeatureFilter::Parse(
61     const base::DictionaryValue* value) {
62   std::string channel_name;
63   if (value->GetString(kFeatureChannelKey, &channel_name)) {
64     channel_ = GetChannelValue(channel_name);
65   }
66
67   // The "trunk" channel uses VersionInfo::CHANNEL_UNKNOWN, so we need to keep
68   // track of whether the channel has been set or not separately.
69   channel_has_been_set_ |= value->HasKey(kFeatureChannelKey);
70
71   if (!channel_has_been_set_ && !feature()->HasDependencies()) {
72     return feature()->name() +
73            ": Must supply a value for channel or dependencies.";
74   }
75
76   return std::string();
77 }
78
79 Feature::Availability ChromeChannelFeatureFilter::IsAvailableToManifest(
80     const std::string& extension_id,
81     Manifest::Type type,
82     Manifest::Location location,
83     int manifest_version,
84     Feature::Platform platfortm) const {
85   if (channel_has_been_set_ && channel_ < GetCurrentChannel()) {
86     return Feature::CreateAvailability(
87         Feature::UNSUPPORTED_CHANNEL,
88         base::StringPrintf(
89             "'%s' requires Google Chrome %s channel or newer, but this is the "
90             "%s channel.",
91             feature()->name().c_str(),
92             GetChannelName(channel_).c_str(),
93             GetChannelName(GetCurrentChannel()).c_str()));
94   }
95   return Feature::CreateAvailability(Feature::IS_AVAILABLE, std::string());
96 }
97
98 }  // namespace extensions