Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 1 | // Copyright 2014 The Chromium OS Authors. 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 | |
Alex Vakulenko | 7a1dc0b | 2014-08-15 11:45:46 -0700 | [diff] [blame] | 5 | #include <memory> |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 6 | #include <string> |
Christopher Wiley | 48e3728 | 2014-05-08 14:43:58 -0700 | [diff] [blame] | 7 | #include <sysexits.h> |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 8 | |
Christopher Wiley | 2d0369e | 2015-03-03 12:57:54 -0800 | [diff] [blame] | 9 | #include <base/cancelable_callback.h> |
Chris Sosa | ababc5c | 2014-04-09 15:42:01 -0700 | [diff] [blame] | 10 | #include <base/command_line.h> |
Alex Vakulenko | 61ad4db | 2015-01-20 10:50:04 -0800 | [diff] [blame] | 11 | #include <base/json/json_reader.h> |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 12 | #include <base/logging.h> |
Christopher Wiley | 48e3728 | 2014-05-08 14:43:58 -0700 | [diff] [blame] | 13 | #include <base/memory/ref_counted.h> |
Christopher Wiley | 2d0369e | 2015-03-03 12:57:54 -0800 | [diff] [blame] | 14 | #include <base/memory/weak_ptr.h> |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 15 | #include <base/strings/stringprintf.h> |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 16 | #include <base/values.h> |
Alex Vakulenko | a904434 | 2014-08-23 19:31:27 -0700 | [diff] [blame] | 17 | #include <chromeos/any.h> |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 18 | #include <chromeos/daemons/dbus_daemon.h> |
Alex Vakulenko | e4879a2 | 2014-08-20 15:47:36 -0700 | [diff] [blame] | 19 | #include <chromeos/data_encoding.h> |
Alex Vakulenko | 63cd5fa | 2014-08-27 08:04:22 -0700 | [diff] [blame] | 20 | #include <chromeos/dbus/data_serialization.h> |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 21 | #include <chromeos/dbus/dbus_method_invoker.h> |
| 22 | #include <chromeos/errors/error.h> |
Alex Vakulenko | e03af6d | 2015-04-20 11:00:54 -0700 | [diff] [blame] | 23 | #include <chromeos/strings/string_utils.h> |
Alex Vakulenko | 576c979 | 2014-09-22 16:49:45 -0700 | [diff] [blame] | 24 | #include <chromeos/variant_dictionary.h> |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 25 | #include <dbus/bus.h> |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 26 | #include <dbus/message.h> |
Christopher Wiley | 48e3728 | 2014-05-08 14:43:58 -0700 | [diff] [blame] | 27 | #include <dbus/object_proxy.h> |
Christopher Wiley | adb901d | 2014-05-07 09:58:45 -0700 | [diff] [blame] | 28 | #include <dbus/object_manager.h> |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 29 | #include <dbus/values_util.h> |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 30 | |
Alex Vakulenko | 2348e42 | 2014-11-21 08:57:57 -0800 | [diff] [blame] | 31 | #include "buffet/dbus-proxies.h" |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 32 | |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 33 | using chromeos::Error; |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 34 | using chromeos::ErrorPtr; |
Christopher Wiley | 2d0369e | 2015-03-03 12:57:54 -0800 | [diff] [blame] | 35 | using org::chromium::Buffet::ManagerProxy; |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 36 | |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 37 | namespace { |
| 38 | |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 39 | void usage() { |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 40 | printf(R"(Possible commands: |
| 41 | - TestMethod <message> |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 42 | - CheckDeviceRegistered |
| 43 | - GetDeviceInfo |
| 44 | - RegisterDevice param1=val1¶m2=val2... |
| 45 | - AddCommand '{"name":"command_name","parameters":{}}' |
Nathan Bullock | ed53c66 | 2015-01-07 09:57:42 -0500 | [diff] [blame] | 46 | - UpdateState prop_name prop_value |
Alex Vakulenko | 61ad4db | 2015-01-20 10:50:04 -0800 | [diff] [blame] | 47 | - GetState |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 48 | - PendingCommands |
Alex Vakulenko | e03af6d | 2015-04-20 11:00:54 -0700 | [diff] [blame] | 49 | - SetCommandVisibility pkg1.cmd1[,pkg2.cm2,...] [all|cloud|local|none] |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 50 | )"); |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Alex Vakulenko | 61ad4db | 2015-01-20 10:50:04 -0800 | [diff] [blame] | 53 | // Helpers for JsonToAny(). |
| 54 | template<typename T> |
| 55 | chromeos::Any GetJsonValue(const base::Value& json, |
| 56 | bool(base::Value::*fnc)(T*) const) { |
| 57 | T val; |
| 58 | CHECK((json.*fnc)(&val)); |
| 59 | return val; |
| 60 | } |
| 61 | |
| 62 | template<typename T> |
| 63 | chromeos::Any GetJsonList(const base::ListValue& list); // Prototype. |
| 64 | |
| 65 | // Converts a JSON value into an Any so it can be sent over D-Bus using |
| 66 | // UpdateState D-Bus method from Buffet. |
| 67 | chromeos::Any JsonToAny(const base::Value& json) { |
| 68 | chromeos::Any prop_value; |
| 69 | switch (json.GetType()) { |
| 70 | case base::Value::TYPE_NULL: |
| 71 | prop_value = nullptr; |
| 72 | break; |
| 73 | case base::Value::TYPE_BOOLEAN: |
| 74 | prop_value = GetJsonValue<bool>(json, &base::Value::GetAsBoolean); |
| 75 | break; |
| 76 | case base::Value::TYPE_INTEGER: |
| 77 | prop_value = GetJsonValue<int>(json, &base::Value::GetAsInteger); |
| 78 | break; |
| 79 | case base::Value::TYPE_DOUBLE: |
| 80 | prop_value = GetJsonValue<double>(json, &base::Value::GetAsDouble); |
| 81 | break; |
| 82 | case base::Value::TYPE_STRING: |
| 83 | prop_value = GetJsonValue<std::string>(json, &base::Value::GetAsString); |
| 84 | break; |
| 85 | case base::Value::TYPE_BINARY: |
| 86 | LOG(FATAL) << "Binary values should not happen"; |
| 87 | break; |
| 88 | case base::Value::TYPE_DICTIONARY: { |
| 89 | const base::DictionaryValue* dict = nullptr; // Still owned by |json|. |
| 90 | CHECK(json.GetAsDictionary(&dict)); |
| 91 | chromeos::VariantDictionary var_dict; |
| 92 | base::DictionaryValue::Iterator it(*dict); |
| 93 | while (!it.IsAtEnd()) { |
| 94 | var_dict.emplace(it.key(), JsonToAny(it.value())); |
| 95 | it.Advance(); |
| 96 | } |
| 97 | prop_value = var_dict; |
| 98 | break; |
| 99 | } |
| 100 | case base::Value::TYPE_LIST: { |
| 101 | const base::ListValue* list = nullptr; // Still owned by |json|. |
| 102 | CHECK(json.GetAsList(&list)); |
| 103 | CHECK(!list->empty()) << "Unable to deduce the type of list elements."; |
| 104 | switch ((*list->begin())->GetType()) { |
| 105 | case base::Value::TYPE_BOOLEAN: |
| 106 | prop_value = GetJsonList<bool>(*list); |
| 107 | break; |
| 108 | case base::Value::TYPE_INTEGER: |
| 109 | prop_value = GetJsonList<int>(*list); |
| 110 | break; |
| 111 | case base::Value::TYPE_DOUBLE: |
| 112 | prop_value = GetJsonList<double>(*list); |
| 113 | break; |
| 114 | case base::Value::TYPE_STRING: |
| 115 | prop_value = GetJsonList<std::string>(*list); |
| 116 | break; |
| 117 | case base::Value::TYPE_DICTIONARY: |
| 118 | prop_value = GetJsonList<chromeos::VariantDictionary>(*list); |
| 119 | break; |
| 120 | default: |
| 121 | LOG(FATAL) << "Unsupported JSON value type for list element: " |
| 122 | << (*list->begin())->GetType(); |
| 123 | } |
| 124 | break; |
| 125 | } |
| 126 | default: |
| 127 | LOG(FATAL) << "Unexpected JSON value type: " << json.GetType(); |
| 128 | break; |
| 129 | } |
| 130 | return prop_value; |
| 131 | } |
| 132 | |
| 133 | template<typename T> |
| 134 | chromeos::Any GetJsonList(const base::ListValue& list) { |
| 135 | std::vector<T> val; |
| 136 | val.reserve(list.GetSize()); |
| 137 | for (const base::Value* v : list) |
| 138 | val.push_back(JsonToAny(*v).Get<T>()); |
| 139 | return val; |
| 140 | } |
| 141 | |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 142 | class Daemon : public chromeos::DBusDaemon { |
Christopher Wiley | 48e3728 | 2014-05-08 14:43:58 -0700 | [diff] [blame] | 143 | public: |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 144 | Daemon() = default; |
| 145 | |
| 146 | protected: |
| 147 | int OnInit() override { |
| 148 | int return_code = chromeos::DBusDaemon::OnInit(); |
| 149 | if (return_code != EX_OK) |
| 150 | return return_code; |
| 151 | |
| 152 | object_manager_.reset(new org::chromium::Buffet::ObjectManagerProxy{bus_}); |
Christopher Wiley | 2d0369e | 2015-03-03 12:57:54 -0800 | [diff] [blame] | 153 | return_code = ScheduleActions(); |
| 154 | if (return_code == EX_USAGE) { |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 155 | usage(); |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 156 | } |
Christopher Wiley | 2d0369e | 2015-03-03 12:57:54 -0800 | [diff] [blame] | 157 | return return_code; |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 160 | void OnShutdown(int* return_code) override { |
| 161 | if (*return_code == EX_OK) |
| 162 | *return_code = exit_code_; |
| 163 | } |
Alex Vakulenko | 7a1dc0b | 2014-08-15 11:45:46 -0700 | [diff] [blame] | 164 | |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 165 | private: |
Christopher Wiley | 2d0369e | 2015-03-03 12:57:54 -0800 | [diff] [blame] | 166 | int ScheduleActions() { |
Alex Vakulenko | fc575a4 | 2015-04-02 14:31:10 -0700 | [diff] [blame] | 167 | auto args = base::CommandLine::ForCurrentProcess()->GetArgs(); |
Christopher Wiley | 2d0369e | 2015-03-03 12:57:54 -0800 | [diff] [blame] | 168 | |
| 169 | // Pop the command off of the args list. |
| 170 | std::string command = args.front(); |
| 171 | args.erase(args.begin()); |
| 172 | base::Callback<void(ManagerProxy*)> job; |
| 173 | if (command.compare("TestMethod") == 0) { |
| 174 | if (!args.empty() && !CheckArgs(command, args, 1)) |
| 175 | return EX_USAGE; |
| 176 | std::string message; |
| 177 | if (!args.empty()) |
| 178 | message = args.back(); |
| 179 | job = base::Bind(&Daemon::CallTestMethod, weak_factory_.GetWeakPtr(), |
| 180 | message); |
Christopher Wiley | 2d0369e | 2015-03-03 12:57:54 -0800 | [diff] [blame] | 181 | } else if (command.compare("CheckDeviceRegistered") == 0 || |
| 182 | command.compare("cr") == 0) { |
| 183 | if (!CheckArgs(command, args, 0)) |
| 184 | return EX_USAGE; |
| 185 | job = base::Bind(&Daemon::CallCheckDeviceRegistered, |
| 186 | weak_factory_.GetWeakPtr()); |
| 187 | } else if (command.compare("GetDeviceInfo") == 0 || |
| 188 | command.compare("di") == 0) { |
| 189 | if (!CheckArgs(command, args, 0)) |
| 190 | return EX_USAGE; |
| 191 | job = base::Bind(&Daemon::CallGetDeviceInfo, |
| 192 | weak_factory_.GetWeakPtr()); |
| 193 | } else if (command.compare("RegisterDevice") == 0 || |
| 194 | command.compare("rd") == 0) { |
| 195 | if (!args.empty() && !CheckArgs(command, args, 1)) |
| 196 | return EX_USAGE; |
| 197 | std::string dict; |
| 198 | if (!args.empty()) |
| 199 | dict = args.back(); |
| 200 | job = base::Bind(&Daemon::CallRegisterDevice, |
| 201 | weak_factory_.GetWeakPtr(), dict); |
| 202 | } else if (command.compare("UpdateState") == 0 || |
| 203 | command.compare("us") == 0) { |
| 204 | if (!CheckArgs(command, args, 2)) |
| 205 | return EX_USAGE; |
| 206 | job = base::Bind(&Daemon::CallUpdateState, weak_factory_.GetWeakPtr(), |
| 207 | args.front(), args.back()); |
| 208 | } else if (command.compare("GetState") == 0 || |
| 209 | command.compare("gs") == 0) { |
| 210 | if (!CheckArgs(command, args, 0)) |
| 211 | return EX_USAGE; |
| 212 | job = base::Bind(&Daemon::CallGetState, weak_factory_.GetWeakPtr()); |
| 213 | } else if (command.compare("AddCommand") == 0 || |
| 214 | command.compare("ac") == 0) { |
| 215 | if (!CheckArgs(command, args, 1)) |
| 216 | return EX_USAGE; |
| 217 | job = base::Bind(&Daemon::CallAddCommand, weak_factory_.GetWeakPtr(), |
| 218 | args.back()); |
| 219 | } else if (command.compare("PendingCommands") == 0 || |
| 220 | command.compare("pc") == 0) { |
| 221 | if (!CheckArgs(command, args, 0)) |
| 222 | return EX_USAGE; |
| 223 | // CallGetPendingCommands relies on ObjectManager but it is being |
| 224 | // initialized asynchronously without a way to get a callback when |
| 225 | // it is ready to be used. So, just wait a bit before calling its |
| 226 | // methods. |
| 227 | base::MessageLoop::current()->PostDelayedTask( |
| 228 | FROM_HERE, |
| 229 | base::Bind(&Daemon::CallGetPendingCommands, |
| 230 | weak_factory_.GetWeakPtr()), |
| 231 | base::TimeDelta::FromMilliseconds(100)); |
Alex Vakulenko | e03af6d | 2015-04-20 11:00:54 -0700 | [diff] [blame] | 232 | } else if (command.compare("SetCommandVisibility") == 0 || |
| 233 | command.compare("cv") == 0) { |
| 234 | if (!CheckArgs(command, args, 2)) |
| 235 | return EX_USAGE; |
| 236 | job = base::Bind(&Daemon::CallSetCommandVisibility, |
| 237 | weak_factory_.GetWeakPtr(), args.front(), args.back()); |
Christopher Wiley | 2d0369e | 2015-03-03 12:57:54 -0800 | [diff] [blame] | 238 | } else { |
| 239 | fprintf(stderr, "Unknown command: '%s'\n", command.c_str()); |
| 240 | return EX_USAGE; |
| 241 | } |
| 242 | if (!job.is_null()) |
| 243 | object_manager_->SetManagerAddedCallback(job); |
| 244 | timeout_task_.Reset( |
| 245 | base::Bind(&Daemon::OnJobTimeout, weak_factory_.GetWeakPtr())); |
| 246 | base::MessageLoop::current()->PostDelayedTask( |
| 247 | FROM_HERE, |
| 248 | timeout_task_.callback(), |
| 249 | base::TimeDelta::FromSeconds(10)); |
| 250 | |
| 251 | return EX_OK; |
| 252 | } |
| 253 | |
| 254 | void OnJobComplete() { |
| 255 | timeout_task_.Cancel(); |
| 256 | Quit(); |
| 257 | } |
| 258 | |
| 259 | void OnJobTimeout() { |
| 260 | fprintf(stderr, "Timed out before completing request."); |
| 261 | Quit(); |
| 262 | } |
| 263 | |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 264 | void ReportError(Error* error) { |
| 265 | fprintf(stderr, "Failed to receive a response: %s\n", |
| 266 | error->GetMessage().c_str()); |
| 267 | exit_code_ = EX_UNAVAILABLE; |
Christopher Wiley | 2d0369e | 2015-03-03 12:57:54 -0800 | [diff] [blame] | 268 | OnJobComplete(); |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | bool CheckArgs(const std::string& command, |
| 272 | const std::vector<std::string>& args, |
| 273 | size_t expected_arg_count) { |
| 274 | if (args.size() == expected_arg_count) |
| 275 | return true; |
| 276 | fprintf(stderr, "Invalid number of arguments for command '%s'\n", |
| 277 | command.c_str()); |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 278 | return false; |
| 279 | } |
| 280 | |
Christopher Wiley | 2d0369e | 2015-03-03 12:57:54 -0800 | [diff] [blame] | 281 | void CallTestMethod(const std::string& message, ManagerProxy* manager_proxy) { |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 282 | ErrorPtr error; |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 283 | std::string response_message; |
Christopher Wiley | 2d0369e | 2015-03-03 12:57:54 -0800 | [diff] [blame] | 284 | if (!manager_proxy->TestMethod(message, &response_message, &error)) { |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 285 | return ReportError(error.get()); |
Christopher Wiley | 48e3728 | 2014-05-08 14:43:58 -0700 | [diff] [blame] | 286 | } |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 287 | printf("Received a response: %s\n", response_message.c_str()); |
Christopher Wiley | 2d0369e | 2015-03-03 12:57:54 -0800 | [diff] [blame] | 288 | OnJobComplete(); |
Christopher Wiley | 48e3728 | 2014-05-08 14:43:58 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Christopher Wiley | 2d0369e | 2015-03-03 12:57:54 -0800 | [diff] [blame] | 291 | void CallCheckDeviceRegistered(ManagerProxy* manager_proxy) { |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 292 | ErrorPtr error; |
Christopher Wiley | 48e3728 | 2014-05-08 14:43:58 -0700 | [diff] [blame] | 293 | std::string device_id; |
Christopher Wiley | 2d0369e | 2015-03-03 12:57:54 -0800 | [diff] [blame] | 294 | if (!manager_proxy->CheckDeviceRegistered(&device_id, &error)) { |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 295 | return ReportError(error.get()); |
Christopher Wiley | 48e3728 | 2014-05-08 14:43:58 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 298 | printf("Device ID: %s\n", |
| 299 | device_id.empty() ? "<unregistered>" : device_id.c_str()); |
Christopher Wiley | 2d0369e | 2015-03-03 12:57:54 -0800 | [diff] [blame] | 300 | OnJobComplete(); |
Christopher Wiley | 48e3728 | 2014-05-08 14:43:58 -0700 | [diff] [blame] | 301 | } |
| 302 | |
Christopher Wiley | 2d0369e | 2015-03-03 12:57:54 -0800 | [diff] [blame] | 303 | void CallGetDeviceInfo(ManagerProxy* manager_proxy) { |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 304 | ErrorPtr error; |
Christopher Wiley | 48e3728 | 2014-05-08 14:43:58 -0700 | [diff] [blame] | 305 | std::string device_info; |
Christopher Wiley | 2d0369e | 2015-03-03 12:57:54 -0800 | [diff] [blame] | 306 | if (!manager_proxy->GetDeviceInfo(&device_info, &error)) { |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 307 | return ReportError(error.get()); |
Christopher Wiley | 48e3728 | 2014-05-08 14:43:58 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Nathan Bullock | 4b6c0fb | 2015-04-01 15:32:58 -0400 | [diff] [blame] | 310 | printf("%s\n", device_info.c_str()); |
Christopher Wiley | 2d0369e | 2015-03-03 12:57:54 -0800 | [diff] [blame] | 311 | OnJobComplete(); |
Christopher Wiley | 48e3728 | 2014-05-08 14:43:58 -0700 | [diff] [blame] | 312 | } |
| 313 | |
Christopher Wiley | 2d0369e | 2015-03-03 12:57:54 -0800 | [diff] [blame] | 314 | void CallRegisterDevice(const std::string& args, |
| 315 | ManagerProxy* manager_proxy) { |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 316 | chromeos::VariantDictionary params; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 317 | if (!args.empty()) { |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 318 | auto key_values = chromeos::data_encoding::WebParamsDecode(args); |
Alex Vakulenko | a0424dd | 2014-06-13 16:10:17 -0700 | [diff] [blame] | 319 | for (const auto& pair : key_values) { |
Alex Vakulenko | a904434 | 2014-08-23 19:31:27 -0700 | [diff] [blame] | 320 | params.insert(std::make_pair(pair.first, chromeos::Any(pair.second))); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 321 | } |
| 322 | } |
| 323 | |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 324 | ErrorPtr error; |
Christopher Wiley | 48e3728 | 2014-05-08 14:43:58 -0700 | [diff] [blame] | 325 | std::string device_id; |
Christopher Wiley | 2d0369e | 2015-03-03 12:57:54 -0800 | [diff] [blame] | 326 | if (!manager_proxy->RegisterDevice(params, &device_id, &error)) { |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 327 | return ReportError(error.get()); |
Christopher Wiley | 48e3728 | 2014-05-08 14:43:58 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 330 | printf("Device registered: %s\n", device_id.c_str()); |
Christopher Wiley | 2d0369e | 2015-03-03 12:57:54 -0800 | [diff] [blame] | 331 | OnJobComplete(); |
Christopher Wiley | 48e3728 | 2014-05-08 14:43:58 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Christopher Wiley | 2d0369e | 2015-03-03 12:57:54 -0800 | [diff] [blame] | 334 | void CallUpdateState(const std::string& prop, |
| 335 | const std::string& value, |
| 336 | ManagerProxy* manager_proxy) { |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 337 | ErrorPtr error; |
Alex Vakulenko | 61ad4db | 2015-01-20 10:50:04 -0800 | [diff] [blame] | 338 | std::string error_message; |
| 339 | std::unique_ptr<base::Value> json(base::JSONReader::ReadAndReturnError( |
| 340 | value, base::JSON_PARSE_RFC, nullptr, &error_message)); |
| 341 | if (!json) { |
| 342 | Error::AddTo(&error, FROM_HERE, chromeos::errors::json::kDomain, |
| 343 | chromeos::errors::json::kParseError, error_message); |
| 344 | return ReportError(error.get()); |
| 345 | } |
| 346 | |
| 347 | chromeos::VariantDictionary property_set{{prop, JsonToAny(*json)}}; |
Christopher Wiley | 2d0369e | 2015-03-03 12:57:54 -0800 | [diff] [blame] | 348 | if (!manager_proxy->UpdateState(property_set, &error)) { |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 349 | return ReportError(error.get()); |
Christopher Wiley | 48e3728 | 2014-05-08 14:43:58 -0700 | [diff] [blame] | 350 | } |
Christopher Wiley | 2d0369e | 2015-03-03 12:57:54 -0800 | [diff] [blame] | 351 | OnJobComplete(); |
Christopher Wiley | 48e3728 | 2014-05-08 14:43:58 -0700 | [diff] [blame] | 352 | } |
| 353 | |
Christopher Wiley | 2d0369e | 2015-03-03 12:57:54 -0800 | [diff] [blame] | 354 | void CallGetState(ManagerProxy* manager_proxy) { |
Alex Vakulenko | 61ad4db | 2015-01-20 10:50:04 -0800 | [diff] [blame] | 355 | std::string json; |
| 356 | ErrorPtr error; |
Christopher Wiley | 2d0369e | 2015-03-03 12:57:54 -0800 | [diff] [blame] | 357 | if (!manager_proxy->GetState(&json, &error)) { |
Alex Vakulenko | 61ad4db | 2015-01-20 10:50:04 -0800 | [diff] [blame] | 358 | return ReportError(error.get()); |
| 359 | } |
Nathan Bullock | 4b6c0fb | 2015-04-01 15:32:58 -0400 | [diff] [blame] | 360 | printf("%s\n", json.c_str()); |
Christopher Wiley | 2d0369e | 2015-03-03 12:57:54 -0800 | [diff] [blame] | 361 | OnJobComplete(); |
Alex Vakulenko | 61ad4db | 2015-01-20 10:50:04 -0800 | [diff] [blame] | 362 | } |
| 363 | |
Christopher Wiley | 2d0369e | 2015-03-03 12:57:54 -0800 | [diff] [blame] | 364 | void CallAddCommand(const std::string& command, ManagerProxy* manager_proxy) { |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 365 | ErrorPtr error; |
Vitaly Buka | 64fc5fc | 2015-03-24 12:42:24 -0700 | [diff] [blame] | 366 | std::string id; |
| 367 | if (!manager_proxy->AddCommand(command, &id, &error)) { |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 368 | return ReportError(error.get()); |
Alex Vakulenko | 665c885 | 2014-09-11 16:57:24 -0700 | [diff] [blame] | 369 | } |
Christopher Wiley | 2d0369e | 2015-03-03 12:57:54 -0800 | [diff] [blame] | 370 | OnJobComplete(); |
Alex Vakulenko | 665c885 | 2014-09-11 16:57:24 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 373 | void CallGetPendingCommands() { |
| 374 | printf("Pending commands:\n"); |
| 375 | for (auto* cmd : object_manager_->GetCommandInstances()) { |
Vitaly Buka | 4129dfa | 2015-04-29 12:16:58 -0700 | [diff] [blame] | 376 | printf("%10s - '%s' (id:%s)\n", cmd->status().c_str(), |
| 377 | cmd->name().c_str(), cmd->id().c_str()); |
Christopher Wiley | adb901d | 2014-05-07 09:58:45 -0700 | [diff] [blame] | 378 | } |
Christopher Wiley | 2d0369e | 2015-03-03 12:57:54 -0800 | [diff] [blame] | 379 | OnJobComplete(); |
Christopher Wiley | adb901d | 2014-05-07 09:58:45 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Alex Vakulenko | e03af6d | 2015-04-20 11:00:54 -0700 | [diff] [blame] | 382 | void CallSetCommandVisibility(const std::string& command_list, |
| 383 | const std::string& visibility, |
| 384 | ManagerProxy* manager_proxy) { |
| 385 | ErrorPtr error; |
| 386 | std::vector<std::string> commands = |
| 387 | chromeos::string_utils::Split(command_list, ",", true, true); |
| 388 | if (!manager_proxy->SetCommandVisibility(commands, visibility, &error)) { |
| 389 | return ReportError(error.get()); |
| 390 | } |
| 391 | OnJobComplete(); |
| 392 | } |
| 393 | |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 394 | std::unique_ptr<org::chromium::Buffet::ObjectManagerProxy> object_manager_; |
Christopher Wiley | c900e48 | 2015-02-15 15:42:04 -0800 | [diff] [blame] | 395 | int exit_code_{EX_OK}; |
Christopher Wiley | 2d0369e | 2015-03-03 12:57:54 -0800 | [diff] [blame] | 396 | base::CancelableCallback<void()> timeout_task_; |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 397 | |
Christopher Wiley | 2d0369e | 2015-03-03 12:57:54 -0800 | [diff] [blame] | 398 | base::WeakPtrFactory<Daemon> weak_factory_{this}; |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 399 | DISALLOW_COPY_AND_ASSIGN(Daemon); |
Christopher Wiley | 48e3728 | 2014-05-08 14:43:58 -0700 | [diff] [blame] | 400 | }; |
| 401 | |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 402 | } // anonymous namespace |
Christopher Wiley | 48e3728 | 2014-05-08 14:43:58 -0700 | [diff] [blame] | 403 | |
| 404 | int main(int argc, char** argv) { |
Alex Vakulenko | fc575a4 | 2015-04-02 14:31:10 -0700 | [diff] [blame] | 405 | base::CommandLine::Init(argc, argv); |
| 406 | base::CommandLine* cl = base::CommandLine::ForCurrentProcess(); |
| 407 | base::CommandLine::StringVector args = cl->GetArgs(); |
Christopher Wiley | 48e3728 | 2014-05-08 14:43:58 -0700 | [diff] [blame] | 408 | if (args.empty()) { |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 409 | usage(); |
Chris Sosa | ababc5c | 2014-04-09 15:42:01 -0700 | [diff] [blame] | 410 | return EX_USAGE; |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 413 | Daemon daemon; |
| 414 | return daemon.Run(); |
Christopher Wiley | 48e3728 | 2014-05-08 14:43:58 -0700 | [diff] [blame] | 415 | } |