Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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/resume_library.h" | |
| 6 | |
| 7 #include "base/message_loop.h" | |
| 8 #include "base/observer_list.h" | |
| 9 #include "chrome/browser/browser_thread.h" | |
| 10 #include "chrome/browser/chromeos/cros/cros_library.h" | |
| 11 #include "third_party/cros/chromeos_resume.h" | |
| 12 | |
| 13 namespace chromeos { | |
| 14 | |
| 15 class ResumeLibraryImpl : public ResumeLibrary { | |
| 16 public: | |
| 17 ResumeLibraryImpl() : connection_(NULL) { | |
| 18 if (CrosLibrary::Get()->EnsureLoaded()) | |
| 19 Init(); | |
| 20 } | |
| 21 | |
| 22 ~ResumeLibraryImpl() { | |
| 23 if (connection_) { | |
| 24 chromeos::DisconnectResume(connection_); | |
| 25 connection_ = NULL; | |
| 26 } | |
| 27 } | |
| 28 | |
| 29 void AddObserver(Observer* observer) { | |
| 30 observers_.AddObserver(observer); | |
| 31 } | |
| 32 | |
| 33 void RemoveObserver(Observer* observer) { | |
| 34 observers_.RemoveObserver(observer); | |
| 35 } | |
| 36 | |
| 37 private: | |
| 38 static void OnSystemResumedHandler(void* object) { | |
| 39 ResumeLibraryImpl* self = static_cast<ResumeLibraryImpl*>(object); | |
| 40 self->OnSystemResumed(); | |
| 41 } | |
| 42 | |
| 43 void Init() { | |
| 44 DCHECK(!connection_) << "Already intialized"; | |
| 45 connection_ = chromeos::MonitorResume(&OnSystemResumedHandler, this); | |
| 46 } | |
| 47 | |
| 48 void OnSystemResumed() { | |
| 49 // Make sure we run on the UI thread. | |
| 50 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | |
| 51 BrowserThread::PostTask( | |
| 52 BrowserThread::UI, FROM_HERE, | |
| 53 NewRunnableMethod(this, | |
| 54 &ResumeLibraryImpl::OnSystemResumed)); | |
| 55 return; | |
| 56 } | |
| 57 | |
| 58 FOR_EACH_OBSERVER(Observer, | |
|
Nikita (slow)
2011/02/11 11:54:51
nit: could fit into 1 line?
glotov
2011/02/11 18:20:56
Done.
| |
| 59 observers_, | |
| 60 SystemResumed()); | |
| 61 } | |
| 62 | |
| 63 chromeos::ResumeConnection connection_; | |
| 64 | |
| 65 ObserverList<Observer> observers_; | |
| 66 | |
| 67 DISALLOW_COPY_AND_ASSIGN(ResumeLibraryImpl); | |
| 68 }; | |
| 69 | |
| 70 class ResumeLibraryStubImpl : public ResumeLibrary { | |
| 71 public: | |
| 72 ResumeLibraryStubImpl() {} | |
| 73 ~ResumeLibraryStubImpl() {} | |
| 74 void AddObserver(Observer* observer) {} | |
| 75 void RemoveObserver(Observer* observer) {} | |
| 76 }; | |
| 77 | |
| 78 // static | |
| 79 ResumeLibrary* ResumeLibrary::GetImpl(bool stub) { | |
| 80 if (stub) | |
| 81 return new ResumeLibraryStubImpl(); | |
| 82 else | |
| 83 return new ResumeLibraryImpl(); | |
| 84 } | |
| 85 | |
| 86 } // namespace chromeos | |
| 87 | |
| 88 // Needed for NewRunnableMethod() call above. | |
|
Nikita (slow)
2011/02/11 11:54:51
Should mention that this class is Singleton.
glotov
2011/02/11 18:20:56
Done.
| |
| 89 DISABLE_RUNNABLE_METHOD_REFCOUNT(chromeos::ResumeLibraryImpl); | |
| OLD | NEW |