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