Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
settinghandler.cc
Go to the documentation of this file.
1 
18 #if !ION_PRODUCTION
19 
21 
22 #include "ion/base/invalid.h"
23 #include "ion/base/setting.h"
25 #include "ion/base/stringutils.h"
28 
29 ION_REGISTER_ASSETS(IonRemoteSettingsRoot);
30 
31 namespace ion {
32 namespace remote {
33 
34 using base::SettingBase;
35 using base::SettingManager;
36 
37 namespace {
38 
44 static const std::string GetAllSettings() {
45  std::string str;
46 
47  const SettingManager::SettingMap settings = SettingManager::GetAllSettings();
48  for (SettingManager::SettingMap::const_iterator it = settings.begin();
49  it != settings.end(); ++it) {
50  const std::string type_desc_string =
51  it->second->GetTypeDescriptor().empty() ? " " :
52  it->second->GetTypeDescriptor();
53  const std::string doc_string =
54  it->second->GetDocString().empty() ? " " : it->second->GetDocString();
55  str += base::UrlEncodeString(it->first) + "/" +
56  base::UrlEncodeString(type_desc_string) + "/" +
57  base::UrlEncodeString(doc_string) + "/" +
58  base::UrlEncodeString(it->second->ToString()) + "|";
59  }
60 
61  return str;
62 }
63 
67 static const std::string SetSettingValue(const HttpServer::QueryMap& args) {
68  HttpServer::QueryMap::const_iterator name_it = args.find("name");
69  HttpServer::QueryMap::const_iterator value_it = args.find("value");
70 
71  std::string response;
72  if (name_it != args.end() && value_it != args.end()) {
73  SettingBase* setting = SettingManager::GetSetting(name_it->second);
77  if (setting && setting->FromString(value_it->second))
78  response = setting->ToString();
79  }
80 
81  return response;
82 }
83 
84 } // anonymous namespace
85 
87  : HttpServer::RequestHandler("/ion/settings") {
88  IonRemoteSettingsRoot::RegisterAssetsOnce();
89 }
90 
92 
94  const std::string& path_in, const HttpServer::QueryMap& args,
95  std::string* content_type) {
96  const std::string path = path_in.empty() ? "index.html" : path_in;
97 
98  if (path == "get_all_settings") {
99  return GetAllSettings();
100  } else if (path == "set_setting_value") {
101  return SetSettingValue(args);
102  } else {
103  const std::string& data = base::ZipAssetManager::GetFileData(
104  "ion/settings/" + path);
105  if (base::IsInvalidReference(data)) {
106  return std::string();
107  } else {
109  if (base::EndsWith(path, "html"))
110  *content_type = "text/html";
111  return data;
112  }
113  }
114 }
115 
116 } // namespace remote
117 } // namespace ion
118 
119 #endif
bool IsInvalidReference(const T &value)
IsInvalidReference() returns true if a passed const reference of type T has an address of InvalidRefe...
Definition: invalid.h:41
const std::string & str
const std::string HandleRequest(const std::string &path, const HttpServer::QueryMap &args, std::string *content_type) override
The HandleRequest() function is passed the path (relative to its base path) of the file or directory ...
static const std::string & GetFileData(const std::string &filename)
Returns the data of the passed filename if the manager contains it.
bool EndsWith(const std::string &target, const std::string &end)
Returns whether target ends with end.
Definition: stringutils.h:81
std::map< std::string, std::string > QueryMap
Definition: httpserver.h:35
ION_API std::string UrlEncodeString(const std::string &str)
Returns a URL-encoded version of a string.
Definition: stringutils.cc:246
ION_REGISTER_ASSETS(IonRemoteSettingsRoot)
Copyright 2016 Google Inc.