Update To 11.40.268.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 // static
26 XWalkRuntimeFeatures* XWalkRuntimeFeatures::GetInstance() {
27   return Singleton<XWalkRuntimeFeatures>::get();
28 }
29
30 XWalkRuntimeFeatures::XWalkRuntimeFeatures()
31   : command_line_(0),
32     initialized_(false),
33     experimental_features_enabled_(false) {}
34
35 void XWalkRuntimeFeatures::Initialize(const CommandLine* cmd) {
36   command_line_ = cmd;
37   initialized_ = true;
38   runtime_features_.clear();
39   if (cmd->HasSwitch(switches::kExperimentalFeatures))
40     experimental_features_enabled_ = true;
41   else
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
46   // --enable/--disable
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"
60              , Experimental);
61 }
62
63 XWalkRuntimeFeatures::~XWalkRuntimeFeatures() {}
64
65 void XWalkRuntimeFeatures::AddFeature(const char* name,
66                                  const char* command_line_switch,
67                                  const char* description,
68                                  RuntimeFeatureStatus status) {
69   RuntimeFeature feature;
70   feature.name = name;
71   feature.description = description;
72   feature.command_line_switch = command_line_switch;
73   feature.status = status;
74   feature.enabled = false;
75
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;
84   } else {
85     feature.enabled = (status == Stable);
86   }
87
88   runtime_features_.push_back(feature);
89 }
90
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;
99
100   std::string output;
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';
110
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;
116
117     it->enabled ? command_line = "--disable-"
118       : command_line = "--enable-";
119     command_line += it->command_line_switch;
120
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';
128   }
129   std::cout << output << std::endl;
130 }
131
132 bool XWalkRuntimeFeatures::isFeatureEnabled(const char* name) const {
133   CHECK(initialized_);
134   if (experimental_features_enabled_)
135     return true;
136
137   RuntimeFeaturesList::const_iterator it = std::find_if(
138     runtime_features_.begin(), runtime_features_.end(),
139       MatchRuntimeFeature(name));
140   if (it == runtime_features_.end())
141     return false;
142   return (*it).enabled;
143 }
144
145 }  // namespace xwalk