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

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: Rebase 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/lazy_instance.h"
7 #include "base/strings/string_number_conversions.h" 8 #include "base/strings/string_number_conversions.h"
8 #include "ui/gfx/display.h" 9 #include "ui/gfx/display.h"
9 #include "ui/gfx/screen.h" 10 #include "ui/gfx/screen.h"
10 11
11 namespace extensions { 12 namespace extensions {
12 13
13 namespace { 14 namespace {
14 15
15 // Converts Rotation enum to integer. 16 // Converts Rotation enum to integer.
16 int RotationToDegrees(gfx::Display::Rotation rotation) { 17 int RotationToDegrees(gfx::Display::Rotation rotation) {
17 switch (rotation) { 18 switch (rotation) {
18 case gfx::Display::ROTATE_0: 19 case gfx::Display::ROTATE_0:
19 return 0; 20 return 0;
20 case gfx::Display::ROTATE_90: 21 case gfx::Display::ROTATE_90:
21 return 90; 22 return 90;
22 case gfx::Display::ROTATE_180: 23 case gfx::Display::ROTATE_180:
23 return 180; 24 return 180;
24 case gfx::Display::ROTATE_270: 25 case gfx::Display::ROTATE_270:
25 return 270; 26 return 270;
26 } 27 }
27 return 0; 28 return 0;
28 } 29 }
29 30
30 // Creates new DisplayUnitInfo struct for |display| and adds it at the end of 31 // Creates new DisplayUnitInfo struct for |display|.
31 // |list|.
32 extensions::api::system_display::DisplayUnitInfo* 32 extensions::api::system_display::DisplayUnitInfo*
33 CreateDisplayUnitInfo(const gfx::Display& display, 33 CreateDisplayUnitInfo(const gfx::Display& display, int64 primary_display_id) {
34 int64 primary_display_id) {
35 extensions::api::system_display::DisplayUnitInfo* unit = 34 extensions::api::system_display::DisplayUnitInfo* unit =
36 new extensions::api::system_display::DisplayUnitInfo(); 35 new extensions::api::system_display::DisplayUnitInfo();
37 const gfx::Rect& bounds = display.bounds(); 36 const gfx::Rect& bounds = display.bounds();
38 const gfx::Rect& work_area = display.work_area(); 37 const gfx::Rect& work_area = display.work_area();
39 unit->id = base::Int64ToString(display.id()); 38 unit->id = base::Int64ToString(display.id());
40 unit->is_primary = (display.id() == primary_display_id); 39 unit->is_primary = (display.id() == primary_display_id);
41 unit->is_internal = display.IsInternal(); 40 unit->is_internal = display.IsInternal();
42 unit->is_enabled = true; 41 unit->is_enabled = true;
43 unit->rotation = RotationToDegrees(display.rotation()); 42 unit->rotation = RotationToDegrees(display.rotation());
44 unit->bounds.left = bounds.x(); 43 unit->bounds.left = bounds.x();
45 unit->bounds.top = bounds.y(); 44 unit->bounds.top = bounds.y();
46 unit->bounds.width = bounds.width(); 45 unit->bounds.width = bounds.width();
47 unit->bounds.height = bounds.height(); 46 unit->bounds.height = bounds.height();
48 unit->work_area.left = work_area.x(); 47 unit->work_area.left = work_area.x();
49 unit->work_area.top = work_area.y(); 48 unit->work_area.top = work_area.y();
50 unit->work_area.width = work_area.width(); 49 unit->work_area.width = work_area.width();
51 unit->work_area.height = work_area.height(); 50 unit->work_area.height = work_area.height();
52 return unit; 51 return unit;
53 } 52 }
54 53
55 } // namespace 54 } // namespace
56 55
57 DisplayInfoProvider::DisplayInfoProvider() { 56 static base::LazyInstance<scoped_ptr<DisplayInfoProvider> >
58 } 57 g_display_info_provider = LAZY_INSTANCE_INITIALIZER;
oshima 2013/09/06 18:30:48 Given that you instantiate new object yourself, th
Haojian Wu 2013/09/07 03:16:53 One big concern: if we use the raw global pointer
59 58
60 DisplayInfoProvider::~DisplayInfoProvider() { 59 DisplayInfoProvider::DisplayInfoProvider() {}
61 }
62 60
63 // Static member intialization. 61 DisplayInfoProvider::~DisplayInfoProvider() {}
64 base::LazyInstance<scoped_refptr<DisplayInfoProvider > >
65 DisplayInfoProvider::provider_ = LAZY_INSTANCE_INITIALIZER;
66 62
67 const DisplayInfo& DisplayInfoProvider::display_info() const { 63 DisplayInfoProvider* DisplayInfoProvider::Get() {
68 return info_; 64 if (g_display_info_provider.Get().get() == NULL)
65 g_display_info_provider.Get() = scoped_ptr<DisplayInfoProvider>(
66 new DisplayInfoProvider);
67 return g_display_info_provider.Get().get();
69 } 68 }
70 69
71 void DisplayInfoProvider::InitializeForTesting( 70 void DisplayInfoProvider::InitializeForTesting(
72 scoped_refptr<DisplayInfoProvider> provider) { 71 DisplayInfoProvider* display_info_provider) {
73 DCHECK(provider.get() != NULL); 72 g_display_info_provider.Get() = scoped_ptr<DisplayInfoProvider>(
74 provider_.Get() = provider; 73 display_info_provider);
75 } 74 }
76 75
77 void DisplayInfoProvider::RequestInfo(const RequestInfoCallback& callback) { 76 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 77 // TODO(scottmg): Native is wrong http://crbug.com/133312
90 gfx::Screen* screen = gfx::Screen::GetNativeScreen(); 78 gfx::Screen* screen = gfx::Screen::GetNativeScreen();
91 int64 primary_id = screen->GetPrimaryDisplay().id(); 79 int64 primary_id = screen->GetPrimaryDisplay().id();
92 std::vector<gfx::Display> displays = screen->GetAllDisplays(); 80 std::vector<gfx::Display> displays = screen->GetAllDisplays();
81 DisplayInfo all_displays;
93 for (int i = 0; i < screen->GetNumDisplays(); ++i) { 82 for (int i = 0; i < screen->GetNumDisplays(); ++i) {
94 linked_ptr<extensions::api::system_display::DisplayUnitInfo> unit( 83 linked_ptr<extensions::api::system_display::DisplayUnitInfo> unit(
95 CreateDisplayUnitInfo(displays[i], primary_id)); 84 CreateDisplayUnitInfo(displays[i], primary_id));
96 UpdateDisplayUnitInfoForPlatform(displays[i], unit.get()); 85 UpdateDisplayUnitInfoForPlatform(displays[i], unit.get());
97 info_.push_back(unit); 86 all_displays.push_back(unit);
98 } 87 }
99 return true; 88 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 } 89 }
109 90
110 } // namespace extensions 91 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698