OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/chromeos/cros/brightness_library.h" | 5 #include "chrome/browser/chromeos/cros/brightness_library.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/observer_list.h" | 10 #include "base/observer_list.h" |
11 #include "chrome/browser/chromeos/cros/cros_library.h" | 11 #include "chrome/browser/chromeos/cros/cros_library.h" |
12 #include "content/browser/browser_thread.h" | 12 #include "content/browser/browser_thread.h" |
13 #include "third_party/cros/chromeos_brightness.h" | 13 #include "third_party/cros/chromeos_brightness.h" |
14 | 14 |
15 namespace chromeos { | 15 namespace chromeos { |
16 | 16 |
17 class BrightnessLibraryImpl : public BrightnessLibrary { | 17 class BrightnessLibraryImpl : public BrightnessLibrary { |
18 public: | 18 public: |
19 BrightnessLibraryImpl() : brightness_connection_(NULL) { | 19 BrightnessLibraryImpl() : brightness_connection_(NULL) {} |
20 if (CrosLibrary::Get()->EnsureLoaded()) | |
21 Init(); | |
22 } | |
23 | 20 |
24 ~BrightnessLibraryImpl() { | 21 ~BrightnessLibraryImpl() { |
25 if (brightness_connection_) { | 22 if (brightness_connection_) { |
26 chromeos::DisconnectBrightness(brightness_connection_); | 23 chromeos::DisconnectBrightness(brightness_connection_); |
27 brightness_connection_ = NULL; | 24 brightness_connection_ = NULL; |
28 } | 25 } |
29 } | 26 } |
30 | 27 |
31 void DecreaseScreenBrightness(bool allow_off) { | 28 void Init() { |
32 if (chromeos::DecreaseScreenBrightness) | 29 if (CrosLibrary::Get()->EnsureLoaded()) { |
33 chromeos::DecreaseScreenBrightness(allow_off); | 30 CHECK(!brightness_connection_) << "Already intialized"; |
34 } | 31 brightness_connection_ = |
35 | 32 chromeos::MonitorBrightnessV2(&BrightnessChangedHandler, this); |
36 void IncreaseScreenBrightness() { | 33 } |
37 if (chromeos::IncreaseScreenBrightness) | |
38 chromeos::IncreaseScreenBrightness(); | |
39 } | 34 } |
40 | 35 |
41 void AddObserver(Observer* observer) { | 36 void AddObserver(Observer* observer) { |
42 observers_.AddObserver(observer); | 37 observers_.AddObserver(observer); |
43 } | 38 } |
44 | 39 |
45 void RemoveObserver(Observer* observer) { | 40 void RemoveObserver(Observer* observer) { |
46 observers_.RemoveObserver(observer); | 41 observers_.RemoveObserver(observer); |
47 } | 42 } |
48 | 43 |
| 44 void DecreaseScreenBrightness(bool allow_off) { |
| 45 if (chromeos::DecreaseScreenBrightness) |
| 46 chromeos::DecreaseScreenBrightness(allow_off); |
| 47 } |
| 48 |
| 49 void IncreaseScreenBrightness() { |
| 50 if (chromeos::IncreaseScreenBrightness) |
| 51 chromeos::IncreaseScreenBrightness(); |
| 52 } |
| 53 |
49 private: | 54 private: |
50 static void BrightnessChangedHandler(void* object, | 55 static void BrightnessChangedHandler(void* object, |
51 int brightness_level, | 56 int brightness_level, |
52 bool user_initiated) { | 57 bool user_initiated) { |
53 BrightnessLibraryImpl* self = static_cast<BrightnessLibraryImpl*>(object); | 58 BrightnessLibraryImpl* self = static_cast<BrightnessLibraryImpl*>(object); |
54 self->OnBrightnessChanged(brightness_level, user_initiated); | 59 self->OnBrightnessChanged(brightness_level, user_initiated); |
55 } | 60 } |
56 | 61 |
57 void Init() { | |
58 DCHECK(!brightness_connection_) << "Already intialized"; | |
59 brightness_connection_ = | |
60 chromeos::MonitorBrightnessV2(&BrightnessChangedHandler, this); | |
61 } | |
62 | |
63 void OnBrightnessChanged(int brightness_level, bool user_initiated) { | 62 void OnBrightnessChanged(int brightness_level, bool user_initiated) { |
64 // Make sure we run on the UI thread. | 63 // Make sure we run on the UI thread. |
65 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 64 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
66 BrowserThread::PostTask( | 65 BrowserThread::PostTask( |
67 BrowserThread::UI, FROM_HERE, | 66 BrowserThread::UI, FROM_HERE, |
68 NewRunnableMethod(this, | 67 NewRunnableMethod(this, |
69 &BrightnessLibraryImpl::OnBrightnessChanged, | 68 &BrightnessLibraryImpl::OnBrightnessChanged, |
70 brightness_level, | 69 brightness_level, |
71 user_initiated)); | 70 user_initiated)); |
72 return; | 71 return; |
73 } | 72 } |
74 | 73 |
75 FOR_EACH_OBSERVER(Observer, | 74 FOR_EACH_OBSERVER(Observer, |
76 observers_, | 75 observers_, |
77 BrightnessChanged(brightness_level, user_initiated)); | 76 BrightnessChanged(brightness_level, user_initiated)); |
78 } | 77 } |
79 | 78 |
80 chromeos::BrightnessConnection brightness_connection_; | 79 chromeos::BrightnessConnection brightness_connection_; |
81 | 80 |
82 ObserverList<Observer> observers_; | 81 ObserverList<Observer> observers_; |
83 | 82 |
84 DISALLOW_COPY_AND_ASSIGN(BrightnessLibraryImpl); | 83 DISALLOW_COPY_AND_ASSIGN(BrightnessLibraryImpl); |
85 }; | 84 }; |
86 | 85 |
87 class BrightnessLibraryStubImpl : public BrightnessLibrary { | 86 class BrightnessLibraryStubImpl : public BrightnessLibrary { |
88 public: | 87 public: |
89 BrightnessLibraryStubImpl() {} | 88 BrightnessLibraryStubImpl() {} |
90 ~BrightnessLibraryStubImpl() {} | 89 ~BrightnessLibraryStubImpl() {} |
| 90 void Init() {} |
| 91 void AddObserver(Observer* observer) {} |
| 92 void RemoveObserver(Observer* observer) {} |
91 void DecreaseScreenBrightness(bool allow_off) {} | 93 void DecreaseScreenBrightness(bool allow_off) {} |
92 void IncreaseScreenBrightness() {} | 94 void IncreaseScreenBrightness() {} |
93 void AddObserver(Observer* observer) {} | |
94 void RemoveObserver(Observer* observer) {} | |
95 }; | 95 }; |
96 | 96 |
97 // static | 97 // static |
98 BrightnessLibrary* BrightnessLibrary::GetImpl(bool stub) { | 98 BrightnessLibrary* BrightnessLibrary::GetImpl(bool stub) { |
| 99 BrightnessLibrary* impl; |
99 if (stub) | 100 if (stub) |
100 return new BrightnessLibraryStubImpl(); | 101 impl = new BrightnessLibraryStubImpl(); |
101 else | 102 else |
102 return new BrightnessLibraryImpl(); | 103 impl = new BrightnessLibraryImpl(); |
| 104 impl->Init(); |
| 105 return impl; |
103 } | 106 } |
104 | 107 |
105 } // namespace chromeos | 108 } // namespace chromeos |
106 | 109 |
107 // Needed for NewRunnableMethod() call above. | 110 // Needed for NewRunnableMethod() call above. |
108 DISABLE_RUNNABLE_METHOD_REFCOUNT(chromeos::BrightnessLibraryImpl); | 111 DISABLE_RUNNABLE_METHOD_REFCOUNT(chromeos::BrightnessLibraryImpl); |
OLD | NEW |