Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(333)

Side by Side Diff: ui/aura/mus/property_converter.cc

Issue 2702423004: Validate incoming window properties. (Closed)
Patch Set: sky comments Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 ValidateResizeBehavior(int64_t value) {
36 // Resize behaviour is a 3 bitfield.
msw 2017/02/23 22:35:12 nit: consistent Behavior/behaviour spelling
Elliot Glaysher 2017/02/23 22:57:34 Done.
37 return value >= 0 && value <= 7;
msw 2017/02/23 22:35:12 optional nit: use value >= 0 && value <= ui::mojom
Elliot Glaysher 2017/02/23 22:57:34 Done.
38 }
39
40 bool ValidateShowState(int64_t value) {
41 return value == int64_t(ui::mojom::ShowState::DEFAULT) ||
msw 2017/02/23 22:35:12 It's a bummer that validating each enum requires l
Elliot Glaysher 2017/02/23 22:57:34 Acknowledged.
42 value == int64_t(ui::mojom::ShowState::NORMAL) ||
43 value == int64_t(ui::mojom::ShowState::MINIMIZED) ||
44 value == int64_t(ui::mojom::ShowState::MAXIMIZED) ||
45 value == int64_t(ui::mojom::ShowState::INACTIVE) ||
46 value == int64_t(ui::mojom::ShowState::FULLSCREEN) ||
47 value == int64_t(ui::mojom::ShowState::DOCKED);
48 }
49
30 } // namespace 50 } // namespace
31 51
52 PropertyConverter::PrimitiveProperty::PrimitiveProperty() {}
53
54 PropertyConverter::PrimitiveProperty::PrimitiveProperty(
55 const PrimitiveProperty& property) = default;
56
57 PropertyConverter::PrimitiveProperty::~PrimitiveProperty() {}
58
59 // static
60 base::RepeatingCallback<bool(int64_t)>
61 PropertyConverter::CreateAcceptAnyValueCallback() {
62 return base::Bind(&AlwaysTrue);
63 }
64
32 PropertyConverter::PropertyConverter() { 65 PropertyConverter::PropertyConverter() {
33 // Add known aura properties with associated mus properties. 66 // Add known aura properties with associated mus properties.
34 RegisterProperty(client::kAlwaysOnTopKey, 67 RegisterProperty(client::kAlwaysOnTopKey,
35 ui::mojom::WindowManager::kAlwaysOnTop_Property); 68 ui::mojom::WindowManager::kAlwaysOnTop_Property,
69 CreateAcceptAnyValueCallback());
msw 2017/02/23 22:35:11 Should we have a special callback for validating b
Elliot Glaysher 2017/02/23 22:57:35 Unsure; are there cases where a non-0, non-1 value
msw 2017/02/23 23:22:35 Yeah, perhaps there's no value in doing so... I wa
36 RegisterProperty(client::kAppIconKey, 70 RegisterProperty(client::kAppIconKey,
37 ui::mojom::WindowManager::kAppIcon_Property); 71 ui::mojom::WindowManager::kAppIcon_Property);
38 RegisterProperty(client::kAppIdKey, 72 RegisterProperty(client::kAppIdKey,
39 ui::mojom::WindowManager::kAppID_Property); 73 ui::mojom::WindowManager::kAppID_Property);
40 RegisterProperty(client::kImmersiveFullscreenKey, 74 RegisterProperty(client::kImmersiveFullscreenKey,
41 ui::mojom::WindowManager::kImmersiveFullscreen_Property); 75 ui::mojom::WindowManager::kImmersiveFullscreen_Property,
76 CreateAcceptAnyValueCallback());
42 RegisterProperty(client::kNameKey, ui::mojom::WindowManager::kName_Property); 77 RegisterProperty(client::kNameKey, ui::mojom::WindowManager::kName_Property);
43 RegisterProperty(client::kPreferredSize, 78 RegisterProperty(client::kPreferredSize,
44 ui::mojom::WindowManager::kPreferredSize_Property); 79 ui::mojom::WindowManager::kPreferredSize_Property);
45 RegisterProperty(client::kResizeBehaviorKey, 80 RegisterProperty(client::kResizeBehaviorKey,
46 ui::mojom::WindowManager::kResizeBehavior_Property); 81 ui::mojom::WindowManager::kResizeBehavior_Property,
82 base::Bind(&ValidateResizeBehavior));
47 RegisterProperty(client::kRestoreBoundsKey, 83 RegisterProperty(client::kRestoreBoundsKey,
48 ui::mojom::WindowManager::kRestoreBounds_Property); 84 ui::mojom::WindowManager::kRestoreBounds_Property);
49 RegisterProperty(client::kShowStateKey, 85 RegisterProperty(client::kShowStateKey,
50 ui::mojom::WindowManager::kShowState_Property); 86 ui::mojom::WindowManager::kShowState_Property,
87 base::Bind(&ValidateShowState));
51 RegisterProperty(client::kWindowIconKey, 88 RegisterProperty(client::kWindowIconKey,
52 ui::mojom::WindowManager::kWindowIcon_Property); 89 ui::mojom::WindowManager::kWindowIcon_Property);
53 RegisterProperty(client::kTitleKey, 90 RegisterProperty(client::kTitleKey,
54 ui::mojom::WindowManager::kWindowTitle_Property); 91 ui::mojom::WindowManager::kWindowTitle_Property);
55 } 92 }
56 93
57 PropertyConverter::~PropertyConverter() {} 94 PropertyConverter::~PropertyConverter() {}
58 95
59 bool PropertyConverter::IsTransportNameRegistered( 96 bool PropertyConverter::IsTransportNameRegistered(
60 const std::string& name) const { 97 const std::string& name) const {
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 const std::string& transport_name, 187 const std::string& transport_name,
151 const std::vector<uint8_t>* data) { 188 const std::vector<uint8_t>* data) {
152 for (const auto& primitive_property : primitive_properties_) { 189 for (const auto& primitive_property : primitive_properties_) {
153 if (primitive_property.second.transport_name == transport_name) { 190 if (primitive_property.second.transport_name == transport_name) {
154 // aura::Window only supports property types that fit in PrimitiveType. 191 // aura::Window only supports property types that fit in PrimitiveType.
155 if (data->size() != 8u) { 192 if (data->size() != 8u) {
156 DVLOG(2) << "Property size mismatch (PrimitiveType): " 193 DVLOG(2) << "Property size mismatch (PrimitiveType): "
157 << transport_name; 194 << transport_name;
158 return; 195 return;
159 } 196 }
197 const int64_t v = *reinterpret_cast<const int64_t*>(&data->front());
198 if (!primitive_property.second.validator.Run(v))
199 return;
msw 2017/02/23 22:35:12 We should probably log when invalid property value
Elliot Glaysher 2017/02/23 22:57:35 Done.
160 const PrimitiveType value = mojo::ConvertTo<PrimitiveType>(*data); 200 const PrimitiveType value = mojo::ConvertTo<PrimitiveType>(*data);
161 // TODO(msw): Should aura::Window just store all properties by name? 201 // TODO(msw): Should aura::Window just store all properties by name?
162 window->SetPropertyInternal( 202 window->SetPropertyInternal(
163 primitive_property.first, primitive_property.second.property_name, 203 primitive_property.first, primitive_property.second.property_name,
164 nullptr, value, primitive_property.second.default_value); 204 nullptr, value, primitive_property.second.default_value);
165 return; 205 return;
166 } 206 }
167 } 207 }
168 208
169 for (const auto& image_property : image_properties_) { 209 for (const auto& image_property : image_properties_) {
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
226 const std::string& transport_name, 266 const std::string& transport_name,
227 const std::vector<uint8_t>& transport_data, 267 const std::vector<uint8_t>& transport_data,
228 PrimitiveType* value) { 268 PrimitiveType* value) {
229 // aura::Window only supports property types that fit in PrimitiveType. 269 // aura::Window only supports property types that fit in PrimitiveType.
230 if (transport_data.size() != 8u) { 270 if (transport_data.size() != 8u) {
231 DVLOG(2) << "Property size mismatch (PrimitiveType): " << transport_name; 271 DVLOG(2) << "Property size mismatch (PrimitiveType): " << transport_name;
232 return false; 272 return false;
233 } 273 }
234 for (const auto& primitive_property : primitive_properties_) { 274 for (const auto& primitive_property : primitive_properties_) {
235 if (primitive_property.second.transport_name == transport_name) { 275 if (primitive_property.second.transport_name == transport_name) {
276 const int64_t v =
277 *reinterpret_cast<const int64_t*>(&transport_data.front());
278 if (!primitive_property.second.validator.Run(v))
279 return false;
msw 2017/02/23 22:35:12 Ditto
Elliot Glaysher 2017/02/23 22:57:35 Done.
236 *value = mojo::ConvertTo<PrimitiveType>(transport_data); 280 *value = mojo::ConvertTo<PrimitiveType>(transport_data);
237 return true; 281 return true;
238 } 282 }
239 } 283 }
240 return false; 284 return false;
241 } 285 }
242 286
243 void PropertyConverter::RegisterProperty( 287 void PropertyConverter::RegisterProperty(
244 const WindowProperty<gfx::ImageSkia*>* property, 288 const WindowProperty<gfx::ImageSkia*>* property,
245 const char* transport_name) { 289 const char* transport_name) {
(...skipping 23 matching lines...) Expand all
269 } 313 }
270 314
271 void PropertyConverter::RegisterProperty( 315 void PropertyConverter::RegisterProperty(
272 const WindowProperty<base::string16*>* property, 316 const WindowProperty<base::string16*>* property,
273 const char* transport_name) { 317 const char* transport_name) {
274 string16_properties_[property] = transport_name; 318 string16_properties_[property] = transport_name;
275 transport_names_.insert(transport_name); 319 transport_names_.insert(transport_name);
276 } 320 }
277 321
278 } // namespace aura 322 } // namespace aura
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698