Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/aura/mus/property_converter.h" | 5 #include "ui/aura/mus/property_converter.h" |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "mojo/public/cpp/bindings/type_converter.h" | 8 #include "mojo/public/cpp/bindings/type_converter.h" |
| 9 #include "services/ui/public/cpp/property_type_converters.h" | 9 #include "services/ui/public/cpp/property_type_converters.h" |
| 10 #include "services/ui/public/interfaces/window_manager.mojom.h" | 10 #include "services/ui/public/interfaces/window_manager.mojom.h" |
| 11 #include "ui/aura/client/aura_constants.h" | 11 #include "ui/aura/client/aura_constants.h" |
| 12 #include "ui/base/class_property.h" | 12 #include "ui/base/class_property.h" |
| 13 | 13 |
| 14 namespace aura { | 14 namespace aura { |
| 15 | 15 |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 // Get the WindowProperty's value as a byte array. Only supports aura properties | 18 // Get the WindowProperty's value as a byte array. Only supports aura properties |
| 19 // that point to types with a matching std::vector<uint8_t> mojo::TypeConverter. | 19 // that point to types with a matching std::vector<uint8_t> mojo::TypeConverter. |
| 20 template <typename T> | 20 template <typename T> |
| 21 std::unique_ptr<std::vector<uint8_t>> GetArray(Window* window, | 21 std::unique_ptr<std::vector<uint8_t>> GetArray(Window* window, |
| 22 const WindowProperty<T>* key) { | 22 const WindowProperty<T>* key) { |
| 23 const T value = window->GetProperty(key); | 23 const T value = window->GetProperty(key); |
| 24 if (!value) | 24 if (!value) |
| 25 return base::MakeUnique<std::vector<uint8_t>>(); | 25 return base::MakeUnique<std::vector<uint8_t>>(); |
| 26 return base::MakeUnique<std::vector<uint8_t>>( | 26 return base::MakeUnique<std::vector<uint8_t>>( |
| 27 mojo::ConvertTo<std::vector<uint8_t>>(*value)); | 27 mojo::ConvertTo<std::vector<uint8_t>>(*value)); |
| 28 } | 28 } |
| 29 | 29 |
| 30 // A validator that always returns true regardless of its input. | |
| 31 bool AlwaysTrue(int64_t value) { | |
| 32 return true; | |
| 33 } | |
| 34 | |
| 35 bool ValidateResizeBehaviour(int64_t value) { | |
| 36 // Resize behaviour is a 3 bitfield. | |
| 37 return value >= 0 && | |
| 38 value <= (ui::mojom::kResizeBehaviorCanMaximize | | |
| 39 ui::mojom::kResizeBehaviorCanMinimize | | |
| 40 ui::mojom::kResizeBehaviorCanResize); | |
| 41 } | |
| 42 | |
| 43 bool ValidateShowState(int64_t value) { | |
| 44 return value == int64_t(ui::mojom::ShowState::DEFAULT) || | |
| 45 value == int64_t(ui::mojom::ShowState::NORMAL) || | |
| 46 value == int64_t(ui::mojom::ShowState::MINIMIZED) || | |
| 47 value == int64_t(ui::mojom::ShowState::MAXIMIZED) || | |
| 48 value == int64_t(ui::mojom::ShowState::INACTIVE) || | |
| 49 value == int64_t(ui::mojom::ShowState::FULLSCREEN) || | |
| 50 value == int64_t(ui::mojom::ShowState::DOCKED); | |
| 51 } | |
| 52 | |
| 30 } // namespace | 53 } // namespace |
| 31 | 54 |
| 55 PropertyConverter::PrimitiveProperty::PrimitiveProperty() {} | |
| 56 | |
| 57 PropertyConverter::PrimitiveProperty::PrimitiveProperty( | |
| 58 const PrimitiveProperty& property) = default; | |
| 59 | |
| 60 PropertyConverter::PrimitiveProperty::~PrimitiveProperty() {} | |
| 61 | |
| 62 // static | |
| 63 base::RepeatingCallback<bool(int64_t)> | |
| 64 PropertyConverter::CreateAcceptAnyValueCallback() { | |
| 65 return base::Bind(&AlwaysTrue); | |
| 66 } | |
| 67 | |
| 32 PropertyConverter::PropertyConverter() { | 68 PropertyConverter::PropertyConverter() { |
| 33 // Add known aura properties with associated mus properties. | 69 // Add known aura properties with associated mus properties. |
| 34 RegisterProperty(client::kAlwaysOnTopKey, | 70 RegisterProperty(client::kAlwaysOnTopKey, |
| 35 ui::mojom::WindowManager::kAlwaysOnTop_Property); | 71 ui::mojom::WindowManager::kAlwaysOnTop_Property, |
| 72 CreateAcceptAnyValueCallback()); | |
| 36 RegisterProperty(client::kAppIconKey, | 73 RegisterProperty(client::kAppIconKey, |
| 37 ui::mojom::WindowManager::kAppIcon_Property); | 74 ui::mojom::WindowManager::kAppIcon_Property); |
| 38 RegisterProperty(client::kAppIdKey, | 75 RegisterProperty(client::kAppIdKey, |
| 39 ui::mojom::WindowManager::kAppID_Property); | 76 ui::mojom::WindowManager::kAppID_Property); |
| 40 RegisterProperty(client::kImmersiveFullscreenKey, | 77 RegisterProperty(client::kImmersiveFullscreenKey, |
| 41 ui::mojom::WindowManager::kImmersiveFullscreen_Property); | 78 ui::mojom::WindowManager::kImmersiveFullscreen_Property, |
| 79 CreateAcceptAnyValueCallback()); | |
| 42 RegisterProperty(client::kNameKey, ui::mojom::WindowManager::kName_Property); | 80 RegisterProperty(client::kNameKey, ui::mojom::WindowManager::kName_Property); |
| 43 RegisterProperty(client::kPreferredSize, | 81 RegisterProperty(client::kPreferredSize, |
| 44 ui::mojom::WindowManager::kPreferredSize_Property); | 82 ui::mojom::WindowManager::kPreferredSize_Property); |
| 45 RegisterProperty(client::kResizeBehaviorKey, | 83 RegisterProperty(client::kResizeBehaviorKey, |
| 46 ui::mojom::WindowManager::kResizeBehavior_Property); | 84 ui::mojom::WindowManager::kResizeBehavior_Property, |
| 85 base::Bind(&ValidateResizeBehaviour)); | |
| 47 RegisterProperty(client::kRestoreBoundsKey, | 86 RegisterProperty(client::kRestoreBoundsKey, |
| 48 ui::mojom::WindowManager::kRestoreBounds_Property); | 87 ui::mojom::WindowManager::kRestoreBounds_Property); |
| 49 RegisterProperty(client::kShowStateKey, | 88 RegisterProperty(client::kShowStateKey, |
| 50 ui::mojom::WindowManager::kShowState_Property); | 89 ui::mojom::WindowManager::kShowState_Property, |
| 90 base::Bind(&ValidateShowState)); | |
| 51 RegisterProperty(client::kWindowIconKey, | 91 RegisterProperty(client::kWindowIconKey, |
| 52 ui::mojom::WindowManager::kWindowIcon_Property); | 92 ui::mojom::WindowManager::kWindowIcon_Property); |
| 53 RegisterProperty(client::kTitleKey, | 93 RegisterProperty(client::kTitleKey, |
| 54 ui::mojom::WindowManager::kWindowTitle_Property); | 94 ui::mojom::WindowManager::kWindowTitle_Property); |
| 55 } | 95 } |
| 56 | 96 |
| 57 PropertyConverter::~PropertyConverter() {} | 97 PropertyConverter::~PropertyConverter() {} |
| 58 | 98 |
| 59 bool PropertyConverter::IsTransportNameRegistered( | 99 bool PropertyConverter::IsTransportNameRegistered( |
| 60 const std::string& name) const { | 100 const std::string& name) const { |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 150 const std::string& transport_name, | 190 const std::string& transport_name, |
| 151 const std::vector<uint8_t>* data) { | 191 const std::vector<uint8_t>* data) { |
| 152 for (const auto& primitive_property : primitive_properties_) { | 192 for (const auto& primitive_property : primitive_properties_) { |
| 153 if (primitive_property.second.transport_name == transport_name) { | 193 if (primitive_property.second.transport_name == transport_name) { |
| 154 // aura::Window only supports property types that fit in PrimitiveType. | 194 // aura::Window only supports property types that fit in PrimitiveType. |
| 155 if (data->size() != 8u) { | 195 if (data->size() != 8u) { |
| 156 DVLOG(2) << "Property size mismatch (PrimitiveType): " | 196 DVLOG(2) << "Property size mismatch (PrimitiveType): " |
| 157 << transport_name; | 197 << transport_name; |
| 158 return; | 198 return; |
| 159 } | 199 } |
| 200 const int64_t v = *reinterpret_cast<const int64_t*>(&data->front()); | |
| 201 if (!primitive_property.second.validator.Run(v)) { | |
| 202 DVLOG(2) << "Property value rejected (PrimitiveType): " | |
| 203 << transport_name; | |
| 204 return; | |
| 205 } | |
| 160 const PrimitiveType value = mojo::ConvertTo<PrimitiveType>(*data); | 206 const PrimitiveType value = mojo::ConvertTo<PrimitiveType>(*data); |
| 161 // TODO(msw): Should aura::Window just store all properties by name? | 207 // TODO(msw): Should aura::Window just store all properties by name? |
| 162 window->SetPropertyInternal( | 208 window->SetPropertyInternal( |
| 163 primitive_property.first, primitive_property.second.property_name, | 209 primitive_property.first, primitive_property.second.property_name, |
| 164 nullptr, value, primitive_property.second.default_value); | 210 nullptr, value, primitive_property.second.default_value); |
| 165 return; | 211 return; |
| 166 } | 212 } |
| 167 } | 213 } |
| 168 | 214 |
| 169 for (const auto& image_property : image_properties_) { | 215 for (const auto& image_property : image_properties_) { |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 226 const std::string& transport_name, | 272 const std::string& transport_name, |
| 227 const std::vector<uint8_t>& transport_data, | 273 const std::vector<uint8_t>& transport_data, |
| 228 PrimitiveType* value) { | 274 PrimitiveType* value) { |
| 229 // aura::Window only supports property types that fit in PrimitiveType. | 275 // aura::Window only supports property types that fit in PrimitiveType. |
| 230 if (transport_data.size() != 8u) { | 276 if (transport_data.size() != 8u) { |
| 231 DVLOG(2) << "Property size mismatch (PrimitiveType): " << transport_name; | 277 DVLOG(2) << "Property size mismatch (PrimitiveType): " << transport_name; |
| 232 return false; | 278 return false; |
| 233 } | 279 } |
| 234 for (const auto& primitive_property : primitive_properties_) { | 280 for (const auto& primitive_property : primitive_properties_) { |
| 235 if (primitive_property.second.transport_name == transport_name) { | 281 if (primitive_property.second.transport_name == transport_name) { |
| 282 const int64_t v = | |
| 283 *reinterpret_cast<const int64_t*>(&transport_data.front()); | |
| 284 if (!primitive_property.second.validator.Run(v)) { | |
| 285 DVLOG(2) << "Property value rejected (PrimitiveType): " | |
| 286 << transport_name; | |
| 287 return false; | |
| 288 } | |
| 236 *value = mojo::ConvertTo<PrimitiveType>(transport_data); | 289 *value = mojo::ConvertTo<PrimitiveType>(transport_data); |
|
sky
2017/02/24 04:09:42
Isn't this line the same as 282? PrimitiveType is
Elliot Glaysher
2017/02/24 20:50:43
Removed duplicates here and in SetPropertyFromTran
| |
| 237 return true; | 290 return true; |
| 238 } | 291 } |
| 239 } | 292 } |
| 240 return false; | 293 return false; |
| 241 } | 294 } |
| 242 | 295 |
| 243 void PropertyConverter::RegisterProperty( | 296 void PropertyConverter::RegisterProperty( |
| 244 const WindowProperty<gfx::ImageSkia*>* property, | 297 const WindowProperty<gfx::ImageSkia*>* property, |
| 245 const char* transport_name) { | 298 const char* transport_name) { |
| 246 image_properties_[property] = transport_name; | 299 image_properties_[property] = transport_name; |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 269 } | 322 } |
| 270 | 323 |
| 271 void PropertyConverter::RegisterProperty( | 324 void PropertyConverter::RegisterProperty( |
| 272 const WindowProperty<base::string16*>* property, | 325 const WindowProperty<base::string16*>* property, |
| 273 const char* transport_name) { | 326 const char* transport_name) { |
| 274 string16_properties_[property] = transport_name; | 327 string16_properties_[property] = transport_name; |
| 275 transport_names_.insert(transport_name); | 328 transport_names_.insert(transport_name); |
| 276 } | 329 } |
| 277 | 330 |
| 278 } // namespace aura | 331 } // namespace aura |
| OLD | NEW |