Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "ui/display/mojo/display_snapshot_mojo_struct_traits.h" | |
| 6 | |
| 7 #include "ipc/ipc_message_utils.h" | |
| 8 #include "ui/display/mojo/display_constants_struct_traits.h" | |
| 9 #include "ui/display/mojo/display_mode_struct_traits.h" | |
| 10 #include "ui/display/types/display_constants.h" | |
| 11 #include "ui/gfx/geometry/mojo/geometry_struct_traits.h" | |
| 12 #include "ui/gfx/geometry/size.h" | |
| 13 | |
| 14 namespace mojo { | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 // Return std::numeric_limits<uint64_t>::max() if no index is found. | |
|
kylechar
2017/02/15 16:48:45
Please read through the style guide section on com
thanhph
2017/02/16 11:48:21
Cool, thanks!
| |
| 19 static uint64_t GetModeIndex( | |
| 20 const display::DisplaySnapshot::DisplayModeList& modes, | |
| 21 const display::DisplayMode* mode) { | |
| 22 if (!mode) | |
| 23 return std::numeric_limits<uint64_t>::max(); | |
| 24 | |
| 25 for (size_t i = 0; i < modes.size(); ++i) { | |
| 26 if (*modes[i].get() == *mode) | |
| 27 return i; | |
| 28 } | |
| 29 NOTREACHED(); | |
| 30 return std::numeric_limits<uint64_t>::max(); | |
| 31 } | |
| 32 | |
| 33 } // namespace | |
| 34 | |
| 35 // static | |
| 36 std::vector<std::unique_ptr<display::DisplayMode>> | |
| 37 StructTraits<display::mojom::DisplaySnapshotMojoDataView, | |
| 38 std::unique_ptr<display::DisplaySnapshotMojo>>:: | |
| 39 modes( | |
| 40 const std::unique_ptr<display::DisplaySnapshotMojo>& display_snapshot) { | |
| 41 std::vector<std::unique_ptr<display::DisplayMode>> display_mode_list; | |
| 42 | |
| 43 for (const auto& display_mode : display_snapshot->modes()) | |
| 44 display_mode_list.push_back(display_mode->Clone()); | |
| 45 | |
| 46 return display_mode_list; | |
| 47 } | |
| 48 | |
| 49 // static | |
| 50 uint64_t StructTraits<display::mojom::DisplaySnapshotMojoDataView, | |
| 51 std::unique_ptr<display::DisplaySnapshotMojo>>:: | |
| 52 current_mode_index( | |
| 53 const std::unique_ptr<display::DisplaySnapshotMojo>& display_snapshot) { | |
| 54 return GetModeIndex(display_snapshot->modes(), | |
| 55 display_snapshot->current_mode()); | |
| 56 } | |
| 57 | |
| 58 // static | |
| 59 uint64_t StructTraits<display::mojom::DisplaySnapshotMojoDataView, | |
| 60 std::unique_ptr<display::DisplaySnapshotMojo>>:: | |
| 61 native_mode_index( | |
| 62 const std::unique_ptr<display::DisplaySnapshotMojo>& display_snapshot) { | |
| 63 return GetModeIndex(display_snapshot->modes(), | |
| 64 display_snapshot->native_mode()); | |
| 65 } | |
| 66 | |
| 67 // static | |
| 68 bool StructTraits<display::mojom::DisplaySnapshotMojoDataView, | |
| 69 std::unique_ptr<display::DisplaySnapshotMojo>>:: | |
| 70 Read(display::mojom::DisplaySnapshotMojoDataView data, | |
| 71 std::unique_ptr<display::DisplaySnapshotMojo>* out) { | |
| 72 gfx::Point origin; | |
| 73 if (!data.ReadOrigin(&origin)) | |
| 74 return false; | |
| 75 | |
| 76 gfx::Size physical_size; | |
| 77 if (!data.ReadPhysicalSize(&physical_size)) | |
| 78 return false; | |
| 79 | |
| 80 display::DisplayConnectionType type; | |
| 81 if (!data.ReadType(&type)) | |
| 82 return false; | |
| 83 | |
| 84 std::string display_name; | |
| 85 if (!data.ReadDisplayName(&display_name)) | |
| 86 return false; | |
| 87 | |
| 88 base::FilePath file_path; | |
| 89 if (!data.ReadSysPath(&file_path)) | |
| 90 return false; | |
| 91 | |
| 92 // There is a type mismatch between vectors containing unique_ptr<T> vs | |
| 93 // unique_ptr<const T>. We deserialize into a vector of unique_ptr<T> | |
| 94 // then create a vector of unique_ptr<const T> after. | |
| 95 std::vector<std::unique_ptr<display::DisplayMode>> non_const_modes; | |
| 96 if (!data.ReadModes(&non_const_modes)) | |
| 97 return false; | |
| 98 display::DisplaySnapshot::DisplayModeList modes; | |
| 99 for (auto& mode : non_const_modes) | |
| 100 modes.push_back(std::move(mode)); | |
| 101 | |
| 102 // Get current_mode pointer from modes array. | |
| 103 const display::DisplayMode* current_mode = nullptr; | |
| 104 if (data.has_current_mode()) { | |
| 105 size_t current_mode_index = data.current_mode_index(); | |
| 106 if (current_mode_index >= modes.size()) | |
| 107 return false; | |
| 108 current_mode = modes[current_mode_index].get(); | |
| 109 } | |
| 110 | |
| 111 // Get native_mode pointer from modes array. | |
| 112 const display::DisplayMode* native_mode = nullptr; | |
| 113 if (data.has_native_mode()) { | |
| 114 size_t native_mode_index = data.native_mode_index(); | |
| 115 if (native_mode_index >= modes.size()) | |
| 116 return false; | |
| 117 native_mode = modes[native_mode_index].get(); | |
| 118 } | |
| 119 | |
| 120 std::vector<uint8_t> edid; | |
| 121 if (!data.ReadEdid(&edid)) | |
| 122 return false; | |
| 123 | |
| 124 gfx::Size maximum_cursor_size; | |
| 125 if (!data.ReadMaximumCursorSize(&maximum_cursor_size)) | |
| 126 return false; | |
| 127 | |
| 128 *out = base::MakeUnique<display::DisplaySnapshotMojo>( | |
| 129 data.display_id(), origin, physical_size, type, | |
| 130 data.is_aspect_preserving_scaling(), data.has_overscan(), | |
| 131 data.has_color_correction_matrix(), display_name, file_path, | |
| 132 data.product_id(), std::move(modes), std::move(edid), current_mode, | |
| 133 native_mode, maximum_cursor_size); | |
| 134 return true; | |
| 135 } | |
| 136 | |
| 137 } // namespace mojo | |
| OLD | NEW |