Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / common / xwalk_runtime_features.cc
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.
4
5 #include "xwalk/runtime/common/xwalk_runtime_features.h"
6
7 #include <algorithm>
8 #include <functional>
9 #include <iostream> // NOLINT
10
11 #include "base/logging.h"
12 #include "xwalk/runtime/common/xwalk_switches.h"
13
14 namespace xwalk {
15
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;
21   }
22   const std::string name;
23 };
24
25 XWalkRuntimeFeatures::RuntimeFeature::RuntimeFeature()
26     : status(Experimental),
27       enabled(false) {
28 }
29
30 // static
31 XWalkRuntimeFeatures* XWalkRuntimeFeatures::GetInstance() {
32   return Singleton<XWalkRuntimeFeatures>::get();
33 }
34
35 XWalkRuntimeFeatures::XWalkRuntimeFeatures()
36   : command_line_(0),
37     initialized_(false),
38     experimental_features_enabled_(false) {}
39
40 void XWalkRuntimeFeatures::Initialize(const CommandLine* cmd) {
41   command_line_ = cmd;
42   initialized_ = true;
43   runtime_features_.clear();
44   if (cmd->HasSwitch(switches::kExperimentalFeatures))
45     experimental_features_enabled_ = true;
46   else
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
51   // --enable/--disable
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"
65              , Experimental);
66 }
67
68 XWalkRuntimeFeatures::~XWalkRuntimeFeatures() {}
69
70 void XWalkRuntimeFeatures::AddFeature(const char* name,
71                                  const char* command_line_switch,
72                                  const char* description,
73                                  RuntimeFeatureStatus status) {
74   RuntimeFeature feature;
75   feature.name = name;
76   feature.description = description;
77   feature.command_line_switch = command_line_switch;
78   feature.status = status;
79   feature.enabled = false;
80
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;
89   } else {
90     feature.enabled = (status == Stable);
91   }
92
93   runtime_features_.push_back(feature);
94 }
95
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;
104
105   std::string output;
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';
115
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;
121
122     it->enabled ? command_line = "--disable-"
123       : command_line = "--enable-";
124     command_line += it->command_line_switch;
125
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';
133   }
134   std::cout << output << std::endl;
135 }
136
137 bool XWalkRuntimeFeatures::isFeatureEnabled(const char* name) const {
138   CHECK(initialized_);
139   if (experimental_features_enabled_)
140     return true;
141
142   RuntimeFeaturesList::const_iterator it = std::find_if(
143     runtime_features_.begin(), runtime_features_.end(),
144       MatchRuntimeFeature(name));
145   if (it == runtime_features_.end())
146     return false;
147   return (*it).enabled;
148 }
149
150 }  // namespace xwalk