| // Copyright 2019 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #include "ui/events/ozone/chromeos/gesture_properties_service.h" |
| |
| namespace ui { |
| |
| namespace { |
| |
| using ozone::mojom::GesturePropValue; |
| |
| ozone::mojom::GesturePropValuePtr GesturePropValueFromProp(GesturesProp* prop) { |
| if (prop == nullptr) { |
| return nullptr; |
| } |
| switch (prop->type()) { |
| case GesturesProp::PropertyType::PT_INT: |
| return GesturePropValue::NewInts(prop->GetIntValue()); |
| case GesturesProp::PropertyType::PT_SHORT: |
| return GesturePropValue::NewShorts(prop->GetShortValue()); |
| case GesturesProp::PropertyType::PT_BOOL: |
| return GesturePropValue::NewBools(prop->GetBoolValue()); |
| case GesturesProp::PropertyType::PT_STRING: |
| return GesturePropValue::NewStr(prop->GetStringValue()); |
| case GesturesProp::PropertyType::PT_REAL: |
| return GesturePropValue::NewReals(prop->GetDoubleValue()); |
| } |
| } |
| |
| bool PropertyTypeMatchesValues(ui::GesturePropertyProvider::PropertyType type, |
| GesturePropValue::Tag values_tag) { |
| switch (type) { |
| case ui::GesturePropertyProvider::PT_INT: |
| return values_tag == GesturePropValue::Tag::INTS; |
| case ui::GesturePropertyProvider::PT_SHORT: |
| return values_tag == GesturePropValue::Tag::SHORTS; |
| case ui::GesturePropertyProvider::PT_BOOL: |
| return values_tag == GesturePropValue::Tag::BOOLS; |
| case ui::GesturePropertyProvider::PT_STRING: |
| return values_tag == GesturePropValue::Tag::STR; |
| case ui::GesturePropertyProvider::PT_REAL: |
| return values_tag == GesturePropValue::Tag::REALS; |
| } |
| // This should never happen. |
| return false; |
| } |
| |
| bool TrySetPropertyValues(GesturesProp* property, |
| ozone::mojom::GesturePropValuePtr values) { |
| switch (property->type()) { |
| case ui::GesturePropertyProvider::PT_INT: |
| return property->SetIntValue(values->get_ints()); |
| case ui::GesturePropertyProvider::PT_SHORT: |
| return property->SetShortValue(values->get_shorts()); |
| case ui::GesturePropertyProvider::PT_BOOL: |
| return property->SetBoolValue(values->get_bools()); |
| case ui::GesturePropertyProvider::PT_STRING: |
| return property->SetStringValue(values->get_str()); |
| case ui::GesturePropertyProvider::PT_REAL: |
| return property->SetDoubleValue(values->get_reals()); |
| } |
| } |
| |
| } // namespace |
| |
| GesturePropertiesService::GesturePropertiesService( |
| GesturePropertyProvider* provider, |
| ozone::mojom::GesturePropertiesServiceRequest request) |
| : prop_provider_(provider), binding_(this, std::move(request)) {} |
| |
| void GesturePropertiesService::ListDevices(ListDevicesCallback reply) { |
| base::flat_map<int, std::string> response = {}; |
| std::vector<int> ids; |
| prop_provider_->GetDeviceIdsByType(DT_ALL, &ids); |
| for (size_t i = 0; i < ids.size(); ++i) { |
| response.emplace(ids[i], prop_provider_->GetDeviceNameById(ids[i])); |
| } |
| std::move(reply).Run(response); |
| } |
| |
| void GesturePropertiesService::ListProperties(int device_id, |
| ListPropertiesCallback reply) { |
| std::vector<std::string> response = |
| prop_provider_->GetPropertyNamesById(device_id); |
| std::move(reply).Run(response); |
| } |
| |
| void GesturePropertiesService::GetProperty(int device_id, |
| const std::string& name, |
| GetPropertyCallback reply) { |
| bool is_read_only = true; |
| GesturesProp* property = prop_provider_->GetProperty(device_id, name); |
| ozone::mojom::GesturePropValuePtr prop_value = |
| GesturePropValueFromProp(property); |
| if (property != nullptr) { |
| is_read_only = property->IsReadOnly(); |
| } |
| std::move(reply).Run(is_read_only, std::move(prop_value)); |
| } |
| |
| void GesturePropertiesService::SetProperty( |
| int device_id, |
| const std::string& name, |
| ozone::mojom::GesturePropValuePtr values, |
| SetPropertyCallback reply) { |
| GesturesProp* property = prop_provider_->GetProperty(device_id, name); |
| if (property == NULL) { |
| std::move(reply).Run(ozone::mojom::SetGesturePropErrorCode::NOT_FOUND); |
| return; |
| } |
| if (property->IsReadOnly()) { |
| std::move(reply).Run(ozone::mojom::SetGesturePropErrorCode::READ_ONLY); |
| return; |
| } |
| if (!PropertyTypeMatchesValues(property->type(), values->which())) { |
| std::move(reply).Run(ozone::mojom::SetGesturePropErrorCode::TYPE_MISMATCH); |
| return; |
| } |
| size_t num_values; |
| switch (values->which()) { |
| case ozone::mojom::GesturePropValue::Tag::INTS: |
| num_values = values->get_ints().size(); |
| break; |
| case ozone::mojom::GesturePropValue::Tag::SHORTS: |
| num_values = values->get_shorts().size(); |
| break; |
| case ozone::mojom::GesturePropValue::Tag::BOOLS: |
| num_values = values->get_bools().size(); |
| break; |
| case ozone::mojom::GesturePropValue::Tag::STR: |
| num_values = 1; |
| break; |
| case ozone::mojom::GesturePropValue::Tag::REALS: |
| num_values = values->get_reals().size(); |
| break; |
| } |
| if (num_values != property->count()) { |
| std::move(reply).Run(ozone::mojom::SetGesturePropErrorCode::SIZE_MISMATCH); |
| return; |
| } |
| |
| bool did_set = TrySetPropertyValues(property, std::move(values)); |
| std::move(reply).Run( |
| did_set ? ozone::mojom::SetGesturePropErrorCode::SUCCESS |
| : ozone::mojom::SetGesturePropErrorCode::UNKNOWN_ERROR); |
| } |
| |
| } // namespace ui |