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

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

Issue 2499933003: Expand aura::PropertyConverter support. (Closed)
Patch Set: Fix the other new unit test. Created 4 years, 1 month 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
« no previous file with comments | « ui/aura/mus/property_converter.h ('k') | ui/aura/mus/property_converter_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "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,
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 } 27 }
28 28
29 } // namespace 29 } // namespace
30 30
31 PropertyConverter::PropertyConverter() {} 31 PropertyConverter::PropertyConverter() {
32 // Add known aura properties with associated mus properties.
33 RegisterProperty(client::kAlwaysOnTopKey,
34 ui::mojom::WindowManager::kAlwaysOnTop_Property);
35 RegisterProperty(client::kAppIdKey,
36 ui::mojom::WindowManager::kAppID_Property);
37 RegisterProperty(client::kExcludeFromMruKey,
38 ui::mojom::WindowManager::kExcludeFromMru_Property);
39 RegisterProperty(client::kRestoreBoundsKey,
40 ui::mojom::WindowManager::kRestoreBounds_Property);
41 }
32 42
33 PropertyConverter::~PropertyConverter() {} 43 PropertyConverter::~PropertyConverter() {}
34 44
35 bool PropertyConverter::ConvertPropertyForTransport( 45 bool PropertyConverter::ConvertPropertyForTransport(
36 Window* window, 46 Window* window,
37 const void* key, 47 const void* key,
38 std::string* server_property_name, 48 std::string* transport_name,
39 std::unique_ptr<std::vector<uint8_t>>* server_property_value) { 49 std::unique_ptr<std::vector<uint8_t>>* transport_value) {
40 *server_property_name = GetTransportNameForPropertyKey(key); 50 *transport_name = GetTransportNameForPropertyKey(key);
41 if (!server_property_name->empty()) { 51 if (transport_name->empty())
42 // TODO(msw): Using the int64_t accessor is wasteful for bool, etc. 52 return false;
43 const int64_t value = window->GetPropertyInternal(key, 0); 53
44 *server_property_value = base::MakeUnique<std::vector<uint8_t>>( 54 auto rect_key = static_cast<const WindowProperty<gfx::Rect*>*>(key);
45 mojo::ConvertTo<std::vector<uint8_t>>(value)); 55 if (rect_properties_.count(rect_key) > 0) {
56 *transport_value = GetArray(window, rect_key);
46 return true; 57 return true;
47 } 58 }
48 DVLOG(2) << "Unknown aura property key: " << key; 59
49 return false; 60 auto string_key = static_cast<const WindowProperty<std::string*>*>(key);
61 if (string_properties_.count(string_key) > 0) {
62 *transport_value = GetArray(window, string_key);
63 return true;
64 }
65
66 // Handle primitive property types generically.
67 DCHECK_GT(primitive_properties_.count(key), 0u);
68 // TODO(msw): Using the int64_t accessor is wasteful for smaller types.
69 const int64_t value = window->GetPropertyInternal(key, 0);
70 *transport_value = base::MakeUnique<std::vector<uint8_t>>(
71 mojo::ConvertTo<std::vector<uint8_t>>(value));
72 return true;
50 } 73 }
51 74
52 std::string PropertyConverter::GetTransportNameForPropertyKey(const void* key) { 75 std::string PropertyConverter::GetTransportNameForPropertyKey(const void* key) {
53 if (key == client::kAlwaysOnTopKey) 76 if (primitive_properties_.count(key) > 0)
54 return ui::mojom::WindowManager::kAlwaysOnTop_Property; 77 return primitive_properties_[key].second;
78
79 auto rect_key = static_cast<const WindowProperty<gfx::Rect*>*>(key);
80 if (rect_properties_.count(rect_key) > 0)
81 return rect_properties_[rect_key];
82
83 auto string_key = static_cast<const WindowProperty<std::string*>*>(key);
84 if (string_properties_.count(string_key) > 0)
85 return string_properties_[string_key];
86
55 return std::string(); 87 return std::string();
56 } 88 }
57 89
58 void PropertyConverter::SetPropertyFromTransportValue( 90 void PropertyConverter::SetPropertyFromTransportValue(
59 Window* window, 91 Window* window,
60 const std::string& server_property_name, 92 const std::string& transport_name,
61 const std::vector<uint8_t>* data) { 93 const std::vector<uint8_t>* data) {
62 const void* key = nullptr; 94 for (const auto& primitive_property : primitive_properties_) {
63 const char* name = nullptr; 95 if (primitive_property.second.second == transport_name) {
64 if (GetPropertyKeyForTransportName(server_property_name, &key, &name)) { 96 // aura::Window only supports property types that fit in int64_t.
65 DCHECK(key); 97 if (data->size() != 8u) {
66 DCHECK(name); 98 DVLOG(2) << "Property size mismatch (int64_t): " << transport_name;
67 // Aura window only supports property types that fit in int64_t. 99 return;
68 CHECK_LE(8u, data->size()) << " Property type not supported: " << key; 100 }
69 const int64_t value = mojo::ConvertTo<int64_t>(*data); 101 const int64_t value = mojo::ConvertTo<int64_t>(*data);
70 // TODO(msw): Should aura::Window just store all properties by name? 102 // TODO(msw): Should aura::Window just store all properties by name?
71 window->SetPropertyInternal(key, name, nullptr, value, 0); 103 window->SetPropertyInternal(primitive_property.first,
72 } else { 104 primitive_property.second.first, nullptr,
73 DVLOG(2) << "Unknown mus property name: " << server_property_name; 105 value, 0);
106 return;
107 }
74 } 108 }
109
110 for (const auto& rect_property : rect_properties_) {
111 if (rect_property.second == transport_name) {
112 if (data->size() != 16u) {
113 DVLOG(2) << "Property size mismatch (gfx::Rect): " << transport_name;
114 return;
115 }
116 const gfx::Rect value = mojo::ConvertTo<gfx::Rect>(*data);
117 window->SetProperty(rect_property.first, new gfx::Rect(value));
118 return;
119 }
120 }
121
122 for (const auto& string_property : string_properties_) {
123 if (string_property.second == transport_name) {
124 // TODO(msw): Validate the data somehow, before trying to convert?
125 const std::string value = mojo::ConvertTo<std::string>(*data);
126 window->SetProperty(string_property.first, new std::string(value));
127 return;
128 }
129 }
130
131 DVLOG(2) << "Unknown mus property name: " << transport_name;
132 }
133
134 void PropertyConverter::RegisterProperty(
135 const WindowProperty<gfx::Rect*>* property,
136 const char* transport_name) {
137 rect_properties_[property] = transport_name;
138 }
139
140 void PropertyConverter::RegisterProperty(
141 const WindowProperty<std::string*>* property,
142 const char* transport_name) {
143 string_properties_[property] = transport_name;
75 } 144 }
76 145
77 } // namespace aura 146 } // namespace aura
OLDNEW
« no previous file with comments | « ui/aura/mus/property_converter.h ('k') | ui/aura/mus/property_converter_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698