summaryrefslogtreecommitdiff
path: root/chromium/ui/aura/mus/property_converter.cc
blob: 7a0a67e0177bdb1764127d8d5663b0739d813249 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/aura/mus/property_converter.h"

#include "base/memory/ptr_util.h"
#include "mojo/public/cpp/bindings/type_converter.h"
#include "services/ui/public/cpp/property_type_converters.h"
#include "services/ui/public/interfaces/window_manager.mojom.h"
#include "ui/aura/client/aura_constants.h"
#include "ui/base/class_property.h"

namespace aura {

namespace {

// Get the WindowProperty's value as a byte array. Only supports aura properties
// that point to types with a matching std::vector<uint8_t> mojo::TypeConverter.
template <typename T>
std::unique_ptr<std::vector<uint8_t>> GetArray(Window* window,
                                               const WindowProperty<T>* key) {
  const T value = window->GetProperty(key);
  if (!value)
    return std::make_unique<std::vector<uint8_t>>();
  return std::make_unique<std::vector<uint8_t>>(
      mojo::ConvertTo<std::vector<uint8_t>>(*value));
}

// A validator that always returns true regardless of its input.
bool AlwaysTrue(int64_t value) {
  return true;
}

bool ValidateResizeBehaviour(int64_t value) {
  // Resize behaviour is a 3 bitfield.
  return value >= 0 &&
         value <= (ui::mojom::kResizeBehaviorCanMaximize |
                   ui::mojom::kResizeBehaviorCanMinimize |
                   ui::mojom::kResizeBehaviorCanResize);
}

bool ValidateShowState(int64_t value) {
  return value == int64_t(ui::mojom::ShowState::DEFAULT) ||
         value == int64_t(ui::mojom::ShowState::NORMAL) ||
         value == int64_t(ui::mojom::ShowState::MINIMIZED) ||
         value == int64_t(ui::mojom::ShowState::MAXIMIZED) ||
         value == int64_t(ui::mojom::ShowState::INACTIVE) ||
         value == int64_t(ui::mojom::ShowState::FULLSCREEN);
}

bool ValidateWindowCornerRadius(int64_t value) {
  return value >= -1;
}

}  // namespace

PropertyConverter::PrimitiveProperty::PrimitiveProperty() {}

PropertyConverter::PrimitiveProperty::PrimitiveProperty(
    const PrimitiveProperty& property) = default;

PropertyConverter::PrimitiveProperty::~PrimitiveProperty() {}

// static
base::RepeatingCallback<bool(int64_t)>
PropertyConverter::CreateAcceptAnyValueCallback() {
  return base::Bind(&AlwaysTrue);
}

PropertyConverter::PropertyConverter() {
  // Add known aura properties with associated mus properties.
  RegisterImageSkiaProperty(client::kAppIconKey,
                            ui::mojom::WindowManager::kAppIcon_Property);
  RegisterImageSkiaProperty(client::kWindowIconKey,
                            ui::mojom::WindowManager::kWindowIcon_Property);
  RegisterPrimitiveProperty(client::kAlwaysOnTopKey,
                            ui::mojom::WindowManager::kAlwaysOnTop_Property,
                            CreateAcceptAnyValueCallback());
  RegisterPrimitiveProperty(client::kDrawAttentionKey,
                            ui::mojom::WindowManager::kDrawAttention_Property,
                            CreateAcceptAnyValueCallback());
  RegisterPrimitiveProperty(
      client::kImmersiveFullscreenKey,
      ui::mojom::WindowManager::kImmersiveFullscreen_Property,
      CreateAcceptAnyValueCallback());
  RegisterPrimitiveProperty(client::kResizeBehaviorKey,
                            ui::mojom::WindowManager::kResizeBehavior_Property,
                            base::Bind(&ValidateResizeBehaviour));
  RegisterPrimitiveProperty(client::kShowStateKey,
                            ui::mojom::WindowManager::kShowState_Property,
                            base::Bind(&ValidateShowState));
  RegisterRectProperty(client::kRestoreBoundsKey,
                       ui::mojom::WindowManager::kRestoreBounds_Property);
  RegisterSizeProperty(client::kPreferredSize,
                       ui::mojom::WindowManager::kPreferredSize_Property);
  RegisterSizeProperty(client::kMinimumSize,
                       ui::mojom::WindowManager::kMinimumSize_Property);
  RegisterStringProperty(client::kNameKey,
                         ui::mojom::WindowManager::kName_Property);
  RegisterString16Property(client::kTitleKey,
                           ui::mojom::WindowManager::kWindowTitle_Property);
  RegisterPrimitiveProperty(
      client::kWindowCornerRadiusKey,
      ui::mojom::WindowManager::kWindowCornerRadius_Property,
      base::BindRepeating(&ValidateWindowCornerRadius));
}

PropertyConverter::~PropertyConverter() {}

bool PropertyConverter::IsTransportNameRegistered(
    const std::string& name) const {
  return transport_names_.count(name) > 0;
}

bool PropertyConverter::ConvertPropertyForTransport(
    Window* window,
    const void* key,
    std::string* transport_name,
    std::unique_ptr<std::vector<uint8_t>>* transport_value) {
  *transport_name = GetTransportNameForPropertyKey(key);
  if (transport_name->empty())
    return false;

  auto* image_key = static_cast<const WindowProperty<gfx::ImageSkia*>*>(key);
  if (image_properties_.count(image_key) > 0) {
    const gfx::ImageSkia* value = window->GetProperty(image_key);
    if (value) {
      // TODO(crbug.com/667566): Support additional scales or gfx::Image[Skia].
      SkBitmap bitmap = value->GetRepresentation(1.f).sk_bitmap();
      *transport_value = std::make_unique<std::vector<uint8_t>>(
          mojo::ConvertTo<std::vector<uint8_t>>(bitmap));
    } else {
      *transport_value = std::make_unique<std::vector<uint8_t>>();
    }
    return true;
  }

  auto* rect_key = static_cast<const WindowProperty<gfx::Rect*>*>(key);
  if (rect_properties_.count(rect_key) > 0) {
    *transport_value = GetArray(window, rect_key);
    return true;
  }

  auto* size_key = static_cast<const WindowProperty<gfx::Size*>*>(key);
  if (size_properties_.count(size_key) > 0) {
    *transport_value = GetArray(window, size_key);
    return true;
  }

  auto* string_key = static_cast<const WindowProperty<std::string*>*>(key);
  if (string_properties_.count(string_key) > 0) {
    *transport_value = GetArray(window, string_key);
    return true;
  }

  auto* string16_key = static_cast<const WindowProperty<base::string16*>*>(key);
  if (string16_properties_.count(string16_key) > 0) {
    *transport_value = GetArray(window, string16_key);
    return true;
  }

  // Handle primitive property types generically.
  DCHECK_GT(primitive_properties_.count(key), 0u);
  PrimitiveType default_value = primitive_properties_[key].default_value;
  // TODO(msw): Using the int64_t accessor is wasteful for smaller types.
  const PrimitiveType value = window->GetPropertyInternal(key, default_value);
  *transport_value = std::make_unique<std::vector<uint8_t>>(
      mojo::ConvertTo<std::vector<uint8_t>>(value));
  return true;
}

std::string PropertyConverter::GetTransportNameForPropertyKey(const void* key) {
  if (primitive_properties_.count(key) > 0)
    return primitive_properties_[key].transport_name;

  auto* image_key = static_cast<const WindowProperty<gfx::ImageSkia*>*>(key);
  if (image_properties_.count(image_key) > 0)
    return image_properties_[image_key];

  auto* rect_key = static_cast<const WindowProperty<gfx::Rect*>*>(key);
  if (rect_properties_.count(rect_key) > 0)
    return rect_properties_[rect_key];

  auto* size_key = static_cast<const WindowProperty<gfx::Size*>*>(key);
  if (size_properties_.count(size_key) > 0)
    return size_properties_[size_key];

  auto* string_key = static_cast<const WindowProperty<std::string*>*>(key);
  if (string_properties_.count(string_key) > 0)
    return string_properties_[string_key];

  auto* string16_key = static_cast<const WindowProperty<base::string16*>*>(key);
  if (string16_properties_.count(string16_key) > 0)
    return string16_properties_[string16_key];

  return std::string();
}

void PropertyConverter::SetPropertyFromTransportValue(
    Window* window,
    const std::string& transport_name,
    const std::vector<uint8_t>* data) {
  for (const auto& primitive_property : primitive_properties_) {
    if (primitive_property.second.transport_name == transport_name) {
      // aura::Window only supports property types that fit in PrimitiveType.
      if (data->size() != 8u) {
        DVLOG(2) << "Property size mismatch (PrimitiveType): "
                 << transport_name;
        return;
      }
      const PrimitiveType value = mojo::ConvertTo<PrimitiveType>(*data);
      if (!primitive_property.second.validator.Run(value)) {
        DVLOG(2) << "Property value rejected (PrimitiveType): "
                 << transport_name;
        return;
      }
      // TODO(msw): Should aura::Window just store all properties by name?
      window->SetPropertyInternal(
          primitive_property.first, primitive_property.second.property_name,
          nullptr, value, primitive_property.second.default_value);
      return;
    }
  }

  for (const auto& image_property : image_properties_) {
    if (image_property.second == transport_name) {
      // TODO(msw): Validate the data somehow, before trying to convert?
      // TODO(crbug.com/667566): Support additional scales or gfx::Image[Skia].
      const SkBitmap bitmap = mojo::ConvertTo<SkBitmap>(*data);
      const gfx::ImageSkia image = gfx::ImageSkia::CreateFrom1xBitmap(bitmap);
      window->SetProperty(image_property.first, new gfx::ImageSkia(image));
      return;
    }
  }

  for (const auto& rect_property : rect_properties_) {
    if (rect_property.second == transport_name) {
      if (data->size() != 16u) {
        DVLOG(2) << "Property size mismatch (gfx::Rect): " << transport_name;
        return;
      }
      const gfx::Rect value = mojo::ConvertTo<gfx::Rect>(*data);
      window->SetProperty(rect_property.first, new gfx::Rect(value));
      return;
    }
  }

  for (const auto& size_property : size_properties_) {
    if (size_property.second == transport_name) {
      if (data->size() != 8u) {
        DVLOG(2) << "Property size mismatch (gfx::Size): " << transport_name;
        return;
      }
      const gfx::Size value = mojo::ConvertTo<gfx::Size>(*data);
      window->SetProperty(size_property.first, new gfx::Size(value));
      return;
    }
  }

  for (const auto& string_property : string_properties_) {
    if (string_property.second == transport_name) {
      // TODO(msw): Validate the data somehow, before trying to convert?
      const std::string value = mojo::ConvertTo<std::string>(*data);
      window->SetProperty(string_property.first, new std::string(value));
      return;
    }
  }

  for (const auto& string16_property : string16_properties_) {
    if (string16_property.second == transport_name) {
      // TODO(msw): Validate the data somehow, before trying to convert?
      const base::string16 value = mojo::ConvertTo<base::string16>(*data);
      window->SetProperty(string16_property.first, new base::string16(value));
      return;
    }
  }

  DVLOG(2) << "Unknown mus property name: " << transport_name;
}

bool PropertyConverter::GetPropertyValueFromTransportValue(
    const std::string& transport_name,
    const std::vector<uint8_t>& transport_data,
    PrimitiveType* value) {
  // aura::Window only supports property types that fit in PrimitiveType.
  if (transport_data.size() != 8u) {
    DVLOG(2) << "Property size mismatch (PrimitiveType): " << transport_name;
    return false;
  }
  for (const auto& primitive_property : primitive_properties_) {
    if (primitive_property.second.transport_name == transport_name) {
      PrimitiveType v = mojo::ConvertTo<PrimitiveType>(transport_data);
      if (!primitive_property.second.validator.Run(v)) {
        DVLOG(2) << "Property value rejected (PrimitiveType): "
                 << transport_name;
        return false;
      }
      *value = v;
      return true;
    }
  }
  return false;
}

void PropertyConverter::RegisterImageSkiaProperty(
    const WindowProperty<gfx::ImageSkia*>* property,
    const char* transport_name) {
  image_properties_[property] = transport_name;
  transport_names_.insert(transport_name);
}

void PropertyConverter::RegisterRectProperty(
    const WindowProperty<gfx::Rect*>* property,
    const char* transport_name) {
  rect_properties_[property] = transport_name;
  transport_names_.insert(transport_name);
}

void PropertyConverter::RegisterSizeProperty(
    const WindowProperty<gfx::Size*>* property,
    const char* transport_name) {
  size_properties_[property] = transport_name;
  transport_names_.insert(transport_name);
}

void PropertyConverter::RegisterStringProperty(
    const WindowProperty<std::string*>* property,
    const char* transport_name) {
  string_properties_[property] = transport_name;
  transport_names_.insert(transport_name);
}

void PropertyConverter::RegisterString16Property(
    const WindowProperty<base::string16*>* property,
    const char* transport_name) {
  string16_properties_[property] = transport_name;
  transport_names_.insert(transport_name);
}

}  // namespace aura