Roll src/third_party/WebKit a3b4a2e:7441784 (svn 202551:202552)
[chromium-blink-merge.git] / extensions / browser / api / system_display / display_info_provider.cc
blobd7f6d78e324ef5d44cbe1f269cf209734672c61d
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
3 // found in the LICENSE file.
5 #include "extensions/browser/api/system_display/display_info_provider.h"
7 #include "base/strings/string_number_conversions.h"
8 #include "extensions/common/api/system_display.h"
9 #include "ui/gfx/display.h"
10 #include "ui/gfx/screen.h"
12 namespace extensions {
14 namespace {
16 // Created on demand and will leak when the process exits.
17 DisplayInfoProvider* g_display_info_provider = NULL;
19 // Converts Rotation enum to integer.
20 int RotationToDegrees(gfx::Display::Rotation rotation) {
21 switch (rotation) {
22 case gfx::Display::ROTATE_0:
23 return 0;
24 case gfx::Display::ROTATE_90:
25 return 90;
26 case gfx::Display::ROTATE_180:
27 return 180;
28 case gfx::Display::ROTATE_270:
29 return 270;
31 return 0;
34 // Creates new DisplayUnitInfo struct for |display|.
35 api::system_display::DisplayUnitInfo* CreateDisplayUnitInfo(
36 const gfx::Display& display,
37 int64 primary_display_id) {
38 api::system_display::DisplayUnitInfo* unit =
39 new api::system_display::DisplayUnitInfo();
40 const gfx::Rect& bounds = display.bounds();
41 const gfx::Rect& work_area = display.work_area();
42 unit->id = base::Int64ToString(display.id());
43 unit->is_primary = (display.id() == primary_display_id);
44 unit->is_internal = display.IsInternal();
45 unit->is_enabled = true;
46 unit->rotation = RotationToDegrees(display.rotation());
47 unit->bounds.left = bounds.x();
48 unit->bounds.top = bounds.y();
49 unit->bounds.width = bounds.width();
50 unit->bounds.height = bounds.height();
51 unit->work_area.left = work_area.x();
52 unit->work_area.top = work_area.y();
53 unit->work_area.width = work_area.width();
54 unit->work_area.height = work_area.height();
55 return unit;
58 } // namespace
60 DisplayInfoProvider::~DisplayInfoProvider() {
63 // static
64 DisplayInfoProvider* DisplayInfoProvider::Get() {
65 if (g_display_info_provider == NULL)
66 g_display_info_provider = DisplayInfoProvider::Create();
67 return g_display_info_provider;
70 // static
71 void DisplayInfoProvider::InitializeForTesting(
72 DisplayInfoProvider* display_info_provider) {
73 DCHECK(display_info_provider);
74 g_display_info_provider = display_info_provider;
77 void DisplayInfoProvider::EnableUnifiedDesktop(bool enable) {}
79 DisplayInfo DisplayInfoProvider::GetAllDisplaysInfo() {
80 // TODO(scottmg): Native is wrong http://crbug.com/133312
81 gfx::Screen* screen = gfx::Screen::GetNativeScreen();
82 int64 primary_id = screen->GetPrimaryDisplay().id();
83 std::vector<gfx::Display> displays = screen->GetAllDisplays();
84 DisplayInfo all_displays;
85 for (const gfx::Display& display : displays) {
86 linked_ptr<api::system_display::DisplayUnitInfo> unit(
87 CreateDisplayUnitInfo(display, primary_id));
88 UpdateDisplayUnitInfoForPlatform(display, unit.get());
89 all_displays.push_back(unit);
91 return all_displays;
94 DisplayInfoProvider::DisplayInfoProvider() {
97 } // namespace extensions