| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 The Chromium 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 #ifndef SERVICES_UI_PUBLIC_CPP_WINDOW_PROPERTY_H_ | |
| 6 #define SERVICES_UI_PUBLIC_CPP_WINDOW_PROPERTY_H_ | |
| 7 | |
| 8 #include <stdint.h> | |
| 9 | |
| 10 // To define a new WindowProperty: | |
| 11 // | |
| 12 // #include "services/ui/public/cpp/window_property.h" | |
| 13 // | |
| 14 // MUS_DECLARE_EXPORTED_WINDOW_PROPERTY_TYPE(FOO_EXPORT, MyType); | |
| 15 // namespace foo { | |
| 16 // // Use this to define an exported property that is primitive, | |
| 17 // // or a pointer you don't want automatically deleted. | |
| 18 // MUS_DEFINE_WINDOW_PROPERTY_KEY(MyType, kMyKey, MyDefault); | |
| 19 // | |
| 20 // // Use this to define an exported property whose value is a heap | |
| 21 // // allocated object, and has to be owned and freed by the window. | |
| 22 // MUS_DEFINE_OWNED_WINDOW_PROPERTY_KEY(gfx::Rect, kRestoreBoundsKey, | |
| 23 // nullptr); | |
| 24 // | |
| 25 // // Use this to define a non exported property that is primitive, | |
| 26 // // or a pointer you don't want to automatically deleted, and is used | |
| 27 // // only in a specific file. This will define the property in an unnamed | |
| 28 // // namespace which cannot be accessed from another file. | |
| 29 // MUS_DEFINE_LOCAL_WINDOW_PROPERTY_KEY(MyType, kMyKey, MyDefault); | |
| 30 // | |
| 31 // } // foo namespace | |
| 32 // | |
| 33 // To define a new type used for WindowProperty. | |
| 34 // | |
| 35 // // outside all namespaces: | |
| 36 // MUS_DECLARE_EXPORTED_WINDOW_PROPERTY_TYPE(FOO_EXPORT, MyType) | |
| 37 // | |
| 38 // If a property type is not exported, use | |
| 39 // MUS_DECLARE_WINDOW_PROPERTY_TYPE(MyType) which is a shorthand for | |
| 40 // MUS_DECLARE_EXPORTED_WINDOW_PROPERTY_TYPE(, MyType). | |
| 41 | |
| 42 namespace ui { | |
| 43 | |
| 44 template <typename T> | |
| 45 void Window::SetSharedProperty(const std::string& name, const T& data) { | |
| 46 const std::vector<uint8_t> bytes = | |
| 47 mojo::TypeConverter<std::vector<uint8_t>, T>::Convert(data); | |
| 48 SetSharedPropertyInternal(name, &bytes); | |
| 49 } | |
| 50 | |
| 51 template <typename T> | |
| 52 T Window::GetSharedProperty(const std::string& name) const { | |
| 53 DCHECK(HasSharedProperty(name)); | |
| 54 auto it = properties_.find(name); | |
| 55 return mojo::TypeConverter<T, std::vector<uint8_t>>::Convert(it->second); | |
| 56 } | |
| 57 | |
| 58 namespace { | |
| 59 | |
| 60 // No single new-style cast works for every conversion to/from int64_t, so we | |
| 61 // need this helper class. A third specialization is needed for bool because | |
| 62 // MSVC warning C4800 (forcing value to bool) is not suppressed by an explicit | |
| 63 // cast (!). | |
| 64 template <typename T> | |
| 65 class WindowPropertyCaster { | |
| 66 public: | |
| 67 static int64_t ToInt64(T x) { return static_cast<int64_t>(x); } | |
| 68 static T FromInt64(int64_t x) { return static_cast<T>(x); } | |
| 69 }; | |
| 70 template <typename T> | |
| 71 class WindowPropertyCaster<T*> { | |
| 72 public: | |
| 73 static int64_t ToInt64(T* x) { return reinterpret_cast<int64_t>(x); } | |
| 74 static T* FromInt64(int64_t x) { return reinterpret_cast<T*>(x); } | |
| 75 }; | |
| 76 template <> | |
| 77 class WindowPropertyCaster<bool> { | |
| 78 public: | |
| 79 static int64_t ToInt64(bool x) { return static_cast<int64_t>(x); } | |
| 80 static bool FromInt64(int64_t x) { return x != 0; } | |
| 81 }; | |
| 82 | |
| 83 } // namespace | |
| 84 | |
| 85 template <typename T> | |
| 86 struct WindowProperty { | |
| 87 T default_value; | |
| 88 const char* name; | |
| 89 Window::PropertyDeallocator deallocator; | |
| 90 }; | |
| 91 | |
| 92 template <typename T> | |
| 93 void Window::SetLocalProperty(const WindowProperty<T>* property, T value) { | |
| 94 int64_t old = SetLocalPropertyInternal( | |
| 95 property, property->name, | |
| 96 value == property->default_value ? nullptr : property->deallocator, | |
| 97 WindowPropertyCaster<T>::ToInt64(value), | |
| 98 WindowPropertyCaster<T>::ToInt64(property->default_value)); | |
| 99 if (property->deallocator && | |
| 100 old != WindowPropertyCaster<T>::ToInt64(property->default_value)) { | |
| 101 (*property->deallocator)(old); | |
| 102 } | |
| 103 } | |
| 104 | |
| 105 template <typename T> | |
| 106 T Window::GetLocalProperty(const WindowProperty<T>* property) const { | |
| 107 return WindowPropertyCaster<T>::FromInt64(GetLocalPropertyInternal( | |
| 108 property, WindowPropertyCaster<T>::ToInt64(property->default_value))); | |
| 109 } | |
| 110 | |
| 111 template <typename T> | |
| 112 void Window::ClearLocalProperty(const WindowProperty<T>* property) { | |
| 113 SetLocalProperty(property, property->default_value); | |
| 114 } | |
| 115 | |
| 116 } // namespace ui | |
| 117 | |
| 118 // Macros to instantiate the property getter/setter template functions. | |
| 119 #define MUS_DECLARE_EXPORTED_WINDOW_PROPERTY_TYPE(EXPORT, T) \ | |
| 120 template EXPORT void ui::Window::SetLocalProperty( \ | |
| 121 const ui::WindowProperty<T>*, T); \ | |
| 122 template EXPORT T ui::Window::GetLocalProperty(const ui::WindowProperty<T>*) \ | |
| 123 const; \ | |
| 124 template EXPORT void ui::Window::ClearLocalProperty( \ | |
| 125 const ui::WindowProperty<T>*); | |
| 126 #define MUS_DECLARE_WINDOW_PROPERTY_TYPE(T) \ | |
| 127 MUS_DECLARE_EXPORTED_WINDOW_PROPERTY_TYPE(, T) | |
| 128 | |
| 129 #define MUS_DEFINE_WINDOW_PROPERTY_KEY(TYPE, NAME, DEFAULT) \ | |
| 130 static_assert(sizeof(TYPE) <= sizeof(int64_t), \ | |
| 131 "Property type must fit in 64 bits"); \ | |
| 132 namespace { \ | |
| 133 const ui::WindowProperty<TYPE> NAME##_Value = {DEFAULT, #NAME, nullptr}; \ | |
| 134 } \ | |
| 135 const ui::WindowProperty<TYPE>* const NAME = &NAME##_Value; | |
| 136 | |
| 137 #define MUS_DEFINE_LOCAL_WINDOW_PROPERTY_KEY(TYPE, NAME, DEFAULT) \ | |
| 138 static_assert(sizeof(TYPE) <= sizeof(int64_t), \ | |
| 139 "Property type must fit in 64 bits"); \ | |
| 140 namespace { \ | |
| 141 const ui::WindowProperty<TYPE> NAME##_Value = {DEFAULT, #NAME, nullptr}; \ | |
| 142 const ui::WindowProperty<TYPE>* const NAME = &NAME##_Value; \ | |
| 143 } | |
| 144 | |
| 145 #define MUS_DEFINE_OWNED_WINDOW_PROPERTY_KEY(TYPE, NAME, DEFAULT) \ | |
| 146 namespace { \ | |
| 147 void Deallocator##NAME(int64_t p) { \ | |
| 148 enum { type_must_be_complete = sizeof(TYPE) }; \ | |
| 149 delete ui::WindowPropertyCaster<TYPE*>::FromInt64(p); \ | |
| 150 } \ | |
| 151 const ui::WindowProperty<TYPE*> NAME##_Value = {DEFAULT, #NAME, \ | |
| 152 &Deallocator##NAME}; \ | |
| 153 } \ | |
| 154 const ui::WindowProperty<TYPE*>* const NAME = &NAME##_Value; | |
| 155 | |
| 156 #endif // SERVICES_UI_PUBLIC_CPP_WINDOW_PROPERTY_H_ | |
| OLD | NEW |