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

Side by Side Diff: chrome/browser/extensions/display_info_provider_chromeos.cc

Issue 476103002: Make DisplayInfoProvider an interface (Closed) Base URL: git@github.com:tmpsantos/chromium.git@display_info
Patch Set: Added a comment about a global intentionally leaking. Created 6 years, 4 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
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "chrome/browser/extensions/api/system_display/display_info_provider.h" 5 #include "chrome/browser/extensions/display_info_provider_chromeos.h"
6 6
7 #include "ash/display/display_controller.h" 7 #include "ash/display/display_controller.h"
8 #include "ash/display/display_manager.h" 8 #include "ash/display/display_manager.h"
9 #include "ash/shell.h" 9 #include "ash/shell.h"
10 #include "base/message_loop/message_loop_proxy.h" 10 #include "base/message_loop/message_loop_proxy.h"
11 #include "base/strings/string_number_conversions.h" 11 #include "base/strings/string_number_conversions.h"
12 #include "chrome/common/extensions/api/system_display.h"
12 #include "ui/gfx/display.h" 13 #include "ui/gfx/display.h"
13 #include "ui/gfx/point.h" 14 #include "ui/gfx/point.h"
14 #include "ui/gfx/rect.h" 15 #include "ui/gfx/rect.h"
15 16
16 using ash::DisplayManager; 17 using ash::DisplayManager;
17 18
18 namespace extensions { 19 namespace extensions {
19 20
20 using api::system_display::Bounds; 21 using api::system_display::Bounds;
21 using api::system_display::DisplayUnitInfo; 22 using api::system_display::DisplayUnitInfo;
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
58 bool PointIsOverRadiusVector(const gfx::Point& point, 59 bool PointIsOverRadiusVector(const gfx::Point& point,
59 const gfx::Point& vector) { 60 const gfx::Point& vector) {
60 // |point| is left of |vector| if its radius vector's scalar product with a 61 // |point| is left of |vector| if its radius vector's scalar product with a
61 // vector orthogonal (and facing the positive side) to |vector| is positive. 62 // vector orthogonal (and facing the positive side) to |vector| is positive.
62 // 63 //
63 // An orthogonal vector of (a, b) is (b, -a), as the scalar product of these 64 // An orthogonal vector of (a, b) is (b, -a), as the scalar product of these
64 // two is 0. 65 // two is 0.
65 // So, (x, y) is over (a, b) if x * b + y * (-a) >= 0, which is equivalent to 66 // So, (x, y) is over (a, b) if x * b + y * (-a) >= 0, which is equivalent to
66 // x * b >= y * a. 67 // x * b >= y * a.
67 return static_cast<int64>(point.x()) * static_cast<int64>(vector.y()) >= 68 return static_cast<int64>(point.x()) * static_cast<int64>(vector.y()) >=
68 static_cast<int64>(point.y()) * static_cast<int64>(vector.x()); 69 static_cast<int64>(point.y()) * static_cast<int64>(vector.x());
69 } 70 }
70 71
71 // Created ash::DisplayLayout value for |rectangle| compared to the |reference| 72 // Created ash::DisplayLayout value for |rectangle| compared to the |reference|
72 // rectangle. 73 // rectangle.
73 // The layout consists of two values: 74 // The layout consists of two values:
74 // - position: Whether the rectangle is positioned left, right, over or under 75 // - position: Whether the rectangle is positioned left, right, over or under
75 // the reference. 76 // the reference.
76 // - offset: The rectangle's offset from the reference origin along the axis 77 // - offset: The rectangle's offset from the reference origin along the axis
77 // opposite the position direction (if the rectangle is left or right along 78 // opposite the position direction (if the rectangle is left or right along
78 // y-axis, otherwise along x-axis). 79 // y-axis, otherwise along x-axis).
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
115 // Note that the coordinate system is scaled by 2. 116 // Note that the coordinate system is scaled by 2.
116 center.Offset(0, -2 * reference.height()); 117 center.Offset(0, -2 * reference.height());
117 // Choose the vector orientation so the points on the diagonal are considered 118 // Choose the vector orientation so the points on the diagonal are considered
118 // to be left. 119 // to be left.
119 gfx::Point up_diag(-2 * reference.width(), 2 * reference.height()); 120 gfx::Point up_diag(-2 * reference.width(), 2 * reference.height());
120 121
121 bool is_bottom_right = PointIsOverRadiusVector(center, up_diag); 122 bool is_bottom_right = PointIsOverRadiusVector(center, up_diag);
122 123
123 ash::DisplayLayout::Position position; 124 ash::DisplayLayout::Position position;
124 if (is_top_right) { 125 if (is_top_right) {
125 position = is_bottom_right ? ash::DisplayLayout::RIGHT : 126 position =
126 ash::DisplayLayout::TOP; 127 is_bottom_right ? ash::DisplayLayout::RIGHT : ash::DisplayLayout::TOP;
127 } else { 128 } else {
128 position = 129 position =
129 is_bottom_right ? ash::DisplayLayout::BOTTOM : ash::DisplayLayout::LEFT; 130 is_bottom_right ? ash::DisplayLayout::BOTTOM : ash::DisplayLayout::LEFT;
130 } 131 }
131 132
132 // If the rectangle with the calculated position would not have common side 133 // If the rectangle with the calculated position would not have common side
133 // with the reference, try to position it so it shares another edge with the 134 // with the reference, try to position it so it shares another edge with the
134 // reference. 135 // reference.
135 if (is_top_right == is_bottom_right) { 136 if (is_top_right == is_bottom_right) {
136 if (rectangle.y() > reference.y() + reference.height()) { 137 if (rectangle.y() > reference.y() + reference.height()) {
(...skipping 20 matching lines...) Expand all
157 return ash::DisplayLayout::FromInts(position, rectangle.x()); 158 return ash::DisplayLayout::FromInts(position, rectangle.x());
158 } 159 }
159 } 160 }
160 161
161 // Updates the display layout for the target display in reference to the primary 162 // Updates the display layout for the target display in reference to the primary
162 // display. 163 // display.
163 void UpdateDisplayLayout(const gfx::Rect& primary_display_bounds, 164 void UpdateDisplayLayout(const gfx::Rect& primary_display_bounds,
164 int primary_display_id, 165 int primary_display_id,
165 const gfx::Rect& target_display_bounds, 166 const gfx::Rect& target_display_bounds,
166 int target_display_id) { 167 int target_display_id) {
167 ash::DisplayLayout layout = GetLayoutForRectangles(primary_display_bounds, 168 ash::DisplayLayout layout =
168 target_display_bounds); 169 GetLayoutForRectangles(primary_display_bounds, target_display_bounds);
169 ash::Shell::GetInstance()->display_manager()-> 170 ash::Shell::GetInstance()->display_manager()->SetLayoutForCurrentDisplays(
170 SetLayoutForCurrentDisplays(layout); 171 layout);
171 } 172 }
172 173
173 // Validates that parameters passed to the SetInfo function are valid for the 174 // Validates that parameters passed to the SetInfo function are valid for the
174 // desired display and the current display manager state. 175 // desired display and the current display manager state.
175 // Returns whether the parameters are valid. On failure |error| is set to the 176 // Returns whether the parameters are valid. On failure |error| is set to the
176 // error message. 177 // error message.
177 bool ValidateParamsForDisplay(const DisplayProperties& info, 178 bool ValidateParamsForDisplay(const DisplayProperties& info,
178 const gfx::Display& display, 179 const gfx::Display& display,
179 DisplayManager* display_manager, 180 DisplayManager* display_manager,
180 int64 primary_display_id, 181 int64 primary_display_id,
(...skipping 13 matching lines...) Expand all
194 } 195 }
195 196
196 if (*info.mirroring_source_id == base::Int64ToString(display.id())) { 197 if (*info.mirroring_source_id == base::Int64ToString(display.id())) {
197 *error = "Not allowed to mirror self."; 198 *error = "Not allowed to mirror self.";
198 return false; 199 return false;
199 } 200 }
200 } 201 }
201 202
202 // If mirroring source parameter is specified, no other parameter should be 203 // If mirroring source parameter is specified, no other parameter should be
203 // set as when the mirroring is applied the display list could change. 204 // set as when the mirroring is applied the display list could change.
204 if (info.mirroring_source_id && (info.is_primary || info.bounds_origin_x || 205 if (info.mirroring_source_id &&
205 info.bounds_origin_y || info.rotation || info.overscan)) { 206 (info.is_primary || info.bounds_origin_x || info.bounds_origin_y ||
207 info.rotation || info.overscan)) {
206 *error = "No other parameter should be set alongside mirroringSourceId."; 208 *error = "No other parameter should be set alongside mirroringSourceId.";
207 return false; 209 return false;
208 } 210 }
209 211
210 // The bounds cannot be changed for the primary display and should be inside 212 // The bounds cannot be changed for the primary display and should be inside
211 // a reasonable bounds. Note that the display is considered primary if the 213 // a reasonable bounds. Note that the display is considered primary if the
212 // info has 'isPrimary' parameter set, as this will be applied before bounds 214 // info has 'isPrimary' parameter set, as this will be applied before bounds
213 // origin changes. 215 // origin changes.
214 if (info.bounds_origin_x || info.bounds_origin_y) { 216 if (info.bounds_origin_x || info.bounds_origin_y) {
215 if (is_primary) { 217 if (is_primary) {
216 *error = "Bounds origin not allowed for the primary display."; 218 *error = "Bounds origin not allowed for the primary display.";
217 return false; 219 return false;
218 } 220 }
219 if (info.bounds_origin_x && 221 if (info.bounds_origin_x && (*info.bounds_origin_x > kMaxBoundsOrigin ||
220 (*info.bounds_origin_x > kMaxBoundsOrigin || 222 *info.bounds_origin_x < -kMaxBoundsOrigin)) {
221 *info.bounds_origin_x < -kMaxBoundsOrigin)) {
222 *error = "Bounds origin x out of bounds."; 223 *error = "Bounds origin x out of bounds.";
223 return false; 224 return false;
224 } 225 }
225 if (info.bounds_origin_y && 226 if (info.bounds_origin_y && (*info.bounds_origin_y > kMaxBoundsOrigin ||
226 (*info.bounds_origin_y > kMaxBoundsOrigin || 227 *info.bounds_origin_y < -kMaxBoundsOrigin)) {
227 *info.bounds_origin_y < -kMaxBoundsOrigin)) {
228 *error = "Bounds origin y out of bounds."; 228 *error = "Bounds origin y out of bounds.";
229 return false; 229 return false;
230 } 230 }
231 } 231 }
232 232
233 // Verify the rotation value is valid. 233 // Verify the rotation value is valid.
234 if (info.rotation && !IsValidRotationValue(*info.rotation)) { 234 if (info.rotation && !IsValidRotationValue(*info.rotation)) {
235 *error = "Invalid rotation."; 235 *error = "Invalid rotation.";
236 return false; 236 return false;
237 } 237 }
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
272 gfx::Display GetTargetDisplay(const std::string& display_id_str, 272 gfx::Display GetTargetDisplay(const std::string& display_id_str,
273 DisplayManager* manager) { 273 DisplayManager* manager) {
274 int64 display_id; 274 int64 display_id;
275 if (!base::StringToInt64(display_id_str, &display_id)) { 275 if (!base::StringToInt64(display_id_str, &display_id)) {
276 // This should return invalid display. 276 // This should return invalid display.
277 return gfx::Display(); 277 return gfx::Display();
278 } 278 }
279 return manager->GetDisplayForId(display_id); 279 return manager->GetDisplayForId(display_id);
280 } 280 }
281 281
282 // Updates the display with |display_id_str| id according to |info|. Returns 282 } // namespace
283 // whether the display was successfully updated. On failure, no display 283
284 // parameters should be changed, and |error| should be set to the error string. 284 DisplayInfoProviderChromeOS::DisplayInfoProviderChromeOS() {
285 bool SetInfoImpl(const std::string& display_id_str, 285 }
286 const DisplayProperties& info, 286
287 std::string* error) { 287 DisplayInfoProviderChromeOS::~DisplayInfoProviderChromeOS() {
288 }
289
290 bool DisplayInfoProviderChromeOS::SetInfo(const std::string& display_id_str,
291 const DisplayProperties& info,
292 std::string* error) {
288 DisplayManager* display_manager = 293 DisplayManager* display_manager =
289 ash::Shell::GetInstance()->display_manager(); 294 ash::Shell::GetInstance()->display_manager();
290 DCHECK(display_manager); 295 DCHECK(display_manager);
291 ash::DisplayController* display_controller = 296 ash::DisplayController* display_controller =
292 ash::Shell::GetInstance()->display_controller(); 297 ash::Shell::GetInstance()->display_controller();
293 DCHECK(display_controller); 298 DCHECK(display_controller);
294 299
295 const gfx::Display target = GetTargetDisplay(display_id_str, display_manager); 300 const gfx::Display target = GetTargetDisplay(display_id_str, display_manager);
296 301
297 if (target.id() == gfx::Display::kInvalidDisplayID) { 302 if (target.id() == gfx::Display::kInvalidDisplayID) {
298 *error = "Display not found."; 303 *error = "Display not found.";
299 return false; 304 return false;
300 } 305 }
301 306
302 int64 display_id = target.id(); 307 int64 display_id = target.id();
303 // TODO(scottmg): Native is wrong http://crbug.com/133312 308 // TODO(scottmg): Native is wrong http://crbug.com/133312
304 const gfx::Display& primary = 309 const gfx::Display& primary =
305 gfx::Screen::GetNativeScreen()->GetPrimaryDisplay(); 310 gfx::Screen::GetNativeScreen()->GetPrimaryDisplay();
306 311
307 if (!ValidateParamsForDisplay(info, target, display_manager, primary.id(), 312 if (!ValidateParamsForDisplay(
308 error)) { 313 info, target, display_manager, primary.id(), error)) {
309 return false; 314 return false;
310 } 315 }
311 316
312 // Process 'isPrimary' parameter. 317 // Process 'isPrimary' parameter.
313 if (info.is_primary && *info.is_primary && target.id() != primary.id()) 318 if (info.is_primary && *info.is_primary && target.id() != primary.id())
314 display_controller->SetPrimaryDisplayId(display_id); 319 display_controller->SetPrimaryDisplayId(display_id);
315 320
316 // Process 'mirroringSourceId' parameter. 321 // Process 'mirroringSourceId' parameter.
317 if (info.mirroring_source_id && 322 if (info.mirroring_source_id &&
318 info.mirroring_source_id->empty() == display_manager->IsMirrored()) { 323 info.mirroring_source_id->empty() == display_manager->IsMirrored()) {
319 display_controller->ToggleMirrorMode(); 324 display_controller->ToggleMirrorMode();
320 } 325 }
321 326
322 // Process 'overscan' parameter. 327 // Process 'overscan' parameter.
323 if (info.overscan) { 328 if (info.overscan) {
324 display_manager->SetOverscanInsets( 329 display_manager->SetOverscanInsets(display_id,
325 display_id, 330 gfx::Insets(info.overscan->top,
326 gfx::Insets(info.overscan->top, info.overscan->left, 331 info.overscan->left,
327 info.overscan->bottom, info.overscan->right)); 332 info.overscan->bottom,
333 info.overscan->right));
328 } 334 }
329 335
330 // Process 'rotation' parameter. 336 // Process 'rotation' parameter.
331 if (info.rotation) { 337 if (info.rotation) {
332 display_manager->SetDisplayRotation(display_id, 338 display_manager->SetDisplayRotation(display_id,
333 DegreesToRotation(*info.rotation)); 339 DegreesToRotation(*info.rotation));
334 } 340 }
335 341
336 // Process new display origin parameters. 342 // Process new display origin parameters.
337 gfx::Point new_bounds_origin = target.bounds().origin(); 343 gfx::Point new_bounds_origin = target.bounds().origin();
338 if (info.bounds_origin_x) 344 if (info.bounds_origin_x)
339 new_bounds_origin.set_x(*info.bounds_origin_x); 345 new_bounds_origin.set_x(*info.bounds_origin_x);
340 if (info.bounds_origin_y) 346 if (info.bounds_origin_y)
341 new_bounds_origin.set_y(*info.bounds_origin_y); 347 new_bounds_origin.set_y(*info.bounds_origin_y);
342 348
343 if (new_bounds_origin != target.bounds().origin()) { 349 if (new_bounds_origin != target.bounds().origin()) {
344 gfx::Rect target_bounds = target.bounds(); 350 gfx::Rect target_bounds = target.bounds();
345 target_bounds.Offset(new_bounds_origin.x() - target.bounds().x(), 351 target_bounds.Offset(new_bounds_origin.x() - target.bounds().x(),
346 new_bounds_origin.y() - target.bounds().y()); 352 new_bounds_origin.y() - target.bounds().y());
347 UpdateDisplayLayout(primary.bounds(), primary.id(), 353 UpdateDisplayLayout(
348 target_bounds, target.id()); 354 primary.bounds(), primary.id(), target_bounds, target.id());
349 } 355 }
350 356
351 return true; 357 return true;
352 } 358 }
353 359
354 } // namespace 360 void DisplayInfoProviderChromeOS::UpdateDisplayUnitInfoForPlatform(
355
356 bool DisplayInfoProvider::SetInfo(const std::string& display_id,
357 const DisplayProperties& info,
358 std::string* error) {
359 return SetInfoImpl(display_id, info, error);
360 }
361
362 void DisplayInfoProvider::UpdateDisplayUnitInfoForPlatform(
363 const gfx::Display& display, 361 const gfx::Display& display,
364 extensions::api::system_display::DisplayUnitInfo* unit) { 362 extensions::api::system_display::DisplayUnitInfo* unit) {
365
366 ash::DisplayManager* display_manager = 363 ash::DisplayManager* display_manager =
367 ash::Shell::GetInstance()->display_manager(); 364 ash::Shell::GetInstance()->display_manager();
368 unit->name = display_manager->GetDisplayNameForId(display.id()); 365 unit->name = display_manager->GetDisplayNameForId(display.id());
369 if (display_manager->IsMirrored()) { 366 if (display_manager->IsMirrored()) {
370 unit->mirroring_source_id = 367 unit->mirroring_source_id =
371 base::Int64ToString(display_manager->mirrored_display_id()); 368 base::Int64ToString(display_manager->mirrored_display_id());
372 } 369 }
373 370
374 const float dpi = display.device_scale_factor() * kDpi96; 371 const float dpi = display.device_scale_factor() * kDpi96;
375 unit->dpi_x = dpi; 372 unit->dpi_x = dpi;
376 unit->dpi_y = dpi; 373 unit->dpi_y = dpi;
377 374
378 const gfx::Insets overscan_insets = 375 const gfx::Insets overscan_insets =
379 display_manager->GetOverscanInsets(display.id()); 376 display_manager->GetOverscanInsets(display.id());
380 unit->overscan.left = overscan_insets.left(); 377 unit->overscan.left = overscan_insets.left();
381 unit->overscan.top = overscan_insets.top(); 378 unit->overscan.top = overscan_insets.top();
382 unit->overscan.right = overscan_insets.right(); 379 unit->overscan.right = overscan_insets.right();
383 unit->overscan.bottom = overscan_insets.bottom(); 380 unit->overscan.bottom = overscan_insets.bottom();
384 } 381 }
385 382
383 // static
384 DisplayInfoProvider* DisplayInfoProvider::Create() {
385 return new DisplayInfoProviderChromeOS();
386 }
387
386 } // namespace extensions 388 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698