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

Side by Side Diff: chrome/browser/extensions/api/system_display/display_info_provider.cc

Issue 23441032: [SystemInfo API] Rewrite DisplayInfoProvider without SystemInfoProvider. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add missing head file Created 7 years, 3 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/api/system_display/display_info_provider.h"
6 6
7 #include "base/strings/string_number_conversions.h" 7 #include "base/strings/string_number_conversions.h"
8 #include "ui/gfx/display.h" 8 #include "ui/gfx/display.h"
9 #include "ui/gfx/screen.h" 9 #include "ui/gfx/screen.h"
10 10
11 namespace extensions { 11 namespace extensions {
12 12
13 namespace { 13 namespace {
14 14
15 // Converts Rotation enum to integer. 15 // Converts Rotation enum to integer.
16 int RotationToDegrees(gfx::Display::Rotation rotation) { 16 int RotationToDegrees(gfx::Display::Rotation rotation) {
17 switch (rotation) { 17 switch (rotation) {
18 case gfx::Display::ROTATE_0: 18 case gfx::Display::ROTATE_0:
19 return 0; 19 return 0;
20 case gfx::Display::ROTATE_90: 20 case gfx::Display::ROTATE_90:
21 return 90; 21 return 90;
22 case gfx::Display::ROTATE_180: 22 case gfx::Display::ROTATE_180:
23 return 180; 23 return 180;
24 case gfx::Display::ROTATE_270: 24 case gfx::Display::ROTATE_270:
25 return 270; 25 return 270;
26 } 26 }
27 return 0; 27 return 0;
28 } 28 }
29 29
30 // Creates new DisplayUnitInfo struct for |display| and adds it at the end of 30 // Creates new DisplayUnitInfo struct for |display|.
31 // |list|.
32 extensions::api::system_display::DisplayUnitInfo* 31 extensions::api::system_display::DisplayUnitInfo*
33 CreateDisplayUnitInfo(const gfx::Display& display, 32 CreateDisplayUnitInfo(const gfx::Display& display, int64 primary_display_id) {
34 int64 primary_display_id) {
35 extensions::api::system_display::DisplayUnitInfo* unit = 33 extensions::api::system_display::DisplayUnitInfo* unit =
36 new extensions::api::system_display::DisplayUnitInfo(); 34 new extensions::api::system_display::DisplayUnitInfo();
37 const gfx::Rect& bounds = display.bounds(); 35 const gfx::Rect& bounds = display.bounds();
38 const gfx::Rect& work_area = display.work_area(); 36 const gfx::Rect& work_area = display.work_area();
39 unit->id = base::Int64ToString(display.id()); 37 unit->id = base::Int64ToString(display.id());
40 unit->is_primary = (display.id() == primary_display_id); 38 unit->is_primary = (display.id() == primary_display_id);
41 unit->is_internal = display.IsInternal(); 39 unit->is_internal = display.IsInternal();
42 unit->is_enabled = true; 40 unit->is_enabled = true;
43 unit->rotation = RotationToDegrees(display.rotation()); 41 unit->rotation = RotationToDegrees(display.rotation());
44 unit->bounds.left = bounds.x(); 42 unit->bounds.left = bounds.x();
45 unit->bounds.top = bounds.y(); 43 unit->bounds.top = bounds.y();
46 unit->bounds.width = bounds.width(); 44 unit->bounds.width = bounds.width();
47 unit->bounds.height = bounds.height(); 45 unit->bounds.height = bounds.height();
48 unit->work_area.left = work_area.x(); 46 unit->work_area.left = work_area.x();
49 unit->work_area.top = work_area.y(); 47 unit->work_area.top = work_area.y();
50 unit->work_area.width = work_area.width(); 48 unit->work_area.width = work_area.width();
51 unit->work_area.height = work_area.height(); 49 unit->work_area.height = work_area.height();
52 return unit; 50 return unit;
53 } 51 }
54 52
55 } // namespace 53 scoped_ptr<DisplayInfoProvider> g_display_info_provider;
Hongbo Min 2013/09/04 06:58:02 It is a common practice to use a base::LazyInstanc
Haojian Wu 2013/09/04 12:21:03 Done.
56 54
57 DisplayInfoProvider::DisplayInfoProvider() { 55 } // namespace
58 }
59 56
60 DisplayInfoProvider::~DisplayInfoProvider() { 57 DisplayInfoProvider::DisplayInfoProvider() {}
61 }
62 58
63 // Static member intialization. 59 DisplayInfoProvider::~DisplayInfoProvider() {}
64 base::LazyInstance<scoped_refptr<DisplayInfoProvider > >
65 DisplayInfoProvider::provider_ = LAZY_INSTANCE_INITIALIZER;
66 60
67 const DisplayInfo& DisplayInfoProvider::display_info() const { 61 DisplayInfoProvider* DisplayInfoProvider::Get() {
68 return info_; 62 if (g_display_info_provider.get() == NULL)
63 g_display_info_provider.reset(new DisplayInfoProvider());
64 return g_display_info_provider.get();
69 } 65 }
70 66
71 void DisplayInfoProvider::InitializeForTesting( 67 void DisplayInfoProvider::InitializeForTesting(
72 scoped_refptr<DisplayInfoProvider> provider) { 68 DisplayInfoProvider* display_info_provider) {
73 DCHECK(provider.get() != NULL); 69 g_display_info_provider.reset(display_info_provider);
74 provider_.Get() = provider;
75 } 70 }
76 71
77 void DisplayInfoProvider::RequestInfo(const RequestInfoCallback& callback) { 72 DisplayInfo DisplayInfoProvider::GetAllDisplaysInfo() {
78 bool success = QueryInfo();
79
80 base::MessageLoopProxy::current()->PostTask(
81 FROM_HERE,
82 base::Bind(callback, success));
83 }
84
85 #if !defined(OS_WIN)
86 bool DisplayInfoProvider::QueryInfo() {
87 info_.clear();
88
89 // TODO(scottmg): Native is wrong http://crbug.com/133312 73 // TODO(scottmg): Native is wrong http://crbug.com/133312
90 gfx::Screen* screen = gfx::Screen::GetNativeScreen(); 74 gfx::Screen* screen = gfx::Screen::GetNativeScreen();
91 int64 primary_id = screen->GetPrimaryDisplay().id(); 75 int64 primary_id = screen->GetPrimaryDisplay().id();
92 std::vector<gfx::Display> displays = screen->GetAllDisplays(); 76 std::vector<gfx::Display> displays = screen->GetAllDisplays();
77 DisplayInfo all_displays;
93 for (int i = 0; i < screen->GetNumDisplays(); ++i) { 78 for (int i = 0; i < screen->GetNumDisplays(); ++i) {
94 linked_ptr<extensions::api::system_display::DisplayUnitInfo> unit( 79 linked_ptr<extensions::api::system_display::DisplayUnitInfo> unit(
95 CreateDisplayUnitInfo(displays[i], primary_id)); 80 CreateDisplayUnitInfo(displays[i], primary_id));
96 UpdateDisplayUnitInfoForPlatform(displays[i], unit.get()); 81 UpdateDisplayUnitInfoForPlatform(displays[i], unit.get());
97 info_.push_back(unit); 82 all_displays.push_back(unit);
98 } 83 }
99 return true; 84 return all_displays;
100 }
101 #endif
102
103 // static
104 DisplayInfoProvider* DisplayInfoProvider::Get() {
105 if (provider_.Get().get() == NULL)
106 provider_.Get() = new DisplayInfoProvider();
107 return provider_.Get();
108 } 85 }
109 86
110 } // namespace extensions 87 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698