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

Side by Side Diff: chrome/browser/chromeos/cros/brightness_library.cc

Issue 7493061: cros: Apply the Init() model to BrightnessLibrary API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 4 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 | Annotate | Revision Log
« no previous file with comments | « chrome/browser/chromeos/cros/brightness_library.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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() {}
stevenjb 2011/07/29 19:19:15 Still need to initialize brightness_connection_ to
tfarina 2011/07/29 19:44:22 Like in ScreenLockLibraryImpl, we didn't need this
20 if (CrosLibrary::Get()->EnsureLoaded()) 20
21 Init(); 21 ~BrightnessLibraryImpl() {
22 if (brightness_connection_)
23 chromeos::DisconnectBrightness(brightness_connection_);
22 } 24 }
23 25
24 ~BrightnessLibraryImpl() { 26 void Init() {
25 if (brightness_connection_) { 27 if (CrosLibrary::Get()->EnsureLoaded()) {
26 chromeos::DisconnectBrightness(brightness_connection_); 28 DCHECK(!brightness_connection_) << "Already intialized";
stevenjb 2011/07/29 19:19:15 This should either be a CHECK, or we should call D
tfarina 2011/07/29 19:44:22 Changed to CHECK.
27 brightness_connection_ = NULL; 29 brightness_connection_ =
30 chromeos::MonitorBrightnessV2(&BrightnessChangedHandler, this);
28 } 31 }
29 } 32 }
30 33
34 void AddObserver(Observer* observer) {
35 observers_.AddObserver(observer);
36 }
37
38 void RemoveObserver(Observer* observer) {
39 observers_.RemoveObserver(observer);
40 }
41
31 void DecreaseScreenBrightness(bool allow_off) { 42 void DecreaseScreenBrightness(bool allow_off) {
32 if (chromeos::DecreaseScreenBrightness) 43 if (chromeos::DecreaseScreenBrightness)
33 chromeos::DecreaseScreenBrightness(allow_off); 44 chromeos::DecreaseScreenBrightness(allow_off);
34 } 45 }
35 46
36 void IncreaseScreenBrightness() { 47 void IncreaseScreenBrightness() {
37 if (chromeos::IncreaseScreenBrightness) 48 if (chromeos::IncreaseScreenBrightness)
38 chromeos::IncreaseScreenBrightness(); 49 chromeos::IncreaseScreenBrightness();
39 } 50 }
40 51
41 void AddObserver(Observer* observer) {
42 observers_.AddObserver(observer);
43 }
44
45 void RemoveObserver(Observer* observer) {
46 observers_.RemoveObserver(observer);
47 }
48
49 private: 52 private:
50 static void BrightnessChangedHandler(void* object, 53 static void BrightnessChangedHandler(void* object,
51 int brightness_level, 54 int brightness_level,
52 bool user_initiated) { 55 bool user_initiated) {
53 BrightnessLibraryImpl* self = static_cast<BrightnessLibraryImpl*>(object); 56 BrightnessLibraryImpl* self = static_cast<BrightnessLibraryImpl*>(object);
54 self->OnBrightnessChanged(brightness_level, user_initiated); 57 self->OnBrightnessChanged(brightness_level, user_initiated);
55 } 58 }
56 59
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) { 60 void OnBrightnessChanged(int brightness_level, bool user_initiated) {
64 // Make sure we run on the UI thread. 61 // Make sure we run on the UI thread.
65 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { 62 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
66 BrowserThread::PostTask( 63 BrowserThread::PostTask(
67 BrowserThread::UI, FROM_HERE, 64 BrowserThread::UI, FROM_HERE,
68 NewRunnableMethod(this, 65 NewRunnableMethod(this,
69 &BrightnessLibraryImpl::OnBrightnessChanged, 66 &BrightnessLibraryImpl::OnBrightnessChanged,
70 brightness_level, 67 brightness_level,
71 user_initiated)); 68 user_initiated));
72 return; 69 return;
73 } 70 }
74 71
75 FOR_EACH_OBSERVER(Observer, 72 FOR_EACH_OBSERVER(Observer,
76 observers_, 73 observers_,
77 BrightnessChanged(brightness_level, user_initiated)); 74 BrightnessChanged(brightness_level, user_initiated));
78 } 75 }
79 76
80 chromeos::BrightnessConnection brightness_connection_; 77 chromeos::BrightnessConnection brightness_connection_;
81 78
82 ObserverList<Observer> observers_; 79 ObserverList<Observer> observers_;
83 80
84 DISALLOW_COPY_AND_ASSIGN(BrightnessLibraryImpl); 81 DISALLOW_COPY_AND_ASSIGN(BrightnessLibraryImpl);
85 }; 82 };
86 83
87 class BrightnessLibraryStubImpl : public BrightnessLibrary { 84 class BrightnessLibraryStubImpl : public BrightnessLibrary {
88 public: 85 public:
89 BrightnessLibraryStubImpl() {} 86 BrightnessLibraryStubImpl() {}
90 ~BrightnessLibraryStubImpl() {} 87 ~BrightnessLibraryStubImpl() {}
88 void Init() {}
89 void AddObserver(Observer* observer) {}
90 void RemoveObserver(Observer* observer) {}
91 void DecreaseScreenBrightness(bool allow_off) {} 91 void DecreaseScreenBrightness(bool allow_off) {}
92 void IncreaseScreenBrightness() {} 92 void IncreaseScreenBrightness() {}
93 void AddObserver(Observer* observer) {}
94 void RemoveObserver(Observer* observer) {}
95 }; 93 };
96 94
97 // static 95 // static
98 BrightnessLibrary* BrightnessLibrary::GetImpl(bool stub) { 96 BrightnessLibrary* BrightnessLibrary::GetImpl(bool stub) {
97 BrightnessLibrary* impl;
99 if (stub) 98 if (stub)
100 return new BrightnessLibraryStubImpl(); 99 impl = new BrightnessLibraryStubImpl();
101 else 100 else
102 return new BrightnessLibraryImpl(); 101 impl = new BrightnessLibraryImpl();
102 impl->Init();
103 return impl;
103 } 104 }
104 105
105 } // namespace chromeos 106 } // namespace chromeos
106 107
107 // Needed for NewRunnableMethod() call above. 108 // Needed for NewRunnableMethod() call above.
108 DISABLE_RUNNABLE_METHOD_REFCOUNT(chromeos::BrightnessLibraryImpl); 109 DISABLE_RUNNABLE_METHOD_REFCOUNT(chromeos::BrightnessLibraryImpl);
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/cros/brightness_library.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698