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 "mojo/public/cpp/bindings/type_converter.h" | 7 #include "mojo/public/cpp/bindings/type_converter.h" |
| 8 #include "services/ui/public/cpp/property_type_converters.h" | 8 #include "services/ui/public/cpp/property_type_converters.h" |
| 9 #include "services/ui/public/interfaces/window_manager.mojom.h" | 9 #include "services/ui/public/interfaces/window_manager.mojom.h" |
| 10 #include "ui/aura/client/aura_constants.h" | 10 #include "ui/aura/client/aura_constants.h" |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 30 | 30 |
| 31 PropertyConverter::PropertyConverter() { | 31 PropertyConverter::PropertyConverter() { |
| 32 // Add known aura properties with associated mus properties. | 32 // Add known aura properties with associated mus properties. |
| 33 RegisterProperty(client::kAlwaysOnTopKey, | 33 RegisterProperty(client::kAlwaysOnTopKey, |
| 34 ui::mojom::WindowManager::kAlwaysOnTop_Property); | 34 ui::mojom::WindowManager::kAlwaysOnTop_Property); |
| 35 RegisterProperty(client::kAppIdKey, | 35 RegisterProperty(client::kAppIdKey, |
| 36 ui::mojom::WindowManager::kAppID_Property); | 36 ui::mojom::WindowManager::kAppID_Property); |
| 37 RegisterProperty(client::kExcludeFromMruKey, | 37 RegisterProperty(client::kExcludeFromMruKey, |
| 38 ui::mojom::WindowManager::kExcludeFromMru_Property); | 38 ui::mojom::WindowManager::kExcludeFromMru_Property); |
| 39 RegisterProperty(client::kNameKey, ui::mojom::WindowManager::kName_Property); | 39 RegisterProperty(client::kNameKey, ui::mojom::WindowManager::kName_Property); |
| 40 RegisterProperty(client::kPreferredSize, | |
| 41 ui::mojom::WindowManager::kPreferredSize_Property); | |
| 40 RegisterProperty(client::kRestoreBoundsKey, | 42 RegisterProperty(client::kRestoreBoundsKey, |
| 41 ui::mojom::WindowManager::kRestoreBounds_Property); | 43 ui::mojom::WindowManager::kRestoreBounds_Property); |
| 44 RegisterProperty(client::kShowStateKey, | |
| 45 ui::mojom::WindowManager::kShowState_Property); | |
| 42 RegisterProperty(client::kTitleKey, | 46 RegisterProperty(client::kTitleKey, |
| 43 ui::mojom::WindowManager::kWindowTitle_Property); | 47 ui::mojom::WindowManager::kWindowTitle_Property); |
| 44 } | 48 } |
| 45 | 49 |
| 46 PropertyConverter::~PropertyConverter() {} | 50 PropertyConverter::~PropertyConverter() {} |
| 47 | 51 |
| 52 void PropertyConverter::ApplyAllTransportProperties( | |
| 53 Window* window, | |
| 54 const std::map<std::string, std::vector<uint8_t>>& properties) { | |
| 55 for (auto& pair : properties) | |
|
msw
2016/11/29 01:15:45
It seems easy for a client to inline this; why add
sky
2016/11/29 04:19:36
I need to call it a couple of times, but it's not
| |
| 56 SetPropertyFromTransportValue(window, pair.first, &pair.second); | |
| 57 } | |
| 58 | |
| 48 bool PropertyConverter::ConvertPropertyForTransport( | 59 bool PropertyConverter::ConvertPropertyForTransport( |
| 49 Window* window, | 60 Window* window, |
| 50 const void* key, | 61 const void* key, |
| 51 std::string* transport_name, | 62 std::string* transport_name, |
| 52 std::unique_ptr<std::vector<uint8_t>>* transport_value) { | 63 std::unique_ptr<std::vector<uint8_t>>* transport_value) { |
| 53 *transport_name = GetTransportNameForPropertyKey(key); | 64 *transport_name = GetTransportNameForPropertyKey(key); |
| 54 if (transport_name->empty()) | 65 if (transport_name->empty()) |
| 55 return false; | 66 return false; |
| 56 | 67 |
| 57 auto image_key = static_cast<const WindowProperty<gfx::ImageSkia*>*>(key); | 68 auto image_key = static_cast<const WindowProperty<gfx::ImageSkia*>*>(key); |
| 58 if (image_properties_.count(image_key) > 0) { | 69 if (image_properties_.count(image_key) > 0) { |
| 59 const gfx::ImageSkia* value = window->GetProperty(image_key); | 70 const gfx::ImageSkia* value = window->GetProperty(image_key); |
| 60 if (value) { | 71 if (value) { |
| 61 // TODO(crbug.com/667566): Support additional scales or gfx::Image[Skia]. | 72 // TODO(crbug.com/667566): Support additional scales or gfx::Image[Skia]. |
| 62 SkBitmap bitmap = value->GetRepresentation(1.f).sk_bitmap(); | 73 SkBitmap bitmap = value->GetRepresentation(1.f).sk_bitmap(); |
| 63 *transport_value = base::MakeUnique<std::vector<uint8_t>>( | 74 *transport_value = base::MakeUnique<std::vector<uint8_t>>( |
| 64 mojo::ConvertTo<std::vector<uint8_t>>(bitmap)); | 75 mojo::ConvertTo<std::vector<uint8_t>>(bitmap)); |
| 65 } else { | 76 } else { |
| 66 *transport_value = base::MakeUnique<std::vector<uint8_t>>(); | 77 *transport_value = base::MakeUnique<std::vector<uint8_t>>(); |
| 67 } | 78 } |
| 68 return true; | 79 return true; |
| 69 } | 80 } |
| 70 | 81 |
| 71 auto rect_key = static_cast<const WindowProperty<gfx::Rect*>*>(key); | 82 auto rect_key = static_cast<const WindowProperty<gfx::Rect*>*>(key); |
| 72 if (rect_properties_.count(rect_key) > 0) { | 83 if (rect_properties_.count(rect_key) > 0) { |
| 73 *transport_value = GetArray(window, rect_key); | 84 *transport_value = GetArray(window, rect_key); |
| 74 return true; | 85 return true; |
| 75 } | 86 } |
| 76 | 87 |
| 88 auto size_key = static_cast<const WindowProperty<gfx::Size*>*>(key); | |
| 89 if (size_properties_.count(size_key) > 0) { | |
| 90 *transport_value = GetArray(window, size_key); | |
| 91 return true; | |
| 92 } | |
| 93 | |
| 77 auto string_key = static_cast<const WindowProperty<std::string*>*>(key); | 94 auto string_key = static_cast<const WindowProperty<std::string*>*>(key); |
| 78 if (string_properties_.count(string_key) > 0) { | 95 if (string_properties_.count(string_key) > 0) { |
| 79 *transport_value = GetArray(window, string_key); | 96 *transport_value = GetArray(window, string_key); |
| 80 return true; | 97 return true; |
| 81 } | 98 } |
| 82 | 99 |
| 83 auto string16_key = static_cast<const WindowProperty<base::string16*>*>(key); | 100 auto string16_key = static_cast<const WindowProperty<base::string16*>*>(key); |
| 84 if (string16_properties_.count(string16_key) > 0) { | 101 if (string16_properties_.count(string16_key) > 0) { |
| 85 *transport_value = GetArray(window, string16_key); | 102 *transport_value = GetArray(window, string16_key); |
| 86 return true; | 103 return true; |
| 87 } | 104 } |
| 88 | 105 |
| 89 // Handle primitive property types generically. | 106 // Handle primitive property types generically. |
| 90 DCHECK_GT(primitive_properties_.count(key), 0u); | 107 DCHECK_GT(primitive_properties_.count(key), 0u); |
| 91 // TODO(msw): Using the int64_t accessor is wasteful for smaller types. | 108 // TODO(msw): Using the int64_t accessor is wasteful for smaller types. |
| 92 const int64_t value = window->GetPropertyInternal(key, 0); | 109 const PrimitiveType value = window->GetPropertyInternal(key, 0); |
| 93 *transport_value = base::MakeUnique<std::vector<uint8_t>>( | 110 *transport_value = base::MakeUnique<std::vector<uint8_t>>( |
| 94 mojo::ConvertTo<std::vector<uint8_t>>(value)); | 111 mojo::ConvertTo<std::vector<uint8_t>>(value)); |
| 95 return true; | 112 return true; |
| 96 } | 113 } |
| 97 | 114 |
| 98 std::string PropertyConverter::GetTransportNameForPropertyKey(const void* key) { | 115 std::string PropertyConverter::GetTransportNameForPropertyKey(const void* key) { |
| 99 if (primitive_properties_.count(key) > 0) | 116 if (primitive_properties_.count(key) > 0) |
| 100 return primitive_properties_[key].second; | 117 return primitive_properties_[key].second; |
| 101 | 118 |
| 102 auto image_key = static_cast<const WindowProperty<gfx::ImageSkia*>*>(key); | 119 auto image_key = static_cast<const WindowProperty<gfx::ImageSkia*>*>(key); |
| 103 if (image_properties_.count(image_key) > 0) | 120 if (image_properties_.count(image_key) > 0) |
| 104 return image_properties_[image_key]; | 121 return image_properties_[image_key]; |
| 105 | 122 |
| 106 auto rect_key = static_cast<const WindowProperty<gfx::Rect*>*>(key); | 123 auto rect_key = static_cast<const WindowProperty<gfx::Rect*>*>(key); |
| 107 if (rect_properties_.count(rect_key) > 0) | 124 if (rect_properties_.count(rect_key) > 0) |
| 108 return rect_properties_[rect_key]; | 125 return rect_properties_[rect_key]; |
| 109 | 126 |
| 127 auto size_key = static_cast<const WindowProperty<gfx::Size*>*>(key); | |
| 128 if (size_properties_.count(size_key) > 0) | |
| 129 return size_properties_[size_key]; | |
| 130 | |
| 110 auto string_key = static_cast<const WindowProperty<std::string*>*>(key); | 131 auto string_key = static_cast<const WindowProperty<std::string*>*>(key); |
| 111 if (string_properties_.count(string_key) > 0) | 132 if (string_properties_.count(string_key) > 0) |
| 112 return string_properties_[string_key]; | 133 return string_properties_[string_key]; |
| 113 | 134 |
| 114 auto string16_key = static_cast<const WindowProperty<base::string16*>*>(key); | 135 auto string16_key = static_cast<const WindowProperty<base::string16*>*>(key); |
| 115 if (string16_properties_.count(string16_key) > 0) | 136 if (string16_properties_.count(string16_key) > 0) |
| 116 return string16_properties_[string16_key]; | 137 return string16_properties_[string16_key]; |
| 117 | 138 |
| 118 return std::string(); | 139 return std::string(); |
| 119 } | 140 } |
| 120 | 141 |
| 121 void PropertyConverter::SetPropertyFromTransportValue( | 142 void PropertyConverter::SetPropertyFromTransportValue( |
| 122 Window* window, | 143 Window* window, |
| 123 const std::string& transport_name, | 144 const std::string& transport_name, |
| 124 const std::vector<uint8_t>* data) { | 145 const std::vector<uint8_t>* data) { |
| 125 for (const auto& primitive_property : primitive_properties_) { | 146 for (const auto& primitive_property : primitive_properties_) { |
| 126 if (primitive_property.second.second == transport_name) { | 147 if (primitive_property.second.second == transport_name) { |
| 127 // aura::Window only supports property types that fit in int64_t. | 148 // aura::Window only supports property types that fit in PrimitiveType. |
| 128 if (data->size() != 8u) { | 149 if (data->size() != 8u) { |
| 129 DVLOG(2) << "Property size mismatch (int64_t): " << transport_name; | 150 DVLOG(2) << "Property size mismatch (PrimitiveType): " |
| 151 << transport_name; | |
| 130 return; | 152 return; |
| 131 } | 153 } |
| 132 const int64_t value = mojo::ConvertTo<int64_t>(*data); | 154 const PrimitiveType value = mojo::ConvertTo<PrimitiveType>(*data); |
| 133 // TODO(msw): Should aura::Window just store all properties by name? | 155 // TODO(msw): Should aura::Window just store all properties by name? |
| 134 window->SetPropertyInternal(primitive_property.first, | 156 window->SetPropertyInternal(primitive_property.first, |
| 135 primitive_property.second.first, nullptr, | 157 primitive_property.second.first, nullptr, |
| 136 value, 0); | 158 value, 0); |
| 137 return; | 159 return; |
| 138 } | 160 } |
| 139 } | 161 } |
| 140 | 162 |
| 141 for (const auto& image_property : image_properties_) { | 163 for (const auto& image_property : image_properties_) { |
| 142 if (image_property.second == transport_name) { | 164 if (image_property.second == transport_name) { |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 154 if (data->size() != 16u) { | 176 if (data->size() != 16u) { |
| 155 DVLOG(2) << "Property size mismatch (gfx::Rect): " << transport_name; | 177 DVLOG(2) << "Property size mismatch (gfx::Rect): " << transport_name; |
| 156 return; | 178 return; |
| 157 } | 179 } |
| 158 const gfx::Rect value = mojo::ConvertTo<gfx::Rect>(*data); | 180 const gfx::Rect value = mojo::ConvertTo<gfx::Rect>(*data); |
| 159 window->SetProperty(rect_property.first, new gfx::Rect(value)); | 181 window->SetProperty(rect_property.first, new gfx::Rect(value)); |
| 160 return; | 182 return; |
| 161 } | 183 } |
| 162 } | 184 } |
| 163 | 185 |
| 186 for (const auto& size_property : size_properties_) { | |
| 187 if (size_property.second == transport_name) { | |
| 188 if (data->size() != 8u) { | |
| 189 DVLOG(2) << "Property size mismatch (gfx::Size): " << transport_name; | |
| 190 return; | |
| 191 } | |
| 192 const gfx::Size value = mojo::ConvertTo<gfx::Size>(*data); | |
| 193 window->SetProperty(size_property.first, new gfx::Size(value)); | |
| 194 return; | |
| 195 } | |
| 196 } | |
| 197 | |
| 164 for (const auto& string_property : string_properties_) { | 198 for (const auto& string_property : string_properties_) { |
| 165 if (string_property.second == transport_name) { | 199 if (string_property.second == transport_name) { |
| 166 // TODO(msw): Validate the data somehow, before trying to convert? | 200 // TODO(msw): Validate the data somehow, before trying to convert? |
| 167 const std::string value = mojo::ConvertTo<std::string>(*data); | 201 const std::string value = mojo::ConvertTo<std::string>(*data); |
| 168 window->SetProperty(string_property.first, new std::string(value)); | 202 window->SetProperty(string_property.first, new std::string(value)); |
| 169 return; | 203 return; |
| 170 } | 204 } |
| 171 } | 205 } |
| 172 | 206 |
| 173 for (const auto& string16_property : string16_properties_) { | 207 for (const auto& string16_property : string16_properties_) { |
| 174 if (string16_property.second == transport_name) { | 208 if (string16_property.second == transport_name) { |
| 175 // TODO(msw): Validate the data somehow, before trying to convert? | 209 // TODO(msw): Validate the data somehow, before trying to convert? |
| 176 const base::string16 value = mojo::ConvertTo<base::string16>(*data); | 210 const base::string16 value = mojo::ConvertTo<base::string16>(*data); |
| 177 window->SetProperty(string16_property.first, new base::string16(value)); | 211 window->SetProperty(string16_property.first, new base::string16(value)); |
| 178 return; | 212 return; |
| 179 } | 213 } |
| 180 } | 214 } |
| 181 | 215 |
| 182 DVLOG(2) << "Unknown mus property name: " << transport_name; | 216 DVLOG(2) << "Unknown mus property name: " << transport_name; |
| 183 } | 217 } |
| 184 | 218 |
| 219 bool PropertyConverter::GetPropertyValueFromTransformValue( | |
| 220 const std::string& transport_name, | |
| 221 const std::vector<uint8_t>& transport_data, | |
| 222 PrimitiveType* value) { | |
| 223 // aura::Window only supports property types that fit in PrimitiveType. | |
| 224 if (transport_data.size() != 8u) { | |
| 225 DVLOG(2) << "Property size mismatch (PrimitiveType): " << transport_name; | |
| 226 return false; | |
| 227 } | |
| 228 for (const auto& primitive_property : primitive_properties_) { | |
| 229 if (primitive_property.second.second == transport_name) { | |
| 230 *value = mojo::ConvertTo<PrimitiveType>(transport_data); | |
| 231 return true; | |
| 232 } | |
| 233 } | |
| 234 return false; | |
| 235 } | |
| 236 | |
| 185 void PropertyConverter::RegisterProperty( | 237 void PropertyConverter::RegisterProperty( |
| 186 const WindowProperty<gfx::ImageSkia*>* property, | 238 const WindowProperty<gfx::ImageSkia*>* property, |
| 187 const char* transport_name) { | 239 const char* transport_name) { |
| 188 image_properties_[property] = transport_name; | 240 image_properties_[property] = transport_name; |
| 189 } | 241 } |
| 190 | 242 |
| 191 void PropertyConverter::RegisterProperty( | 243 void PropertyConverter::RegisterProperty( |
| 192 const WindowProperty<gfx::Rect*>* property, | 244 const WindowProperty<gfx::Rect*>* property, |
| 193 const char* transport_name) { | 245 const char* transport_name) { |
| 194 rect_properties_[property] = transport_name; | 246 rect_properties_[property] = transport_name; |
| 195 } | 247 } |
| 196 | 248 |
| 197 void PropertyConverter::RegisterProperty( | 249 void PropertyConverter::RegisterProperty( |
| 250 const WindowProperty<gfx::Size*>* property, | |
| 251 const char* transport_name) { | |
| 252 size_properties_[property] = transport_name; | |
| 253 } | |
| 254 | |
| 255 void PropertyConverter::RegisterProperty( | |
| 198 const WindowProperty<std::string*>* property, | 256 const WindowProperty<std::string*>* property, |
| 199 const char* transport_name) { | 257 const char* transport_name) { |
| 200 string_properties_[property] = transport_name; | 258 string_properties_[property] = transport_name; |
| 201 } | 259 } |
| 202 | 260 |
| 203 void PropertyConverter::RegisterProperty( | 261 void PropertyConverter::RegisterProperty( |
| 204 const WindowProperty<base::string16*>* property, | 262 const WindowProperty<base::string16*>* property, |
| 205 const char* transport_name) { | 263 const char* transport_name) { |
| 206 string16_properties_[property] = transport_name; | 264 string16_properties_[property] = transport_name; |
| 207 } | 265 } |
| 208 | 266 |
| 209 } // namespace aura | 267 } // namespace aura |
| OLD | NEW |