1 // Copyright (c) 2013 Intel Corporation. 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 "xwalk/runtime/common/xwalk_runtime_features.h"
9 #include <iostream> // NOLINT
11 #include "base/logging.h"
12 #include "xwalk/runtime/common/xwalk_switches.h"
16 struct MatchRuntimeFeature
17 : std::unary_function<XWalkRuntimeFeatures::RuntimeFeature, bool> {
18 explicit MatchRuntimeFeature(const std::string& name) : name(name) {}
19 bool operator()(const XWalkRuntimeFeatures::RuntimeFeature& entry) const {
20 return entry.name == name;
22 const std::string name;
25 XWalkRuntimeFeatures::RuntimeFeature::RuntimeFeature()
26 : status(Experimental),
31 XWalkRuntimeFeatures* XWalkRuntimeFeatures::GetInstance() {
32 return Singleton<XWalkRuntimeFeatures>::get();
35 XWalkRuntimeFeatures::XWalkRuntimeFeatures()
38 experimental_features_enabled_(false) {}
40 void XWalkRuntimeFeatures::Initialize(const CommandLine* cmd) {
43 runtime_features_.clear();
44 if (cmd->HasSwitch(switches::kExperimentalFeatures))
45 experimental_features_enabled_ = true;
47 experimental_features_enabled_ = false;
48 // Add new features here with the following parameters :
49 // - Name of the feature
50 // - Name of the command line switch which will be used after the
52 // - Description of the feature
53 // - Status of the feature : experimental which is turned off by default or
54 // stable which is turned on by default
55 AddFeature("SysApps", "sysapps",
56 "Master switch for the SysApps category of APIs", Stable);
57 AddFeature("RawSocketsAPI", "raw-sockets",
58 "JavaScript support for using TCP and UDP sockets", Stable);
59 AddFeature("DeviceCapabilitiesAPI", "device-capabilities",
60 "JavaScript support for peeking at device capabilities", Stable);
61 AddFeature("StorageAPI", "storage",
62 "JavaScript support to file system beyond W3C spec", Stable);
63 AddFeature("DialogAPI", "dialog",
64 "JavaScript support to create open/save native dialogs"
68 XWalkRuntimeFeatures::~XWalkRuntimeFeatures() {}
70 void XWalkRuntimeFeatures::AddFeature(const char* name,
71 const char* command_line_switch,
72 const char* description,
73 RuntimeFeatureStatus status) {
74 RuntimeFeature feature;
76 feature.description = description;
77 feature.command_line_switch = command_line_switch;
78 feature.status = status;
79 feature.enabled = false;
81 if (experimental_features_enabled_) {
82 feature.enabled = true;
83 } else if (command_line_->HasSwitch(
84 ("disable-" + std::string(command_line_switch)))) {
85 feature.enabled = false;
86 } else if (command_line_->HasSwitch(
87 ("enable-" + std::string(command_line_switch)))) {
88 feature.enabled = true;
90 feature.enabled = (status == Stable);
93 runtime_features_.push_back(feature);
96 void XWalkRuntimeFeatures::DumpFeaturesFlags() {
97 std::cout << "Available runtime features flags : " << std::endl;
98 const std::string command_line_title(" Command Line Switch");
99 const std::string description_title("Description");
100 const std::string status_title("Status");
101 const int command_line_description_space = 16;
102 const int description_status_space = 51;
103 const int status_space = 7;
106 output += command_line_title;
107 output += std::string(command_line_description_space, ' ');
108 output += description_title;
109 output += std::string(description_status_space, ' ');
110 output += status_title + std::string(status_space, ' ') + '\n';
111 int total_length = status_space + description_status_space
112 + command_line_description_space + description_title.length()
113 + command_line_title.length() + status_title.length();
114 output += std::string(total_length, '-') + '\n';
116 RuntimeFeaturesList::const_iterator it = runtime_features_.begin();
117 for (; it != runtime_features_.end(); ++it) {
118 std::string status = (it->status == Stable) ?
119 std::string("Stable") : std::string("Experimental");
120 std::string command_line;
122 it->enabled ? command_line = "--disable-"
123 : command_line = "--enable-";
124 command_line += it->command_line_switch;
126 output += command_line;
127 std::string space(command_line_description_space
128 + command_line_title.length() - command_line.length(), ' ');
129 output += space + it->description;
130 std::string space2(description_status_space + description_title.length()
131 - it->description.length(), ' ');
132 output += space2 + status + '\n';
134 std::cout << output << std::endl;
137 bool XWalkRuntimeFeatures::isFeatureEnabled(const char* name) const {
139 if (experimental_features_enabled_)
142 RuntimeFeaturesList::const_iterator it = std::find_if(
143 runtime_features_.begin(), runtime_features_.end(),
144 MatchRuntimeFeature(name));
145 if (it == runtime_features_.end())
147 return (*it).enabled;