Chromium Code Reviews| Index: ui/display/mojo/display_snapshot_mojo_struct_traits.cc |
| diff --git a/ui/display/mojo/display_snapshot_mojo_struct_traits.cc b/ui/display/mojo/display_snapshot_mojo_struct_traits.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6a7b4412997acb1b68d2772026dbdc15dbfe64b1 |
| --- /dev/null |
| +++ b/ui/display/mojo/display_snapshot_mojo_struct_traits.cc |
| @@ -0,0 +1,136 @@ |
| +// Copyright 2017 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/display/mojo/display_snapshot_mojo_struct_traits.h" |
| + |
| +#include "ipc/ipc_message_utils.h" |
|
kylechar
2017/02/14 21:35:28
I'm not sure what this is doing here? This is a he
thanhph1
2017/02/15 15:37:55
|sys_path| mojom serialization and de-serializatio
|
| +#include "ui/display/mojo/display_constants_struct_traits.h" |
| +#include "ui/display/mojo/display_mode_struct_traits.h" |
| +#include "ui/display/types/display_constants.h" |
| +#include "ui/gfx/geometry/mojo/geometry_struct_traits.h" |
| +#include "ui/gfx/geometry/size.h" |
| + |
| +namespace mojo { |
| + |
| +namespace { |
| + |
| +static uint64_t GetModeIndex( |
| + const display::DisplaySnapshot::DisplayModeList& modes, |
| + const display::DisplayMode* mode) { |
| + if (!mode) |
| + return -1; |
|
kylechar
2017/02/14 21:35:28
You are returning a negative number as an unsigned
thanhph1
2017/02/15 15:37:55
Agree. I will use std::numeric_limits<uint64_t>::m
|
| + |
| + for (size_t i = 0; i < modes.size(); ++i) { |
| + if (*modes[i].get() == *mode) |
| + return i; |
| + } |
| + NOTREACHED(); |
| + return -1; |
| +} |
| + |
| +} // namespace |
| + |
| +// static |
| +std::vector<std::unique_ptr<display::DisplayMode>> |
| +StructTraits<display::mojom::DisplaySnapshotMojoDataView, |
| + std::unique_ptr<display::DisplaySnapshotMojo>>:: |
| + modes( |
| + const std::unique_ptr<display::DisplaySnapshotMojo>& display_snapshot) { |
| + std::vector<std::unique_ptr<display::DisplayMode>> display_mode_list; |
| + |
| + for (const auto& display_mode : display_snapshot->modes()) |
| + display_mode_list.push_back(display_mode->Clone()); |
| + |
| + return display_mode_list; |
| +} |
| + |
| +// static |
| +uint64_t StructTraits<display::mojom::DisplaySnapshotMojoDataView, |
| + std::unique_ptr<display::DisplaySnapshotMojo>>:: |
| + current_mode_index( |
| + const std::unique_ptr<display::DisplaySnapshotMojo>& display_snapshot) { |
| + return GetModeIndex(display_snapshot->modes(), |
| + display_snapshot->current_mode()); |
| +} |
| + |
| +// static |
| +uint64_t StructTraits<display::mojom::DisplaySnapshotMojoDataView, |
| + std::unique_ptr<display::DisplaySnapshotMojo>>:: |
| + native_mode_index( |
| + const std::unique_ptr<display::DisplaySnapshotMojo>& display_snapshot) { |
| + return GetModeIndex(display_snapshot->modes(), |
| + display_snapshot->native_mode()); |
| +} |
| + |
| +// static |
| +bool StructTraits<display::mojom::DisplaySnapshotMojoDataView, |
| + std::unique_ptr<display::DisplaySnapshotMojo>>:: |
| + Read(display::mojom::DisplaySnapshotMojoDataView data, |
| + std::unique_ptr<display::DisplaySnapshotMojo>* out) { |
| + gfx::Point origin; |
| + if (!data.ReadOrigin(&origin)) |
| + return false; |
| + |
| + gfx::Size physical_size; |
| + if (!data.ReadPhysicalSize(&physical_size)) |
| + return false; |
| + |
| + display::DisplayConnectionType type; |
| + if (!data.ReadType(&type)) |
| + return false; |
| + |
| + std::string display_name; |
| + if (!data.ReadDisplayName(&display_name)) |
| + return false; |
| + |
| + base::FilePath file_path; |
| + if (!data.ReadSysPath(&file_path)) |
| + return false; |
| + |
| + // There is a type mismatch between vectors containing unique_ptr<T> vs |
| + // unique_ptr<const T>. We deserialize into a vector of unique_ptr<T> |
| + // then create a vector of unique_ptr<const T> after. |
| + std::vector<std::unique_ptr<display::DisplayMode>> non_const_modes; |
| + if (!data.ReadModes(&non_const_modes)) |
| + return false; |
| + display::DisplaySnapshot::DisplayModeList modes; |
|
kylechar
2017/02/14 21:35:28
display::DisplaySnapshot::DisplayModeList modes(no
thanhph1
2017/02/15 15:37:55
This will fail. I'll keep it as it is.
|
| + for (auto& mode : non_const_modes) |
| + modes.push_back(std::move(mode)); |
| + |
| + // Get current_mode pointer from modes array. |
| + const display::DisplayMode* current_mode = nullptr; |
| + if (data.has_current_mode()) { |
| + size_t current_mode_index = data.current_mode_index(); |
| + if (current_mode_index >= modes.size()) |
| + return false; |
| + current_mode = modes[current_mode_index].get(); |
| + } |
| + |
| + // Get native_mode pointer from modes array. |
| + const display::DisplayMode* native_mode = nullptr; |
| + if (data.has_native_mode()) { |
| + size_t native_mode_index = data.native_mode_index(); |
| + if (native_mode_index >= modes.size()) |
| + return false; |
| + native_mode = modes[native_mode_index].get(); |
| + } |
| + |
| + std::vector<uint8_t> edid; |
| + if (!data.ReadEdid(&edid)) |
| + return false; |
| + |
| + gfx::Size maximum_cursor_size; |
| + if (!data.ReadMaximumCursorSize(&maximum_cursor_size)) |
| + return false; |
| + |
| + *out = base::MakeUnique<display::DisplaySnapshotMojo>( |
| + data.display_id(), origin, physical_size, type, |
| + data.is_aspect_preserving_scaling(), data.has_overscan(), |
| + data.has_color_correction_matrix(), display_name, file_path, |
| + data.product_id(), std::move(modes), std::move(edid), current_mode, |
| + native_mode, maximum_cursor_size); |
| + return true; |
| +} |
| + |
| +} // namespace mojo |