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 #ifndef UI_AURA_MUS_PROPERTY_CONVERTER_H_ | 5 #ifndef UI_AURA_MUS_PROPERTY_CONVERTER_H_ |
| 6 #define UI_AURA_MUS_PROPERTY_CONVERTER_H_ | 6 #define UI_AURA_MUS_PROPERTY_CONVERTER_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <map> | 10 #include <map> |
| 11 #include <memory> | 11 #include <memory> |
| 12 #include <string> | 12 #include <string> |
| 13 #include <vector> | 13 #include <vector> |
| 14 | 14 |
| 15 #include "base/callback_forward.h" | |
| 15 #include "base/macros.h" | 16 #include "base/macros.h" |
| 16 #include "ui/aura/aura_export.h" | 17 #include "ui/aura/aura_export.h" |
| 17 #include "ui/aura/window.h" | 18 #include "ui/aura/window.h" |
| 18 | 19 |
| 19 namespace gfx { | 20 namespace gfx { |
| 20 class Rect; | 21 class Rect; |
| 21 class Size; | 22 class Size; |
| 22 } | 23 } |
| 23 | 24 |
| 24 namespace aura { | 25 namespace aura { |
| 25 | 26 |
| 26 // PropertyConverter is used to convert Window properties for transport to the | 27 // PropertyConverter is used to convert Window properties for transport to the |
| 27 // mus window server and back. Any time a property changes from one side it is | 28 // mus window server and back. Any time a property changes from one side it is |
| 28 // mapped to the other using this class. Not all Window properties need to map | 29 // mapped to the other using this class. Not all Window properties need to map |
| 29 // to server properties, and similarly not all transport properties need map to | 30 // to server properties, and similarly not all transport properties need map to |
| 30 // Window properties. | 31 // Window properties. |
| 31 class AURA_EXPORT PropertyConverter { | 32 class AURA_EXPORT PropertyConverter { |
| 32 public: | 33 public: |
| 33 // All primitive values are stored using this type. | 34 // All primitive values are stored using this type. |
| 34 using PrimitiveType = int64_t; | 35 using PrimitiveType = int64_t; |
| 35 | 36 |
| 36 PropertyConverter(); | 37 PropertyConverter(); |
| 37 ~PropertyConverter(); | 38 ~PropertyConverter(); |
| 38 | 39 |
| 40 // Creates a validation callback for use in RegisterProperty() which will | |
| 41 // accept any value. | |
| 42 static base::RepeatingCallback<bool(int64_t)> CreateAcceptAnyValueCallback(); | |
| 43 | |
| 39 // Returns true if RegisterProperty() has been called with the specified | 44 // Returns true if RegisterProperty() has been called with the specified |
| 40 // transport name. | 45 // transport name. |
| 41 bool IsTransportNameRegistered(const std::string& name) const; | 46 bool IsTransportNameRegistered(const std::string& name) const; |
| 42 | 47 |
| 43 // Maps a property on the Window to a property pushed to the server. Return | 48 // Maps a property on the Window to a property pushed to the server. Return |
| 44 // true if the property should be sent to the server, false if the property | 49 // true if the property should be sent to the server, false if the property |
| 45 // is only used locally. | 50 // is only used locally. |
| 46 bool ConvertPropertyForTransport( | 51 bool ConvertPropertyForTransport( |
| 47 Window* window, | 52 Window* window, |
| 48 const void* key, | 53 const void* key, |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 63 // Returns the value for a particular transport value. All primitives are | 68 // Returns the value for a particular transport value. All primitives are |
| 64 // serialized as a PrimitiveType, so this function may be used for any | 69 // serialized as a PrimitiveType, so this function may be used for any |
| 65 // primitive. Returns true on success and sets |value| accordingly. A return | 70 // primitive. Returns true on success and sets |value| accordingly. A return |
| 66 // value of false indicates the value isn't known or the property type isn't | 71 // value of false indicates the value isn't known or the property type isn't |
| 67 // primitive. | 72 // primitive. |
| 68 bool GetPropertyValueFromTransportValue( | 73 bool GetPropertyValueFromTransportValue( |
| 69 const std::string& transport_name, | 74 const std::string& transport_name, |
| 70 const std::vector<uint8_t>& transport_data, | 75 const std::vector<uint8_t>& transport_data, |
| 71 PrimitiveType* value); | 76 PrimitiveType* value); |
| 72 | 77 |
| 73 // Register a property to support conversion between mus and aura. | 78 // Register a property to support conversion between mus and |
|
msw
2017/02/23 22:35:12
nit: line wrapping
Elliot Glaysher
2017/02/23 22:57:35
Weird that neither emacs nor cl-format correct thi
| |
| 74 template<typename T> | 79 // aura. |validator| is a callback used to validate incoming values from |
| 75 void RegisterProperty(const WindowProperty<T>* property, | 80 // transport_data; if it returns false, the value is rejected. |
| 76 const char* transport_name) { | 81 template <typename T> |
| 82 void RegisterProperty( | |
| 83 const WindowProperty<T>* property, | |
| 84 const char* transport_name, | |
| 85 const base::RepeatingCallback<bool(int64_t)>& validator) { | |
| 77 PrimitiveProperty primitive_property; | 86 PrimitiveProperty primitive_property; |
| 78 primitive_property.property_name = property->name; | 87 primitive_property.property_name = property->name; |
| 79 primitive_property.transport_name = transport_name; | 88 primitive_property.transport_name = transport_name; |
| 80 primitive_property.default_value = | 89 primitive_property.default_value = |
| 81 ui::ClassPropertyCaster<T>::ToInt64(property->default_value); | 90 ui::ClassPropertyCaster<T>::ToInt64(property->default_value); |
| 91 primitive_property.validator = validator; | |
| 82 primitive_properties_[property] = primitive_property; | 92 primitive_properties_[property] = primitive_property; |
| 83 transport_names_.insert(transport_name); | 93 transport_names_.insert(transport_name); |
| 84 } | 94 } |
| 85 | 95 |
| 86 // Specializations for properties to pointer types supporting mojo conversion. | 96 // Specializations for properties to pointer types supporting mojo conversion. |
| 87 void RegisterProperty(const WindowProperty<gfx::ImageSkia*>* property, | 97 void RegisterProperty(const WindowProperty<gfx::ImageSkia*>* property, |
| 88 const char* transport_name); | 98 const char* transport_name); |
| 89 void RegisterProperty(const WindowProperty<gfx::Rect*>* property, | 99 void RegisterProperty(const WindowProperty<gfx::Rect*>* property, |
| 90 const char* transport_name); | 100 const char* transport_name); |
| 91 void RegisterProperty(const WindowProperty<gfx::Size*>* property, | 101 void RegisterProperty(const WindowProperty<gfx::Size*>* property, |
| 92 const char* transport_name); | 102 const char* transport_name); |
| 93 void RegisterProperty(const WindowProperty<std::string*>* property, | 103 void RegisterProperty(const WindowProperty<std::string*>* property, |
| 94 const char* transport_name); | 104 const char* transport_name); |
| 95 void RegisterProperty(const WindowProperty<base::string16*>* property, | 105 void RegisterProperty(const WindowProperty<base::string16*>* property, |
| 96 const char* transport_name); | 106 const char* transport_name); |
| 97 | 107 |
| 98 private: | 108 private: |
| 99 // Contains data needed to store and convert primitive-type properties. | 109 // Contains data needed to store and convert primitive-type properties. |
| 100 struct PrimitiveProperty { | 110 struct AURA_EXPORT PrimitiveProperty { |
| 111 PrimitiveProperty(); | |
| 112 PrimitiveProperty(const PrimitiveProperty& property); | |
| 113 ~PrimitiveProperty(); | |
| 114 | |
| 101 // The aura::WindowProperty::name used for storage. | 115 // The aura::WindowProperty::name used for storage. |
| 102 const char* property_name = nullptr; | 116 const char* property_name = nullptr; |
| 103 // The mus property name used for transport. | 117 // The mus property name used for transport. |
| 104 const char* transport_name = nullptr; | 118 const char* transport_name = nullptr; |
| 105 // The aura::WindowProperty::default_value stored using PrimitiveType. | 119 // The aura::WindowProperty::default_value stored using PrimitiveType. |
| 106 PrimitiveType default_value = 0; | 120 PrimitiveType default_value = 0; |
| 121 // A callback used to validate incoming values. | |
| 122 base::RepeatingCallback<bool(int64_t)> validator; | |
| 107 }; | 123 }; |
| 108 | 124 |
| 109 // A map of aura::WindowProperty<T> to PrimitiveProperty structs. | 125 // A map of aura::WindowProperty<T> to PrimitiveProperty structs. |
| 110 // This supports the internal codepaths for primitive types, eg. T=bool. | 126 // This supports the internal codepaths for primitive types, eg. T=bool. |
| 111 std::map<const void*, PrimitiveProperty> primitive_properties_; | 127 std::map<const void*, PrimitiveProperty> primitive_properties_; |
| 112 | 128 |
| 113 // Maps of aura::WindowProperty<T> to their mus property names. | 129 // Maps of aura::WindowProperty<T> to their mus property names. |
| 114 // This supports types that can be serialized for Mojo, eg. T=std::string*. | 130 // This supports types that can be serialized for Mojo, eg. T=std::string*. |
| 115 std::map<const WindowProperty<gfx::ImageSkia*>*, const char*> | 131 std::map<const WindowProperty<gfx::ImageSkia*>*, const char*> |
| 116 image_properties_; | 132 image_properties_; |
| 117 std::map<const WindowProperty<gfx::Rect*>*, const char*> rect_properties_; | 133 std::map<const WindowProperty<gfx::Rect*>*, const char*> rect_properties_; |
| 118 std::map<const WindowProperty<gfx::Size*>*, const char*> size_properties_; | 134 std::map<const WindowProperty<gfx::Size*>*, const char*> size_properties_; |
| 119 std::map<const WindowProperty<std::string*>*, const char*> string_properties_; | 135 std::map<const WindowProperty<std::string*>*, const char*> string_properties_; |
| 120 std::map<const WindowProperty<base::string16*>*, const char*> | 136 std::map<const WindowProperty<base::string16*>*, const char*> |
| 121 string16_properties_; | 137 string16_properties_; |
| 122 | 138 |
| 123 // Set of transport names supplied to RegisterProperty(). | 139 // Set of transport names supplied to RegisterProperty(). |
| 124 std::set<std::string> transport_names_; | 140 std::set<std::string> transport_names_; |
| 125 | 141 |
| 126 DISALLOW_COPY_AND_ASSIGN(PropertyConverter); | 142 DISALLOW_COPY_AND_ASSIGN(PropertyConverter); |
| 127 }; | 143 }; |
| 128 | 144 |
| 129 } // namespace aura | 145 } // namespace aura |
| 130 | 146 |
| 131 #endif // UI_AURA_MUS_PROPERTY_CONVERTER_H_ | 147 #endif // UI_AURA_MUS_PROPERTY_CONVERTER_H_ |
| OLD | NEW |