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" |
| 11 #include "ui/aura/window_property.h" | 11 #include "ui/aura/window_property.h" |
| 12 | 12 |
| 13 namespace aura { | 13 namespace aura { |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 // Get the aura property key and name for a mus property transport |name|. | 17 // Get the WindowProperty's value as a byte array. Only supports aura properties |
| 18 bool GetPropertyKeyForTransportName(const std::string& name, | 18 // that point to types with a matching std::vector<uint8_t> mojo::TypeConverter. |
| 19 const void** key_out, | 19 template <typename T> |
| 20 const char** name_out) { | 20 std::unique_ptr<std::vector<uint8_t>> GetArray(Window* window, |
|
sky
2016/11/16 17:56:13
optional: naming this as get implies it isn't doin
msw
2016/11/16 22:47:45
Done.
| |
| 21 if (name == ui::mojom::WindowManager::kAlwaysOnTop_Property) { | 21 const WindowProperty<T>* key) { |
| 22 *key_out = client::kAlwaysOnTopKey; | 22 const T value = window->GetProperty(key); |
| 23 *name_out = client::kAlwaysOnTopKey->name; | 23 if (!value) |
| 24 return true; | 24 return base::MakeUnique<std::vector<uint8_t>>(); |
| 25 } | 25 return base::MakeUnique<std::vector<uint8_t>>( |
| 26 return false; | 26 mojo::ConvertTo<std::vector<uint8_t>>(*value)); |
| 27 } | |
| 28 | |
| 29 // Get the primitive aura property value to store for a mus transport value. | |
| 30 int64_t GetAuraValue(const std::vector<uint8_t>& data) { | |
| 31 CHECK_LE(data.size(), 8u); | |
| 32 int64_t value = 0; | |
| 33 for (size_t i = data.size(); i > 0; --i) | |
| 34 value = (value << 8) + data[i - 1]; | |
| 35 return value; | |
| 36 } | |
| 37 | |
| 38 // Get the mus transport value to store for a primitive aura property value. | |
| 39 mojo::Array<uint8_t> GetMusValue(int64_t value) { | |
| 40 mojo::Array<uint8_t> transport_value(8); | |
| 41 transport_value[7] = (value >> 56) & 0xFF; | |
|
sky
2016/11/16 17:56:13
optional: it's more common that the higher bits ar
msw
2016/11/16 22:47:44
Done. I can use existing type converters with stat
| |
| 42 transport_value[6] = (value >> 48) & 0xFF; | |
| 43 transport_value[5] = (value >> 40) & 0xFF; | |
| 44 transport_value[4] = (value >> 32) & 0xFF; | |
| 45 transport_value[3] = (value >> 24) & 0xFF; | |
| 46 transport_value[2] = (value >> 16) & 0xFF; | |
| 47 transport_value[1] = (value >> 8) & 0xFF; | |
| 48 transport_value[0] = value & 0xFF; | |
| 49 return transport_value; | |
| 27 } | 50 } |
| 28 | 51 |
| 29 } // namespace | 52 } // namespace |
| 30 | 53 |
| 31 PropertyConverter::PropertyConverter() {} | 54 PropertyConverter::PropertyConverter() { |
| 55 // Add known aura properties with associated mus properties. | |
| 56 RegisterPrimitiveProperty(client::kAlwaysOnTopKey, | |
| 57 client::kAlwaysOnTopKey->name, | |
| 58 ui::mojom::WindowManager::kAlwaysOnTop_Property); | |
| 59 RegisterPrimitiveProperty(client::kExcludeFromMruKey, | |
| 60 client::kExcludeFromMruKey->name, | |
| 61 ui::mojom::WindowManager::kExcludeFromMru_Property); | |
| 62 RegisterRectProperty(client::kRestoreBoundsKey, | |
| 63 ui::mojom::WindowManager::kRestoreBounds_Property); | |
| 64 RegisterStringProperty(client::kAppIdKey, | |
| 65 ui::mojom::WindowManager::kAppID_Property); | |
| 66 } | |
| 32 | 67 |
| 33 PropertyConverter::~PropertyConverter() {} | 68 PropertyConverter::~PropertyConverter() {} |
| 34 | 69 |
| 35 bool PropertyConverter::ConvertPropertyForTransport( | 70 bool PropertyConverter::ConvertPropertyForTransport( |
| 36 Window* window, | 71 Window* window, |
| 37 const void* key, | 72 const void* key, |
| 38 std::string* server_property_name, | 73 std::string* transport_name, |
| 39 std::unique_ptr<std::vector<uint8_t>>* server_property_value) { | 74 std::unique_ptr<std::vector<uint8_t>>* transport_value) { |
| 40 *server_property_name = GetTransportNameForPropertyKey(key); | 75 *transport_name = GetTransportNameForPropertyKey(key); |
| 41 if (!server_property_name->empty()) { | 76 if (transport_name->empty()) { |
| 42 // TODO(msw): Using the int64_t accessor is wasteful for bool, etc. | 77 DVLOG(2) << "Unknown aura property key: " << key; |
|
sky
2016/11/16 17:56:13
I'm ok with a DVLOG, but be warned there are going
msw
2016/11/16 22:47:45
Yeah, this logging probably isn't too helpful; rem
| |
| 43 const int64_t value = window->GetPropertyInternal(key, 0); | 78 return false; |
| 44 *server_property_value = base::MakeUnique<std::vector<uint8_t>>( | 79 } |
| 45 mojo::ConvertTo<std::vector<uint8_t>>(value)); | 80 |
| 81 auto rect_key = static_cast<const WindowProperty<gfx::Rect*>*>(key); | |
| 82 if (rect_properties_.count(rect_key) > 0) { | |
| 83 *transport_value = GetArray(window, rect_key); | |
| 46 return true; | 84 return true; |
| 47 } | 85 } |
| 48 DVLOG(2) << "Unknown aura property key: " << key; | 86 |
| 49 return false; | 87 auto string_key = static_cast<const WindowProperty<std::string*>*>(key); |
| 88 if (string_properties_.count(string_key) > 0) { | |
| 89 *transport_value = GetArray(window, string_key); | |
| 90 return true; | |
| 91 } | |
| 92 | |
| 93 // Handle primitive property types generically. | |
| 94 DCHECK_GT(primitive_properties_.count(key), 0u); | |
| 95 // TODO(msw): Using the int64_t accessor is wasteful for smaller types. | |
| 96 const int64_t value = window->GetPropertyInternal(key, 0); | |
| 97 *transport_value = base::MakeUnique<std::vector<uint8_t>>(GetMusValue(value)); | |
| 98 return true; | |
| 50 } | 99 } |
| 51 | 100 |
| 52 std::string PropertyConverter::GetTransportNameForPropertyKey(const void* key) { | 101 std::string PropertyConverter::GetTransportNameForPropertyKey(const void* key) { |
| 53 if (key == client::kAlwaysOnTopKey) | 102 if (primitive_properties_.count(key) > 0) |
| 54 return ui::mojom::WindowManager::kAlwaysOnTop_Property; | 103 return primitive_properties_[key].second; |
| 104 | |
| 105 auto rect_key = static_cast<const WindowProperty<gfx::Rect*>*>(key); | |
| 106 if (rect_properties_.count(rect_key) > 0) | |
| 107 return rect_properties_[rect_key]; | |
| 108 | |
| 109 auto string_key = static_cast<const WindowProperty<std::string*>*>(key); | |
| 110 if (string_properties_.count(string_key) > 0) | |
| 111 return string_properties_[string_key]; | |
| 112 | |
| 55 return std::string(); | 113 return std::string(); |
| 56 } | 114 } |
| 57 | 115 |
| 58 void PropertyConverter::SetPropertyFromTransportValue( | 116 void PropertyConverter::SetPropertyFromTransportValue( |
| 59 Window* window, | 117 Window* window, |
| 60 const std::string& server_property_name, | 118 const std::string& transport_name, |
| 61 const std::vector<uint8_t>* data) { | 119 const std::vector<uint8_t>* data) { |
| 62 const void* key = nullptr; | 120 for (const auto& primitive_property : primitive_properties_) { |
| 63 const char* name = nullptr; | 121 if (primitive_property.second.second == transport_name) { |
| 64 if (GetPropertyKeyForTransportName(server_property_name, &key, &name)) { | 122 // aura::Window only supports property types that fit in int64_t. |
| 65 DCHECK(key); | 123 CHECK_LE(data->size(), 8u) << " Property too big: " << transport_name; |
|
sky
2016/11/16 17:56:13
I think this should DVLOG and ignore the value. Th
msw
2016/11/16 22:47:44
Done.
| |
| 66 DCHECK(name); | 124 const int64_t value = GetAuraValue(*data); |
| 67 // Aura window only supports property types that fit in int64_t. | 125 // TODO(msw): Should aura::Window just store all properties by name? |
| 68 CHECK_LE(8u, data->size()) << " Property type not supported: " << key; | 126 window->SetPropertyInternal(primitive_property.first, |
| 69 const int64_t value = mojo::ConvertTo<int64_t>(*data); | 127 primitive_property.second.first, nullptr, |
| 70 // TODO(msw): Should aura::Window just store all properties by name? | 128 value, 0); |
| 71 window->SetPropertyInternal(key, name, nullptr, value, 0); | 129 return; |
| 72 } else { | 130 } |
| 73 DVLOG(2) << "Unknown mus property name: " << server_property_name; | |
| 74 } | 131 } |
| 132 | |
| 133 for (const auto& rect_property : rect_properties_) { | |
| 134 if (rect_property.second == transport_name) { | |
| 135 const gfx::Rect value = mojo::ConvertTo<gfx::Rect>(*data); | |
|
sky
2016/11/16 17:56:13
Similar comment here and 143 about verifying type.
msw
2016/11/16 22:47:44
I added a size check for rect, but I'm not sure wh
| |
| 136 window->SetProperty(rect_property.first, new gfx::Rect(value)); | |
| 137 return; | |
| 138 } | |
| 139 } | |
| 140 | |
| 141 for (const auto& string_property : string_properties_) { | |
| 142 if (string_property.second == transport_name) { | |
| 143 const std::string value = mojo::ConvertTo<std::string>(*data); | |
| 144 window->SetProperty(string_property.first, new std::string(value)); | |
| 145 return; | |
| 146 } | |
| 147 } | |
| 148 | |
| 149 DVLOG(2) << "Unknown mus property name: " << transport_name; | |
| 150 } | |
| 151 | |
| 152 void PropertyConverter::RegisterPrimitiveProperty(const void* key, | |
| 153 const char* aura_name, | |
| 154 const char* transport_name) { | |
| 155 primitive_properties_[key] = PropertyNames(aura_name, transport_name); | |
| 156 } | |
| 157 | |
| 158 void PropertyConverter::RegisterRectProperty( | |
| 159 const WindowProperty<gfx::Rect*>* property, | |
| 160 const char* transport_name) { | |
| 161 rect_properties_[property] = transport_name; | |
| 162 } | |
| 163 | |
| 164 void PropertyConverter::RegisterStringProperty( | |
| 165 const WindowProperty<std::string*>* property, | |
| 166 const char* transport_name) { | |
| 167 string_properties_[property] = transport_name; | |
| 75 } | 168 } |
| 76 | 169 |
| 77 } // namespace aura | 170 } // namespace aura |
| OLD | NEW |