blob: 16e9ec3d9fe8962f6c8da2dbe38f66fdcc6c3b1e [file] [log] [blame]
Vitaly Buka72410b22015-05-13 13:48:59 -07001// Copyright 2015 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
5#include "buffet/base_api_handler.h"
6
7#include "buffet/commands/command_instance.h"
8#include "buffet/commands/command_manager.h"
9#include "buffet/device_registration_info.h"
Vitaly Buka2f7efdb2015-05-27 16:00:21 -070010#include "buffet/states/state_manager.h"
Vitaly Buka72410b22015-05-13 13:48:59 -070011
12namespace buffet {
13
14namespace {
15
16// Helps to get parameters from native_types::Object representing
17// CommandInstance parameters.
Alex Vakulenko534a3122015-05-22 15:48:53 -070018class ParametersReader final {
Vitaly Buka72410b22015-05-13 13:48:59 -070019 public:
20 explicit ParametersReader(const native_types::Object* parameters)
21 : parameters_{parameters} {}
22
23 bool GetParameter(const std::string& name, std::string* value) const {
24 auto it = parameters_->find(name);
25 if (it == parameters_->end())
26 return false;
27 const StringValue* string_value = it->second->GetString();
28 if (!string_value)
29 return false;
30 *value = string_value->GetValue();
31 return true;
32 }
33
Vitaly Buka2f7efdb2015-05-27 16:00:21 -070034 bool GetParameter(const std::string& name, bool* value) const {
35 auto it = parameters_->find(name);
36 if (it == parameters_->end())
37 return false;
38 const BooleanValue* bool_value = it->second->GetBoolean();
39 if (!bool_value)
40 return false;
41 *value = bool_value->GetValue();
42 return true;
43 }
44
Vitaly Buka72410b22015-05-13 13:48:59 -070045 private:
46 const native_types::Object* parameters_;
47};
48
49} // namespace
50
51BaseApiHandler::BaseApiHandler(
52 const base::WeakPtr<DeviceRegistrationInfo>& device_info,
Vitaly Buka2f7efdb2015-05-27 16:00:21 -070053 const std::shared_ptr<StateManager>& state_manager,
Vitaly Buka72410b22015-05-13 13:48:59 -070054 const std::shared_ptr<CommandManager>& command_manager)
Vitaly Buka2f7efdb2015-05-27 16:00:21 -070055 : device_info_{device_info}, state_manager_{state_manager} {
Vitaly Buka72410b22015-05-13 13:48:59 -070056 command_manager->AddOnCommandAddedCallback(base::Bind(
57 &BaseApiHandler::OnCommandAdded, weak_ptr_factory_.GetWeakPtr()));
58}
59
60void BaseApiHandler::OnCommandAdded(CommandInstance* command) {
61 if (command->GetStatus() != CommandInstance::kStatusQueued)
62 return;
63
Vitaly Buka2f7efdb2015-05-27 16:00:21 -070064 if (command->GetName() == "base.updateBaseConfiguration")
65 return UpdateBaseConfiguration(command);
66
Vitaly Buka72410b22015-05-13 13:48:59 -070067 if (command->GetName() == "base.updateDeviceInfo")
68 return UpdateDeviceInfo(command);
69}
70
Vitaly Buka2f7efdb2015-05-27 16:00:21 -070071void BaseApiHandler::UpdateBaseConfiguration(CommandInstance* command) {
72 command->SetProgress({});
73
74 const BuffetConfig& config{device_info_->GetConfig()};
75 std::string anonymous_access_role{config.local_anonymous_access_role()};
76 bool discovery_enabled{config.local_discovery_enabled()};
77 bool pairing_enabled{config.local_pairing_enabled()};
78
79 ParametersReader parameters{&command->GetParameters()};
80 parameters.GetParameter("localAnonymousAccessMaxRole",
81 &anonymous_access_role);
82 parameters.GetParameter("localDiscoveryEnabled", &discovery_enabled);
83 parameters.GetParameter("localPairingEnabled", &pairing_enabled);
84
85 chromeos::VariantDictionary state{
86 {"base.localAnonymousAccessMaxRole", anonymous_access_role},
87 {"base.localDiscoveryEnabled", discovery_enabled},
88 {"base.localPairingEnabled", pairing_enabled},
89 };
90 if (!state_manager_->SetProperties(state, nullptr)) {
91 return command->Abort();
92 }
93
94 if (!device_info_->UpdateBaseConfig(anonymous_access_role, discovery_enabled,
95 pairing_enabled, nullptr)) {
96 return command->Abort();
97 }
98
99 command->Done();
100}
101
Vitaly Buka72410b22015-05-13 13:48:59 -0700102void BaseApiHandler::UpdateDeviceInfo(CommandInstance* command) {
103 command->SetProgress({});
104
105 const BuffetConfig& config{device_info_->GetConfig()};
106 std::string name{config.name()};
107 std::string description{config.description()};
108 std::string location{config.location()};
109
110 ParametersReader parameters(&command->GetParameters());
111 parameters.GetParameter("name", &name);
112 parameters.GetParameter("description", &description);
113 parameters.GetParameter("location", &location);
114
115 if (!device_info_->UpdateDeviceInfo(name, description, location, nullptr)) {
116 return command->Abort();
117 }
118
119 command->Done();
120}
121
122} // namespace buffet