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

Side by Side Diff: ui/display/mojo/display_snapshot_mojo_struct_traits.cc

Issue 2646213002: Write mojom and StructTraits for DisplaySnapshot. (Closed)
Patch Set: Create more unit tests. Refactor/Enhance code readility/format. Created 3 years, 10 months 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
OLDNEW
(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"
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
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 static uint64_t GetModeIndex(
19 const display::DisplaySnapshot::DisplayModeList& modes,
20 const display::DisplayMode* mode) {
21 if (!mode)
22 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
23
24 for (size_t i = 0; i < modes.size(); ++i) {
25 if (*modes[i].get() == *mode)
26 return i;
27 }
28 NOTREACHED();
29 return -1;
30 }
31
32 } // namespace
33
34 // static
35 std::vector<std::unique_ptr<display::DisplayMode>>
36 StructTraits<display::mojom::DisplaySnapshotMojoDataView,
37 std::unique_ptr<display::DisplaySnapshotMojo>>::
38 modes(
39 const std::unique_ptr<display::DisplaySnapshotMojo>& display_snapshot) {
40 std::vector<std::unique_ptr<display::DisplayMode>> display_mode_list;
41
42 for (const auto& display_mode : display_snapshot->modes())
43 display_mode_list.push_back(display_mode->Clone());
44
45 return display_mode_list;
46 }
47
48 // static
49 uint64_t StructTraits<display::mojom::DisplaySnapshotMojoDataView,
50 std::unique_ptr<display::DisplaySnapshotMojo>>::
51 current_mode_index(
52 const std::unique_ptr<display::DisplaySnapshotMojo>& display_snapshot) {
53 return GetModeIndex(display_snapshot->modes(),
54 display_snapshot->current_mode());
55 }
56
57 // static
58 uint64_t StructTraits<display::mojom::DisplaySnapshotMojoDataView,
59 std::unique_ptr<display::DisplaySnapshotMojo>>::
60 native_mode_index(
61 const std::unique_ptr<display::DisplaySnapshotMojo>& display_snapshot) {
62 return GetModeIndex(display_snapshot->modes(),
63 display_snapshot->native_mode());
64 }
65
66 // static
67 bool StructTraits<display::mojom::DisplaySnapshotMojoDataView,
68 std::unique_ptr<display::DisplaySnapshotMojo>>::
69 Read(display::mojom::DisplaySnapshotMojoDataView data,
70 std::unique_ptr<display::DisplaySnapshotMojo>* out) {
71 gfx::Point origin;
72 if (!data.ReadOrigin(&origin))
73 return false;
74
75 gfx::Size physical_size;
76 if (!data.ReadPhysicalSize(&physical_size))
77 return false;
78
79 display::DisplayConnectionType type;
80 if (!data.ReadType(&type))
81 return false;
82
83 std::string display_name;
84 if (!data.ReadDisplayName(&display_name))
85 return false;
86
87 base::FilePath file_path;
88 if (!data.ReadSysPath(&file_path))
89 return false;
90
91 // There is a type mismatch between vectors containing unique_ptr<T> vs
92 // unique_ptr<const T>. We deserialize into a vector of unique_ptr<T>
93 // then create a vector of unique_ptr<const T> after.
94 std::vector<std::unique_ptr<display::DisplayMode>> non_const_modes;
95 if (!data.ReadModes(&non_const_modes))
96 return false;
97 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.
98 for (auto& mode : non_const_modes)
99 modes.push_back(std::move(mode));
100
101 // Get current_mode pointer from modes array.
102 const display::DisplayMode* current_mode = nullptr;
103 if (data.has_current_mode()) {
104 size_t current_mode_index = data.current_mode_index();
105 if (current_mode_index >= modes.size())
106 return false;
107 current_mode = modes[current_mode_index].get();
108 }
109
110 // Get native_mode pointer from modes array.
111 const display::DisplayMode* native_mode = nullptr;
112 if (data.has_native_mode()) {
113 size_t native_mode_index = data.native_mode_index();
114 if (native_mode_index >= modes.size())
115 return false;
116 native_mode = modes[native_mode_index].get();
117 }
118
119 std::vector<uint8_t> edid;
120 if (!data.ReadEdid(&edid))
121 return false;
122
123 gfx::Size maximum_cursor_size;
124 if (!data.ReadMaximumCursorSize(&maximum_cursor_size))
125 return false;
126
127 *out = base::MakeUnique<display::DisplaySnapshotMojo>(
128 data.display_id(), origin, physical_size, type,
129 data.is_aspect_preserving_scaling(), data.has_overscan(),
130 data.has_color_correction_matrix(), display_name, file_path,
131 data.product_id(), std::move(modes), std::move(edid), current_mode,
132 native_mode, maximum_cursor_size);
133 return true;
134 }
135
136 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698