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;
26 XWalkRuntimeFeatures* XWalkRuntimeFeatures::GetInstance() {
27 return Singleton<XWalkRuntimeFeatures>::get();
30 XWalkRuntimeFeatures::XWalkRuntimeFeatures()
33 experimental_features_enabled_(false) {}
35 void XWalkRuntimeFeatures::Initialize(const CommandLine* cmd) {
38 runtime_features_.clear();
39 if (cmd->HasSwitch(switches::kExperimentalFeatures))
40 experimental_features_enabled_ = true;
42 experimental_features_enabled_ = false;
43 // Add new features here with the following parameters :
44 // - Name of the feature
45 // - Name of the command line switch which will be used after the
47 // - Description of the feature
48 // - Status of the feature : experimental which is turned off by default or
49 // stable which is turned on by default
50 AddFeature("SysApps", "sysapps",
51 "Master switch for the SysApps category of APIs", Stable);
52 AddFeature("RawSocketsAPI", "raw-sockets",
53 "JavaScript support for using TCP and UDP sockets", Stable);
54 AddFeature("DeviceCapabilitiesAPI", "device-capabilities",
55 "JavaScript support for peeking at device capabilities", Stable);
56 AddFeature("StorageAPI", "storage",
57 "JavaScript support to file system beyond W3C spec", Stable);
58 AddFeature("DialogAPI", "dialog",
59 "JavaScript support to create open/save native dialogs"
63 XWalkRuntimeFeatures::~XWalkRuntimeFeatures() {}
65 void XWalkRuntimeFeatures::AddFeature(const char* name,
66 const char* command_line_switch,
67 const char* description,
68 RuntimeFeatureStatus status) {
69 RuntimeFeature feature;
71 feature.description = description;
72 feature.command_line_switch = command_line_switch;
73 feature.status = status;
74 feature.enabled = false;
76 if (experimental_features_enabled_) {
77 feature.enabled = true;
78 } else if (command_line_->HasSwitch(
79 ("disable-" + std::string(command_line_switch)))) {
80 feature.enabled = false;
81 } else if (command_line_->HasSwitch(
82 ("enable-" + std::string(command_line_switch)))) {
83 feature.enabled = true;
85 feature.enabled = (status == Stable);
88 runtime_features_.push_back(feature);
91 void XWalkRuntimeFeatures::DumpFeaturesFlags() {
92 std::cout << "Available runtime features flags : " << std::endl;
93 const std::string command_line_title(" Command Line Switch");
94 const std::string description_title("Description");
95 const std::string status_title("Status");
96 const int command_line_description_space = 16;
97 const int description_status_space = 51;
98 const int status_space = 7;
101 output += command_line_title;
102 output += std::string(command_line_description_space, ' ');
103 output += description_title;
104 output += std::string(description_status_space, ' ');
105 output += status_title + std::string(status_space, ' ') + '\n';
106 int total_length = status_space + description_status_space
107 + command_line_description_space + description_title.length()
108 + command_line_title.length() + status_title.length();
109 output += std::string(total_length, '-') + '\n';
111 RuntimeFeaturesList::const_iterator it = runtime_features_.begin();
112 for (; it != runtime_features_.end(); ++it) {
113 std::string status = (it->status == Stable) ?
114 std::string("Stable") : std::string("Experimental");
115 std::string command_line;
117 it->enabled ? command_line = "--disable-"
118 : command_line = "--enable-";
119 command_line += it->command_line_switch;
121 output += command_line;
122 std::string space(command_line_description_space
123 + command_line_title.length() - command_line.length(), ' ');
124 output += space + it->description;
125 std::string space2(description_status_space + description_title.length()
126 - it->description.length(), ' ');
127 output += space2 + status + '\n';
129 std::cout << output << std::endl;
132 bool XWalkRuntimeFeatures::isFeatureEnabled(const char* name) const {
134 if (experimental_features_enabled_)
137 RuntimeFeaturesList::const_iterator it = std::find_if(
138 runtime_features_.begin(), runtime_features_.end(),
139 MatchRuntimeFeature(name));
140 if (it == runtime_features_.end())
142 return (*it).enabled;