- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / system_display / display_info_provider.cc
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.
4
5 #include "chrome/browser/extensions/api/system_display/display_info_provider.h"
6
7 #include "base/strings/string_number_conversions.h"
8 #include "ui/gfx/display.h"
9 #include "ui/gfx/screen.h"
10
11 namespace extensions {
12
13 namespace {
14
15 // Converts Rotation enum to integer.
16 int RotationToDegrees(gfx::Display::Rotation rotation) {
17   switch (rotation) {
18     case gfx::Display::ROTATE_0:
19       return 0;
20     case gfx::Display::ROTATE_90:
21       return 90;
22     case gfx::Display::ROTATE_180:
23       return 180;
24     case gfx::Display::ROTATE_270:
25       return 270;
26   }
27   return 0;
28 }
29
30 // Creates new DisplayUnitInfo struct for |display|.
31 extensions::api::system_display::DisplayUnitInfo*
32 CreateDisplayUnitInfo(const gfx::Display& display, int64 primary_display_id) {
33   extensions::api::system_display::DisplayUnitInfo* unit =
34       new extensions::api::system_display::DisplayUnitInfo();
35   const gfx::Rect& bounds = display.bounds();
36   const gfx::Rect& work_area = display.work_area();
37   unit->id = base::Int64ToString(display.id());
38   unit->is_primary = (display.id() == primary_display_id);
39   unit->is_internal = display.IsInternal();
40   unit->is_enabled = true;
41   unit->rotation = RotationToDegrees(display.rotation());
42   unit->bounds.left = bounds.x();
43   unit->bounds.top = bounds.y();
44   unit->bounds.width = bounds.width();
45   unit->bounds.height = bounds.height();
46   unit->work_area.left = work_area.x();
47   unit->work_area.top = work_area.y();
48   unit->work_area.width = work_area.width();
49   unit->work_area.height = work_area.height();
50   return unit;
51 }
52
53 DisplayInfoProvider* g_display_info_provider = NULL;
54
55 }  // namespace
56
57
58 DisplayInfoProvider::DisplayInfoProvider() {}
59
60 DisplayInfoProvider::~DisplayInfoProvider() {}
61
62 DisplayInfoProvider* DisplayInfoProvider::Get() {
63   if (g_display_info_provider == NULL)
64     g_display_info_provider = new DisplayInfoProvider();
65   return g_display_info_provider;
66 }
67
68 void DisplayInfoProvider::InitializeForTesting(
69     DisplayInfoProvider* display_info_provider) {
70   DCHECK(display_info_provider);
71   g_display_info_provider = display_info_provider;
72 }
73
74 DisplayInfo DisplayInfoProvider::GetAllDisplaysInfo() {
75   // TODO(scottmg): Native is wrong http://crbug.com/133312
76   gfx::Screen* screen = gfx::Screen::GetNativeScreen();
77   int64 primary_id = screen->GetPrimaryDisplay().id();
78   std::vector<gfx::Display> displays = screen->GetAllDisplays();
79   DisplayInfo all_displays;
80   for (int i = 0; i < screen->GetNumDisplays(); ++i) {
81     linked_ptr<extensions::api::system_display::DisplayUnitInfo> unit(
82         CreateDisplayUnitInfo(displays[i], primary_id));
83     UpdateDisplayUnitInfoForPlatform(displays[i], unit.get());
84     all_displays.push_back(unit);
85   }
86   return all_displays;
87 }
88
89 }  // namespace extensions